help-octave
[Top][All Lists]
Advanced

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

Re: Hypermatrix in C++


From: siko1056
Subject: Re: Hypermatrix in C++
Date: Tue, 29 Nov 2016 04:43:25 -0800 (PST)

shamikam wrote
> Hello,
> 
> 1. I have the following code in C++
> 
> octave_value_list out = feval (pstData[0], in, o);
> for (p = 0; p <o; p++)
> {
>         dim_vector u=out(p).dims();
>         if(u.length()>=3)
>         {
>             //Do something with hypermatrix
>         }
> 
>        if(u.length()<=2)
>         {
>             //Do something with 2D matrix
>         }
> }
> 
> Is there a better way to identify 2D/hypermatrices?

Dear Shamika,

This strongly depends, what you are trying to do. It seems like you want to
call some of your native C++ code from Octave and you need to decide the
type of the matrix + need a vectorized version of that matrix.

I suggest you have a look at [1] and you find the "ndims()" function to
shorten "dims().length()", but what should be improved? I don't know neither
your intentions, nor you provided a complete example.


shamikam wrote
> 2. To identify a 2D double matrix and get its matrix dimensions, I'm using
> the following code
> 
> dim_vector d = (out(p).matrix_value().dims());
> pdbl_op = (double*)malloc(sizeof(double) * d(0) * d(1));
> Matrix x=(out(p).matrix_value ());
> for(int l = 0 ; l < d(0) ; l++)
> {
>   for(int m = 0 ; m < d(1) ; m++)
>   {
>     pdbl_op[l+d(0)*m]=x(l,m);
>   }
> }
> 
> How do I access double/complex/int hypermatrix elements? Is there
> something
> like NDArray x=(out(p).ndarray_value());? (I tried this but this shows
> errors)

Again I suggest to first look at all examples in [1] and [2], even if you
already know them, there is lots of copy&paste potential in the provided
examples. To identify your matrix, the header "ov.h" [3], that is included
with the standard headers, contains lots of funtions like

is_XXX_type ()

or 

is_XXX_matrix ()

to check what is burried behind the "octave_value" or "octave_value_list"
items. And there are the corresponding extractors "XXX_value ()". Especially
in your case

NDArray x = (out(p).array_value());

that can also be taken from an example in [1]. Depending on how you later
use the C++-array "pdbl_op", consider using the function "fortran_vec()"
[1]. There Octave grants you read+write access to the underlying data
C++-array in column-major order of your matrix or hypermatrix.

HTH,
Kai

[1]:
https://www.gnu.org/software/octave/doc/interpreter/Matrices-and-Arrays-in-Oct_002dFiles.html
[2]: https://www.gnu.org/software/octave/doc/interpreter/Oct_002dFiles.html
[3]:
http://hg.savannah.gnu.org/hgweb/octave/file/tip/libinterp/octave-value/ov.h



--
View this message in context: 
http://octave.1599824.n4.nabble.com/Hypermatrix-in-C-tp4680803p4680825.html
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

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