1if lisp !*rounded then rounded_was_on := t
2 else rounded_was_on := nil;
3
4mat1  := mat((1,2,3,4,5),(2,3,4,5,6),(3,4,5,6,7),(4,5,6,7,8),(5,6,7,8,9));
5mat2  := mat((1,1,1,1),(2,2,2,2),(3,3,3,3),(4,4,4,4));
6mat3  := mat((x),(x),(x),(x));
7mat4  := mat((3,3),(4,4),(5,5),(6,6));
8mat5  := mat((1,2,1,1),(1,2,3,1),(4,5,1,2),(3,4,5,6));
9mat6  := mat((i+1,i+2,i+3),(4,5,2),(1,i,0));
10mat7  := mat((1,1,0),(1,3,1),(0,1,1));
11mat8  := mat((1,3),(-4,3));
12mat9  := mat((1,2,3,4),(9,8,7,6));
13mat10 := mat((1,0,0,0,2),(0,0,3,0,0),(0,0,0,0,0),(0,2,0,0,0));
14poly  := x^7+x^5+4*x^4+5*x^3+12;
15poly1 := x^2+x*y^3+x*y*z^3+y*x+2+y*3;
16
17on errcont;
18
19
20% Basis matrix manipulations.
21
22add_columns(mat1,1,2,5*y);
23add_rows(mat1,1,2,x);
24
25add_to_columns(mat1,3,1000);
26add_to_columns(mat1,{1,2,3},y);
27add_to_rows(mat1,2,1000);
28add_to_rows(mat1,{1,2,3},x);
29
30augment_columns(mat1,2);
31augment_columns(mat1,{1,2,5});
32stack_rows(mat1,3);
33stack_rows(mat1,{1,3,5});
34
35char_poly(mat1,x);
36
37column_dim(mat2);
38row_dim(mat1);
39
40copy_into(mat7,mat1,2,3);
41copy_into(mat7,mat1,5,5);
42
43diagonal(3);
44% diagonal can take both a list of arguments or just the arguments.
45diagonal({mat2,mat6});
46diagonal(mat1,mat2,mat5);
47
48extend(mat1,3,2,x);
49
50find_companion(mat5,x);
51
52get_columns(mat1,1);
53get_columns(mat1,{1,2});
54get_rows(mat1,3);
55get_rows(mat1,{1,3});
56
57hermitian_tp(mat6);
58
59% matrix_augment and matrix_stack can take both a list of arguments
60% or just the arguments.
61matrix_augment({mat1,mat2});
62matrix_augment(mat4,mat2,mat4);
63matrix_stack(mat1,mat2);
64matrix_stack({mat6,mat((z,z,z)),mat7});
65
66minor(mat1,2,3);
67
68mult_columns(mat1,3,y);
69mult_columns(mat1,{2,3,4},100);
70mult_rows(mat1,2,x);
71mult_rows(mat1,{1,3,5},10);
72
73pivot(mat1,3,3);
74rows_pivot(mat1,3,3,{1,5});
75
76remove_columns(mat1,3);
77remove_columns(mat1,{2,3,4});
78remove_rows(mat1,2);
79remove_rows(mat1,{1,3});
80remove_rows(mat1,{1,2,3,4,5});
81
82swap_columns(mat1,2,4);
83swap_rows(mat1,1,2);
84swap_entries(mat1,{1,1},{5,5});
85
86
87% Constructors - functions that create matrices.
88
89band_matrix(x,5);
90band_matrix({x,y,z},6);
91
92block_matrix(1,2,{mat1,mat2});
93block_matrix(2,3,{mat2,mat3,mat2,mat3,mat2,mat2});
94
95char_matrix(mat1,x);
96
97cfmat := coeff_matrix({x+y+4*z=10,y+x-z=20,x+y+4});
98first cfmat * second cfmat;
99third cfmat;
100
101companion(poly,x);
102
103hessian(poly1,{w,x,y,z});
104
105hilbert(4,1);
106hilbert(3,y+x);
107
108% NOTE WELL. The function tested here used to be called just "jacobian"
109% however us of that name was in conflict with another Reduce package so
110% now it is called mat_jacobian.
111mat_jacobian({x^4,x*y^2,x*y*z^3},{w,x,y,z});
112
113jordan_block(x,5);
114
115make_identity(11);
116
117on rounded; % makes things a bit easier to read.
118random_matrix(3,3,100);
119on not_negative;
120random_matrix(3,3,100);
121on only_integer;
122random_matrix(3,3,100);
123on symmetric;
124random_matrix(3,3,100);
125off symmetric;
126on upper_matrix;
127random_matrix(3,3,100);
128off upper_matrix;
129on lower_matrix;
130random_matrix(3,3,100);
131off lower_matrix;
132on imaginary;
133off not_negative;
134random_matrix(3,3,100);
135off rounded;
136
137% toeplitz and vandermonde can take both a list of arguments or just
138% the arguments.
139toeplitz({1,2,3,4,5});
140toeplitz(x,y,z);
141
142vandermonde({1,2,3,4,5});
143vandermonde(x,y,z);
144
145% kronecker_product
146
147a1 := mat((1,2),(3,4),(5,6));
148a2 := mat((1,x,1),(2,2,2),(3,3,3));
149
150kronecker_product(a1,a2);
151
152clear a1,a2;
153
154% High level algorithms.
155
156on rounded; % makes output easier to read.
157ch := cholesky(mat7);
158tp first ch - second ch;
159tmp := first ch * second ch;
160tmp - mat7;
161off rounded;
162
163gram_schmidt({1,0,0},{1,1,0},{1,1,1});
164gram_schmidt({1,2},{3,4});
165
166on rounded; % again, makes large quotients a bit more readable.
167% The algorithm used for lu_decom sometimes swaps the rows of the input
168% matrix so that (given matrix A, lu_decom(A) = {L,U,vec}), we find L*U
169% does not equal A but a row equivalent of it. The call convert(A,vec)
170% will return this row equivalent (ie: L*U = convert(A,vec)).
171lu := lu_decom(mat5);
172mat5;
173tmp := first lu * second lu;
174tmp1 := convert(mat5,third lu);
175tmp - tmp1;
176% and the complex case...
177lu1 := lu_decom(mat6);
178mat6;
179tmp := first lu1 * second lu1;
180tmp1 := convert(mat6,third lu1);
181tmp - tmp1;
182
183mat9inv := pseudo_inverse(mat9);
184mat9 * mat9inv;
185
186simplex(min,2*x1+14*x2+36*x3,{-2*x1+x2+4*x3>=5,-x1-2*x2-3*x3<=2});
187
188simplex(max,10000 x1 + 1000 x2 + 100 x3 + 10 x4 + x5,{ x1 <= 1, 20 x1 +
189 x2 <= 100, 200 x1 + 20 x2 + x3 <= 10000, 2000 x1 + 200 x2 + 20 x3 + x4
190 <= 1000000, 20000 x1 + 2000 x2 + 200 x3 + 20 x4 + x5 <= 100000000});
191
192simplex(max, 5 x1 + 4 x2 + 3 x3,
193           { 2 x1 + 3 x2 + x3 <= 5,
194             4 x1 + x2 + 2 x3 <= 11,
195             3 x1 + 4 x2 + 2 x3 <= 8 });
196
197simplex(min,3 x1 + 5 x2,{ x1 + 2 x2 >= 2, 22 x1 + x2 >= 3});
198
199simplex(max,10x+5y+5.5z,{5x+3z<=200,0.2x+0.1y+0.5z<=12,0.1x+0.2y+0.3z<=9,
200                         30x+10y+50z<=1500});
201
202%example of extra variables (>=0) being added.
203simplex(min,x-y,{x>=-3});
204
205% unfeasible as simplex algorithm implies all x>=0.
206simplex(min,x,{x<=-100});
207
208% three error examples.
209simplex(maxx,x,{x>=5});
210simplex(max,x,x>=5);
211simplex(max,x,{x<=y});
212
213simplex(max, 346 x11 + 346 x12 + 248 x21 + 248 x22 + 399 x31 + 399 x32 +
214             200 y11 + 200 y12 + 75 y21 + 75 y22 + 2.35 z1 + 3.5 z2,
215{
216 4 x11 + 4 x12 + 2 x21 + 2 x22 + x31 + x32 + 250 y11 + 250 y12 + 125 y21 +
217  125 y22 <= 25000,
218 x11 + x12 + x21 + x22 + x31 + x32 + 2 y11 + 2 y12 + y21 + y22 <= 300,
219 20 x11 + 15 x12 + 30 y11 + 20 y21 + z1 <= 1500,
220 40 x12 + 35 x22 + 50 x32 + 15 y12 + 10 y22 + z2  = 5000,
221 x31  = 0,
222 y11 + y12 <= 50,
223 y21 + y22 <= 100
224});
225
226
227% from Marc van Dongen. Finding the first feasible solution for the
228% solution of systems of linear diophantine inequalities.
229simplex(max,0,{
230  3*x259+4*x261+3*x262+2*x263+x269+2*x270+3*x271+4*x272+5*x273+x229=2,
231  7*x259+11*x261+8*x262+5*x263+3*x269+6*x270+9*x271+12*x272+15*x273+x229=4,
232  2*x259+5*x261+4*x262+3*x263+3*x268+4*x269+5*x270+6*x271+7*x272+8*x273=1,
233  x262+2*x263+5*x268+4*x269+3*x270+2*x271+x272+2*x229=1,
234  x259+x262+2*x263+4*x268+3*x269+2*x270+x271-x273+3*x229=2,
235  x259+2*x261+2*x262+2*x263+3*x268+3*x269+3*x270+3*x271+3*x272+3*x273+x229=1,
236  x259+x261+x262+x263+x268+x269+x270+x271+x272+x273+x229=1});
237
238
239% An example with a bound section:
240simplex(min, 0,
241   {-n2 <= -1, -n1-n2 <= 0, 2*n1+n2 <= 0, 5*n1+2*n2 <= 0, 5*n1-n2 <= 0},
242   {-infinity <= n1, -infinity <= n2 <= 2});
243
244
245svd_ans := svd(mat8);
246tmp := first svd_ans * second svd_ans * tp third svd_ans;
247tmp - mat8;
248
249svd_ans := svd(mat10);
250tmp := first svd_ans * second svd_ans * tp third svd_ans;
251tmp - mat10;
252
253mat10inv := pseudo_inverse(mat10);
254mat10 * mat10inv * mat10;
255mat10inv * mat10 * mat10inv;
256
257mat9inv := pseudo_inverse(mat9);
258mat9 * mat9inv;
259
260% triang_adjoint(in_mat) calculates the
261% triangularizing adjoint of in_mat
262
263triang_adjoint(mat1);
264triang_adjoint(mat2);
265triang_adjoint(mat5);
266triang_adjoint(mat6);
267triang_adjoint(mat7);
268triang_adjoint(mat8);
269triang_adjoint(mat9);
270
271% testing triang_adjoint with random matrices
272
273% the range of the integers is in one case from
274% -1000 to 1000. in the other case it is from
275% -1 to 1 so that the deteminant of the i-th
276% submatrix equals very often to zero.
277
278% random matrix contains arbitrary real values
279off only_integer;
280tmp:=random_matrix(5,5,1000);
281triang_adjoint tmp;
282
283tmp:=random_matrix(1,1,1000);
284triang_adjoint tmp;
285
286% random matrix contains complex real values
287on imaginary;
288tmp:=random_matrix(5,5,1000);
289triang_adjoint tmp;
290
291tmp:=random_matrix(1,1,1000);
292triang_adjoint tmp;
293off imaginary;
294
295% random matrix contains rounded real values
296on rounded;
297tmp:=random_matrix(5,5,1000);
298triang_adjoint tmp;
299
300tmp:=random_matrix(1,1,1000);
301triang_adjoint tmp;
302off rounded;
303
304% random matrix contains only integer values
305on only_integer;
306tmp:=random_matrix(7,7,1000);
307triang_adjoint tmp;
308
309tmp:=random_matrix(7,7,1);
310triang_adjoint tmp;
311
312% random matrix contains only complex integer
313% values
314
315on imaginary;
316tmp:=random_matrix(5,5,1000);
317triang_adjoint tmp;
318
319tmp:=random_matrix(5,5,2);
320triang_adjoint tmp;
321
322% Predicates.
323
324matrixp(mat1);
325matrixp(poly);
326
327squarep(mat2);
328squarep(mat3);
329
330symmetricp(mat1);
331symmetricp(mat3);
332
333if not rounded_was_on then off rounded;
334
335
336end;
337
338
339