[3d124a7] | 1 | // $Id: matrix.lib,v 1.1.1.1 1997-04-25 15:13:26 obachman Exp $ |
---|
| 2 | //(GMG+BM) |
---|
| 3 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 4 | LIBRARY: matrix.lib PROCEDURES FOR MATRIX OPERATIONS |
---|
| 5 | |
---|
| 6 | compress(A); matrix, zero columns from A deleted |
---|
| 7 | concat(A1,A2,..); matrix, concatenation of matrices A1,A2,... |
---|
| 8 | diag(p,n); matrix, nxn diagonal matrix with entries poly p |
---|
| 9 | dsum(A1,A2,..); matrix, direct sum of matrices A1,A2,... |
---|
| 10 | flatten(A); ideal, generated by entries of matrix A |
---|
| 11 | is_complex(c); 1 if list c is a complex, 0 if not |
---|
| 12 | outer(A,B); matrix, outer product of matrices A and B |
---|
| 13 | skewmat(n[,id]); generic skew-symmetric nxn matrix [entries from id] |
---|
| 14 | submat(A,r,c); submatrix of A with rows/cols specified by intvec r/c |
---|
| 15 | symmat(n[,id]); generic symmetric nxn matrix [entries from id] |
---|
| 16 | tensor(A,B); matrix, tensor product of matrices A nd B |
---|
| 17 | unitmat(n); unit square matrix of size n |
---|
| 18 | gauss_col(A); transform constant matrix A into col-reduced nf |
---|
| 19 | gauss_row(A); transform constant matrix A into row-reduced nf |
---|
| 20 | addcol(A,c1,p,c2); add p*(c1-th col) to c2-th column of matrix A, p poly |
---|
| 21 | addrow(A,r1,p,r2); add p*(r1-th row) to r2-th row of matrix A, p poly |
---|
| 22 | multcol(A,c,p); multiply c-th column of A with poly p |
---|
| 23 | multrow(A,r,p); multiply r-th row of A with poly p |
---|
| 24 | permcol(A,i,j); permute i-th and j-th columns |
---|
| 25 | permrow(A,i,j); permute i-th and j-th rows |
---|
| 26 | (parameters in square brackets [] are optional) |
---|
| 27 | |
---|
| 28 | LIB "inout.lib"; |
---|
| 29 | LIB "ring.lib"; |
---|
| 30 | LIB "random.lib"; |
---|
| 31 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 32 | |
---|
| 33 | proc compress (A) |
---|
| 34 | USAGE: compress(A); A matrix/intmat/ideal/module |
---|
| 35 | RETURN: matrix/intmat/ideal/module, zero columns/generators from A deleted |
---|
| 36 | EXAMPLE: example compress; shows an example |
---|
| 37 | { |
---|
| 38 | if( typeof(A)=="matrix" ) { return(matrix(simplify(A,2))); } |
---|
| 39 | if( typeof(A)=="intmat" ) |
---|
| 40 | { |
---|
| 41 | ring r=0,x,lp; |
---|
| 42 | module m=module(matrix(A)); |
---|
| 43 | int c= size(m); |
---|
| 44 | intmat B[nrows(A)][c]; |
---|
| 45 | int i,j; |
---|
| 46 | for( i=1; i<=ncols(A); i++ ) |
---|
| 47 | { |
---|
| 48 | if( m[i]!=[0] ) |
---|
| 49 | { |
---|
| 50 | j=j+1; |
---|
| 51 | B[1..nrows(A),j]=A[1..nrows(A),i]; |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | return(B); |
---|
| 55 | } |
---|
| 56 | return(simplify(A,2)); |
---|
| 57 | } |
---|
| 58 | example |
---|
| 59 | { "EXAMPLE:"; echo = 2; |
---|
| 60 | ring r=0,(x,y,z),ds; |
---|
| 61 | matrix A[3][4]=1,0,3,0,x,0,z,0,x2,0,z2,0; |
---|
| 62 | print(A); |
---|
| 63 | print(compress(A)); |
---|
| 64 | module m=module(A); show(m); |
---|
| 65 | show(compress(m)); |
---|
| 66 | intmat B[3][4]=1,0,3,0,4,0,5,0,6,0,7,0; |
---|
| 67 | compress(B); |
---|
| 68 | } |
---|
| 69 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 70 | proc concat (list #) |
---|
| 71 | USAGE: concat(A1,A2,..); A1,A2,... matrices |
---|
| 72 | RETURN: matrix, concatenation of A1,A2,... . Number of rows of result matrix is |
---|
| 73 | max(nrows(A1),nrows(A2),...) |
---|
| 74 | EXAMPLE: example concat; shows an example |
---|
| 75 | { |
---|
| 76 | int i; |
---|
| 77 | module B=module(#[1]); |
---|
| 78 | for( i=2; i<=size(#); i++ ) { B=B,module(#[i]); } |
---|
| 79 | return(matrix(B)); |
---|
| 80 | } |
---|
| 81 | example |
---|
| 82 | { "EXAMPLE:"; echo = 2; |
---|
| 83 | ring r=0,(x,y,z),ds; |
---|
| 84 | matrix A[3][3]=1,2,3,x,y,z,x2,y2,z2; |
---|
| 85 | matrix B[2][2]=1,0,2,0; matrix C[1][4]=4,5,x,y; |
---|
| 86 | print(A); |
---|
| 87 | print(B); |
---|
| 88 | print(C); |
---|
| 89 | print(concat(A,B,C)); |
---|
| 90 | } |
---|
| 91 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 92 | |
---|
| 93 | proc diag (list #) |
---|
| 94 | USAGE: diag(p,n); p poly, n integer |
---|
| 95 | diag(A); A matrix |
---|
| 96 | RETURN: diag(p,n): diagonal matrix, p times unitmatrix of size n |
---|
| 97 | diag(A) : n*mxn*m diagonal matrix with entries all the entries of the |
---|
| 98 | nxm matrix A, taken from the 1st row, 2nd row etc of A |
---|
| 99 | EXAMPLE: example diag; shows an example |
---|
| 100 | { |
---|
| 101 | if( size(#)==2 ) { return(matrix(#[1]*freemodule(#[2]))); } |
---|
| 102 | if( size(#)==1 ) |
---|
| 103 | { |
---|
| 104 | int i; ideal id=#[1]; |
---|
| 105 | int n=ncols(id); matrix A[n][n]; |
---|
| 106 | for( i=1; i<=n; i++ ) { A[i,i]=id[i]; } |
---|
| 107 | } |
---|
| 108 | return(A); |
---|
| 109 | } |
---|
| 110 | example |
---|
| 111 | { "EXAMPLE:"; echo = 2; |
---|
| 112 | ring r=0,(x,y,z),ds; |
---|
| 113 | print(diag(xy,4)); |
---|
| 114 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
| 115 | print(A); |
---|
| 116 | print(diag(A)); |
---|
| 117 | } |
---|
| 118 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 119 | |
---|
| 120 | proc flatten (matrix A) |
---|
| 121 | USAGE: flatten(A); A matrix |
---|
| 122 | RETURN: ideal, generated by all entries from A |
---|
| 123 | EXAMPLE: example flatten; shows an example |
---|
| 124 | { |
---|
| 125 | return(ideal(A)); |
---|
| 126 | } |
---|
| 127 | example |
---|
| 128 | { "EXAMPLE:"; echo = 2; |
---|
| 129 | ring r=0,(x,y,z),ds; |
---|
| 130 | matrix A[3][3]=1,2,3,x,y,z,7,8,9; |
---|
| 131 | print(A); |
---|
| 132 | flatten(A); |
---|
| 133 | } |
---|
| 134 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 135 | |
---|
| 136 | proc dsum (list #) |
---|
| 137 | USAGE: dsum(A1,A2,..); A1,A2,... matrices |
---|
| 138 | RETURN: matrix, direct sum of A1,A2,... |
---|
| 139 | EXAMPLE: example dsum; shows an example |
---|
| 140 | { |
---|
| 141 | int i,N,a; |
---|
| 142 | list L; |
---|
| 143 | for( i=1; i<=size(#); i++ ) { N=N+nrows(#[i]); } |
---|
| 144 | for( i=1; i<=size(#); i++ ) |
---|
| 145 | { |
---|
| 146 | matrix B[N][ncols(#[i])]; |
---|
| 147 | B[a+1..a+nrows(#[i]),1..ncols(#[i])]=#[i]; |
---|
| 148 | a=a+nrows(#[i]); |
---|
| 149 | L[i]=B; kill B; |
---|
| 150 | } |
---|
| 151 | return(concat(L)); |
---|
| 152 | } |
---|
| 153 | example |
---|
| 154 | { "EXAMPLE:"; echo = 2; |
---|
| 155 | ring r=0,(x,y,z),ds; |
---|
| 156 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
| 157 | matrix B[2][2]=1,x,y,z; |
---|
| 158 | matrix C[1][4]=4,5,x,y; |
---|
| 159 | print(A); |
---|
| 160 | print(B); |
---|
| 161 | print(C); |
---|
| 162 | print(dsum(A,B,C)); |
---|
| 163 | } |
---|
| 164 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 165 | |
---|
| 166 | proc is_complex (list c) |
---|
| 167 | USAGE: is_complex(c); c = list of size-compatible modules or matrices |
---|
| 168 | RETURN: 1 if c[i]*c[i+1]=0 for all i, 0 if not. |
---|
| 169 | NOTE: Ideals are treated internally as 1-line matrices |
---|
| 170 | EXAMPLE: example is_complex; shows an example |
---|
| 171 | { |
---|
| 172 | int i; |
---|
| 173 | module @test; |
---|
| 174 | for( i=1; i<=size(c)-1; i=i+1 ) |
---|
| 175 | { |
---|
| 176 | c[i]=matrix(c[i]); c[i+1]=matrix(c[i+1]); |
---|
| 177 | @test=c[i]*c[i+1]; |
---|
| 178 | if (size(@test)!=0) |
---|
| 179 | { |
---|
| 180 | if( voice==2 ) { "// argument is not a complex at position",i; } |
---|
| 181 | return(0); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | if( voice==2 ) { "// argument is a complex"; } |
---|
| 185 | return(1); |
---|
| 186 | } |
---|
| 187 | example |
---|
| 188 | { "EXAMPLE:"; echo = 2; |
---|
| 189 | ring r=32003,(x,y,z),ds; |
---|
| 190 | ideal i=x4+y5+z6,xyz,yx2+xz2+zy7; |
---|
| 191 | list L=res(i,0); |
---|
| 192 | is_complex(L); |
---|
| 193 | L[4]=matrix(i); |
---|
| 194 | is_complex(L); |
---|
| 195 | } |
---|
| 196 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 197 | |
---|
| 198 | proc outer (matrix A, matrix B) |
---|
| 199 | USAGE: outer(A,B); A,B matrices |
---|
| 200 | RETURN: matrix, outer product of A and B |
---|
| 201 | EXAMPLE: example outer; shows an example |
---|
| 202 | { |
---|
| 203 | int i,j; list L; |
---|
| 204 | int N=nrows(A)*nrows(B); |
---|
| 205 | matrix C[N][ncols(B)]; |
---|
| 206 | for( i=1; i<=ncols(A); i++ ) |
---|
| 207 | { |
---|
| 208 | for( j=1; j<=nrows(A); j++ ) |
---|
| 209 | { |
---|
| 210 | C[(j-1)*nrows(B)+1..j*nrows(B),1..ncols(B)]=A[j,i]*B; |
---|
| 211 | } |
---|
| 212 | L[i]=C; |
---|
| 213 | } |
---|
| 214 | return(concat(L)); |
---|
| 215 | } |
---|
| 216 | example |
---|
| 217 | { "EXAMPLE:"; echo = 2; |
---|
| 218 | ring r=32003,(x,y,z),ds; |
---|
| 219 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
| 220 | matrix B[2][2]=x,y,0,z; |
---|
| 221 | print(A); |
---|
| 222 | print(B); |
---|
| 223 | print(outer(A,B)); |
---|
| 224 | } |
---|
| 225 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 226 | |
---|
| 227 | proc skewmat (int n, list #) |
---|
| 228 | USAGE: skewmat(n[,id]); n integer, id ideal |
---|
| 229 | RETURN: skew-symmetric nxn matrix, with entries from id |
---|
| 230 | (default: id=maxideal(1)) |
---|
| 231 | NOTE: if id has less than n*(n-1)/2 elements, the matrix is filled with 0's, |
---|
| 232 | skewmat(n); creates the generic skew-symmetric matrix |
---|
| 233 | EXAMPLE: example skewmat; shows an example |
---|
| 234 | { |
---|
| 235 | matrix B[n][n]; |
---|
| 236 | if( size(#)==0 ) { ideal id=maxideal(1); } |
---|
| 237 | else { ideal id=#[1]; } |
---|
| 238 | id = id,B[1..n,1..n]; |
---|
| 239 | int i,j; |
---|
| 240 | for( i=0; i<=n-2; i++ ) |
---|
| 241 | { |
---|
| 242 | B[i+1,i+2..n]=id[j+1..j+n-i-1]; |
---|
| 243 | j=j+n-i-1; |
---|
| 244 | } |
---|
| 245 | matrix A=transpose(B); |
---|
| 246 | B=B-A; |
---|
| 247 | return(B); |
---|
| 248 | } |
---|
| 249 | example |
---|
| 250 | { "EXAMPLE:"; echo = 2; |
---|
| 251 | ring R=0,x(1..5),lp; |
---|
| 252 | print(skewmat(4)); // the generic skew-symmetric matrix |
---|
| 253 | changevar("R1",A_Z("a",5),R); |
---|
| 254 | matrix A=skewmat(6,maxideal(1)^2); |
---|
| 255 | print(A); |
---|
| 256 | int n=4; |
---|
| 257 | ideal i = ideal(randommat(1,n*(n-1)/2,maxideal(1),9)); |
---|
| 258 | print(skewmat(n,i)); // skew matrix of generic linear forms |
---|
| 259 | kill R1; |
---|
| 260 | } |
---|
| 261 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 262 | |
---|
| 263 | proc submat (matrix A, intvec r, intvec c) |
---|
| 264 | USAGE: submat(A,r,c); A=matrix, r,c=intvec |
---|
| 265 | RETURN: matrix, submatrix of A with rows specified by intvec r and columns |
---|
| 266 | specified by intvec c |
---|
| 267 | EXAMPLE: example submat; shows an example |
---|
| 268 | { |
---|
| 269 | matrix B[size(r)][size(c)]=A[r,c]; |
---|
| 270 | return(B); |
---|
| 271 | } |
---|
| 272 | example |
---|
| 273 | { "EXAMPLE:"; echo = 2; |
---|
| 274 | ring R=32003,(x,y,z),lp; |
---|
| 275 | matrix A[4][4]=x,y,z,0,1,2,3,4,5,6,7,8,9,x2,y2,z2; |
---|
| 276 | print(A); |
---|
| 277 | intvec v=1,3,4; |
---|
| 278 | matrix B=submat(A,v,1..3); |
---|
| 279 | print(B); |
---|
| 280 | } |
---|
| 281 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 282 | |
---|
| 283 | proc symmat (int n, list #) |
---|
| 284 | USAGE: symmat(n[,id]); n integer, id ideal |
---|
| 285 | RETURN: symmetric nxn matrix, with entries from id (default: id=maxideal(1)) |
---|
| 286 | NOTE: if id has less than n*(n+1)/2 elements, the matrix is filled with 0's, |
---|
| 287 | symmat(n); creates the generic symmetric matrix |
---|
| 288 | EXAMPLE: example symmat; shows an example |
---|
| 289 | { |
---|
| 290 | matrix B[n][n]; |
---|
| 291 | if( size(#)==0 ) { ideal id=maxideal(1); } |
---|
| 292 | else { ideal id=#[1]; } |
---|
| 293 | id = id,B[1..n,1..n]; |
---|
| 294 | int i,j; |
---|
| 295 | for( i=0; i<=n-1; i++ ) |
---|
| 296 | { |
---|
| 297 | B[i+1,i+1..n]=id[j+1..j+n-i]; |
---|
| 298 | j=j+n-i; |
---|
| 299 | } |
---|
| 300 | matrix A=transpose(B); |
---|
| 301 | for( i=1; i<=n; i++ ) { A[i,i]=0; } |
---|
| 302 | B=A+B; |
---|
| 303 | return(B); |
---|
| 304 | } |
---|
| 305 | example |
---|
| 306 | { "EXAMPLE:"; echo = 2; |
---|
| 307 | ring R=0,x(1..10),lp; |
---|
| 308 | print(symmat(4)); // the generic symmetric matrix |
---|
| 309 | changevar("R1",A_Z("a",5),R); |
---|
| 310 | matrix A=symmat(5,maxideal(1)^2); |
---|
| 311 | print(A); |
---|
| 312 | int n=3; |
---|
| 313 | ideal i = ideal(randommat(1,n*(n+1)/2,maxideal(1),9)); |
---|
| 314 | print(symmat(n,i)); // symmetric matrix of generic linear forms |
---|
| 315 | kill R1; |
---|
| 316 | } |
---|
| 317 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 318 | |
---|
| 319 | proc tensor (matrix A, matrix B) |
---|
| 320 | USAGE: tensor(A,B); A,B matrices |
---|
| 321 | RETURN: matrix, tensor product of A and B |
---|
| 322 | EXAMPLE: example tensor; shows an example |
---|
| 323 | { |
---|
| 324 | int i,j; |
---|
| 325 | matrix C=B; |
---|
| 326 | for( i=2; i<=nrows(A); i++ ) { C=dsum(C,B); } |
---|
| 327 | matrix D[nrows(C)][ncols(A)*nrows(B)]; |
---|
| 328 | for( j=1; j<=nrows(B); j++ ) |
---|
| 329 | { |
---|
| 330 | for( i=1; i<=nrows(A); i++ ) |
---|
| 331 | { |
---|
| 332 | D[(i-1)*nrows(B)+j,(j-1)*ncols(A)+1..j*ncols(A)]=A[i,1..ncols(A)]; |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | return(concat(C,D)); |
---|
| 336 | } |
---|
| 337 | example |
---|
| 338 | { "EXAMPLE:"; echo = 2; |
---|
| 339 | ring r=32003,(x,y,z),(c,ds); |
---|
| 340 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
| 341 | matrix B[2][2]=x,y,0,z; |
---|
| 342 | print(A); |
---|
| 343 | print(B); |
---|
| 344 | print(tensor(A,B)); |
---|
| 345 | } |
---|
| 346 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 347 | |
---|
| 348 | proc unitmat (int n) |
---|
| 349 | USAGE: unitmat(n); n integer >= 0 |
---|
| 350 | RETURN: nxn unit matrix |
---|
| 351 | NOTE: needs a basering, diagonal entries are numbers (=1) in the basering |
---|
| 352 | EXAMPLE: example unitmat; shows an example |
---|
| 353 | { |
---|
| 354 | return(matrix(freemodule(n))); |
---|
| 355 | } |
---|
| 356 | example |
---|
| 357 | { "EXAMPLE:"; echo = 2; |
---|
| 358 | ring r=32003,(x,y,z),lp; |
---|
| 359 | print(xyz*unitmat(4)); |
---|
| 360 | print(unitmat(5)); |
---|
| 361 | } |
---|
| 362 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 363 | |
---|
| 364 | proc gauss_col (matrix m) |
---|
| 365 | USAGE: gauss_col(A); A=matrix with constant coefficients |
---|
| 366 | RETURN: matrix = col-reduced normal form of A |
---|
| 367 | EXAMPLE: example gauss_col; shows an example |
---|
| 368 | { |
---|
| 369 | def R=basering; |
---|
| 370 | changeord("@R","ds,c",R); |
---|
| 371 | option(redSB); option(nointStrategy); |
---|
| 372 | matrix m = imap(R,m); |
---|
| 373 | m = matrix(std(m),nrows(m),ncols(m)); |
---|
| 374 | setring R; |
---|
| 375 | m=imap(@R,m); |
---|
| 376 | option(noredSB); |
---|
| 377 | kill @R; |
---|
| 378 | return(m); |
---|
| 379 | } |
---|
| 380 | example |
---|
| 381 | { "EXAMPLE:"; echo = 2; |
---|
| 382 | ring S; |
---|
| 383 | matrix M[3][4] = 1,3,2,4,2,6,4,8,1,3,4,4; |
---|
| 384 | print(M); |
---|
| 385 | print(gauss_col(M)); |
---|
| 386 | } |
---|
| 387 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 388 | |
---|
| 389 | proc gauss_row (matrix m) |
---|
| 390 | USAGE: gauss_row(A); A=matrix with constant coefficients |
---|
| 391 | RETURN: matrix = row-reduced normal form of A |
---|
| 392 | EXAMPLE: example gauss_row; shows an example |
---|
| 393 | { |
---|
| 394 | m = gauss_col(transpose(m)); |
---|
| 395 | return(transpose(m)); |
---|
| 396 | } |
---|
| 397 | example |
---|
| 398 | { "EXAMPLE:"; echo = 2; |
---|
| 399 | ring S; |
---|
| 400 | matrix M[3][4] = 1,3,2,4,2,6,4,8,1,3,4,4; |
---|
| 401 | print(M); |
---|
| 402 | print(gauss_row(M)); |
---|
| 403 | } |
---|
| 404 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 405 | |
---|
| 406 | proc addcol (matrix A, int c1, poly p, int c2) |
---|
| 407 | USAGE: addcol(A,c1,p,c2); A matrix, p poly, c1, c2 positive integers |
---|
| 408 | RETURN: matrix, A being modified by adding p times column c1 to column c2 |
---|
| 409 | EXAMPLE: example addcol; shows an example |
---|
| 410 | { |
---|
| 411 | A[1..nrows(A),c2]=A[1..nrows(A),c2]+p*A[1..nrows(A),c1]; |
---|
| 412 | return(A); |
---|
| 413 | } |
---|
| 414 | example |
---|
| 415 | { "EXAMPLE:"; echo = 2; |
---|
| 416 | ring r=32003,(x,y,z),lp; |
---|
| 417 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
| 418 | print(A); |
---|
| 419 | print(addcol(A,1,xy,2)); |
---|
| 420 | } |
---|
| 421 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 422 | |
---|
| 423 | proc addrow (matrix A, int r1, poly p, int r2) |
---|
| 424 | USAGE: addcol(A,r1,p,r2); A matrix, p poly, r1, r2 positive integers |
---|
| 425 | RETURN: matrix, A being modified by adding p times row r1 to row r2 |
---|
| 426 | EXAMPLE: example addrow; shows an example |
---|
| 427 | { |
---|
| 428 | A[r2,1..ncols(A)]=A[r2,1..ncols(A)]+p*A[r1,1..ncols(A)]; |
---|
| 429 | return(A); |
---|
| 430 | } |
---|
| 431 | example |
---|
| 432 | { "EXAMPLE:"; echo = 2; |
---|
| 433 | ring r=32003,(x,y,z),lp; |
---|
| 434 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
| 435 | print(A); |
---|
| 436 | print(addrow(A,1,xy,3)); |
---|
| 437 | } |
---|
| 438 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 439 | |
---|
| 440 | proc multcol (matrix A, int c, poly p) |
---|
| 441 | USAGE: addcol(A,c,p); A matrix, p poly, c positive integer |
---|
| 442 | RETURN: matrix, A being modified by multiplying column c with p |
---|
| 443 | EXAMPLE: example multcol; shows an example |
---|
| 444 | { |
---|
| 445 | A[1..nrows(A),c]=p*A[1..nrows(A),c]; |
---|
| 446 | return(A); |
---|
| 447 | } |
---|
| 448 | example |
---|
| 449 | { "EXAMPLE:"; echo = 2; |
---|
| 450 | ring r=32003,(x,y,z),lp; |
---|
| 451 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
| 452 | print(A); |
---|
| 453 | print(multcol(A,2,xy)); |
---|
| 454 | } |
---|
| 455 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 456 | |
---|
| 457 | proc multrow (matrix A, int r, poly p) |
---|
| 458 | USAGE: addcol(A,r,p); A matrix, p poly, r positive integer |
---|
| 459 | RETURN: matrix, A being modified by multiplying row r with p |
---|
| 460 | EXAMPLE: example multrow; shows an example |
---|
| 461 | { |
---|
| 462 | A[r,1..ncols(A)]=p*A[r,1..ncols(A)]; |
---|
| 463 | return(A); |
---|
| 464 | } |
---|
| 465 | example |
---|
| 466 | { "EXAMPLE:"; echo = 2; |
---|
| 467 | ring r=32003,(x,y,z),lp; |
---|
| 468 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
| 469 | print(A); |
---|
| 470 | print(multrow(A,2,xy)); |
---|
| 471 | } |
---|
| 472 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 473 | |
---|
| 474 | proc permcol (matrix A, int c1, int c2) |
---|
| 475 | USAGE: permcol(A,c1,c2); A matrix, c1,c2 positive integers |
---|
| 476 | RETURN: matrix, A being modified by permuting column c1 and c2 |
---|
| 477 | EXAMPLE: example permcol; shows an example |
---|
| 478 | { |
---|
| 479 | matrix B=A; |
---|
| 480 | B[1..nrows(B),c1]=A[1..nrows(A),c2]; |
---|
| 481 | B[1..nrows(B),c2]=A[1..nrows(A),c1]; |
---|
| 482 | return(B); |
---|
| 483 | } |
---|
| 484 | example |
---|
| 485 | { "EXAMPLE:"; echo = 2; |
---|
| 486 | ring r=32003,(x,y,z),lp; |
---|
| 487 | matrix A[3][3]=1,x,3,4,y,6,7,z,9; |
---|
| 488 | print(A); |
---|
| 489 | print(permcol(A,2,3)); |
---|
| 490 | } |
---|
| 491 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 492 | |
---|
| 493 | proc permrow (matrix A, int r1, int r2) |
---|
| 494 | USAGE: permrow(A,r1,r2); A matrix, r1,r2 positive integers |
---|
| 495 | RETURN: matrix, A being modified by permuting row r1 and r2 |
---|
| 496 | EXAMPLE: example permrow; shows an example |
---|
| 497 | { |
---|
| 498 | matrix B=A; |
---|
| 499 | B[r1,1..ncols(B)]=A[r2,1..ncols(A)]; |
---|
| 500 | B[r2,1..ncols(B)]=A[r1,1..ncols(A)]; |
---|
| 501 | return(B); |
---|
| 502 | } |
---|
| 503 | example |
---|
| 504 | { "EXAMPLE:"; echo = 2; |
---|
| 505 | ring r=32003,(x,y,z),lp; |
---|
| 506 | matrix A[3][3]=1,2,3,x,y,z,7,8,9; |
---|
| 507 | print(A); |
---|
| 508 | print(permrow(A,2,1)); |
---|
| 509 | } |
---|
| 510 | //////////////////////////////////////////////////////////////////////////////// |
---|