Jump to content

Matrix multiplication: Difference between revisions

m
(→‎{{header|Tailspin}}: define matmul as an operator)
m (→‎{{header|Lambdatalk}}: small change)
Line 2,904:
 
<lang scheme>
{require lib_matrix}
{def mat*mat
{lambda {:a :b}
{A.new {S.map {{lambda {:a :b :i} // loop on :i
{A.new {S.map {{lambda {:a :b :i :j} // loop on :j
{+ {S.map {{lambda {:a :b :i :j :k} // loop on :k
{* {A.get :k {A.get :i :a}} // a[i,k]*b[k,j]
{A.get :j {A.get :k :b}}}
} :a :b :i :j} {S.serie 0 {- {A.length :b} 1}} }}
} :a :b :i} {S.serie 0 {- {A.length {A.get 0 :b}} 1}} }}
} :a :b} {S.serie 0 {- {A.length :a} 1}} }
}}}
-> mat*mat
 
1) applying a matrix to a vector
{def A {A.new {A.new 1 2}
{A.new 3 4}
{A.new 5 6}
{A.new 7 8}}}
-> A
{def B {A.new {A.new 1 2 3}
{A.new 4 5 6}}}
-> B
 
{def M
{mat*mat {A} {B}}
-> {M.new [[91,122,153],
[194,265,336],
[297,408,51-9],]}}
-> BM
[39,54,69]]
 
{def AV {A.new {AM.new [1,2,3]} 2}
-> AV
 
{mat*matM.multiply {AM} {BV}}
-> [14,32,-4]
 
2) matrix multiplication
 
{M.multiply {M} {M}}
-> [[ 30, 36,-12],
[ 66, 81,-12],
[39-24,54-18,69150]]
</lang>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.