DLF - returning transposed vector

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

DLF - returning transposed vector

wkim+
I want to return a row or column vector depending on the vector
type of an argument to my DLF (dynamically linked function).
I declared "ColumnVector y ..." and then computed for y values.
When returning y to the caller, I wanted to transpose if an input
argument was a row vector.  But the following always returned a column
vector.  I confirmed that 'if' branched correctly according to the
input vector type, but either returned the same column vector.  
What was wrong?  

    ColumnVector x = args(0).vector_value();
    ColumnVector y (Ny, 0.0);
    ...
    ...
    if (args(0).matrix_value().cols() > 1) {
        return octave_value (y.transpose());
    } else {
        return octave_value (y);
    }

Since this DLF code will be called quite a lot, I need a fast code and
so I didn't want to copy to a RowVector.  (Perhaps, is x[] style
access recommended over x() (or x.elem()) access as well?)

Thanks.

//--------------------------------------------------------------------
// Wonkoo Kim ([hidden email])


Reply | Threaded
Open this post in threaded view
|

DLF - returning transposed vector

John W. Eaton-6
On 24-Jun-1998, Wonkoo Kim <[hidden email]> wrote:

| I want to return a row or column vector depending on the vector
| type of an argument to my DLF (dynamically linked function).
| I declared "ColumnVector y ..." and then computed for y values.
| When returning y to the caller, I wanted to transpose if an input
| argument was a row vector.  But the following always returned a column
| vector.  I confirmed that 'if' branched correctly according to the
| input vector type, but either returned the same column vector.  
| What was wrong?  
|
|     ColumnVector x = args(0).vector_value();
|     ColumnVector y (Ny, 0.0);
|     ...
|     ...
|     if (args(0).matrix_value().cols() > 1) {
|         return octave_value (y.transpose());
|     } else {
|         return octave_value (y);
|     }

Try

  return octave_value (y, args(0).rows() > 1);

instead.  When constructing an octave_value object from a row or
column vector, you can force the result to be a column vector by
providing a second integer argument with a value greater than 0.  If
the value of the second argument is equal to zero, a row vector is
returned.  If the value of the second argument is less than zero, or
if it is missing, the constructor uses the value of the built-in
variable prefer_column_vectors to decide what orientation a row or
column vector should actually have.  Probably this was not really the
best design decision, but that's the way things are currently
implemented.

jwe