help-octave
[Top][All Lists]
Advanced

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

Re: Problem with a dynamically linked function


From: c.
Subject: Re: Problem with a dynamically linked function
Date: Thu, 5 May 2011 21:09:28 +0200

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.





reply via email to

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