math.matrices: Add a diagonal-matrix word that makes an nxn matrix from a sequence.
Optimize identity-matrix word -- 2-3x faster using diagonal-matrix. Add an eye word that makes an mxn diagonal matrix with a diagonal offset by k.db4
parent
cbf022454b
commit
6f3fe950a7
|
@ -7,14 +7,84 @@ USING: math.matrices math.vectors tools.test math ;
|
|||
3 1 zero-matrix
|
||||
] unit-test
|
||||
|
||||
[
|
||||
{
|
||||
{ { 1 0 0 }
|
||||
{ 0 1 0 }
|
||||
{ 0 0 1 } }
|
||||
] [
|
||||
} [
|
||||
3 identity-matrix
|
||||
] unit-test
|
||||
|
||||
{
|
||||
{ { 1 0 0 }
|
||||
{ 0 2 0 }
|
||||
{ 0 0 3 } }
|
||||
} [
|
||||
{ 1 2 3 } diagonal-matrix
|
||||
] unit-test
|
||||
|
||||
{
|
||||
{
|
||||
{ 1 0 0 }
|
||||
{ 0 1 0 }
|
||||
{ 0 0 1 }
|
||||
}
|
||||
} [
|
||||
3 3 0 eye
|
||||
] unit-test
|
||||
|
||||
{
|
||||
{
|
||||
{ 0 1 0 }
|
||||
{ 0 0 1 }
|
||||
{ 0 0 0 }
|
||||
}
|
||||
} [
|
||||
3 3 1 eye
|
||||
] unit-test
|
||||
|
||||
{
|
||||
{
|
||||
{ 0 0 0 }
|
||||
{ 1 0 0 }
|
||||
{ 0 1 0 }
|
||||
}
|
||||
} [
|
||||
3 3 -1 eye
|
||||
] unit-test
|
||||
|
||||
{
|
||||
{
|
||||
{ 1 0 0 0 }
|
||||
{ 0 1 0 0 }
|
||||
{ 0 0 1 0 }
|
||||
}
|
||||
} [
|
||||
3 4 0 eye
|
||||
] unit-test
|
||||
|
||||
{
|
||||
{
|
||||
{ 0 1 0 }
|
||||
{ 0 0 1 }
|
||||
{ 0 0 0 }
|
||||
{ 0 0 0 }
|
||||
}
|
||||
} [
|
||||
4 3 1 eye
|
||||
] unit-test
|
||||
|
||||
{
|
||||
{
|
||||
{ 0 0 0 }
|
||||
{ 1 0 0 }
|
||||
{ 0 1 0 }
|
||||
{ 0 0 1 }
|
||||
}
|
||||
} [
|
||||
4 3 -1 eye
|
||||
] unit-test
|
||||
|
||||
[
|
||||
{ { 1 0 4 }
|
||||
{ 0 7 0 }
|
||||
|
|
|
@ -9,9 +9,15 @@ IN: math.matrices
|
|||
: zero-matrix ( m n -- matrix )
|
||||
'[ _ 0 <array> ] replicate ;
|
||||
|
||||
: diagonal-matrix ( diagonal-seq -- matrix )
|
||||
dup length dup zero-matrix
|
||||
[ '[ dup _ nth set-nth ] each-index ] keep ; inline
|
||||
|
||||
: identity-matrix ( n -- matrix )
|
||||
#! Make a nxn identity matrix.
|
||||
iota dup [ = 1 0 ? ] cartesian-map ;
|
||||
1 <repetition> diagonal-matrix ; inline
|
||||
|
||||
: eye ( m n k -- matrix )
|
||||
[ [ iota ] bi@ ] dip neg '[ _ + = 1 0 ? ] cartesian-map ;
|
||||
|
||||
:: rotation-matrix3 ( axis theta -- matrix )
|
||||
theta cos :> c
|
||||
|
|
Loading…
Reference in New Issue