kigubkur.op.mtimes
Operate: matrix multiplication (applicable to blocks).
mtimes
(require '[kigubkur.op [mtimes :refer [mtimes]]])
The definition and properties for the matrix multiplication operator mtimes
is described here.
Examples
Example 1
=> (def A [[2 4]
[3 2]])
=> (def B [[1 3]
[-2 5]])
=> (view (mtimes A B))
[-6 26]
[-1 19]
Order -> 2 x 2
=> (view (mtimes B A))
[11 10]
[11 2]
Order -> 2 x 2
Example 2
=> (def A [[2 3 4]
[3 4 5]
[4 5 6]])
=> (def B [[1 -3 5]
[0 2 4]
[3 0 5]])
=> (view (mtimes A B))
[14 0 42]
[18 -1 56]
[22 -2 70]
Order -> 3 x 3
Example 3
=> (def A [[1 -2]
[2 3]])
=> (def B [[1 2 3]
[2 3 1]])
=> (view (mtimes A B))
[-3 -4 1]
[8 13 9]
Order -> 2 x 3
Example 4
=> (def A [[3 -1 3]
[-1 0 2]])
=> (def B [[2 -3]
[1 0]
[3 1]])
=> (view (mtimes A B))
[14 -6]
[4 5]
Order -> 2 x 2
Example 5
=> (def A [[2 1]
[3 2]
[-1 1]])
=> (def B [[1 0 3]
[-1 2 1]])
=> (view (mtimes A B))
[1 2 7]
[1 4 11]
[-2 2 -2]
Order -> 3 x 3
Example 6
=> (def r [[7 3 5]])
=> (def c [[-8] [4] [2]])
=> (mtimes r c)
[[-34]]
Note that mtimes
is the matrix multiplication operator therefore it returns a matrix. So although mtimes
can produce the dot-product it is returned as a single-element matrix. But if the multiplication order is changed such that the column is the multiplier we see
=> (view (mtimes c r))
[-56 -24 -40]
[28 12 20]
[14 6 10]
Order -> 3 x 3
Block
(require '[kigubkur.construct [blocking :refer [block]]])
=> (def L1 [[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]
[1 2 3 4 5 6 7 8 9 10]])
=> (def B1 (block L1 5))
=> (def L2 [[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]
[10 9 8 7 6 5 4 3 2 1]])
=> (def B2 (block L2 5))
=> (pprint B1)
[[{:A11 [[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]],
:A12 [[6 7 8 9 10]
[6 7 8 9 10]
[6 7 8 9 10]
[6 7 8 9 10]
[6 7 8 9 10]]}]
[{:A22 [[6 7 8 9 10]
[6 7 8 9 10]],
:A21 [[1 2 3 4 5]
[1 2 3 4 5]]}]]
=> (pprint B2)
[[{:A11 [[10 9 8 7 6]
[10 9 8 7 6]
[10 9 8 7 6]
[10 9 8 7 6]
[10 9 8 7 6]],
:A12 [[5 4 3 2 1]
[5 4 3 2 1]
[5 4 3 2 1]
[5 4 3 2 1]
[5 4 3 2 1]]}]
[{:A22 [[5 4 3 2 1]
[5 4 3 2 1]
[5 4 3 2 1]
[5 4 3 2 1]
[5 4 3 2 1]],
:A21 [[10 9 8 7 6]
[10 9 8 7 6]
[10 9 8 7 6]
[10 9 8 7 6]
[10 9 8 7 6]]}]]
=> (pprint (mtimes B1 B2))
[[{:A11 [[550 495 440 385 330]
[550 495 440 385 330]
[550 495 440 385 330]
[550 495 440 385 330]
[550 495 440 385 330]],
:A12 [[275 220 165 110 55]
[275 220 165 110 55]
[275 220 165 110 55]
[275 220 165 110 55]
[275 220 165 110 55]]}]
[{:A21 [[550 495 440 385 330]
[550 495 440 385 330]],
:A22 [[275 220 165 110 55]
[275 220 165 110 55]]}]]
mtimes
(mtimes X Y)
Returns the multiplication of two matrices Xm × n1(=u), Y(u=)m2 × n.
Syntax: (mtimes X Y)
s.t X
, Y
are both kigubkur© matrix (or both blocks)
Note:
if xik or ykj ∈ {##NaN
, ##Inf
, ##-Inf
}, (XY)ij = ##NaN
,
otherwise, (XY)ij = k = 1∑nxikykj.