Staying close to the code you provide, it needs these changes to work:
1) You have two "+" in ij-to-n, remove one of the "+" to compile (you
should have seen an error to that effect in the error tool).
2) laplace doesn't compile because the false branch of "if" doesn't have
stack effect ( x x -- x ) meaning you multiply v* in the true branch but in
the false branch you just map onto the minors sequence, leaving both coeffs
and minors. I fixed it minimally, but this is cleaner:
: laplace ( coeffs minors -- n )
dup last-laplace-level? [
[ coeffs-minors laplace ] map
] unless v* sum ;
3) The tests don't run because you need "kernel" in the using list and one
of your tests has an extra quotation token, and your determinant word is
called "det" in determinant.factor but you call it "determinant" in your
tests.
Also, I think this test is wrong:
[ t ] [ { { 1 2 } { 3 4 } } determinant -1 = ] unit-test
Shouldn't it be -2? (1*4)-(2*3) ?
If you fix all those problems, it compiles and the tests run.
See paste code at http://paste.factorcode.org/paste?id=2907.
Best,
John.
On Tue, Apr 16, 2013 at 12:55 AM, leonard <leonard14...@gmail.com> wrote:
> On Tue, Feb 5, 2013 at 5:58 PM, Doug Coleman <doug.cole...@gmail.com>wrote:
>
>> If you interested in finding the determinant of a matrix, the easiest way
>> is probably to bind to LAPACK or LINPACK using our Fortran FFI. Take a look
>> at extra/math/blas/ffi for an example.
>>
>> I ended up writing the whole thing... It's really inefficient, as it
>> makes temporary matrices. You'd probably want some kind of virtual-matrix
>> or matrix view with the row/column removed.
>>
>> : 2x2-det ( mat -- x )
>> first2 [ [ first ] [ second ] bi* * ] [ [ second ] [ first ] bi* * ]
>> 2bi - ;
>>
>> : row-col-pairs ( row matrix -- seq )
>> 2dup length < [ nip 0 swap ] unless
>> length iota [ 2array ] with map ;
>>
>> : sub-matrices ( row matrix -- seq )
>> [ nip ] [ row-col-pairs ] 2bi
>> [ first2 [ swap remove-nth ] dip '[ _ swap remove-nth ] map ] with {
>> } map-as ;
>>
>> :: laplace-expansion ( row matrix -- x )
>> matrix length 2 = [ matrix 2x2-det ] [
>> 0 matrix [ nth ] [ sub-matrices ] 2bi ! cheat, always expand on
>> first row for recursive case
>> [ row swap laplace-expansion ] map
>> v* [ odd? [ neg ] when ] map-index sum
>> ] if ;
>>
>> : determinant ( matrix -- x )
>> ! TODO: Check for square matrix here
>> 0 swap laplace-expansion ;
>>
>> 0 { { 1 2 3 } { 4 5 6 } { 7 8 9 } } laplace-expansion .
>> 0
>> 1 { { 1 2 3 } { 4 5 6 } { 7 8 9 } } laplace-expansion .
>> 0
>> 2 { { 1 2 3 } { 4 5 6 } { 7 8 9 } } laplace-expansion .
>> 0
>>
>> { { 40 39 38 37 } { 1 1 1 831 } { 22 22 1110 299 } { 13 14 15 17 } }
>> determinant
>>
>> http://www.wolframalpha.com/input/?i=Determinant%5B%5B40%2C39%2C38%2C37%5D%2C%5B1%2C1%2C1%2C831%5D%2C%5B22%2C22%2C1110%2C299%5D%2C%5B13%2C14%2C15%2C17%5D%5D
>>
>>
>> If you have time, try adding a more efficient version to arrays.shaped?
>> (Or bind to LINPACK or implement a better algorithm!)
>>
>> Have fun,
>> Doug
>>
>
> Ok Doug. Made an attempt to roll my own determinant.
>
> Had fun writing a bunch of helper words, but got stuck on the important
> part, the recursive case.
>
> The compiler doesn't like the stack effects in the if statement in my
> recursive case.
>
> Think I'm at the point where I can't proceed without a suggestion.
>
> - Leonard
>
>
>
> ------------------------------------------------------------------------------
> Precog is a next-generation analytics platform capable of advanced
> analytics on semi-structured data. The platform includes APIs for building
> apps and a phenomenal toolset for data science. Developers can use
> our toolset for easy data analysis & visualization. Get a free account!
> http://www2.precog.com/precogplatform/slashdotnewsletter
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk