lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Mike,

I was finally able to reproduce and understand the problem. It was
actually a very subtle error related to matrices/vectors and not to
permutations.

The solution I've adopted is a little bit ugly so I was willing to
have you point of view to see if I can get something better.

A very short background, the GSL library does defines the types for
gsl_vector and gsl_matrix and two related types, gsl_vector_view and
gsl_matrix_view. Here the relevant cdefs:

----------------
typedef struct {
  size_t size;
  double * data;
} gsl_block;

typedef struct {
  size_t size;
  size_t stride;
  double * data;
  gsl_block * block;
  int owner;
} gsl_vector;

typedef struct {
  size_t size1;
  size_t size2;
  size_t tda;
  double * data;
  gsl_block * block;
  int owner;
} gsl_matrix;

typedef struct {
  gsl_vector vector;
} gsl_vector_view;

typedef struct {
  gsl_matrix matrix;
} gsl_matrix_view;
--------------------

Here what I was doing in my code, basically creating a matrix as a
userdata object with gsl.new, take a matrix_view to it to have a cdata
object to the same underlying data and then storing the cdata and its
related usedata matrix in a Lua table.

Here the code:

local M = { _data = {} }

-- map cdata objects to its related userdata object
-- useful both for lookup and to prevent premature disposing of userdata
local function store(obj, cdata)
   local t = M._data
   t[cdata] = obj
   return cdata
end

-- related table lookup
local function object(cdata)
   local t = M._data
   return t[cdata]
end

local function get_matrix(n1, n2)
   local m = gsl.new(n1, n2)
   local s = cgsl.gsl_matrix_submatrix (m, 0, 0, n1, n2)
   return store(m, s.matrix)
end

local function get_vector(nr)
   local m = gsl.new(nr, 1)
   local v = cgsl.gsl_matrix_column (m, 0)
   return store(m, v.vector)
end
---------------------

So it seems that the problem was that I was indexing with the cdata
s.matrix while the original cdata was 's' itself. I guess that Lua was
disposing of the original memory allocated for 's' because it was not
referenced.

Here the solution that I found:

------------------
local M = { _data = {} }

local function store(obj, ckey, cdata)
   local t = M._data
   t[ckey] = obj
   t[#t+1] = cdata
   return ckey
end

local function object(cdata)
   local t = M._data
   return t[cdata]
end

local function get_matrix(n1, n2)
   local m = gsl.new(n1, n2)
   local s = cgsl.gsl_matrix_submatrix (m, 0, 0, n1, n2)
   return store(m, s.matrix, s)
end

local function get_vector(nr)
   local m = gsl.new(nr, 1)
   local v = cgsl.gsl_matrix_column (m, 0)
   return store(m, v.vector, v)
end
-----------------------------

This solution seems to *really* solve the problem.

Mike, could you confirm my analysis of the problem ? Could you suggest
a better solution to what I've found or may be give some general
recommendations ?

Thanks in advance for any help.

Francesco