Changeset e9ade0f in git
- Timestamp:
- Feb 22, 2007, 3:46:51 PM (16 years ago)
- Branches:
- (u'spielwiese', '828514cf6e480e4bafc26df99217bf2a1ed1ef45')
- Children:
- 05023393670050d3a3678f07351eda09ade7a6eb
- Parents:
- ebe987f7339e431a23e717106ffe22dacf14a1a2
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/claptmpl.cc
rebe987 re9ade0f 3 3 * Computer Algebra System SINGULAR * 4 4 ****************************************/ 5 // $Id: claptmpl.cc,v 1. 39 2007-02-22 10:45:12bricken Exp $5 // $Id: claptmpl.cc,v 1.40 2007-02-22 14:46:51 bricken Exp $ 6 6 /* 7 7 * ABSTRACT - instantiation of all templates … … 227 227 //template class std::vector<std::vector<NoroPlaceHolder> >; 228 228 template class std::vector<DataNoroCacheNode*>; 229 template SparseRow* noro_red_to_non_poly_t<unsigned char>(poly p, int &len, NoroCache* cache,slimgb_alg* c); 230 template SparseRow* noro_red_to_non_poly_t<unsigned short>(poly p, int &len, NoroCache* cache,slimgb_alg* c); 231 template SparseRow* noro_red_to_non_poly_t<unsigned int>(poly p, int &len, NoroCache* cache,slimgb_alg* c); 229 template SparseRow* noro_red_to_non_poly_t<tgb_uint16>(poly p, int &len, NoroCache* cache,slimgb_alg* c); 230 template SparseRow* noro_red_to_non_poly_t<tgb_uint32>(poly p, int &len, NoroCache* cache,slimgb_alg* c); 231 template SparseRow* noro_red_to_non_poly_t<tgb_uint8>(poly p, int &len, NoroCache* cache,slimgb_alg* c); 232 template void simplest_gauss_modp<tgb_uint16> (tgb_uint16* a, int nrows,int ncols); 233 template void simplest_gauss_modp<tgb_uint32> (tgb_uint32* a, int nrows,int ncols); 234 template void simplest_gauss_modp<tgb_uint8> (tgb_uint8* a, int nrows,int ncols); 235 template poly row_to_poly<tgb_uint8>(tgb_uint8* row, poly* terms, int tn, ring r); 236 template poly row_to_poly<tgb_uint32>(tgb_uint32* row, poly* terms, int tn, ring r); 237 template poly row_to_poly<tgb_uint16>(tgb_uint16* row, poly* terms, int tn, ring r); 232 238 //std::priority_queue<MonRedRes> 233 239 #endif -
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 -
kernel/tgb_internal.h
rebe987 re9ade0f 5 5 * Computer Algebra System SINGULAR * 6 6 ****************************************/ 7 /* $Id: tgb_internal.h,v 1.5 6 2007-02-22 10:45:00 bricken Exp $ */7 /* $Id: tgb_internal.h,v 1.57 2007-02-22 14:46:50 bricken Exp $ */ 8 8 /* 9 9 * ABSTRACT: tgb internal .h file … … 739 739 static wlen_type pair_weighted_length(int i, int j, slimgb_alg* c); 740 740 wlen_type pELength(poly p, ring r); 741 void simplest_gauss_modp(number* a, int nrows,int ncols);742 741 int terms_sort_crit(const void* a, const void* b); 742 //void simplest_gauss_modp(number* a, int nrows,int ncols); 743 743 // a: a[0,0],a[0,1]....a[nrows-1,ncols-1] 744 744 // assume: field is Zp 745 #endif 745 #ifdef USE_NORO 746 #define slim_prec_cast(a) (unsigned int) (a) 747 #define F4mat_to_number_type(a) (number_type) slim_prec_cast(a) 748 749 template <class number_type > void write_poly_to_row(number_type* row, poly h, poly*terms, int tn, ring r){ 750 //poly* base=row; 751 while(h!=NULL){ 752 //Print("h:%i\n",h); 753 number coef=p_GetCoeff(h,r); 754 poly* ptr_to_h=(poly*) bsearch(&h,terms,tn,sizeof(poly),terms_sort_crit); 755 assume(ptr_to_h!=NULL); 756 int pos=ptr_to_h-terms; 757 row[pos]=F4mat_to_number_type(coef); 758 //number_type_array[base+pos]=coef; 759 pIter(h); 760 } 761 } 762 template <class number_type > poly row_to_poly(number_type* row, poly* terms, int tn, ring r){ 763 poly h=NULL; 764 int j; 765 number_type zero=0;//;npInit(0); 766 for(j=tn-1;j>=0;j--){ 767 if (!(zero==(row[j]))){ 768 poly t=terms[j]; 769 t=p_LmInit(t,r); 770 p_SetCoeff(t,(number) row[j],r); 771 pNext(t)=h; 772 h=t; 773 } 774 775 } 776 return h; 777 } 778 template <class number_type > int modP_lastIndexRow(number_type* row,int ncols){ 779 int lastIndex; 780 const number_type zero=0;//npInit(0); 781 for(lastIndex=ncols-1;lastIndex>=0;lastIndex--){ 782 if (!(row[lastIndex]==zero)){ 783 return lastIndex; 784 } 785 } 786 return -1; 787 } 788 template <class number_type>class ModPMatrixBackSubstProxyOnArray; 789 template <class number_type > class ModPMatrixProxyOnArray{ 790 public: 791 friend class ModPMatrixBackSubstProxyOnArray<number_type>; 792 793 int ncols,nrows; 794 ModPMatrixProxyOnArray(number_type* array, int nrows, int ncols){ 795 this->ncols=ncols; 796 this->nrows=nrows; 797 rows=(number_type**) omalloc(nrows*sizeof(number_type*)); 798 startIndices=(int*)omalloc(nrows*sizeof(int)); 799 int i; 800 for(i=0;i<nrows;i++){ 801 rows[i]=array+(i*ncols); 802 updateStartIndex(i,-1); 803 } 804 } 805 ~ModPMatrixProxyOnArray(){ 806 omfree(rows); 807 omfree(startIndices); 808 } 809 810 void permRows(int i, int j){ 811 number_type* h=rows[i]; 812 rows[i]=rows[j]; 813 rows[j]=h; 814 int hs=startIndices[i]; 815 startIndices[i]=startIndices[j]; 816 startIndices[j]=hs; 817 } 818 void multiplyRow(int row, number_type coef){ 819 int i; 820 number_type* row_array=rows[row]; 821 for(i=startIndices[row];i<ncols;i++){ 822 row_array[i]=F4mat_to_number_type(npMult((number) row_array[i],(number) coef)); 823 } 824 } 825 void reduceOtherRowsForward(int r){ 826 827 //assume rows "under r" have bigger or equal start index 828 number_type* row_array=rows[r]; 829 number_type zero=F4mat_to_number_type(npInit(0)); 830 int start=startIndices[r]; 831 number_type coef=row_array[start]; 832 assume(start<ncols); 833 int other_row; 834 assume(!(npIsZero((number) row_array[start]))); 835 if (!(npIsOne((number) coef))) 836 multiplyRow(r,F4mat_to_number_type(npInvers((number) coef))); 837 assume(npIsOne((number) row_array[start])); 838 int lastIndex=modP_lastIndexRow(row_array, ncols); 839 number minus_one=npInit(-1); 840 for (other_row=r+1;other_row<nrows;other_row++){ 841 assume(startIndices[other_row]>=start); 842 if (startIndices[other_row]==start){ 843 int i; 844 number_type* other_row_array=rows[other_row]; 845 number coef2=npNeg((number) other_row_array[start]); 846 if (coef2==minus_one){ 847 for(i=start;i<=lastIndex;i++){ 848 if (row_array[i]!=zero) 849 other_row_array[i]=F4mat_to_number_type(npSubM((number) other_row_array[i], (number) row_array[i])); 850 } 851 }else { 852 //assume(FALSE); 853 for(i=start;i<=lastIndex;i++){ 854 if (row_array[i]!=zero) 855 other_row_array[i]=F4mat_to_number_type(npAddM(npMult(coef2,(number) row_array[i]),(number) other_row_array[i])); 856 } 857 } 858 updateStartIndex(other_row,start); 859 assume(npIsZero((number) other_row_array[start])); 860 } 861 } 862 } 863 void updateStartIndex(int row,int lower_bound){ 864 number_type* row_array=rows[row]; 865 assume((lower_bound<0)||(npIsZero((number) row_array[lower_bound]))); 866 int i; 867 //number_type zero=npInit(0); 868 for(i=lower_bound+1;i<ncols;i++){ 869 if (!(row_array[i]==0)) 870 break; 871 } 872 startIndices[row]=i; 873 } 874 int getStartIndex(int row){ 875 return startIndices[row]; 876 } 877 BOOLEAN findPivot(int &r, int &c){ 878 //row>=r, col>=c 879 880 while(c<ncols){ 881 int i; 882 for(i=r;i<nrows;i++){ 883 assume(startIndices[i]>=c); 884 if (startIndices[i]==c){ 885 //r=i; 886 if (r!=i) 887 permRows(r,i); 888 return TRUE; 889 } 890 } 891 c++; 892 } 893 return FALSE; 894 } 895 protected: 896 number_type** rows; 897 int* startIndices; 898 }; 899 template <class number_type > class ModPMatrixBackSubstProxyOnArray{ 900 int *startIndices; 901 number_type** rows; 902 int *lastReducibleIndices; 903 int ncols; 904 int nrows; 905 int nonZeroUntil; 906 public: 907 void multiplyRow(int row, number_type coef){ 908 int i; 909 number_type* row_array=rows[row]; 910 for(i=startIndices[row];i<ncols;i++){ 911 row_array[i]=F4mat_to_number_type(npMult((number) row_array[i],(number) coef)); 912 } 913 } 914 ModPMatrixBackSubstProxyOnArray<number_type> (ModPMatrixProxyOnArray<number_type> & p){ 915 // (number_type* array, int nrows, int ncols, int* startIndices, number_type** rows){ 916 //we borrow some parameters ;-) 917 //we assume, that nobody changes the order of the rows 918 this->startIndices=p.startIndices; 919 this->rows=p.rows; 920 this->ncols=p.ncols; 921 this->nrows=p.nrows; 922 lastReducibleIndices=(int*) omalloc(nrows*sizeof(int)); 923 nonZeroUntil=0; 924 while(nonZeroUntil<nrows){ 925 if (startIndices[nonZeroUntil]<ncols){ 926 927 nonZeroUntil++; 928 } else break; 929 930 } 931 if (TEST_OPT_PROT) 932 Print("rank:%i\n",nonZeroUntil); 933 nonZeroUntil--; 934 int i; 935 for(i=0;i<=nonZeroUntil;i++){ 936 assume(startIndices[i]<ncols); 937 assume(!(npIsZero((number) rows[i][startIndices[i]]))); 938 assume(startIndices[i]>=i); 939 updateLastReducibleIndex(i,nonZeroUntil+1); 940 } 941 } 942 void updateLastReducibleIndex(int r, int upper_bound){ 943 number_type* row_array=rows[r]; 944 if (upper_bound>nonZeroUntil) upper_bound=nonZeroUntil+1; 945 int i; 946 const number_type zero=0;//npInit(0); 947 for(i=upper_bound-1;i>r;i--){ 948 int start=startIndices[i]; 949 assume(start<ncols); 950 if (!(row_array[start]==zero)){ 951 lastReducibleIndices[r]=start; 952 return; 953 } 954 } 955 lastReducibleIndices[r]=-1; 956 } 957 void backwardSubstitute(int r){ 958 int start=startIndices[r]; 959 assume(start<ncols); 960 number_type zero=0;//npInit(0); 961 number_type* row_array=rows[r]; 962 assume((!(npIsZero((number) row_array[start])))); 963 assume(start<ncols); 964 int other_row; 965 if (!(npIsOne((number) row_array[r]))){ 966 //it should be one, but this safety is not expensive 967 multiplyRow(r, F4mat_to_number_type(npInvers((number) row_array[start]))); 968 } 969 int lastIndex=modP_lastIndexRow(row_array, ncols); 970 assume(lastIndex<ncols); 971 assume(lastIndex>=0); 972 for(other_row=r-1;other_row>=0;other_row--){ 973 assume(lastReducibleIndices[other_row]<=start); 974 if (lastReducibleIndices[other_row]==start){ 975 number_type* other_row_array=rows[other_row]; 976 number coef=npNeg((number) other_row_array[start]); 977 assume(!(npIsZero(coef))); 978 int i; 979 assume(start>startIndices[other_row]); 980 for(i=start;i<=lastIndex;i++){ 981 if (row_array[i]!=zero) 982 other_row_array[i]=F4mat_to_number_type(npAddM(npMult(coef,(number)row_array[i]),(number)other_row_array[i])); 983 } 984 updateLastReducibleIndex(other_row,r); 985 } 986 } 987 } 988 ~ModPMatrixBackSubstProxyOnArray<number_type>(){ 989 omfree(lastReducibleIndices); 990 } 991 void backwardSubstitute(){ 992 int i; 993 for(i=nonZeroUntil;i>0;i--){ 994 backwardSubstitute(i); 995 } 996 } 997 }; 998 template <class number_type > void simplest_gauss_modp(number_type* a, int nrows,int ncols){ 999 //use memmoves for changing rows 1000 if (TEST_OPT_PROT) 1001 PrintS("StartGauss\n"); 1002 ModPMatrixProxyOnArray<number_type> mat(a,nrows,ncols); 1003 1004 int c=0; 1005 int r=0; 1006 while(mat.findPivot(r,c)){ 1007 //int pivot=find_pivot() 1008 mat.reduceOtherRowsForward(r); 1009 r++; 1010 c++; 1011 } 1012 ModPMatrixBackSubstProxyOnArray<number_type> backmat(mat); 1013 backmat.backwardSubstitute(); 1014 //backward substitutions 1015 if (TEST_OPT_PROT) 1016 PrintS("StopGauss\n"); 1017 } 1018 #endif 1019 1020 #endif
Note: See TracChangeset
for help on using the changeset viewer.