avocado-old/packages/math/matrix.js

32 lines
755 B
JavaScript
Raw Normal View History

2019-03-17 23:45:48 -05:00
// Matrix operations.
// **Matrix** is a utility class to help with matrix operations. A
// matrix is implemented as an n-element array. Data is stored in row-major
// order.
2019-09-08 03:20:34 -05:00
export const copy = (matrix) => matrix.map((row) => [...row])
2019-03-17 23:45:48 -05:00
2019-09-08 03:20:34 -05:00
export const equals = (l, r) =>
2019-03-17 23:45:48 -05:00
return false unless l.length is r.length
return true if l.length is 0
return false unless l[0].length is r[0].length
for lrow, y in l
rrow = r[y]
for lindex, x in lrow
unless lindex is rrow[x]
return false
return true
2019-09-08 03:20:34 -05:00
export const size = (matrix) =>
2019-03-17 23:45:48 -05:00
return 0 if 0 is matrix.length
return matrix.length * matrix[0].length
2019-09-08 03:20:34 -05:00
export const sizeVector = (matrix) =>
2019-03-17 23:45:48 -05:00
return [0, 0] if 0 is matrix.length
return [matrix[0].length, matrix.length]