class Matrix
class Matrix
lib/matrix.tya:2
Matrix provides the matrix/Matrix standard library API.
Source
# Matrix provides the matrix/Matrix standard library API.
class Matrix
# Matrix.cols stores instance state.
# @type Nil
cols: nil
# Matrix.data stores instance state.
# @type Array
data: []
# Matrix.rows stores instance state.
# @type Array
rows: []
# Matrix.initialize provides the matrix/Matrix standard library operation.
# @param data Array data value.
# @return Self the initialized object.
initialize: data = nil ->
if data == nil
data = [[0]]
cols = self.validate_data(data)
self.rows = data.len()
self.cols = cols
self.data = data
# Matrix.add provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Any the resulting value.
add: a, b = nil ->
if b == nil
b = a
a = self
if a.rows != b.rows or a.cols != b.cols
raise error("matrix.add: dimension mismatch")
out_data = []
i = 0
while i < a.rows
row = []
j = 0
while j < a.cols
row.push(a.data[i][j] + b.data[i][j])
j = j + 1
out_data.push(row)
i = i + 1
Matrix(out_data)
# Matrix.at provides the matrix/Matrix standard library operation.
# @param m Any m value.
# @param r Int r value.
# @param c Any c value.
# @return Any the resulting value.
at: m, r, c = nil ->
if c == nil
c = r
r = m
m = self
if r < 0 or r >= m.rows or c < 0 or c >= m.cols
raise error("matrix.at: index out of range")
m.data[r][c]
# Matrix.det provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @return Any the resulting value.
det: a = nil ->
if a == nil
a = self
if a.rows != a.cols
raise error("matrix.det: not square")
n = a.rows
if n > 4
raise error("matrix.det: size > 4 not supported in v0.24")
d = a.data
if n == 1
return d[0][0]
if n == 2
return d[0][0] * d[1][1] - d[0][1] * d[1][0]
if n == 3
return d[0][0] * (d[1][1] * d[2][2] - d[1][2] * d[2][1]) - d[0][1] * (d[1][0] * d[2][2] - d[1][2] * d[2][0]) + d[0][2] * (d[1][0] * d[2][1] - d[1][1] * d[2][0])
sum = 0
j = 0
while j < 4
minor_data = []
r = 1
while r < 4
row = []
c = 0
while c < 4
if c != j
row.push(d[r][c])
c = c + 1
minor_data.push(row)
r = r + 1
sub_m = Matrix(minor_data)
cofactor = self.det(sub_m) * d[0][j]
if j % 2 == 0
sum = sum + cofactor
else
sum = sum - cofactor
j = j + 1
sum
# Matrix.equal? provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Boolean whether the condition is true.
equal?: a, b = nil ->
if b == nil
b = a
a = self
if a.rows != b.rows or a.cols != b.cols
return false
equal(a.data, b.data)
# Matrix.identity provides the matrix/Matrix standard library operation.
# @param n Int n value.
# @return Any the resulting value.
identity: n ->
data = []
i = 0
while i < n
row = []
j = 0
while j < n
if i == j
row.push(1)
else
row.push(0)
j = j + 1
data.push(row)
i = i + 1
Matrix(data)
# Matrix.mul provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Any the resulting value.
mul: a, b = nil ->
if b == nil
b = a
a = self
if a.cols != b.rows
raise error("matrix.mul: inner dimensions disagree")
out_data = []
i = 0
while i < a.rows
row = []
j = 0
while j < b.cols
sum = 0
k = 0
while k < a.cols
sum = sum + a.data[i][k] * b.data[k][j]
k = k + 1
row.push(sum)
j = j + 1
out_data.push(row)
i = i + 1
Matrix(out_data)
# Matrix.new provides the matrix/Matrix standard library operation.
# @param data Array data value.
# @return Self the resulting value.
new: data ->
Matrix(data)
# Matrix.put provides the matrix/Matrix standard library operation.
# @param m Any m value.
# @param r Int r value.
# @param c Any c value.
# @param value String value value.
# @return Any the resulting value.
put: m, r, c, value = nil ->
if value == nil
value = c
c = r
r = m
m = self
if r < 0 or r >= m.rows or c < 0 or c >= m.cols
raise error("matrix.put: index out of range")
m.data[r][c] = value
nil
# Matrix.scale provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param k Any k value.
# @return Any the resulting value.
scale: a, k = nil ->
if k == nil
k = a
a = self
out_data = []
i = 0
while i < a.rows
row = []
j = 0
while j < a.cols
row.push(a.data[i][j] * k)
j = j + 1
out_data.push(row)
i = i + 1
Matrix(out_data)
# Matrix.sub provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Any the resulting value.
sub: a, b = nil ->
if b == nil
b = a
a = self
if a.rows != b.rows or a.cols != b.cols
raise error("matrix.sub: dimension mismatch")
out_data = []
i = 0
while i < a.rows
row = []
j = 0
while j < a.cols
row.push(a.data[i][j] - b.data[i][j])
j = j + 1
out_data.push(row)
i = i + 1
Matrix(out_data)
# Matrix.transpose provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @return Any the resulting value.
transpose: a = nil ->
if a == nil
a = self
out_data = []
j = 0
while j < a.cols
row = []
i = 0
while i < a.rows
row.push(a.data[i][j])
i = i + 1
out_data.push(row)
j = j + 1
Matrix(out_data)
# Matrix.validate_data provides the matrix/Matrix standard library operation.
# @param data Array data value.
# @return Boolean the resulting value.
validate_data: data ->
if data.class != Array
raise error("matrix.new: data must be an array of arrays")
rows = data.len()
if rows == 0
raise error("matrix.new: empty matrix")
if data[0].class != Array
raise error("matrix.new: rows must be arrays")
cols = data[0].len()
i = 0
while i < rows
if data[i].class != Array or data[i].len() != cols
raise error("matrix.new: inconsistent row lengths")
j = 0
while j < cols
if data[i][j].class != Number
raise error("matrix.new: elements must be numbers")
j = j + 1
i = i + 1
cols
# Matrix.zero provides the matrix/Matrix standard library operation.
# @param rows Array rows value.
# @param cols Any cols value.
# @return Any the resulting value.
zero: rows, cols ->
data = []
i = 0
while i < rows
row = []
j = 0
while j < cols
row.push(0)
j = j + 1
data.push(row)
i = i + 1
Matrix(data)
Instance Variables
cols
Matrix.cols
lib/matrix.tya:5
Matrix.cols stores instance state.
Source
# Matrix.cols stores instance state.
# @type Nil
cols: nil
data
Matrix.data
lib/matrix.tya:9
Matrix.data stores instance state.
Source
# Matrix.data stores instance state.
# @type Array
data: []
rows
Matrix.rows
lib/matrix.tya:13
Matrix.rows stores instance state.
Source
# Matrix.rows stores instance state.
# @type Array
rows: []
Methods
add
Matrix.add(a, b = nil)
lib/matrix.tya:30
Matrix.add provides the matrix/Matrix standard library operation.
Source
# Matrix.add provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Any the resulting value.
add: a, b = nil ->
if b == nil
b = a
a = self
if a.rows != b.rows or a.cols != b.cols
raise error("matrix.add: dimension mismatch")
out_data = []
i = 0
while i < a.rows
row = []
j = 0
while j < a.cols
row.push(a.data[i][j] + b.data[i][j])
j = j + 1
out_data.push(row)
i = i + 1
Matrix(out_data)
at
Matrix.at(m, r, c = nil)
lib/matrix.tya:53
Matrix.at provides the matrix/Matrix standard library operation.
Source
# Matrix.at provides the matrix/Matrix standard library operation.
# @param m Any m value.
# @param r Int r value.
# @param c Any c value.
# @return Any the resulting value.
at: m, r, c = nil ->
if c == nil
c = r
r = m
m = self
if r < 0 or r >= m.rows or c < 0 or c >= m.cols
raise error("matrix.at: index out of range")
m.data[r][c]
det
Matrix.det(a = nil)
lib/matrix.tya:65
Matrix.det provides the matrix/Matrix standard library operation.
Source
# Matrix.det provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @return Any the resulting value.
det: a = nil ->
if a == nil
a = self
if a.rows != a.cols
raise error("matrix.det: not square")
n = a.rows
if n > 4
raise error("matrix.det: size > 4 not supported in v0.24")
d = a.data
if n == 1
return d[0][0]
if n == 2
return d[0][0] * d[1][1] - d[0][1] * d[1][0]
if n == 3
return d[0][0] * (d[1][1] * d[2][2] - d[1][2] * d[2][1]) - d[0][1] * (d[1][0] * d[2][2] - d[1][2] * d[2][0]) + d[0][2] * (d[1][0] * d[2][1] - d[1][1] * d[2][0])
sum = 0
j = 0
while j < 4
minor_data = []
r = 1
while r < 4
row = []
c = 0
while c < 4
if c != j
row.push(d[r][c])
c = c + 1
minor_data.push(row)
r = r + 1
sub_m = Matrix(minor_data)
cofactor = self.det(sub_m) * d[0][j]
if j % 2 == 0
sum = sum + cofactor
else
sum = sum - cofactor
j = j + 1
sum
equal?
Matrix.equal?(a, b = nil)
lib/matrix.tya:107
Matrix.equal? provides the matrix/Matrix standard library operation.
Source
# Matrix.equal? provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Boolean whether the condition is true.
equal?: a, b = nil ->
if b == nil
b = a
a = self
if a.rows != b.rows or a.cols != b.cols
return false
equal(a.data, b.data)
identity
Matrix.identity(n)
lib/matrix.tya:118
Matrix.identity provides the matrix/Matrix standard library operation.
Source
# Matrix.identity provides the matrix/Matrix standard library operation.
# @param n Int n value.
# @return Any the resulting value.
identity: n ->
data = []
i = 0
while i < n
row = []
j = 0
while j < n
if i == j
row.push(1)
else
row.push(0)
j = j + 1
data.push(row)
i = i + 1
Matrix(data)
initialize
Matrix.initialize(data = nil)
lib/matrix.tya:18
Matrix.initialize provides the matrix/Matrix standard library operation.
Source
# Matrix.initialize provides the matrix/Matrix standard library operation.
# @param data Array data value.
# @return Self the initialized object.
initialize: data = nil ->
if data == nil
data = [[0]]
cols = self.validate_data(data)
self.rows = data.len()
self.cols = cols
self.data = data
mul
Matrix.mul(a, b = nil)
lib/matrix.tya:138
Matrix.mul provides the matrix/Matrix standard library operation.
Source
# Matrix.mul provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Any the resulting value.
mul: a, b = nil ->
if b == nil
b = a
a = self
if a.cols != b.rows
raise error("matrix.mul: inner dimensions disagree")
out_data = []
i = 0
while i < a.rows
row = []
j = 0
while j < b.cols
sum = 0
k = 0
while k < a.cols
sum = sum + a.data[i][k] * b.data[k][j]
k = k + 1
row.push(sum)
j = j + 1
out_data.push(row)
i = i + 1
Matrix(out_data)
new
Matrix.new(data)
lib/matrix.tya:164
Matrix.new provides the matrix/Matrix standard library operation.
Source
# Matrix.new provides the matrix/Matrix standard library operation.
# @param data Array data value.
# @return Self the resulting value.
new: data ->
Matrix(data)
put
Matrix.put(m, r, c, value = nil)
lib/matrix.tya:173
Matrix.put provides the matrix/Matrix standard library operation.
Source
# Matrix.put provides the matrix/Matrix standard library operation.
# @param m Any m value.
# @param r Int r value.
# @param c Any c value.
# @param value String value value.
# @return Any the resulting value.
put: m, r, c, value = nil ->
if value == nil
value = c
c = r
r = m
m = self
if r < 0 or r >= m.rows or c < 0 or c >= m.cols
raise error("matrix.put: index out of range")
m.data[r][c] = value
nil
scale
Matrix.scale(a, k = nil)
lib/matrix.tya:188
Matrix.scale provides the matrix/Matrix standard library operation.
Source
# Matrix.scale provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param k Any k value.
# @return Any the resulting value.
scale: a, k = nil ->
if k == nil
k = a
a = self
out_data = []
i = 0
while i < a.rows
row = []
j = 0
while j < a.cols
row.push(a.data[i][j] * k)
j = j + 1
out_data.push(row)
i = i + 1
Matrix(out_data)
sub
Matrix.sub(a, b = nil)
lib/matrix.tya:208
Matrix.sub provides the matrix/Matrix standard library operation.
Source
# Matrix.sub provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @param b Int b value.
# @return Any the resulting value.
sub: a, b = nil ->
if b == nil
b = a
a = self
if a.rows != b.rows or a.cols != b.cols
raise error("matrix.sub: dimension mismatch")
out_data = []
i = 0
while i < a.rows
row = []
j = 0
while j < a.cols
row.push(a.data[i][j] - b.data[i][j])
j = j + 1
out_data.push(row)
i = i + 1
Matrix(out_data)
transpose
Matrix.transpose(a = nil)
lib/matrix.tya:229
Matrix.transpose provides the matrix/Matrix standard library operation.
Source
# Matrix.transpose provides the matrix/Matrix standard library operation.
# @param a Int a value.
# @return Any the resulting value.
transpose: a = nil ->
if a == nil
a = self
out_data = []
j = 0
while j < a.cols
row = []
i = 0
while i < a.rows
row.push(a.data[i][j])
i = i + 1
out_data.push(row)
j = j + 1
Matrix(out_data)
validate_data
Matrix.validate_data(data)
lib/matrix.tya:247
Matrix.validate_data provides the matrix/Matrix standard library operation.
Source
# Matrix.validate_data provides the matrix/Matrix standard library operation.
# @param data Array data value.
# @return Boolean the resulting value.
validate_data: data ->
if data.class != Array
raise error("matrix.new: data must be an array of arrays")
rows = data.len()
if rows == 0
raise error("matrix.new: empty matrix")
if data[0].class != Array
raise error("matrix.new: rows must be arrays")
cols = data[0].len()
i = 0
while i < rows
if data[i].class != Array or data[i].len() != cols
raise error("matrix.new: inconsistent row lengths")
j = 0
while j < cols
if data[i][j].class != Number
raise error("matrix.new: elements must be numbers")
j = j + 1
i = i + 1
cols
zero
Matrix.zero(rows, cols)
lib/matrix.tya:272
Matrix.zero provides the matrix/Matrix standard library operation.
Source
# Matrix.zero provides the matrix/Matrix standard library operation.
# @param rows Array rows value.
# @param cols Any cols value.
# @return Any the resulting value.
zero: rows, cols ->
data = []
i = 0
while i < rows
row = []
j = 0
while j < cols
row.push(0)
j = j + 1
data.push(row)
i = i + 1
Matrix(data)