Changeset b4e057 in git


Ignore:
Timestamp:
Jul 15, 1996, 10:33:18 AM (27 years ago)
Author:
Rüdiger Stobbe <stobbe@…>
Branches:
(u'spielwiese', '8e0ad00ce244dfd0756200662572aef8402f13d5')
Children:
9f2b6f23a8e1f2de750bc3241e6e692d3f57c0d1
Parents:
7155e15ec589aec0c180c45fd0f345978d42c34b
Message:
"changed interface to linearSystemSolve to use the class CFMatrix
"


git-svn-id: file:///usr/local/Singular/svn/trunk@44 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • factory/cf_linsys.cc

    r7155e1 rb4e057  
    11// emacs edit mode for this file is -*- C++ -*-
    2 // $Id: cf_linsys.cc,v 1.1 1996-07-08 08:22:51 stobbe Exp $
     2// $Id: cf_linsys.cc,v 1.2 1996-07-15 08:33:18 stobbe Exp $
    33
    44/*
    55$Log: not supported by cvs2svn $
     6Revision 1.1  1996/07/08 08:22:51  stobbe
     7"New function determinant.
     8"
     9
    610Revision 1.0  1996/05/17 10:59:44  stobbe
    711Initial revision
     
    2226int determinant ( int **extmat, int n );
    2327
    24 static CanonicalForm bound ( CanonicalForm ** M, int rows, int cols );
     28static CanonicalForm bound ( const CFMatrix & M );
    2529CanonicalForm detbound ( const CFMatrix & M, int rows );
    2630
    27 bool matrix_in_Z( const CFMatrix & M, int rows )
     31bool
     32matrix_in_Z( const CFMatrix & M, int rows )
    2833{
    2934    int i, j;
     
    3540}
    3641
    37 bool betterpivot ( const CanonicalForm & oldpivot, const CanonicalForm & newpivot )
     42bool
     43matrix_in_Z( const CFMatrix & M )
     44{
     45    int i, j, rows = M.rows(), cols = M.columns();
     46    for ( i = 1; i <= rows; i++ )
     47        for ( j = 1; j <= cols; j++ )
     48            if ( ! M(i,j).inZ() )
     49                return false;
     50    return true;
     51}
     52
     53bool
     54betterpivot ( const CanonicalForm & oldpivot, const CanonicalForm & newpivot )
    3855{
    3956    if ( newpivot.isZero() )
     
    5168bool fuzzy_result;
    5269
    53 void
    54 linearSystemSolve( CanonicalForm ** M, int rows, int cols )
    55 {
    56     CanonicalForm ** MM = new (CanonicalForm*)[rows];
    57     int ** mm = new (int*)[rows];
    58     CanonicalForm Q, Qhalf, mnew, qnew, B;
    59     int i, j, p, pno;
    60     bool ok;
    61 
    62     // initialize room to hold the result and the result mod p
    63     for ( i = 0; i < rows; i++ ) {
    64         MM[i] = new CanonicalForm[cols];
    65         mm[i] = new int[cols];
    66     }
    67 
    68     // calculate the bound for the result
    69     B = bound( M, rows, cols );
    70     cout << "bound = " << B << endl;
    71 
    72     // find a first solution mod p
    73     pno = 0;
    74     do {
    75         cout << "trying prime(" << pno << ") = " << flush;
    76         p = cf_getBigPrime( pno );
    77         cout << p << endl;
    78         setCharacteristic( p );
    79         // map matrix into char p
    80         for ( i = 0; i < rows; i++ )
    81             for ( j = 0; j < cols; j++ )
    82                 mm[i][j] = mapinto( M[i][j] ).intval();
    83         // solve mod p
    84         ok = solve( mm, rows, cols );
    85         pno++;
    86     } while ( ! ok );
    87     // initialize the result matrix with first solution
    88     setCharacteristic( 0 );
    89     for ( i = 0; i < rows; i++ )
    90         for ( j = rows; j < cols; j++ )
    91             MM[i][j] = mm[i][j];
    92     // Q so far
    93     Q = p;
    94     while ( Q < B && pno < cf_getNumBigPrimes() ) {
     70bool
     71linearSystemSolve( CFMatrix & M )
     72{
     73    if ( ! matrix_in_Z( M ) ) {
     74        int nrows = M.rows(), ncols = M.columns();
     75        int i, j, k;
     76        CanonicalForm rowpivot, pivotrecip;
     77        // triangularization
     78        for ( i = 1; i <= nrows; i++ ) {
     79            //find "pivot"
     80            for (j = i; j <= nrows; j++ )
     81                if ( M(j,i) != 0 ) break;
     82            if ( j > nrows ) return false;
     83            if ( j != i )
     84                M.swapRow( i, j );
     85            pivotrecip = 1 / M(i,i);
     86            for ( j = 1; j <= ncols; j++ )
     87                M(i,j) *= pivotrecip;
     88            for ( j = i+1; j <= nrows; j++ ) {
     89                rowpivot = M(j,i);
     90                if ( rowpivot == 0 ) continue;
     91                for ( k = i; k <= ncols; k++ )
     92                    M(j,k) -= M(i,k) * rowpivot;
     93            }
     94        }
     95        // matrix is now upper triangular with 1s down the diagonal
     96        // back-substitute
     97        for ( i = nrows-1; i > 0; i-- ) {
     98            for ( j = nrows+1; j <= ncols; j++ ) {
     99                for ( k = i+1; k <= nrows; k++ )
     100                    M(i,j) -= M(k,j) * M(i,k);
     101            }
     102        }
     103        return true;
     104    }
     105    else {
     106        int rows = M.rows(), cols = M.columns();
     107        CFMatrix MM( rows, cols );
     108        int ** mm = new (int*)[rows];
     109        CanonicalForm Q, Qhalf, mnew, qnew, B;
     110        int i, j, p, pno;
     111        bool ok;
     112
     113        // initialize room to hold the result and the result mod p
     114        for ( i = 0; i < rows; i++ ) {
     115            mm[i] = new int[cols];
     116        }
     117
     118        // calculate the bound for the result
     119        B = bound( M );
     120        DEBOUTLN( cerr, "bound = ",  B );
     121
     122        // find a first solution mod p
     123        pno = 0;
    95124        do {
    96             cout << "trying prime(" << pno << ") = " << flush;
     125            DEBOUT( cerr, "trying prime(", pno ); DEBOUTLN( cerr, ") = ", ' ' );
    97126            p = cf_getBigPrime( pno );
    98127            cout << p << endl;
    99128            setCharacteristic( p );
     129            // map matrix into char p
    100130            for ( i = 0; i < rows; i++ )
    101131                for ( j = 0; j < cols; j++ )
    102                     mm[i][j] = mapinto( M[i][j] ).intval();
     132                    mm[i][j] = mapinto( M(i,j) ).intval();
    103133            // solve mod p
    104134            ok = solve( mm, rows, cols );
    105135            pno++;
    106136        } while ( ! ok );
    107         // found a solution mod p
    108         // now chinese remainder it to a solution mod Q*p
     137        // initialize the result matrix with first solution
    109138        setCharacteristic( 0 );
    110139        for ( i = 0; i < rows; i++ )
    111             for ( j = rows; j < cols; j++ ) {
    112                 chineseRemainder( MM[i][j], Q, CanonicalForm(mm[i][j]), CanonicalForm(p), mnew, qnew );
    113                 MM[i][j] = mnew;
    114             }
    115         Q = qnew;
    116     }
    117     if ( pno == cf_getNumBigPrimes() )
    118         fuzzy_result = true;
    119     else
    120         fuzzy_result = false;
    121     // store the result in M
    122     Qhalf = Q / 2;
    123     for ( i = 0; i < rows; i++ ) {
    124         for ( j = rows; j < cols; j++ )
    125             if ( MM[i][j] > Qhalf )
    126                 M[i][j] = MM[i][j] - Q;
    127             else
    128                 M[i][j] = MM[i][j];
    129         delete [] MM[i];
    130         delete [] mm[i];
    131     }
    132     delete [] MM;
    133     delete [] mm;
     140            for ( j = rows; j < cols; j++ )
     141                MM(i,j) = mm[i][j];
     142        // Q so far
     143        Q = p;
     144        while ( Q < B && pno < cf_getNumBigPrimes() ) {
     145            do {
     146                cout << "trying prime(" << pno << ") = " << flush;
     147                p = cf_getBigPrime( pno );
     148                cout << p << endl;
     149                setCharacteristic( p );
     150                for ( i = 0; i < rows; i++ )
     151                    for ( j = 0; j < cols; j++ )
     152                        mm[i][j] = mapinto( M(i,j) ).intval();
     153                // solve mod p
     154                ok = solve( mm, rows, cols );
     155                pno++;
     156            } while ( ! ok );
     157            // found a solution mod p
     158            // now chinese remainder it to a solution mod Q*p
     159            setCharacteristic( 0 );
     160            for ( i = 0; i < rows; i++ )
     161                for ( j = rows; j < cols; j++ ) {
     162                    chineseRemainder( MM[i][j], Q, CanonicalForm(mm[i][j]), CanonicalForm(p), mnew, qnew );
     163                    MM(i,j) = mnew;
     164                }
     165            Q = qnew;
     166        }
     167        if ( pno == cf_getNumBigPrimes() )
     168            fuzzy_result = true;
     169        else
     170            fuzzy_result = false;
     171        // store the result in M
     172        Qhalf = Q / 2;
     173        for ( i = 0; i < rows; i++ ) {
     174            for ( j = rows; j < cols; j++ )
     175                if ( MM(i,j) > Qhalf )
     176                    M(i,j) = MM(i,j) - Q;
     177                else
     178                    M(i,j) = MM(i,j);
     179            delete [] mm[i];
     180        }
     181        delete [] mm;
     182        return ! fuzzy_result;
     183    }
    134184}
    135185
     
    257307
    258308static CanonicalForm
    259 bound ( CanonicalForm ** M, int rows, int cols )
    260 {
     309bound ( const CFMatrix & M )
     310{
     311    int rows = M.rows(), cols = M.columns();
    261312    CanonicalForm sum = 0;
    262313    int i, j;
    263314    for ( i = 0; i < rows; i++ )
    264315        for ( j = 0; j < rows; j++ )
    265             sum += M[i][j] * M[i][j];
     316            sum += M(i,j) * M(i,j);
    266317    CanonicalForm vmax = 0, vsum;
    267318    for ( j = rows; j < cols; j++ ) {
    268319        vsum = 0;
    269320        for ( i = 0; i < rows; i++ )
    270             vsum += M[i][j] * M[i][j];
     321            vsum += M(i,j) * M(i,j);
    271322        if ( vsum > vmax ) vmax = vsum;
    272323    }
Note: See TracChangeset for help on using the changeset viewer.