This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
RefinedCode's profile picture

My Matrix Library Giving me bios:267 error because of writing to global.

Started by RefinedCode, 30 July 2012 - 06:25 PM
RefinedCode #1
Posted 30 July 2012 - 08:25 PM
Hey guys I was converting a matrix library from java to lua so i recreated all the fuctions and used prototyping for the class but when I use loadfile() and load the chunk into a test program, and then run Matrix.Create({4,5}) I get the error:


bios:267: Attempt to write to global


Now I will post my code, but it is alot so i dont blame you for not reading through it:


--Matrix
--Implements a mathematical Matrix.
--The matrix object
Matrix = {
  matrix = {}
}
Matrix_mt = {__index = Matrix}
--Create a column matrix
function Matrix.createColumnMatrix(inputTable)
d = {}
for i=1, #inputTable do
  t = {i}
  table.insert(d,t)
end
return Matrix.Create(d)
end
--Create a row matrix
function Matrix.createRowMatrix(inputTable)
d = {}
t = {}
for i=1, #inputTable do
  table.insert(t,inputTable[i])
end
table.insert(d,t)
return Matrix.Create(d)
end
--constructor
--Can either take a 2d array of boolean, numbers, or create
--a  blank array with just the number of rows and cols
function Matrix.Create(args)
if type(args)=="table" then
  --Copy master object
  local self = {}
  setmetatable(self, Matrix_mt)
  --Create blank matrix with number of rows and cols
  if type(args[1])=="number" then
   t = {}
   for x=1, args[1] do
    d = {}
    for y=1, args[2] do
	 table.insert(d,0)
    end
    table.insert(t,d)
   end
   self.matrix = t
   return self
  elseif type(args[1])=="table" then
   --Create matrix with boolean values
   if type(args[1][1])=="boolean" then
    for x=1, #args do
	 d = {}
	 for y=1, #args[1] do
	  if args[x][y] then
	   table.insert(d,1)
	  else
	   table.insert(d,-1)
	  end
	 end
	 table.insert(t,d)
    end
    self.matrix = t
    return self
   --Create matrix with number values
   elseif type(args[1][1])=="number" then
    for x=1, #args do
	 d = {}
	 for y=1, #args[1] do
	  table.insert(d,args[x][y])
	 end
	 table.insert(t,d)
    end
    self.matrix = t
    return self
   end
  end
end
end
--Get rows
function Matrix:getRows()
return #self.matrix
end
--Return the specified row
function Matrix:getRow(row)
if row> self:getRows() then
  print("Row ",row, " does not exist!")
  return
end
return Matrix.createRowMatrix(self.matrix[row])
end
--Get columns
function Matrix:getColumns()
return #self.matrix[1]
end
--Return the specified column
function Matrix:getColumn(col)
if col > self:getColumns() then
  print("Column ",col, " does not exist!")
  return
end
t = {}
for i=1, self:getRows() do
  table.insert(t,self.matrix[i][col])
end
return Matrix.createColumnMatrix(t)
end
--Validate Row and Column
function Matrix:validate(row,col)
if row > self:getRows() or row < 0 then
  print("The row " .. row .. " is out of range!")
elseif col > self:getColumns() or col < 0 then
  print("The column " .. col .. " is out of range!")
end
end
--Convert matrix to string
function Matrix:toString()
str = "["
for x=1, self:getRows() do
  str = str.."("
  for y=1, self:getColumns()-1 do
   str = str..self.matrix[x][y]
  end
  str = str..self.matrix[x][self:getColumns()]
  str = str..")\n"
end
str = str.."]"
return str
end
--Set a cell
function Matrix:set(row, col, value)
self:validate(row,col)
self.matrix[row][col] = value
end
--Get a cell
function Matrix:get(row,col)
self:validate(row,col)
return self.matrix[row][col]
end
--Add a scalar to every values
function Matrix:add(row, col, value)
self:validate(row,col)
self:set(row,col,self:get(row,col)+value)
end
--Clear the Matrix
function Matrix:clear()
for x = 1, self:getRows() do
  for y=1, self:getColumns() do
   self:set(x,y,0)
  end
end
end
--clone the Matrix
function Matrix:clone()
  local t2 = {}
   for k,v in pairs(self.matrix) do
  t2[k] = v
   end
return Matrix.Create(t2)
end
--compare to another matrix
function Matrix:equals(matrix, ...)
if #arg == 0 then
  return self:equals(matrix,10)
else
  precision = math.floor(10^arg[1])
  for x = 1, self:getRows() do
   for y=1, self:getColumns() do
    print(math.floor(self:get(x,y)*precision))
    if math.floor(self:get(x,y)*precision) ~= math.floor(matrix:get(x,y)*precision) then
	 return false
    end
   end
  end
  return true
end
end
-- Read a packed array
--Returns the new index after the matrix has been read
function Matrix:fromPackedArray(array, index)
for r=1,self:getRows() do
  for c=1,self:getColumns() do
   self.matrix[r][c] = array[index]
   index = index+1
  end
end
return index
end
function Matrix:toPackedArray()
result = {}
index =1
for r=1,self:getRows() do
  for c=1,self:getColumns() do
   result[index] =self.matrix[r][c]
   index = index+1
  end
end
return result
end
--Check if vector
function Matrix:isVector()
if self:getRows() == 1 then
  return true
else
  return self:getColumns() == 1
end
end
--Check if the whole matrix is zero
function Matrix:isZero()
for r=1,self:getRows() do
  for c=1,self:getColumns() do
   if self.matrix[r][c] ~= 0 then
    return false
   end
  end
end
return true
end
--Randomize the Matrix
function Matrix:randomize(min,max)
for r=1,self:getRows() do
  for c=1,self:getColumns() do
   self.matrix[r][c] = (math.random() * (max-min)) + min
  end
end
end
--Get size
function Matrix:size()
return (#self.matrix[1] * #self.matrix)
end
--Get sum of all elements
function Matrix:sum()
result = 0
for r=1,self:getRows() do
  for c=1,self:getColumns() do
   result = result+ self.matrix[r][c]
  end
end
return result
end



If anyone knows what this error is, and what part of my code calls it. I would really appreciate it. I am a novice with lua.
RefinedCode #2
Posted 01 August 2012 - 04:10 AM
Well Using shell.run("Matrix.lua") fixed it all. Wasnt even part of the code i posted. Sorry!