Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize creation of vector matrix #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions lua/matrix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
LUA MODULE

matrix v$(_VERSION) - matrix functions implemented with Lua tables

SYNOPSIS

local matrix = require 'matrix'
m1 = matrix{{8,4,1},{6,8,3}}
m2 = matrix{{-8,1,3},{5,2,1}}
assert(m1 + m2 == matrix{{0,5,4},{11,10,4}})

DESCRIPTION

With simple matrices this script is quite useful, though for more
exact calculations, one would probably use a program like Matlab instead.
Matrices of size 100x100 can still be handled very well.
The error for the determinant and the inverted matrix is around 10^-9
with a 100x100 matrix and an element range from -100 to 100.

Characteristics:

- functions called via matrix.<function> should be able to handle
any table matrix of structure t[i][j] = value
- can handle a type of complex matrix
Expand All @@ -39,7 +39,7 @@ DESCRIPTION
or use num = vec1:scalar( vec2 ), where num is a number

API

matrix function list:

matrix.add
Expand Down Expand Up @@ -83,7 +83,7 @@ API
matrix.tostring
matrix.transpose
matrix.type

See code and test_matrix.lua.

DEPENDENCIES
Expand All @@ -100,11 +100,11 @@ DOWNLOAD/INSTALL
./util.mk
cd tmp/*
luarocks make

LICENSE

Licensed under the same terms as Lua itself.

Developers:
Michael Lutz (chillcode) - original author
David Manura http://lua-users.org/wiki/DavidManura
Expand Down Expand Up @@ -134,7 +134,7 @@ function matrix:new( rows, columns, value )
if type( rows ) == "table" then
-- check for vector
if type(rows[1]) ~= "table" then -- expect a vector
return setmetatable( {{rows[1]},{rows[2]},{rows[3]}},matrix_meta )
rows[1], rows[2], rows[3] = {rows[1]}, {rows[2]}, {rows[3]}
end
return setmetatable( rows,matrix_meta )
end
Expand Down Expand Up @@ -324,22 +324,22 @@ function matrix.det( m1 )

-- check if matrix is quadratic
assert(#m1 == #m1[1], "matrix not square")

local size = #m1

if size == 1 then
return m1[1][1]
end

if size == 2 then
return m1[1][1]*m1[2][2] - m1[2][1]*m1[1][2]
end

if size == 3 then
return ( m1[1][1]*m1[2][2]*m1[3][3] + m1[1][2]*m1[2][3]*m1[3][1] + m1[1][3]*m1[2][1]*m1[3][2]
- m1[1][3]*m1[2][2]*m1[3][1] - m1[1][1]*m1[2][3]*m1[3][2] - m1[1][2]*m1[2][1]*m1[3][3] )
end

--// no symbolic matrix supported below here
local e = m1[1][1]
local zero = type(e) == "table" and e.zero or 0
Expand Down Expand Up @@ -491,7 +491,7 @@ function matrix.dogauss( mtx )
end
-- start parsing rows
for i = j-1,1,-1 do
-- check if element is not already zero
-- check if element is not already zero
if mtx[i][j] ~= zero then
local factor = mtx[i][j]
for _j = j+1,columns do
Expand Down Expand Up @@ -721,7 +721,7 @@ function matrix.type( mtx )
end
return "number"
end

-- local functions to copy matrix values
local num_copy = function( num )
return num
Expand Down Expand Up @@ -987,7 +987,7 @@ end
--// matrix.scalar ( m1, m2 )
-- returns the Scalar Product of two 3x1 matrices (vectors)
function matrix.scalar( m1, m2 )
return m1[1][1]*m2[1][1] + m1[2][1]*m2[2][1] + m1[3][1]*m2[3][1]
return m1[1][1]*m2[1][1] + m1[2][1]*m2[2][1] + m1[3][1]*m2[3][1]
end

--// matrix.cross ( m1, m2 )
Expand Down Expand Up @@ -1050,7 +1050,7 @@ end
--////////////////////////--

--// MetaTable
-- as we declaired on top of the page
-- as we declared on top of the page
-- local/shared metatable
-- matrix_meta

Expand Down