[Top][All Lists]

[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 12:59:40 +0100

On Thu, Mar 12, 2009 at 12:47 PM, José Luis García Pallero
<address@hidden> wrote:
>> > Ok. For example:
>> >
>> > 1. I create a matrix -> a=[1 2 3;4 5 6];
>> > 2. I pass the matrix to an *.oct function
>> > 3. In oct function I get the pointer to the data -> double*
>> > data=a.fortran_vec();
>> > 4. Do something...
>> > Is the correct working way with normal matrices?
>> >
>> Yes, but it will make a copy, and you need to return the modified
>> matrix as a return value, otherwise the argument will not be modified.
>> If you need only to read the matrix data, query data () which gives
>> you a const pointer.
> Well, for read data I'll use the data() method, no problem. But, for
> example, if I need to solve a triangular system Ax=b with various b and I
> want to store the solutions in the same input b for save memory, one
> possibility is to use the data() method with a const_cast<double*>, for
> obtain permission for write. If I pass b from octave as a normal matrix,
> data() and const_cast should work, I suppose.

No, no, no, that's bad - Octave just doesn't work this way. If you do
this, you can modify something else.

b = my_b;
my_tr_solve (A, b);

if my_tr_solve uses const_cast on data (), my_b will still share the
data with b and will be also modified (!)

In short, you can use this method, in rare cases and *extremely*
carefully, if it's just an internal use and you *know*  the variable
passed to the function is not aliased (a local temporary, for
But I advise you not to - if you really need to save memory at this
level, it's probably better to stay in C++ all the time.

There have been several discussions about the possibility of somehow
optimizing calls like x = myfunc (x), but it's not a trivial problem.

>> Not sure I understand exactly, but you matrix_value called on a
>> diagonal matrix object will convert it to a full one.
>> The diagonal data will, of course, be copied.
> Ok, my ideas about diagonal matrices are now clear.
> --
> *****************************************
> José Luis García Pallero
> address@hidden
> (o<
> / / \
> V_/_
> Use Debian GNU/Linux and enjoy!
> *****************************************

RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic

reply via email to

[Prev in Thread] Current Thread [Next in Thread]