Changeset e9ade0f in git for kernel/tgb.cc
- Timestamp:
- Feb 22, 2007, 3:46:51 PM (17 years ago)
- Branches:
- (u'spielwiese', 'd0474371d8c5d8068ab70bfb42719c97936b18a6')
- Children:
- 05023393670050d3a3678f07351eda09ade7a6eb
- Parents:
- ebe987f7339e431a23e717106ffe22dacf14a1a2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/tgb.cc
rebe987 re9ade0f 5 5 * Computer Algebra System SINGULAR * 6 6 ****************************************/ 7 /* $Id: tgb.cc,v 1.14 6 2007-02-22 13:56:32bricken Exp $ */7 /* $Id: tgb.cc,v 1.147 2007-02-22 14:46:49 bricken Exp $ */ 8 8 /* 9 9 * ABSTRACT: slimgb and F4 implementation … … 1827 1827 return TRUE; 1828 1828 } 1829 staticint terms_sort_crit(const void* a, const void* b){1829 int terms_sort_crit(const void* a, const void* b){ 1830 1830 return -pLmCmp(*((poly*) a),*((poly*) b)); 1831 1831 } … … 1866 1866 } 1867 1867 typedef unsigned short number_type; 1868 #define prec_cast(a) (unsigned int) (a) 1869 #define to_number_type(a) (number_type) prec_cast(a) 1870 int modP_lastIndexRow(number_type* row,int ncols){ 1871 int lastIndex; 1872 const number_type zero=0;//npInit(0); 1873 for(lastIndex=ncols-1;lastIndex>=0;lastIndex--){ 1874 if (!(row[lastIndex]==zero)){ 1875 return lastIndex; 1876 } 1877 } 1878 return -1; 1879 } 1880 1881 class ModPMatrixProxyOnArray{ 1882 public: 1883 friend class ModPMatrixBackSubstProxOnArray; 1884 int ncols,nrows; 1885 ModPMatrixProxyOnArray(number_type* array, int nrows, int ncols){ 1886 this->ncols=ncols; 1887 this->nrows=nrows; 1888 rows=(number_type**) omalloc(nrows*sizeof(number_type*)); 1889 startIndices=(int*)omalloc(nrows*sizeof(int)); 1890 int i; 1891 for(i=0;i<nrows;i++){ 1892 rows[i]=array+(i*ncols); 1893 updateStartIndex(i,-1); 1894 } 1895 } 1896 ~ModPMatrixProxyOnArray(){ 1897 omfree(rows); 1898 omfree(startIndices); 1899 } 1900 1901 void permRows(int i, int j){ 1902 number_type* h=rows[i]; 1903 rows[i]=rows[j]; 1904 rows[j]=h; 1905 int hs=startIndices[i]; 1906 startIndices[i]=startIndices[j]; 1907 startIndices[j]=hs; 1908 } 1909 void multiplyRow(int row, number_type coef){ 1910 int i; 1911 number_type* row_array=rows[row]; 1912 for(i=startIndices[row];i<ncols;i++){ 1913 row_array[i]=to_number_type(npMult((number) row_array[i],(number) coef)); 1914 } 1915 } 1916 void reduceOtherRowsForward(int r){ 1917 1918 //assume rows "under r" have bigger or equal start index 1919 number_type* row_array=rows[r]; 1920 number_type zero=to_number_type(npInit(0)); 1921 int start=startIndices[r]; 1922 number_type coef=row_array[start]; 1923 assume(start<ncols); 1924 int other_row; 1925 assume(!(npIsZero((number) row_array[start]))); 1926 if (!(npIsOne((number) coef))) 1927 multiplyRow(r,to_number_type(npInvers((number) coef))); 1928 assume(npIsOne((number) row_array[start])); 1929 int lastIndex=modP_lastIndexRow(row_array, ncols); 1930 number minus_one=npInit(-1); 1931 for (other_row=r+1;other_row<nrows;other_row++){ 1932 assume(startIndices[other_row]>=start); 1933 if (startIndices[other_row]==start){ 1934 int i; 1935 number_type* other_row_array=rows[other_row]; 1936 number coef2=npNeg((number) other_row_array[start]); 1937 if (coef2==minus_one){ 1938 for(i=start;i<=lastIndex;i++){ 1939 if (row_array[i]!=zero) 1940 other_row_array[i]=to_number_type(npSubM((number) other_row_array[i], (number) row_array[i])); 1941 } 1942 }else { 1943 //assume(FALSE); 1944 for(i=start;i<=lastIndex;i++){ 1945 if (row_array[i]!=zero) 1946 other_row_array[i]=to_number_type(npAddM(npMult(coef2,(number) row_array[i]),(number) other_row_array[i])); 1947 } 1948 } 1949 updateStartIndex(other_row,start); 1950 assume(npIsZero((number) other_row_array[start])); 1951 } 1952 } 1953 } 1954 void updateStartIndex(int row,int lower_bound){ 1955 number_type* row_array=rows[row]; 1956 assume((lower_bound<0)||(npIsZero((number) row_array[lower_bound]))); 1957 int i; 1958 //number_type zero=npInit(0); 1959 for(i=lower_bound+1;i<ncols;i++){ 1960 if (!(row_array[i]==0)) 1961 break; 1962 } 1963 startIndices[row]=i; 1964 } 1965 int getStartIndex(int row){ 1966 return startIndices[row]; 1967 } 1968 BOOLEAN findPivot(int &r, int &c){ 1969 //row>=r, col>=c 1970 1971 while(c<ncols){ 1972 int i; 1973 for(i=r;i<nrows;i++){ 1974 assume(startIndices[i]>=c); 1975 if (startIndices[i]==c){ 1976 //r=i; 1977 if (r!=i) 1978 permRows(r,i); 1979 return TRUE; 1980 } 1981 } 1982 c++; 1983 } 1984 return FALSE; 1985 } 1986 protected: 1987 number_type** rows; 1988 int* startIndices; 1989 }; 1990 class ModPMatrixBackSubstProxOnArray{ 1991 int *startIndices; 1992 number_type** rows; 1993 int *lastReducibleIndices; 1994 int ncols; 1995 int nrows; 1996 int nonZeroUntil; 1997 public: 1998 void multiplyRow(int row, number_type coef){ 1999 int i; 2000 number_type* row_array=rows[row]; 2001 for(i=startIndices[row];i<ncols;i++){ 2002 row_array[i]=to_number_type(npMult((number) row_array[i],(number) coef)); 2003 } 2004 } 2005 ModPMatrixBackSubstProxOnArray(ModPMatrixProxyOnArray& p){ 2006 // (number_type* array, int nrows, int ncols, int* startIndices, number_type** rows){ 2007 //we borrow some parameters ;-) 2008 //we assume, that nobody changes the order of the rows 2009 this->startIndices=p.startIndices; 2010 this->rows=p.rows; 2011 this->ncols=p.ncols; 2012 this->nrows=p.nrows; 2013 lastReducibleIndices=(int*) omalloc(nrows*sizeof(int)); 2014 nonZeroUntil=0; 2015 while(nonZeroUntil<nrows){ 2016 if (startIndices[nonZeroUntil]<ncols){ 2017 2018 nonZeroUntil++; 2019 } else break; 2020 2021 } 2022 if (TEST_OPT_PROT) 2023 Print("rank:%i\n",nonZeroUntil); 2024 nonZeroUntil--; 2025 int i; 2026 for(i=0;i<=nonZeroUntil;i++){ 2027 assume(startIndices[i]<ncols); 2028 assume(!(npIsZero((number) rows[i][startIndices[i]]))); 2029 assume(startIndices[i]>=i); 2030 updateLastReducibleIndex(i,nonZeroUntil+1); 2031 } 2032 } 2033 void updateLastReducibleIndex(int r, int upper_bound){ 2034 number_type* row_array=rows[r]; 2035 if (upper_bound>nonZeroUntil) upper_bound=nonZeroUntil+1; 2036 int i; 2037 const number_type zero=0;//npInit(0); 2038 for(i=upper_bound-1;i>r;i--){ 2039 int start=startIndices[i]; 2040 assume(start<ncols); 2041 if (!(row_array[start]==zero)){ 2042 lastReducibleIndices[r]=start; 2043 return; 2044 } 2045 } 2046 lastReducibleIndices[r]=-1; 2047 } 2048 void backwardSubstitute(int r){ 2049 int start=startIndices[r]; 2050 assume(start<ncols); 2051 number_type zero=0;//npInit(0); 2052 number_type* row_array=rows[r]; 2053 assume((!(npIsZero((number) row_array[start])))); 2054 assume(start<ncols); 2055 int other_row; 2056 if (!(npIsOne((number) row_array[r]))){ 2057 //it should be one, but this safety is not expensive 2058 multiplyRow(r, to_number_type(npInvers((number) row_array[start]))); 2059 } 2060 int lastIndex=modP_lastIndexRow(row_array, ncols); 2061 assume(lastIndex<ncols); 2062 assume(lastIndex>=0); 2063 for(other_row=r-1;other_row>=0;other_row--){ 2064 assume(lastReducibleIndices[other_row]<=start); 2065 if (lastReducibleIndices[other_row]==start){ 2066 number_type* other_row_array=rows[other_row]; 2067 number coef=npNeg((number) other_row_array[start]); 2068 assume(!(npIsZero(coef))); 2069 int i; 2070 assume(start>startIndices[other_row]); 2071 for(i=start;i<=lastIndex;i++){ 2072 if (row_array[i]!=zero) 2073 other_row_array[i]=to_number_type(npAddM(npMult(coef,(number)row_array[i]),(number)other_row_array[i])); 2074 } 2075 updateLastReducibleIndex(other_row,r); 2076 } 2077 } 2078 } 2079 ~ModPMatrixBackSubstProxOnArray(){ 2080 omfree(lastReducibleIndices); 2081 } 2082 void backwardSubstitute(){ 2083 int i; 2084 for(i=nonZeroUntil;i>0;i--){ 2085 backwardSubstitute(i); 2086 } 2087 } 2088 }; 2089 void simplest_gauss_modp(number_type* a, int nrows,int ncols){ 2090 //use memmoves for changing rows 2091 if (TEST_OPT_PROT) 2092 PrintS("StartGauss\n"); 2093 ModPMatrixProxyOnArray mat(a,nrows,ncols); 2094 2095 int c=0; 2096 int r=0; 2097 while(mat.findPivot(r,c)){ 2098 //int pivot=find_pivot() 2099 mat.reduceOtherRowsForward(r); 2100 r++; 2101 c++; 2102 } 2103 ModPMatrixBackSubstProxOnArray backmat(mat); 2104 backmat.backwardSubstitute(); 2105 //backward substitutions 2106 if (TEST_OPT_PROT) 2107 PrintS("StopGauss\n"); 2108 } 2109 static void write_poly_to_row(number_type* row, poly h, poly*terms, int tn, ring r){ 2110 //poly* base=row; 2111 while(h!=NULL){ 2112 //Print("h:%i\n",h); 2113 number coef=p_GetCoeff(h,r); 2114 poly* ptr_to_h=(poly*) bsearch(&h,terms,tn,sizeof(poly),terms_sort_crit); 2115 assume(ptr_to_h!=NULL); 2116 int pos=ptr_to_h-terms; 2117 row[pos]=to_number_type(coef); 2118 //number_type_array[base+pos]=coef; 2119 pIter(h); 2120 } 2121 } 2122 static poly row_to_poly(number_type* row, poly* terms, int tn, ring r){ 2123 poly h=NULL; 2124 int j; 2125 number_type zero=0;//;npInit(0); 2126 for(j=tn-1;j>=0;j--){ 2127 if (!(zero==(row[j]))){ 2128 poly t=terms[j]; 2129 t=p_LmInit(t,r); 2130 p_SetCoeff(t,(number) row[j],r); 2131 pNext(t)=h; 2132 h=t; 2133 } 2134 2135 } 2136 return h; 2137 } 1868 1869 2138 1870 #ifdef USE_NORO 2139 1871 #ifndef NORO_CACHE … … 2781 2513 return -pLmCmp(((TermNoroDataNode*) a)->t,((TermNoroDataNode*) b)->t); 2782 2514 } 2515 2783 2516 void noro_step(poly*p,int &pn,slimgb_alg* c){ 2784 2517 //Print("Input rows %d\n",pn); … … 2789 2522 2790 2523 NoroCache cache; 2791 #ifndef NORO_NON_POLY 2792 std::vector<std::vector<NoroPlaceHolder> > place_holders(pn); 2793 #else 2524 2794 2525 SparseRow** srows=(SparseRow**) omalloc(pn*sizeof(SparseRow*)); 2795 #endif 2526 2796 2527 for(j=0;j<pn;j++){ 2797 2528 … … 2801 2532 //number coef; 2802 2533 2803 #ifndef NORO_NON_POLY 2804 std::vector<NoroPlaceHolder> ph=noro_red(h,h_len,&cache,c); 2805 place_holders[j]=ph; 2806 #else 2534 2807 2535 srows[j]=noro_red_to_non_poly(h,h_len,&cache,c); 2808 #endif 2536 2809 2537 } 2810 2538 std::vector<DataNoroCacheNode*> irr_nodes; … … 2831 2559 qsort(term_nodes,n,sizeof(TermNoroDataNode),term_nodes_sort_crit); 2832 2560 poly* terms=(poly*) omalloc(n*sizeof(poly)); 2833 #ifndef NORO_NON_POLY 2834 for(j=0;j<n;j++){ 2835 term_nodes[j].node->term_index=j; 2836 terms[j]=term_nodes[j].t; 2837 } 2838 #else 2561 2839 2562 int* old_to_new_indices=(int*) omalloc(cache.nIrreducibleMonomials*sizeof(int)); 2840 2563 for(j=0;j<n;j++){ … … 2843 2566 terms[j]=term_nodes[j].t; 2844 2567 } 2845 #endif 2568 2846 2569 //if (TEST_OPT_PROT) 2847 2570 // Print("Evaluate Rows \n"); 2848 #ifndef NORO_NON_POLY 2849 cache.evaluateRows(); 2850 #endif 2571 2851 2572 number_type* number_array=(number_type*) omalloc(n*pn*sizeof(number_type)); 2852 2573 memset(number_array,0,sizeof(number_type)*n*pn); 2853 2574 number zero=npInit(0); 2854 if (TEST_OPT_PROT) 2855 Print("Evaluate Place Holders\n"); 2575 2856 2576 for(j=0;j<pn;j++){ 2857 2577 int i; … … 2860 2580 row[i]=zero; 2861 2581 }*/ 2862 #ifndef NORO_NON_POLY 2863 cache.evaluatePlaceHolder(row,place_holders[j]); 2864 #else 2582 2865 2583 SparseRow* srow=srows[j]; 2866 2584 for(i=0;i<srow->len;i++){ 2867 2585 int idx=old_to_new_indices[srow->idx_array[i]]; 2868 row[idx]= to_number_type(srow->coef_array[i]);2586 row[idx]=F4mat_to_number_type(srow->coef_array[i]); 2869 2587 } 2870 2588 delete srow; 2871 #endif 2589 2872 2590 } 2873 2591
Note: See TracChangeset
for help on using the changeset viewer.