Hello,

I'm trying to create a word for the determinant of a square matrix, for the 
general case where the matrix is 4x4 or larger. The implementation is almost 
complete, except for a type error I can't figure out.

USING: arrays fry kernel math math.matrices math.vectors sequences ;
IN: determinant-mini

: transpose ( matrix -- transposed )
    [ dim second <iota> ] keep cols ;

: exclude-col ( matrix n -- others )
    [ dup dim second <iota> ] dip '[ _ = ] { } reject-as
    swap cols transpose ;

: alternating-sign ( seq odd-elts? -- seq' )
    '[ 2 mod zero? _ = [ neg ] unless ] map-index ;

: alternating-first ( matrix -- alternating )
    first t alternating-sign ;

DEFER: (ndeterminant)
: determinants-rest ( n matrix -- seq )
    rest <repetition>
    [ exclude-col dup length (ndeterminant) ] map-index ; recursive

: (ndeterminant) ( matrix n -- ndet )
    swap [ alternating-first ] [ determinants-rest ] bi v* sum ;
    recursive

The code compiles; use it like `{ { 1 -3 } { 2 5 } } 2 (ndeterminant)`; you 
have to provide the number of rows in the matrix.

I'm referring to https://www.mathsisfun.com/algebra/matrix-determinant.html to 
help me write the determinant of an n*n matrix where n>3. The algorithm I have 
implemented is correct, and this code will work for any n > 1 however.

All the individual parts of the above code work properly and there is no error 
when I run its separate parts in the listener, but in unit tests or when 
calling (ndeterminant) directly I get a type error for using < between an array 
(a element of the input matrix it seems) and an index like 0.

No suitable arithmetic method
left    { 1 3 }
right   0
generic <

I can't really tell where it's coming from but I think it's thrown inside 
determinants-rest's quotation to map-index.

I've been over this code so many times now, another pair of eyes to tell me 
what I'm obviously missing would be appreciated.

Thanks in advance!

Note: you can see the full implementation on the current revision here: 
https://github.com/catb0t/factor/blob/e4fc99f4926bc2e856e64234008a88c0d1e27310/basis/math/matrices/matrices.factor#L346
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to