|
From: | alejandro zamora soto |
Subject: | RE: Problem with a dynamically linked function |
Date: | Fri, 6 May 2011 11:42:38 +0200 |
Dear Carlo, thank you for you answer, you are right, with your modification everything works. I was wondering if you know where the standard function in the octave source tree used to override this problem is. I guess one should always do this conversion of vectors at the beginning of the programs, Cheers Alex > Subject: Re: Problem with a dynamically linked function > From: address@hidden > Date: Thu, 5 May 2011 21:09:28 +0200 > CC: address@hidden > To: address@hidden > > > On 5 May 2011, at 16:31, alejandro zamora soto wrote: > > > Dear all, > > > > I wrote a program with Octave and now I want to rewrite my functions in C++. > > > > First of all, I'm trying to use C++ to write a linked extension for Octave of a toy program that permutes the indices > > of a tensor, in Octave: > > > > function gamp=mi_funcion(gam,perm_index) > > gamp=permute(gam, perm_index); > > end > > > > Then if i write this: > > > > > > #include <octave/oct.h> > > #include <iostream> > > > > DEFUN_DLD(mi_funcion, args, , > > "Return b = permute(a,perm_index).") > > { > > const MArray<int> perm_index=args(1).array_value(); > > MArray<double> a(args(0).array_value()); > > > > // std::cout << perm_index << std::endl; > > > > > > MArray<double> b( a.permute(perm_index)); > > return octave_value(b); > > } > > > > I don't have problems during the compilation with "mkoctfile", > > But I have problems when I use this function: > > > > "error: mi_funcion: permute: permutation vector contains an invalid element" > > > > could you help me to solve this problem? > > > > > > Kind regards, > > > > Alex Zamora > > How did you call the function from within Octave? > > If you tried to use it the same way as 'permute' I guess your problem is with indexing. > While the interpreter uses 1-based indexing the C++ uses 0-based indexing, > the following works for me: > > -------------------------------------------------------- > #include <octave/oct.h> > > DEFUN_DLD(mi_funcion, args, , > "Return b = permute(a,perm_index).") > { > > Array<octave_idx_type> perm_index=args(1).array_value (); > for (octave_idx_type ii = 0; ii < perm_index.numel (); ii++) > perm_index (ii) = perm_index (ii) - 1; > Array<double> a=args(0).array_value (); > > > Array<double> b = a.permute (perm_index); > return octave_value (b); > > } > -------------------------------------------------------- > > >> a = reshape (1:16, [4 2 2]); > >> b = mi_funcion (a, [3 1 2]); > >> c = permute (a, [3 1 2]); > >> size (b) > ans = > > 2 4 2 > > >> size (c) > ans = > > 2 4 2 > > >> size (a) > ans = > > 4 2 2 > > >> > -------------------------------------------------------- > > c. > > > |
[Prev in Thread] | Current Thread | [Next in Thread] |