bridge/class-master/depend/pari/src/basemath/alglin1.c
/* Gaussan Elimination. If a is square, return a^(-1)*b;
 * if a has more rows than columns and b is NULL, return c such that c a = Id.
 * a is a (not necessarily square) matrix
 * b is a matrix or column vector, NULL meaning: take the identity matrix,
 *   effectively returning the inverse of a
 * If a and b are empty, the result is the empty matrix.
 *
 * li: number of rows of a and b
 * aco: number of columns of a
 * bco: number of columns of b (if matrix)
 */
Gaussan Elimination. If a is square, return a^(-1)*b;
if a has more rows than columns and b is NULL, return c such that c a = Id.
a is a (not necessarily square) matrix
b is a matrix or column vector, NULL meaning: take the identity matrix,
effectively returning the inverse of a
If a and b are empty, the result is the empty matrix.

number of rows of a and b
aco: number of columns of a
bco: number of columns of b (if matrix)
Gaussan Elimination . If a is square return a^ ( - 1 ) * b ; if a has more rows than columns and b is NULL return c such that c a = Id . a is a ( not necessarily square ) matrix b is a matrix or column vector NULL meaning : take the identity matrix effectively returning the inverse of a If a and b are empty the result is the empty matrix . number of rows of a and b aco : number of columns of a bco : number of columns of b ( if matrix )
static GEN
RgM_solve_basecase(GEN a, GEN b)
{
  pari_sp av = avma;
  long i, j, k, li, bco, aco;
  int iscol;
  pivot_fun pivot;
  GEN p, u, data;
  avma = av;
  if (lg(a)-1 == 2 && nbrows(a) == 2) {
    GEN u = gcoeff(a,1,1), v = gcoeff(a,1,2);
    GEN w = gcoeff(a,2,1), x = gcoeff(a,2,2);
    GEN D = gsub(gmul(u,x), gmul(v,w)), ainv;
    if (gequal0(D)) return NULL;
    ainv = mkmat2(mkcol2(x, gneg(w)), mkcol2(gneg(v), u));
    ainv = gmul(ainv, ginv(D));
    if (b) ainv = gmul(ainv, b);
    return gerepileupto(av, ainv);
  }
  if (!init_gauss(a, &b, &aco, &li, &iscol)) return cgetg(1, iscol?t_COL:t_MAT);
  pivot = get_pivot_fun(a, a, &data);
  a = RgM_shallowcopy(a);
  bco = lg(b)-1;
  if(DEBUGLEVEL>4) err_printf("Entering gauss\n");
  p = NULL; 
  for (i=1; i<=aco; i++)
  {
    k = pivot(a, data, i, NULL);
    if (k > li) return NULL;
    if (k != i)
    { 
      for (j=i; j<=aco; j++) swap(gcoeff(a,i,j), gcoeff(a,k,j));
      for (j=1; j<=bco; j++) swap(gcoeff(b,i,j), gcoeff(b,k,j));
    }
    p = gcoeff(a,i,i);
    if (i == aco) break;
    for (k=i+1; k<=li; k++)
    {
      GEN m = gcoeff(a,k,i);
      if (!gequal0(m))
      {
        m = gdiv(m,p);
        for (j=i+1; j<=aco; j++) _submul(gel(a,j),k,i,m);
        for (j=1;   j<=bco; j++) _submul(gel(b,j),k,i,m);
      }
    }
    if (gc_needed(av,1))
    {
      if(DEBUGMEM>1) pari_warn(warnmem,"gauss. i=%ld",i);
      gerepileall(av,2, &a,&b);
    }
  }
  if(DEBUGLEVEL>4) err_printf("Solving the triangular system\n");
  u = cgetg(bco+1,t_MAT);
  for (j=1; j<=bco; j++) gel(u,j) = get_col(a,gel(b,j),p,aco);
  return gerepilecopy(av, iscol? gel(u,1): u);
}
static GEN RgM_solve_basecase ( GEN a , GEN b ) { pari_sp av = avma ; long i , j , k , li , bco , aco ; int iscol ; pivot_fun pivot ; GEN p , u , data ; avma = av ; if ( lg ( a ) - 1 == 2 && nbrows ( a ) == 2 ) { GEN u = gcoeff ( a , 1 , 1 ) , v = gcoeff ( a , 1 , 2 ) ; GEN w = gcoeff ( a , 2 , 1 ) , x = gcoeff ( a , 2 , 2 ) ; GEN D = gsub ( gmul ( u , x ) , gmul ( v , w ) ) , ainv ; if ( gequal0 ( D ) ) return NULL ; ainv = mkmat2 ( mkcol2 ( x , gneg ( w ) ) , mkcol2 ( gneg ( v ) , u ) ) ; ainv = gmul ( ainv , ginv ( D ) ) ; if ( b ) ainv = gmul ( ainv , b ) ; return gerepileupto ( av , ainv ) ; } if ( ! init_gauss ( a , & b , & aco , & li , & iscol ) ) return cgetg ( 1 , iscol ? t_COL : t_MAT ) ; pivot = get_pivot_fun ( a , a , & data ) ; a = RgM_shallowcopy ( a ) ; bco = lg ( b ) - 1 ; if ( DEBUGLEVEL > 4 ) err_printf ( " \n " ) ; p = NULL ; for ( i = 1 ; i <= aco ; i ++ ) { k = pivot ( a , data , i , NULL ) ; if ( k > li ) return NULL ; if ( k != i ) { for ( j = i ; j <= aco ; j ++ ) swap ( gcoeff ( a , i , j ) , gcoeff ( a , k , j ) ) ; for ( j = 1 ; j <= bco ; j ++ ) swap ( gcoeff ( b , i , j ) , gcoeff ( b , k , j ) ) ; } p = gcoeff ( a , i , i ) ; if ( i == aco ) break ; for ( k = i + 1 ; k <= li ; k ++ ) { GEN m = gcoeff ( a , k , i ) ; if ( ! gequal0 ( m ) ) { m = gdiv ( m , p ) ; for ( j = i + 1 ; j <= aco ; j ++ ) _submul ( gel ( a , j ) , k , i , m ) ; for ( j = 1 ; j <= bco ; j ++ ) _submul ( gel ( b , j ) , k , i , m ) ; } } if ( gc_needed ( av , 1 ) ) { if ( DEBUGMEM > 1 ) pari_warn ( warnmem , " " , i ) ; gerepileall ( av , 2 , & a , & b ) ; } } if ( DEBUGLEVEL > 4 ) err_printf ( " \n " ) ; u = cgetg ( bco + 1 , t_MAT ) ; for ( j = 1 ; j <= bco ; j ++ ) gel ( u , j ) = get_col ( a , gel ( b , j ) , p , aco ) ; return gerepilecopy ( av , iscol ? gel ( u , 1 ) : u ) ; }
Gaussan Elimination.
Gaussan Elimination .
/* 2x2 matrix, start by inverting a */ /* gcc -Wall */ /* k is the line where we find the pivot */ /* exchange the lines s.t. k = i */