added vector/matrix product

This commit is contained in:
Walter Oggioni
2019-01-03 17:38:24 +01:00
parent cd6ca6f3ec
commit 6aee03eac6

View File

@@ -8,7 +8,7 @@ import random
type type
SMatrix*[ROWS, COLUMNS: static[int], T] = object SMatrix*[ROWS, COLUMNS: static[int], T] = object
data : array[0..(ROWS*COLUMNS - 1), T] data : array[0..(ROWS*COLUMNS - 1), T]
SquareSMatrix[SIZE: static[int], T] = SMatrix[SIZE, SIZE, T] SquareSMatrix*[SIZE: static[int], T] = SMatrix[SIZE, SIZE, T]
proc size*[ROWS, COLUMNS : static[int], T](m : SMatrix) : (int, int) = (m.rows, m.columns) proc size*[ROWS, COLUMNS : static[int], T](m : SMatrix) : (int, int) = (m.rows, m.columns)
@@ -63,7 +63,12 @@ proc `*`*[ROWS1, COLUMNS2, COMMON : static[int], T](
for k in 0...m1.columns: for k in 0...m1.columns:
result[i, j] = result[i, j] + m1[i, k] * m2[k, j] result[i, j] = result[i, j] + m1[i, k] * m2[k, j]
proc `*`*[ROWS, COLUMNS : static[int], T](m1 : SMatrix[ROWS, COLUMNS, T], v2 : SVector[ROWS, T]) : SVector[ROWS, T] = proc `*`*[SIZE : static[int], T](v : SVector[SIZE, T], m : SquareSMatrix[SIZE, T]) : SVector[SIZE, T] =
for i in 0...m.rows:
for j in 0...m.columns:
result[j] += m[i, j] * v[i]
proc `*`*[SIZE : static[int], T](m1 : SquareSMatrix[SIZE, T], v2 : SVector[SIZE, T]) : SVector[SIZE, T] =
for i in 0...m1.rows: for i in 0...m1.rows:
for j in 0...m1.columns: for j in 0...m1.columns:
result[i] += m1[i, j] * v2[j] result[i] += m1[i, j] * v2[j]