1 | // $Id: matrix.lib,v 1.6 1998-05-05 11:55:31 krueger Exp $ |
---|
2 | // (GMG/BM, last modified 22.06.96) |
---|
3 | /////////////////////////////////////////////////////////////////////////////// |
---|
4 | |
---|
5 | version="$Id: matrix.lib,v 1.6 1998-05-05 11:55:31 krueger Exp $"; |
---|
6 | info=" |
---|
7 | LIBRARY: matrix.lib PROCEDURES FOR MATRIX OPERATIONS |
---|
8 | |
---|
9 | compress(A); matrix, zero columns from A deleted |
---|
10 | concat(A1,A2,..); matrix, concatenation of matrices A1,A2,... |
---|
11 | diag(p,n); matrix, nxn diagonal matrix with entries poly p |
---|
12 | dsum(A1,A2,..); matrix, direct sum of matrices A1,A2,... |
---|
13 | flatten(A); ideal, generated by entries of matrix A |
---|
14 | genericmat(n,m[,id]); generic nxm matrix [entries from id] |
---|
15 | is_complex(c); 1 if list c is a complex, 0 if not |
---|
16 | outer(A,B); matrix, outer product of matrices A and B |
---|
17 | power(A,n); matrix/intmat, n-th power of matrix/intmat A |
---|
18 | skewmat(n[,id]); generic skew-symmetric nxn matrix [entries from id] |
---|
19 | submat(A,r,c); submatrix of A with rows/cols specified by intvec r/c |
---|
20 | symmat(n[,id]); generic symmetric nxn matrix [entries from id] |
---|
21 | tensor(A,B); matrix, tensor product of matrices A nd B |
---|
22 | unitmat(n); unit square matrix of size n |
---|
23 | gauss_col(A); transform constant matrix A into col-reduced nf |
---|
24 | gauss_row(A); transform constant matrix A into row-reduced nf |
---|
25 | addcol(A,c1,p,c2); add p*(c1-th col) to c2-th column of matrix A, p poly |
---|
26 | addrow(A,r1,p,r2); add p*(r1-th row) to r2-th row of matrix A, p poly |
---|
27 | multcol(A,c,p); multiply c-th column of A with poly p |
---|
28 | multrow(A,r,p); multiply r-th row of A with poly p |
---|
29 | permcol(A,i,j); permute i-th and j-th columns |
---|
30 | permrow(A,i,j); permute i-th and j-th rows |
---|
31 | (parameters in square brackets [] are optional) |
---|
32 | "; |
---|
33 | |
---|
34 | LIB "inout.lib"; |
---|
35 | LIB "ring.lib"; |
---|
36 | LIB "random.lib"; |
---|
37 | /////////////////////////////////////////////////////////////////////////////// |
---|
38 | |
---|
39 | proc compress (A) |
---|
40 | "USAGE: compress(A); A matrix/ideal/module/intmat/intvec |
---|
41 | RETURN: same type, zero columns/generators from A deleted |
---|
42 | (in an intvec zero elements are deleted) |
---|
43 | EXAMPLE: example compress; shows an example |
---|
44 | " |
---|
45 | { |
---|
46 | if( typeof(A)=="matrix" ) { return(matrix(simplify(A,2))); } |
---|
47 | if( typeof(A)=="intmat" or typeof(A)=="intvec" ) |
---|
48 | { |
---|
49 | ring r=0,x,lp; |
---|
50 | if( typeof(A)=="intvec" ) { intmat C=transpose(A); kill A; intmat A=C; } |
---|
51 | module m = matrix(A); |
---|
52 | intmat B[nrows(A)][size(m)]; |
---|
53 | int i,j; |
---|
54 | for( i=1; i<=ncols(A); i=i+1 ) |
---|
55 | { |
---|
56 | if( m[i]!=[0] ) |
---|
57 | { |
---|
58 | j=j+1; |
---|
59 | B[1..nrows(A),j]=A[1..nrows(A),i]; |
---|
60 | } |
---|
61 | } |
---|
62 | if( defined(C) ) { return(intvec(B)); } |
---|
63 | return(B); |
---|
64 | } |
---|
65 | return(simplify(A,2)); |
---|
66 | } |
---|
67 | example |
---|
68 | { "EXAMPLE:"; echo = 2; |
---|
69 | ring r=0,(x,y,z),ds; |
---|
70 | matrix A[3][4]=1,0,3,0,x,0,z,0,x2,0,z2,0; |
---|
71 | print(A); |
---|
72 | print(compress(A)); |
---|
73 | module m=module(A); show(m); |
---|
74 | show(compress(m)); |
---|
75 | intmat B[3][4]=1,0,3,0,4,0,5,0,6,0,7,0; |
---|
76 | compress(B); |
---|
77 | intvec C=0,0,1,2,0,3; |
---|
78 | compress(C); |
---|
79 | } |
---|
80 | //////////////////////////////////////////////////////////////////////////////// |
---|
81 | proc concat (list #) |
---|
82 | "USAGE: concat(A1,A2,..); A1,A2,... matrices |
---|
83 | RETURN: matrix, concatenation of A1,A2,... . Number of rows of result matrix is |
---|
84 | max(nrows(A1),nrows(A2),...) |
---|
85 | EXAMPLE: example concat; shows an example |
---|
86 | " |
---|
87 | { |
---|
88 | int i; |
---|
89 | module B=module(#[1]); |
---|
90 | for( i=2; i<=size(#); i=i+1 ) { B=B,module(#[i]); } |
---|
91 | return(matrix(B)); |
---|
92 | } |
---|
93 | example |
---|
94 | { "EXAMPLE:"; echo = 2; |
---|
95 | ring r=0,(x,y,z),ds; |
---|
96 | matrix A[3][3]=1,2,3,x,y,z,x2,y2,z2; |
---|
97 | matrix B[2][2]=1,0,2,0; matrix C[1][4]=4,5,x,y; |
---|
98 | print(A); |
---|
99 | print(B); |
---|
100 | print(C); |
---|
101 | print(concat(A,B,C)); |
---|
102 | } |
---|
103 | //////////////////////////////////////////////////////////////////////////////// |
---|
104 | |
---|
105 | proc diag (list #) |
---|
106 | "USAGE: diag(p,n); p poly, n integer |
---|
107 | diag(A); A matrix |
---|
108 | RETURN: diag(p,n): diagonal matrix, p times unitmatrix of size n |
---|
109 | diag(A) : n*mxn*m diagonal matrix with entries all the entries of the |
---|
110 | nxm matrix A, taken from the 1st row, 2nd row etc of A |
---|
111 | EXAMPLE: example diag; shows an example |
---|
112 | " |
---|
113 | { |
---|
114 | if( size(#)==2 ) { return(matrix(#[1]*freemodule(#[2]))); } |
---|
115 | if( size(#)==1 ) |
---|
116 | { |
---|
117 | int i; ideal id=#[1]; |
---|
118 | int n=ncols(id); matrix A[n][n]; |
---|
119 | for( i=1; i<=n; i=i+1 ) { A[i,i]=id[i]; } |
---|
120 | } |
---|
121 | return(A); |
---|
122 | } |
---|
123 | example |
---|
124 | { "EXAMPLE:"; echo = 2; |
---|
125 | ring r=0,(x,y,z),ds; |
---|
126 | print(diag(xy,4)); |
---|
127 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
128 | print(A); |
---|
129 | print(diag(A)); |
---|
130 | } |
---|
131 | //////////////////////////////////////////////////////////////////////////////// |
---|
132 | |
---|
133 | proc dsum (list #) |
---|
134 | "USAGE: dsum(A1,A2,..); A1,A2,... matrices |
---|
135 | RETURN: matrix, direct sum of A1,A2,... |
---|
136 | EXAMPLE: example dsum; shows an example |
---|
137 | " |
---|
138 | { |
---|
139 | int i,N,a; |
---|
140 | list L; |
---|
141 | for( i=1; i<=size(#); i=i+1 ) { N=N+nrows(#[i]); } |
---|
142 | for( i=1; i<=size(#); i=i+1 ) |
---|
143 | { |
---|
144 | matrix B[N][ncols(#[i])]; |
---|
145 | B[a+1..a+nrows(#[i]),1..ncols(#[i])]=#[i]; |
---|
146 | a=a+nrows(#[i]); |
---|
147 | L[i]=B; kill B; |
---|
148 | } |
---|
149 | return(concat(L)); |
---|
150 | } |
---|
151 | example |
---|
152 | { "EXAMPLE:"; echo = 2; |
---|
153 | ring r=0,(x,y,z),ds; |
---|
154 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
155 | matrix B[2][2]=1,x,y,z; |
---|
156 | matrix C[1][4]=4,5,x,y; |
---|
157 | print(A); |
---|
158 | print(B); |
---|
159 | print(C); |
---|
160 | print(dsum(A,B,C)); |
---|
161 | } |
---|
162 | //////////////////////////////////////////////////////////////////////////////// |
---|
163 | |
---|
164 | proc flatten (matrix A) |
---|
165 | "USAGE: flatten(A); A matrix |
---|
166 | RETURN: ideal, generated by all entries from A |
---|
167 | EXAMPLE: example flatten; shows an example |
---|
168 | " |
---|
169 | { |
---|
170 | return(ideal(A)); |
---|
171 | } |
---|
172 | example |
---|
173 | { "EXAMPLE:"; echo = 2; |
---|
174 | ring r=0,(x,y,z),ds; |
---|
175 | matrix A[3][3]=1,2,3,x,y,z,7,8,9; |
---|
176 | print(A); |
---|
177 | flatten(A); |
---|
178 | } |
---|
179 | //////////////////////////////////////////////////////////////////////////////// |
---|
180 | |
---|
181 | proc genericmat (int n,int m,list #) |
---|
182 | "USAGE: genericmat(n,m[,id]); n,m=integers, id=ideal |
---|
183 | RETURN: nxm matrix, with entries from id (default: id=maxideal(1)) |
---|
184 | NOTE: if id has less than nxm elements, the matrix is filled with 0's, |
---|
185 | genericmat(n,m); creates the generic nxm matrix |
---|
186 | EXAMPLE: example genericmat; shows an example |
---|
187 | " |
---|
188 | { |
---|
189 | if( size(#)==0 ) { ideal id=maxideal(1); } |
---|
190 | if( size(#)==1 ) { ideal id=#[1]; } |
---|
191 | if( size(#)>=2 ) { "// give 3 arguments, 3-rd argument must be an ideal"; } |
---|
192 | matrix B[n][m]=id; |
---|
193 | return(B); |
---|
194 | } |
---|
195 | example |
---|
196 | { "EXAMPLE:"; echo = 2; |
---|
197 | ring R=0,x(1..16),lp; |
---|
198 | print(genericmat(4,4)); // the generic 4x4 matrix |
---|
199 | changevar("R1",A_Z("a",4),R); |
---|
200 | matrix A=genericmat(4,5,maxideal(1)^3); |
---|
201 | print(A); |
---|
202 | int n,m=4,3; |
---|
203 | ideal i = ideal(randommat(1,n*m,maxideal(1),9)); |
---|
204 | print(genericmat(n,m,i)); // matrix of generic linear forms |
---|
205 | kill R1; |
---|
206 | } |
---|
207 | /////////////////////////////////////////////////////////////////////////////// |
---|
208 | |
---|
209 | proc is_complex (list c) |
---|
210 | "USAGE: is_complex(c); c = list of size-compatible modules or matrices |
---|
211 | RETURN: 1 if c[i]*c[i+1]=0 for all i, 0 if not. |
---|
212 | NOTE: Ideals are treated internally as 1-line matrices |
---|
213 | EXAMPLE: example is_complex; shows an example |
---|
214 | " |
---|
215 | { |
---|
216 | int i; |
---|
217 | module @test; |
---|
218 | for( i=1; i<=size(c)-1; i=i+1 ) |
---|
219 | { |
---|
220 | c[i]=matrix(c[i]); c[i+1]=matrix(c[i+1]); |
---|
221 | @test=c[i]*c[i+1]; |
---|
222 | if (size(@test)!=0) |
---|
223 | { |
---|
224 | if( voice==2 ) { "// argument is not a complex at position",i; } |
---|
225 | return(0); |
---|
226 | } |
---|
227 | } |
---|
228 | if( voice==2 ) { "// argument is a complex"; } |
---|
229 | return(1); |
---|
230 | } |
---|
231 | example |
---|
232 | { "EXAMPLE:"; echo = 2; |
---|
233 | ring r=32003,(x,y,z),ds; |
---|
234 | ideal i=x4+y5+z6,xyz,yx2+xz2+zy7; |
---|
235 | list L=res(i,0); |
---|
236 | is_complex(L); |
---|
237 | L[4]=matrix(i); |
---|
238 | is_complex(L); |
---|
239 | } |
---|
240 | //////////////////////////////////////////////////////////////////////////////// |
---|
241 | |
---|
242 | proc outer (matrix A, matrix B) |
---|
243 | "USAGE: outer(A,B); A,B matrices |
---|
244 | RETURN: matrix, outer product of A and B |
---|
245 | EXAMPLE: example outer; shows an example |
---|
246 | " |
---|
247 | { |
---|
248 | int i,j; list L; |
---|
249 | int triv = nrows(B)*ncols(B); |
---|
250 | if( triv==1 ) |
---|
251 | { |
---|
252 | return(B[1,1]*A); |
---|
253 | } |
---|
254 | else |
---|
255 | { |
---|
256 | int N = nrows(A)*nrows(B); |
---|
257 | matrix C[N][ncols(B)]; |
---|
258 | for( i=1; i<=ncols(A); i=i+1 ) |
---|
259 | { |
---|
260 | for( j=1; j<=nrows(A); j=j+1 ) |
---|
261 | { |
---|
262 | C[(j-1)*nrows(B)+1..j*nrows(B),1..ncols(B)]=A[j,i]*B; |
---|
263 | } |
---|
264 | L[i]=C; |
---|
265 | } |
---|
266 | return(concat(L)); |
---|
267 | } |
---|
268 | } |
---|
269 | example |
---|
270 | { "EXAMPLE:"; echo = 2; |
---|
271 | ring r=32003,(x,y,z),ds; |
---|
272 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
273 | matrix B[2][2]=x,y,0,z; |
---|
274 | print(A); |
---|
275 | print(B); |
---|
276 | print(outer(A,B)); |
---|
277 | } |
---|
278 | //////////////////////////////////////////////////////////////////////////////// |
---|
279 | |
---|
280 | proc power ( A, int n) |
---|
281 | "USAGE: power(A,n); A a square-matrix of type intmat or matrix, n=integer |
---|
282 | RETURN: inmat resp. matrix, the n-th power of A |
---|
283 | NOTE: for intamt and big n the result may be wrong because of int overflow |
---|
284 | EXAMPLE: example power; shows an example |
---|
285 | " |
---|
286 | { |
---|
287 | //---------------------------- type checking ---------------------------------- |
---|
288 | if( typeof(A)!="matrix" and typeof(A)!="intmat" ) |
---|
289 | { |
---|
290 | "// no matrix or intmat!"; |
---|
291 | return (A); |
---|
292 | } |
---|
293 | if( ncols(A) != nrows(A) ) |
---|
294 | { |
---|
295 | "// not a suare matrix!"; |
---|
296 | return(); |
---|
297 | } |
---|
298 | //---------------------------- trivial cases ---------------------------------- |
---|
299 | int ii; |
---|
300 | if( n <= 0 ) |
---|
301 | { |
---|
302 | if( typeof(A)=="matrix" ) |
---|
303 | { |
---|
304 | return (unitmat(nrows(A))); |
---|
305 | } |
---|
306 | if( typeof(A)=="intmat" ) |
---|
307 | { |
---|
308 | intmat B[nrows(A)][nrows(A)]; |
---|
309 | for( ii=1; ii<=nrows(A); ii++ ) |
---|
310 | { |
---|
311 | B[ii,ii] = 1; |
---|
312 | } |
---|
313 | return (B); |
---|
314 | } |
---|
315 | } |
---|
316 | if( n == 1 ) { return (A); } |
---|
317 | //---------------------------- sub procedure ---------------------------------- |
---|
318 | proc matpow (A, int n) |
---|
319 | { |
---|
320 | def B = A*A; |
---|
321 | int ii= 2; |
---|
322 | int jj= 4; |
---|
323 | while( jj <= n ) |
---|
324 | { |
---|
325 | B=B*B; |
---|
326 | ii=jj; |
---|
327 | jj=2*jj; |
---|
328 | } |
---|
329 | return(B,n-ii); |
---|
330 | } |
---|
331 | //----------------------------- main program ---------------------------------- |
---|
332 | list L = matpow(A,n); |
---|
333 | def B = L[1]; |
---|
334 | ii = L[2]; |
---|
335 | while( ii>=2 ) |
---|
336 | { |
---|
337 | L = matpow(A,ii); |
---|
338 | B = B*L[1]; |
---|
339 | ii= L[2]; |
---|
340 | } |
---|
341 | if( ii == 0) { return(B); } |
---|
342 | if( ii == 1) { return(A*B); } |
---|
343 | } |
---|
344 | example |
---|
345 | { "EXAMPLE:"; echo = 2; |
---|
346 | intmat A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
347 | print(power(A,3));""; |
---|
348 | ring r=0,(x,y,z),dp; |
---|
349 | matrix B[4][4]=0,x,y,z,0,0,y,z,0,0,0,z,x,y,z,0; |
---|
350 | print(power(B,3));""; |
---|
351 | matrix C[3][3]=1,2,3,4,5,6,7,8,9; |
---|
352 | power(C,50); |
---|
353 | } |
---|
354 | //////////////////////////////////////////////////////////////////////////////// |
---|
355 | |
---|
356 | proc skewmat (int n, list #) |
---|
357 | "USAGE: skewmat(n[,id]); n integer, id ideal |
---|
358 | RETURN: skew-symmetric nxn matrix, with entries from id |
---|
359 | (default: id=maxideal(1)) |
---|
360 | NOTE: if id has less than n*(n-1)/2 elements, the matrix is filled with 0's, |
---|
361 | skewmat(n); creates the generic skew-symmetric matrix |
---|
362 | EXAMPLE: example skewmat; shows an example |
---|
363 | " |
---|
364 | { |
---|
365 | matrix B[n][n]; |
---|
366 | if( size(#)==0 ) { ideal id=maxideal(1); } |
---|
367 | else { ideal id=#[1]; } |
---|
368 | id = id,B[1..n,1..n]; |
---|
369 | int i,j; |
---|
370 | for( i=0; i<=n-2; i=i+1 ) |
---|
371 | { |
---|
372 | B[i+1,i+2..n]=id[j+1..j+n-i-1]; |
---|
373 | j=j+n-i-1; |
---|
374 | } |
---|
375 | matrix A=transpose(B); |
---|
376 | B=B-A; |
---|
377 | return(B); |
---|
378 | } |
---|
379 | example |
---|
380 | { "EXAMPLE:"; echo = 2; |
---|
381 | ring R=0,x(1..5),lp; |
---|
382 | print(skewmat(4)); // the generic skew-symmetric matrix |
---|
383 | changevar("R1",A_Z("a",5),R); |
---|
384 | matrix A=skewmat(6,maxideal(1)^2); |
---|
385 | print(A); |
---|
386 | int n=4; |
---|
387 | ideal i = ideal(randommat(1,n*(n-1) div 2,maxideal(1),9)); |
---|
388 | print(skewmat(n,i)); // skew matrix of generic linear forms |
---|
389 | kill R1; |
---|
390 | } |
---|
391 | //////////////////////////////////////////////////////////////////////////////// |
---|
392 | |
---|
393 | proc submat (matrix A, intvec r, intvec c) |
---|
394 | "USAGE: submat(A,r,c); A=matrix, r,c=intvec |
---|
395 | RETURN: matrix, submatrix of A with rows specified by intvec r and columns |
---|
396 | specified by intvec c |
---|
397 | EXAMPLE: example submat; shows an example |
---|
398 | " |
---|
399 | { |
---|
400 | matrix B[size(r)][size(c)]=A[r,c]; |
---|
401 | return(B); |
---|
402 | } |
---|
403 | example |
---|
404 | { "EXAMPLE:"; echo = 2; |
---|
405 | ring R=32003,(x,y,z),lp; |
---|
406 | matrix A[4][4]=x,y,z,0,1,2,3,4,5,6,7,8,9,x2,y2,z2; |
---|
407 | print(A); |
---|
408 | intvec v=1,3,4; |
---|
409 | matrix B=submat(A,v,1..3); |
---|
410 | print(B); |
---|
411 | } |
---|
412 | //////////////////////////////////////////////////////////////////////////////// |
---|
413 | |
---|
414 | proc symmat (int n, list #) |
---|
415 | "USAGE: symmat(n[,id]); n integer, id ideal |
---|
416 | RETURN: symmetric nxn matrix, with entries from id (default: id=maxideal(1)) |
---|
417 | NOTE: if id has less than n*(n+1)/2 elements, the matrix is filled with 0's, |
---|
418 | symmat(n); creates the generic symmetric matrix |
---|
419 | EXAMPLE: example symmat; shows an example |
---|
420 | " |
---|
421 | { |
---|
422 | matrix B[n][n]; |
---|
423 | if( size(#)==0 ) { ideal id=maxideal(1); } |
---|
424 | else { ideal id=#[1]; } |
---|
425 | id = id,B[1..n,1..n]; |
---|
426 | int i,j; |
---|
427 | for( i=0; i<=n-1; i=i+1 ) |
---|
428 | { |
---|
429 | B[i+1,i+1..n]=id[j+1..j+n-i]; |
---|
430 | j=j+n-i; |
---|
431 | } |
---|
432 | matrix A=transpose(B); |
---|
433 | for( i=1; i<=n; i=i+1 ) { A[i,i]=0; } |
---|
434 | B=A+B; |
---|
435 | return(B); |
---|
436 | } |
---|
437 | example |
---|
438 | { "EXAMPLE:"; echo = 2; |
---|
439 | ring R=0,x(1..10),lp; |
---|
440 | print(symmat(4)); // the generic symmetric matrix |
---|
441 | changevar("R1",A_Z("a",5),R); |
---|
442 | matrix A=symmat(5,maxideal(1)^2); |
---|
443 | print(A); |
---|
444 | int n=3; |
---|
445 | ideal i = ideal(randommat(1,n*(n+1) div 2,maxideal(1),9)); |
---|
446 | print(symmat(n,i)); // symmetric matrix of generic linear forms |
---|
447 | kill R1; |
---|
448 | } |
---|
449 | //////////////////////////////////////////////////////////////////////////////// |
---|
450 | |
---|
451 | proc tensor (matrix A, matrix B) |
---|
452 | "USAGE: tensor(A,B); A,B matrices |
---|
453 | RETURN: matrix, tensor product of A and B |
---|
454 | EXAMPLE: example tensor; shows an example |
---|
455 | " |
---|
456 | { |
---|
457 | int i,j; |
---|
458 | matrix C=B; |
---|
459 | for( i=2; i<=nrows(A); i=i+1 ) { C=dsum(C,B); } |
---|
460 | matrix D[nrows(C)][ncols(A)*nrows(B)]; |
---|
461 | for( j=1; j<=nrows(B); j=j+1 ) |
---|
462 | { |
---|
463 | for( i=1; i<=nrows(A); i=i+1 ) |
---|
464 | { |
---|
465 | D[(i-1)*nrows(B)+j,(j-1)*ncols(A)+1..j*ncols(A)]=A[i,1..ncols(A)]; |
---|
466 | } |
---|
467 | } |
---|
468 | return(concat(C,D)); |
---|
469 | } |
---|
470 | example |
---|
471 | { "EXAMPLE:"; echo = 2; |
---|
472 | ring r=32003,(x,y,z),(c,ds); |
---|
473 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
474 | matrix B[2][2]=x,y,0,z; |
---|
475 | print(A); |
---|
476 | print(B); |
---|
477 | print(tensor(A,B)); |
---|
478 | } |
---|
479 | //////////////////////////////////////////////////////////////////////////////// |
---|
480 | |
---|
481 | proc unitmat (int n) |
---|
482 | "USAGE: unitmat(n); n integer >= 0 |
---|
483 | RETURN: nxn unit matrix |
---|
484 | NOTE: needs a basering, diagonal entries are numbers (=1) in the basering |
---|
485 | EXAMPLE: example unitmat; shows an example |
---|
486 | " |
---|
487 | { |
---|
488 | return(matrix(freemodule(n))); |
---|
489 | } |
---|
490 | example |
---|
491 | { "EXAMPLE:"; echo = 2; |
---|
492 | ring r=32003,(x,y,z),lp; |
---|
493 | print(xyz*unitmat(4)); |
---|
494 | print(unitmat(5)); |
---|
495 | } |
---|
496 | /////////////////////////////////////////////////////////////////////////////// |
---|
497 | |
---|
498 | proc gauss_col (matrix A) |
---|
499 | "USAGE: gauss_col(A); A=matrix with constant coefficients |
---|
500 | RETURN: matrix = col-reduced lower-triagonal normal form of A |
---|
501 | NOTE: the procedure sets the global option-command: option(noredSB); |
---|
502 | EXAMPLE: example gauss_col; shows an example |
---|
503 | " |
---|
504 | { |
---|
505 | def R=basering; |
---|
506 | changeord("@R","ds,c",R); |
---|
507 | option(redSB); option(nointStrategy); |
---|
508 | matrix A = imap(R,A); |
---|
509 | A = matrix(std(A),nrows(A),ncols(A)); |
---|
510 | setring R; |
---|
511 | A=imap(@R,A); |
---|
512 | option(noredSB); |
---|
513 | kill @R; |
---|
514 | return(A); |
---|
515 | } |
---|
516 | example |
---|
517 | { "EXAMPLE:"; echo = 2; |
---|
518 | ring S=0,x,dp; |
---|
519 | matrix A[5][4] = 3, 1,1,-1, |
---|
520 | 13, 8,6,-7, |
---|
521 | 14,10,6,-7, |
---|
522 | 7, 4,3,-3, |
---|
523 | 2, 1,0, 3; |
---|
524 | print(gauss_col(A)); |
---|
525 | } |
---|
526 | /////////////////////////////////////////////////////////////////////////////// |
---|
527 | |
---|
528 | proc gauss_row (matrix A) |
---|
529 | "USAGE: gauss_row(A); A=matrix with constant coefficients |
---|
530 | RETURN: matrix = row-reduced upper-triangular normal form of A |
---|
531 | NOTE: may be used to solve a system of linear equations |
---|
532 | see proc 'linearsolve' from 'solve.lib' for a different method |
---|
533 | the procedure sets the global option-command: option(noredSB); |
---|
534 | EXAMPLE: example gauss_row; shows an example |
---|
535 | " |
---|
536 | { |
---|
537 | A = gauss_col(transpose(A)); |
---|
538 | return(transpose(A)); |
---|
539 | } |
---|
540 | example |
---|
541 | { "EXAMPLE:"; echo = 2; |
---|
542 | ring S=0,x,dp; |
---|
543 | matrix A[4][5] = 3, 1,1,-1,2, |
---|
544 | 13, 8,6,-7,1, |
---|
545 | 14,10,6,-7,1, |
---|
546 | 7, 4,3,-3,3; |
---|
547 | print(gauss_row(A)); |
---|
548 | } |
---|
549 | //////////////////////////////////////////////////////////////////////////////// |
---|
550 | |
---|
551 | proc addcol (matrix A, int c1, poly p, int c2) |
---|
552 | "USAGE: addcol(A,c1,p,c2); A matrix, p poly, c1, c2 positive integers |
---|
553 | RETURN: matrix, A being modified by adding p times column c1 to column c2 |
---|
554 | EXAMPLE: example addcol; shows an example |
---|
555 | " |
---|
556 | { |
---|
557 | A[1..nrows(A),c2]=A[1..nrows(A),c2]+p*A[1..nrows(A),c1]; |
---|
558 | return(A); |
---|
559 | } |
---|
560 | example |
---|
561 | { "EXAMPLE:"; echo = 2; |
---|
562 | ring r=32003,(x,y,z),lp; |
---|
563 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
564 | print(A); |
---|
565 | print(addcol(A,1,xy,2)); |
---|
566 | } |
---|
567 | //////////////////////////////////////////////////////////////////////////////// |
---|
568 | |
---|
569 | proc addrow (matrix A, int r1, poly p, int r2) |
---|
570 | "USAGE: addcol(A,r1,p,r2); A matrix, p poly, r1, r2 positive integers |
---|
571 | RETURN: matrix, A being modified by adding p times row r1 to row r2 |
---|
572 | EXAMPLE: example addrow; shows an example |
---|
573 | " |
---|
574 | { |
---|
575 | A[r2,1..ncols(A)]=A[r2,1..ncols(A)]+p*A[r1,1..ncols(A)]; |
---|
576 | return(A); |
---|
577 | } |
---|
578 | example |
---|
579 | { "EXAMPLE:"; echo = 2; |
---|
580 | ring r=32003,(x,y,z),lp; |
---|
581 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
582 | print(A); |
---|
583 | print(addrow(A,1,xy,3)); |
---|
584 | } |
---|
585 | //////////////////////////////////////////////////////////////////////////////// |
---|
586 | |
---|
587 | proc multcol (matrix A, int c, poly p) |
---|
588 | "USAGE: addcol(A,c,p); A matrix, p poly, c positive integer |
---|
589 | RETURN: matrix, A being modified by multiplying column c with p |
---|
590 | EXAMPLE: example multcol; shows an example |
---|
591 | " |
---|
592 | { |
---|
593 | A[1..nrows(A),c]=p*A[1..nrows(A),c]; |
---|
594 | return(A); |
---|
595 | } |
---|
596 | example |
---|
597 | { "EXAMPLE:"; echo = 2; |
---|
598 | ring r=32003,(x,y,z),lp; |
---|
599 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
600 | print(A); |
---|
601 | print(multcol(A,2,xy)); |
---|
602 | } |
---|
603 | //////////////////////////////////////////////////////////////////////////////// |
---|
604 | |
---|
605 | proc multrow (matrix A, int r, poly p) |
---|
606 | "USAGE: addcol(A,r,p); A matrix, p poly, r positive integer |
---|
607 | RETURN: matrix, A being modified by multiplying row r with p |
---|
608 | EXAMPLE: example multrow; shows an example |
---|
609 | " |
---|
610 | { |
---|
611 | A[r,1..ncols(A)]=p*A[r,1..ncols(A)]; |
---|
612 | return(A); |
---|
613 | } |
---|
614 | example |
---|
615 | { "EXAMPLE:"; echo = 2; |
---|
616 | ring r=32003,(x,y,z),lp; |
---|
617 | matrix A[3][3]=1,2,3,4,5,6,7,8,9; |
---|
618 | print(A); |
---|
619 | print(multrow(A,2,xy)); |
---|
620 | } |
---|
621 | //////////////////////////////////////////////////////////////////////////////// |
---|
622 | |
---|
623 | proc permcol (matrix A, int c1, int c2) |
---|
624 | "USAGE: permcol(A,c1,c2); A matrix, c1,c2 positive integers |
---|
625 | RETURN: matrix, A being modified by permuting column c1 and c2 |
---|
626 | EXAMPLE: example permcol; shows an example |
---|
627 | " |
---|
628 | { |
---|
629 | matrix B=A; |
---|
630 | B[1..nrows(B),c1]=A[1..nrows(A),c2]; |
---|
631 | B[1..nrows(B),c2]=A[1..nrows(A),c1]; |
---|
632 | return(B); |
---|
633 | } |
---|
634 | example |
---|
635 | { "EXAMPLE:"; echo = 2; |
---|
636 | ring r=32003,(x,y,z),lp; |
---|
637 | matrix A[3][3]=1,x,3,4,y,6,7,z,9; |
---|
638 | print(A); |
---|
639 | print(permcol(A,2,3)); |
---|
640 | } |
---|
641 | //////////////////////////////////////////////////////////////////////////////// |
---|
642 | |
---|
643 | proc permrow (matrix A, int r1, int r2) |
---|
644 | "USAGE: permrow(A,r1,r2); A matrix, r1,r2 positive integers |
---|
645 | RETURN: matrix, A being modified by permuting row r1 and r2 |
---|
646 | EXAMPLE: example permrow; shows an example |
---|
647 | " |
---|
648 | { |
---|
649 | matrix B=A; |
---|
650 | B[r1,1..ncols(B)]=A[r2,1..ncols(A)]; |
---|
651 | B[r2,1..ncols(B)]=A[r1,1..ncols(A)]; |
---|
652 | return(B); |
---|
653 | } |
---|
654 | example |
---|
655 | { "EXAMPLE:"; echo = 2; |
---|
656 | ring r=32003,(x,y,z),lp; |
---|
657 | matrix A[3][3]=1,2,3,x,y,z,7,8,9; |
---|
658 | print(A); |
---|
659 | print(permrow(A,2,1)); |
---|
660 | } |
---|
661 | //////////////////////////////////////////////////////////////////////////////// |
---|