[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Get pointer to data in Matrix classes [possible solution]
From: |
Jaroslav Hajek |
Subject: |
Re: Get pointer to data in Matrix classes [possible solution] |
Date: |
Thu, 12 Mar 2009 10:39:16 +0100 |
On Thu, Mar 12, 2009 at 10:30 AM, José Luis García Pallero
<address@hidden> wrote:
> Hi,
>
> From my last message:
>
> Exist any method in Matrix and Column/RowVector classes to get a pointer for
> access directly to the memory? I have some C functions that works with
> matrices stored as vectors in column major order, and I would like to link
>
> them with octave without duplicate memory copying the original data.
>
> I wrote this example function for fill a matrix fron a C function:
>
> #include<oct.h>
> //assign data
> void DataAssign(double* data,int nData);
>
> //octave function
> DEFUN_DLD(test,args,,"Assign test")
> {
> //return list
> octave_value_list retval;
> //input arguments
> int rows=args(0).int_value();
> int cols=args(1).int_value();
>
> //creating the output matrix
> Matrix mat(rows,cols);
> //address of data
> double* data=const_cast<double*>(mat.data());
> //data assingn
> DataAssign(data,rows*cols);
> //assignment to return list
>
> retval(0) = mat;
> //returning results
> return retval;
> }
> void DataAssign(double* data,int nData)
> {
> //assign
> for(int i=0;i<nData;i++)
> data[i] = i;
> //end of the function
>
> return;
> }
>
> This code appears to run OK in my tests. In liboctave.pdf says that Array<T>
> class has a method named data() that retirns a const pointer
> to the memory space where the data are stored. I suppose that Matrix class
> inherit the method. Are the same for Row/ColumnVector classes?
>
> Is correct the conversion double* data=const_cast<double*>(mat.data());? It
> runs but I don't know if exist any implication for future
> operations with Matrix-Matrix operations.
>
The standard method for getting writable data pointer is (somewhat
unfortunately) called fortran_vec.
This will also correctly make a copy of the matrix if it is aliased.
Const-casting data () will work in your case (you've just created the
matrix, so you know that it's unaliased, but never do it on matrices
passed from outside).
regards
--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz
- Get pointer to data in Matrix classes [possible solution], José Luis García Pallero, 2009/03/12
- Re: Get pointer to data in Matrix classes [possible solution],
Jaroslav Hajek <=
- Re: Get pointer to data in Matrix classes [possible solution], José Luis García Pallero, 2009/03/12
- Re: Get pointer to data in Matrix classes [possible solution], Jaroslav Hajek, 2009/03/12
- Re: Get pointer to data in Matrix classes [possible solution], José Luis García Pallero, 2009/03/12
- Re: Get pointer to data in Matrix classes [possible solution], Søren Hauberg, 2009/03/12
- Re: Get pointer to data in Matrix classes [possible solution], Jaroslav Hajek, 2009/03/12
- Re: Get pointer to data in Matrix classes [possible solution], José Luis García Pallero, 2009/03/12
- Re: Get pointer to data in Matrix classes [possible solution], Jaroslav Hajek, 2009/03/12
- Re: Get pointer to data in Matrix classes [possible solution], José Luis García Pallero, 2009/03/12