1 //-----------------------------------------------------------------------------
3 // Input: Matrix on stack
5 // Output: Determinant on stack
9 // > det(((1,2),(3,4)))
14 // Uses Gaussian elimination for numerical matrices.
16 //-----------------------------------------------------------------------------
26 else if (p1
->u
.tensor
->ndim
!= 2)
28 else if (p1
->u
.tensor
->dim
[0] != p1
->u
.tensor
->dim
[1])
44 if (check_arg() == 0) {
52 n
= p1
->u
.tensor
->nelem
;
54 a
= p1
->u
.tensor
->elem
;
56 for (i
= 0; i
< n
; i
++)
63 for (i
= 0; i
< p1
->u
.tensor
->nelem
; i
++)
64 push(p1
->u
.tensor
->elem
[i
]);
65 determinant(p1
->u
.tensor
->dim
[0]);
71 // determinant of n * n matrix elements on the stack
76 int h
, i
, j
, k
, q
, s
, sign
, t
;
81 a
= (int *) malloc(3 * n
* sizeof (int));
90 for (i
= 0; i
< n
; i
++) {
107 for (i
= 0; i
< n
; i
++) {
110 multiply(); // FIXME -- problem here
115 /* next permutation (Knuth's algorithm P) */
135 a
[j
- c
[j
] + s
] = a
[j
- q
+ s
];
144 stack
[h
] = stack
[tos
- 1];
149 //-----------------------------------------------------------------------------
151 // Input: Matrix on stack
153 // Output: Determinant on stack
157 // Uses Gaussian elimination which is faster for numerical matrices.
159 // Gaussian Elimination works by walking down the diagonal and clearing
160 // out the columns below it.
162 //-----------------------------------------------------------------------------
171 if (check_arg() == 0) {
189 n
= p1
->u
.tensor
->dim
[0];
191 for (i
= 0; i
< n
* n
; i
++)
192 push(p1
->u
.tensor
->elem
[i
]);
201 //-----------------------------------------------------------------------------
203 // Input: n * n matrix elements on stack
205 // Output: p1 determinant
209 // upper diagonal matrix on stack
211 //-----------------------------------------------------------------------------
213 #define M(i, j) stack[h + n * (i) + (j)]
224 for (d
= 0; d
< n
- 1; d
++) {
226 // diagonal element zero?
228 if (equal(M(d
, d
), zero
)) {
232 for (i
= d
+ 1; i
< n
; i
++)
233 if (!equal(M(i
, d
), zero
))
243 for (j
= d
; j
< n
; j
++) {
263 // update lower diagonal matrix
265 for (i
= d
+ 1; i
< n
; i
++) {
278 M(i
, d
) = zero
; // clear column below pivot d
280 for (j
= d
+ 1; j
< n
; j
++) {
291 // last diagonal element
294 push(M(n
- 1, n
- 1));