Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --/ Matrix class for Lua \--
- Matrix = {}
- Matrix.__index = Matrix
- -- \
- -- | Constructor
- -- /
- -- Returns a (n,p) order matrix.
- function Matrix:new(n, p)
- if n < 0 or p < 0 then
- return nil
- end
- if n == 0 or p == 0 then
- n, p = 0, 0
- end
- local t = {n=n, p=p}
- setmetatable(t, self)
- for i=1, n do
- t[i] = {}
- for j=1, p do
- t[i][j] = 0
- end
- end
- return t
- end
- -- \
- -- | Methods
- -- /
- -- Prints a matrix to the shell.
- function Matrix:print()
- if self.n == 0 or self.p == 0 then
- print("||")
- return
- end
- for i=1, self.n do
- io.write("|")
- for j=1, self.p do
- io.write(self[i][j])
- if j ~= self.p then
- io.write(" ")
- end
- end
- io.write("|\n")
- end
- end
- -- Fills a matrix with values from 'list'.
- function Matrix:fill(list)
- if self.n * self.p ~= #list then
- return
- end
- for i=1, self.n do
- for j=1, self.p do
- self[i][j] = list[(i-1)*self.p + j]
- end
- end
- end
- -- Fills a matrix' row 'i' with values from 'list'.
- function Matrix:fillRow(list, i)
- if i < 1 or i > self.n then
- return
- end
- if self.p ~= #list then
- return
- end
- self[i] = list
- end
- -- Fills a matrix' column 'j' with values from 'list'.
- function Matrix:fillColumn(list, j)
- if j < 1 or j > self.p then
- return
- end
- if self.n ~= #list then
- return
- end
- for i=1, self.n do
- self[i][j] = list[i]
- end
- end
- -- Checks if a matrix has the same dimensions of 'm'.
- function Matrix:isEquivalent(m)
- return self.n == m.n and self.p == m.p
- end
- -- Checks if a matrix is symetric.
- function Matrix:isSymetric()
- return self == self:transpose()
- end
- -- Checks if a matrix is anti-symetric.
- function Matrix:isAntiSymetric()
- return self == -self:transpose()
- end
- -- Checks if a matrix is upper triangular.
- function Matrix:isUpperTriangular()
- for i=1, self.n do
- for j=1, i-1 do
- if self[i][j] ~= 0 then
- return false
- end
- end
- end
- return true
- end
- -- Checks if a matrix is lower triangular.
- function Matrix:isLowerTriangular()
- for i=1, self.n do
- for j=i+1, self.p do
- if self[i][j] ~= 0 then
- return false
- end
- end
- end
- return true
- end
- -- Checks if a matrix is the inverse of 'm'
- function Matrix:isInverse(m)
- if self:isEquivalent(m) == false or self.n ~= self.p then
- return false
- end
- local t = self * m;
- if t:isUpperTriangular() == false then
- return false
- end
- if t:isLowerTriangular() == false then
- return false
- end
- for i=1, t.n do
- if (t[i][i] ~= 1) then
- return false
- end
- end
- return true
- end
- -- Returns a 'n'-th order square matrix.
- function Matrix:square(n)
- return Matrix:new(n, n)
- end
- -- Returns a 'n'-th order diagonal matrix, using values from 'list'.
- function Matrix:diagonal(list)
- local t = Matrix:square(#list)
- for i=1, #list do
- t[i][i] = list[i]
- end
- return t
- end
- -- Returns a 'n'-th order identity matrix.
- function Matrix:identity(n)
- local t = Matrix:square(n)
- for i=1, n do
- t[i][i] = 1
- end
- return t
- end
- -- Returns the transposed of a matrix.
- function Matrix:transpose()
- local t = Matrix:new(self.p, self.n)
- for i=1, self.n do
- t:fillColumn(self[i],i)
- end
- return t
- end
- -- \
- -- | Operators overcharge
- -- /
- -- Checks if 'a' equals 'b'
- function Matrix.equals(a, b)
- if a:isEquivalent(b) == false then
- return false
- end
- for i=1, a.n do
- for j=1, a.p do
- if a[i][j] ~= b[i][j] then
- return false
- end
- end
- end
- return true
- end
- -- Returns the matrix sum of 'a' and 'b'
- function Matrix.sum(a, b)
- if a:isEquivalent(b) == false then
- return nil
- end
- local t = Matrix:new(a.n, a.p)
- for i=1, a.n do
- for j=1, a.p do
- t[i][j] = a[i][j] + b[i][j]
- end
- end
- return t
- end
- -- Returns the matrix difference of 'a' and 'b'
- function Matrix.difference(a, b)
- if a:isEquivalent(b) == false then
- return nil
- end
- local t = Matrix:new(a.n, a.p)
- for i=1, a.n do
- for j=1, a.p do
- t[i][j] = a[i][j] - b[i][j]
- end
- end
- return t
- end
- -- Returns the negative of a matrix
- function Matrix.negative(m)
- return Matrix:new(m.n, m.p) - m
- end
- -- Returns the product of a matrix and a real 'r'
- function Matrix.rproduct(m, r)
- local t = Matrix:new(m.n, m.p)
- for i=1, t.n do
- for j=1, t.p do
- t[i][j] = m[i][j] * r
- end
- end
- return t
- end
- -- Returns the matrix product of 'a' and 'b'
- function Matrix.product(a, b)
- if getmetatable(a) ~= Matrix then
- return Matrix.rproduct(b, a)
- end
- if getmetatable(b) ~= Matrix then
- return Matrix.rproduct(a, b)
- end
- if a.p ~= b.n then
- return nil
- end
- local t = Matrix:new(a.n, b.p)
- for i=1, a.n do
- for j=1, b.p do
- t[i][j] = 0
- for k=1, a.p do
- t[i][j] = t[i][j] + a[i][k] * b[k][j]
- end
- end
- end
- return t
- end
- Matrix.__add = Matrix.sum
- Matrix.__sub = Matrix.difference
- Matrix.__mul = Matrix.product
- Matrix.__pow = nil
- Matrix.__unm = Matrix.negative
- Matrix.__concat = nil
- Matrix.__eq = Matrix.equals
- Matrix.__lt = nil
- Matrix.__le = nil
- -- EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement