File: misc.gel

package info (click to toggle)
genius 1.0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 25,308 kB
  • sloc: ansic: 75,620; xml: 71,565; sh: 4,445; makefile: 1,927; lex: 523; yacc: 298; perl: 54
file content (374 lines) | stat: -rw-r--r-- 11,004 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
SetHelp("ApplyOverMatrix", "matrix", "Apply a function over all entries of a matrix and return a matrix of the results")
function ApplyOverMatrix(a,func) = (
	local *;
	if(not IsMatrix(a)) then
		(error("ApplyOverMatrix: argument 1 must be a matrix");bailout)
	else if(not IsFunction(func)) then
		(error("ApplyOverMatrix: argument 2 must be a function");bailout);
	r = zeros(rows(a),columns(a));
	for i = 1 to rows(a) do (
		for j = 1 to columns(a) do (
			r@(i,j) = func(a@(i,j))
		)
	);
	r
);

SetHelp("ApplyOverMatrix2", "matrix", "Apply a function over all entries of 2 matrices (or 1 value and 1 matrix) and return a matrix of the results")
function ApplyOverMatrix2(a,b,func) = (
	local *;
	if(not IsMatrix(a) and not IsMatrix(b)) then
		(error("ApplyOverMatrix2: argument 1 or 2 must be a matrix");bailout)
	else if(not IsFunction(func)) then
		(error("ApplyOverMatrix2: argument 3 must be a function");bailout)

	else if(IsMatrix(a) and IsMatrix(b) and
		(rows(a)!=rows(b) or columns(a)!=columns(b))) then
		(error("ApplyOverMatrix2: cannot apply a function over two matrices of different sizes");bailout);

	if IsMatrix(a) and IsMatrix(b) then (
	        r = zeros(rows(a),columns(a));
		for i = 1 to rows(a) do (
			for j = 1 to columns(a) do (
				r@(i,j) = func(a@(i,j),b@(i,j))
			)
		)
	) else if IsMatrix(a) then (
	        r = zeros(rows(a),columns(a));
		for i = 1 to rows(a) do (
			for j = 1 to columns(a) do (
				r@(i,j) = func(a@(i,j),b)
			)
		)
	) else (
	        r = zeros(rows(b),columns(b));
		for i = 1 to rows(b) do (
			for j = 1 to columns(b) do (
				r@(i,j) = func(a,b@(i,j))
			)
		)
	);
	r
);

#calculate a trace function
SetHelp("Trace", "linear_algebra","Calculate the trace of a matrix");
function Trace(m) = (
	if(not IsMatrix(m) or not IsValueOnly(m)) then
		(error("Trace: argument not a value only matrix");bailout)
	else if(rows(m)!=columns(m)) then
		(error("Trace: matrix not a square");bailout);
	sum i = 1 to rows(m) do m@(i,i)
);
SetHelpAlias("Trace", "trace")
trace = Trace

#calculate convolution of two horizontal vectors
SetHelp("Convolution", "linear_algebra","Calculate convolution of two horizontal vectors");
function Convolution(a,b) = (
	if(not IsMatrix(a) or not IsValueOnly(a) or
	   not IsMatrix(b) or not IsValueOnly(b) or
	   rows(a)>1 or rows(b)>1) then
		(error("Convolution: arguments not value only horizontal vectors");bailout)
	else if(columns(a)!=columns(b)) then
		(error("Convolution: arguments must be identical vectors");bailout);
	ca = columns(a);
	sum i = 1 to ca do a@(1,i)*b@(1,ca-i+1)
);
SetHelpAlias("Convolution", "convol")
convol = Convolution

#calculate convolution of two horizontal vectors and return the result
#not added together but in a vector
SetHelp("ConvolutionVector", "linear_algebra","Calculate convolution of two horizontal vectors");
function ConvolutionVector(a,b) = (
	if(not IsMatrix(a) or not IsValueOnly(a) or
	   not IsMatrix(b) or not IsValueOnly(b) or
	   rows(a)>1 or rows(b)>1) then
		(error("ConvolutionVector: arguments not value only horizontal vectors");bailout)
	else if(columns(a)!=columns(b)) then
		(error("ConvolutionVector: arguments must be identical vectors");bailout);
	ca = columns(a);
	r = zeros (1,ca);
	for i = 1 to ca do (
		r@(1,i) = a@(1,i)*b@(1,ca-i+1)
	);
	r
);

#calculate the sum of all elements in a matrix
SetHelp("MatrixSum", "matrix","Calculate the sum of all elements in a matrix");
function MatrixSum(a) = (
	if IsNull(a) then return 0
	else if(not IsMatrix(a) or not IsValueOnly(a)) then
		(error("MatrixSum: argument not a value only matrix");bailout);
	sum n in a do n
);

SetHelp("MatrixSumSquares", "matrix","Calculate the sum of squares of all elements in a matrix");
function MatrixSumSquares(a) = (
	if IsNull(a) then return 0
	else if(not IsMatrix(a) or not IsValueOnly(a)) then
		(error("MatrixSumSquares: argument not a value only matrix");bailout);
	sum n in a do n^2
);

#calculate the product of all elements in a matrix
SetHelp("MatrixProduct","matrix", "Calculate the product of all elements in a matrix")
function MatrixProduct(a) = (
	if(not IsMatrix(a) or not IsValueOnly(a)) then
		(error("matprod: argument not a value only matrix");bailout);
	prod n in a do n
);

SetHelp("Submatrix", "matrix", "Return column(s) and row(s) from a matrix")
function Submatrix(m,r,c) = [m@(r,c)]

SetHelp("ComplementSubmatrix", "matrix", "Remove column(s) and row(s) from a matrix");
function ComplementSubmatrix(m,r,c) = [m@(IndexComplement(r, rows(m)), IndexComplement (c, columns (m)))]

# Minor of a matrix (determinant of a submatrix given by deleting
# one row and one column)
SetHelp("Minor","linear_algebra", "Get the i-j minor of a matrix")
function Minor(M,i,j) = det (ComplementSubmatrix (M, i, j))

#classical adjoint (adjugate) of a matrix
SetHelp("adj","linear_algebra", "Get the classical adjoint (adjugate) of a matrix");
function adj(m) = (
	if(not IsMatrix(m) or not IsValueOnly(m)) then
		(error("adj: argument not a value-only matrix");bailout)
	else if(rows(m)!=columns(m)) then
		(error("adj: argument not a square matrix");bailout)
	else if(rows(m)<2) then
		(error("adj: argument cannot be 1x1 matrix");bailout);

	a = zeros (rows(m),rows(m));
	for i = 1 to rows(m) do (
		for j = 1 to rows(m) do (
			a@(j,i) = ((-1)^(i+j))*Minor(m,i,j)
		)
	);
	a
);
SetHelpAlias ("adj", "Adjugate")
Adjugate = adj

SetHelp("MinimizeFunction","functions","Find the first value where f(x)=0");
function MinimizeFunction(func,x,incr) = (
	local *;
	if(not IsValue(x) or not IsValue(incr)) then
		(error("MinimizeFunction: x,incr arguments not values");bailout)
	else if(not IsFunction(func)) then
		(error("MinimizeFunction: func argument not a function");bailout);
	while(func(x)>0) do increment x by incr;
	x
);

SetHelp("MakeDiagonal","matrix","Make diagonal matrix from a vector");
function MakeDiagonal(v,arg...) = (
	if IsValue (v) and IsNull (arg) then
		return [v]
	else if IsMatrix (v) and IsNull (arg) then
		m = v
	else if IsValue (v) and IsMatrix (arg) then
		m = [v,arg]
	else
		(error("MakeDiagonal: arguments not a vector or a list of values");bailout);
	r = zeros (elements(m),elements(m));
	for i = 1 to elements(m) do (
		r@(i,i) = m@(i)
	);
	r
);
SetHelpAlias("MakeDiagonal","diag")
diag = MakeDiagonal

SetHelp("SwapRows","matrix","Swap two rows in a matrix");
function SwapRows(m,row1,row2) = (
	if(not IsMatrix(m) or not IsInteger(row1) or
	   not IsInteger(row2)) then
		(error("SwapRows: arguments are not the right type");bailout)
	else if(row1>rows(m) or row2>rows(m)) then
		(error("SwapRows: argument out of range");bailout)
	else if(row1 != row2) then (
		tmp = m@(row1,);
		m@(row1,) = m@(row2,);
		m@(row2,) = tmp
	);
	m
);

SetHelp("RowSum","matrix","Calculate sum of each row in a matrix");
function RowSum(m) = (
	if IsNull(m) then return null
	else if not IsMatrix(m) then
		(error("RowSum: argument not matrix");bailout);
	r = zeros (rows(m),1);
	for i = 1 to rows(m) do (
		for j = 1 to columns(m) do
			increment r@(i,1) by + m@(i,j)
	);
	r
);

SetHelp("RowSumSquares","matrix","Calculate sum of squares of each row in a matrix");
function RowSumSquares(m) = (
	if IsNull(m) then return null
	else if not IsMatrix(m) then
		(error("RowSumSquares: argument not matrix");bailout);
	r = zeros (rows(m),1);
	for i = 1 to rows(m) do (
		for j = 1 to columns(m) do
			increment r@(i,1) by m@(i,j)^2
	);
	r
);

#sort a horizontal vector
SetHelp("SortVector","matrix","Sort vector elements");
function SortVector(v) = (
	if IsNull(v) then return null
	else if not IsVector(v) then
		(error("SortVector: argument not a vector");bailout);

	# cross between bubble and quicksort.  Bubble sort is faster in GEL
	# for short arrays

	function bubble(v) = (
		j = elements(v)-1;
		do (
			unsorted = false;
			for i = 1 to j do (
				if v@(i) > v@(i+1) then (
					v@(i) swapwith v@(i+1);
					unsorted = true
				)
			);
			increment j by -1
		) while unsorted;
		v
	);
	function quicksort(v) = (
		if elements(v) <= 9 then
			bubble(v)
		else (
			pe = IntegerQuotient(elements(v),2);
			piv = v@(pe);
			less = more = .;
			k = 1;
			j = 1;
			for i=1 to pe-1 do (
				if v@(i) <= piv then (
					less@(k) = v@(i);
					increment k
				) else (
					more@(j) = v@(i);
					increment j
				)
			);
			for i=pe+1 to elements(v) do (
				if v@(i) <= piv then (
					less@(k) = v@(i);
					increment k
				) else (
					more@(j) = v@(i);
					increment j
				)
			);
			[quicksort(less), piv, quicksort(more)]
		)
	);
	quicksort(v)
);

SetHelp("ReverseVector","matrix","Reverse elements in a vector");
function ReverseVector(v) = (
	if IsNull(v) then return null
	else if not IsVector(v) then
		(error("ReverseVector: argument not a vector");bailout);
	ev = elements(v);
	for i=1 to ceil((ev-1)/2) do (
		v@(ev-i+1) swapwith v@(i)
	);
	v
);

SetHelp("ShuffleVector","matrix","Shuffle elements in a vector");
function ShuffleVector(v) = (
	if IsNull(v) then return null
	else if not IsVector(v) then
		(error("ShuffleVector: argument not a vector");bailout);

	ev = elements(v);
	for i=ev to 2 by -1 do (
		v@(i) swapwith v@(randint(i)+1)
	);
	v
);

SetHelp("UpperTriangular", "matrix", "Zero out entries below the diagonal")
function UpperTriangular(M) = (
	if not IsMatrix(M) or not IsMatrixSquare(M) then
		(error("UpperTriangular: argument not a square matrix");bailout);
	for i=2 to rows(M) do (
		for j=1 to i-1 do (
			M@(i,j) = 0
		)
	);
	M
)

SetHelp("LowerTriangular", "matrix", "Zero out entries above the diagonal")
function LowerTriangular(M) = (
	if not IsMatrix(M) or not IsMatrixSquare(M) then
		(error("LowerTriangular: argument not a square matrix");bailout);
	UpperTriangular (M.').'
)

SetHelp("CompoundMatrix", "matrix", "Calculate the kth compound matrix of A")
function CompoundMatrix(k,A) = (
	if not IsInteger(k) or k < 1 or k > min(columns(A),rows(A)) or not IsMatrix(A) then
		(error("CompoundMatrix: arguments of right type/size");bailout);
	C=[0];
	gamma = Combinations(k,rows(A));
	omega = Combinations(k,columns(A));
	for i=1 to elements(gamma) do
		for j=1 to elements(omega) do
			C@(i,j) = det (A@(gamma@(i),omega@(j)));
	C
)

SetHelp("MakeVector", "matrix", "Make column vector out of matrix by putting columns above each other")
function MakeVector(A) = (
	if IsNull(A) then
		return null
	else if not IsMatrix(A) then
		(error("MakeVector: argument not a matrix");bailout)
	else if columns(A) == 1 then
		return A
	else if rows(A) == 1 then
		return A.';
	r = null;
	for k=1 to columns(A) do (
		r = [r;A@(,k)]
	);
	r
)
SetHelpAlias("MakeVector", "MakeColumnVector")
MakeColumnVector = MakeVector

SetHelp("MakeRowVector", "matrix", "Make column vector out of matrix by putting rows after each other")
function MakeRowVector(A) = (
	if IsNull(A) then return null
	else if not IsMatrix(A) then
		(error("MakeRowVector: argument not a matrix");bailout)
	else if rows(A) == 1 then
		return A
	else if columns(A) == 1 then
		return A.';
	r = null;
	for k=1 to rows(A) do (
		r = [r,A@(k,)]
	);
	r
)