matrices
Usage
require 'matrices.rb'
a = Matrix.new [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = Matrix.new [[1, 2, 3], [4, 5, 6]]
c = Matrix.new [[7, 8], [9, 10], [11, 12]]
d = Matrix.new [[1, -2, 3], [-4, 5, 6], [7, 8, -9]]
e = Matrix.new [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
u = Vector.new [8, -7]
v = Vector.new [5, -2, 18]
w = Vector.new [8, 13, -7]
x = Vector.new [0, 0.2, 0.8, 0]
y = Vector.new [0.4, 0.3, 0, 0]
z = Vector.new [0, 1, 0]
a.add(c.multiply(b))
# => [[40, 56, 72], [53, 73, 93], [66, 90, 114]]
c.multiply(b).subtract(a)
# => [[38, 52, 66], [45, 63, 81], [52, 74, 96]]
b.multiply(c)
# => [[58, 64], [139, 154]]
b.scale(3.5)
# => [[3.5, 7.0, 10.5], [14.0, 17.5, 21.0]]
d.determinant()
# => -306.0
a.submatrix(1, 0)
# => [[2, 3], [8, 9]]
a.minor(1, 0)
# => -6.0
a.adjoint()
# => [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
c.transpose()
# => [[7, 9, 11], [8, 10, 12]]
e.minors()
# => [[2.0, 2.0, 2.0], [-2.0, 3.0, 3.0], [0.0, -10.0, 0.0]]
e.cofactors()
# => [[2.0, -2.0, 2.0], [2.0, 3.0, -3.0], [0.0, 10.0, 0.0]]
e.inverse()
# => [[0.2, 0.2, 0.0], [-0.2, 0.3, 1.0], [0.2, -0.3, 0.0]]
a.swap_rows(1, 2)
# => [[1, 2, 3], [7, 8, 9], [4, 5, 6]]
a.trace()
# => 15
d.leading_diagonal()
# => -45
a.is_square?
# => true
empty_matrix(4, 3)
# => [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
unit_matrix(3)
# => [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
empty_vector(3)
# => [0, 0, 0]
x.is_vector?
# => true
x.is_stochastic?
# => true
y.is_stochastic?
# => false
a.multiply(z)
# => [[2, 5, 8]]
u.magnitude.round(2)
# => 10.63
u.direction.round(2)
# => -0.72
v.dimension()
# => 3
v.dot(w)
# => -112
v.similarity(w).round(3)
# => -0.355
v.cross(w)
# => [-220, -179, 30]
u.parallel?(Vector.new [4,-3.5])
# => true
y.unit()
# => [0.8, 0.6, 0, 0]