Element-wise operations: Difference between revisions
Content deleted Content added
Added C# implementation |
Added Factor |
||
Line 569: | Line 569: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Factor}}== |
|||
The <code>math.matrices</code> vocabulary provides matrix-matrix and matrix-scalar arithmetic words. I wasn't able to find any for exponentiation, so I wrote them. |
|||
<lang factor>USING: combinators.extras formatting kernel math.functions |
|||
math.matrices math.vectors prettyprint sequences ; |
|||
: show ( a b words -- ) |
|||
[ |
|||
3dup execute( x x -- x ) [ unparse ] dip |
|||
"%u %u %s = %u\n" printf |
|||
] 2with each ; inline |
|||
: m^n ( m n -- m ) [ ^ ] curry matrix-map ; |
|||
: m^ ( m m -- m ) [ v^ ] 2map ; |
|||
{ { 1 2 } { 3 4 } } { { 5 6 } { 7 8 } } { m+ m- m* m/ m^ } |
|||
{ { -1 9 4 } { 5 -13 0 } } 3 { m+n m-n m*n m/n m^n } |
|||
[ show ] 3bi@</lang> |
|||
{{out}} |
|||
<pre> |
|||
{ { 1 2 } { 3 4 } } { { 5 6 } { 7 8 } } m+ = { { 6 8 } { 10 12 } } |
|||
{ { 1 2 } { 3 4 } } { { 5 6 } { 7 8 } } m- = { { -4 -4 } { -4 -4 } } |
|||
{ { 1 2 } { 3 4 } } { { 5 6 } { 7 8 } } m* = { { 5 12 } { 21 32 } } |
|||
{ { 1 2 } { 3 4 } } { { 5 6 } { 7 8 } } m/ = { { 1/5 1/3 } { 3/7 1/2 } } |
|||
{ { 1 2 } { 3 4 } } { { 5 6 } { 7 8 } } m^ = { { 1 64 } { 2187 65536 } } |
|||
{ { -1 9 4 } { 5 -13 0 } } 3 m+n = { { 2 12 7 } { 8 -10 3 } } |
|||
{ { -1 9 4 } { 5 -13 0 } } 3 m-n = { { -4 6 1 } { 2 -16 -3 } } |
|||
{ { -1 9 4 } { 5 -13 0 } } 3 m*n = { { -3 27 12 } { 15 -39 0 } } |
|||
{ { -1 9 4 } { 5 -13 0 } } 3 m/n = { { -1/3 3 1+1/3 } { 1+2/3 -4-1/3 0 } } |
|||
{ { -1 9 4 } { 5 -13 0 } } 3 m^n = { { -1 729 64 } { 125 -2197 0 } } |
|||
</pre> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |