Problem with a dynamically linked function

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with a dynamically linked function

alejandro zamora soto
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






_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem with a dynamically linked function

Jordi Gutiérrez Hermoso-2
On 5 May 2011 09:31, alejandro zamora soto <[hidden email]> wrote:

>    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:

The permute function in Octave already does this. Why do you want to
re-implement it?

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem with a dynamically linked function

c.-2
In reply to this post by alejandro zamora soto

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.



_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

RE: Problem with a dynamically linked function

alejandro zamora soto
In reply to this post by Jordi Gutiérrez Hermoso-2
Dear Jordi,

My idea is to write a function wich takes different tensor with
different dimensions and finally gives a tensor as a result of
tensor multiplication.
This is the first time I write it in C++ and just I wanted to
implement a toy function, instead the final function. For
me is useful because I want to know how to write correctly
the vector or array with the indices for the permutation.

regards,
alex

> Date: Thu, 5 May 2011 12:42:27 -0500
> Subject: Re: Problem with a dynamically linked function
> From: [hidden email]
> To: [hidden email]
> CC: [hidden email]
>
> On 5 May 2011 09:31, alejandro zamora soto <[hidden email]> wrote:
>
> >    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:
>
> The permute function in Octave already does this. Why do you want to
> re-implement it?
>
> - Jordi G. H.

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

RE: Problem with a dynamically linked function

alejandro zamora soto
In reply to this post by c.-2
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: [hidden email]
> Date: Thu, 5 May 2011 21:09:28 +0200
> CC: [hidden email]
> To: [hidden email]
>
>
> 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.
>
>
>

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Problem with a dynamically linked function

c.-2

On 6 May 2011, at 11:42, alejandro zamora soto wrote:

> 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

You can have a look at the code for the built-in function 'permute' which is in the file src/data.cc in the Octave source tree.
It seems to me that it's does essentially the same as in the example I sent, though ...

 Array<int> vec = args(1).int_vector_value ();
 int n = vec.length ();
 for (int i = 0; i < n; i++)
        vec(i)--;
c.


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

RE: Problem with a dynamically linked function

alejandro zamora soto
Thank you very much, Carlo

I will take a look at this file...

regards,
Alex

> Subject: Re: Problem with a dynamically linked function
> From: [hidden email]
> Date: Fri, 6 May 2011 12:40:05 +0200
> CC: [hidden email]
> To: [hidden email]
>
>
> On 6 May 2011, at 11:42, alejandro zamora soto wrote:
>
> > 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
>
> You can have a look at the code for the built-in function 'permute' which is in the file src/data.cc in the Octave source tree.
> It seems to me that it's does essentially the same as in the example I sent, though ...
>
> Array<int> vec = args(1).int_vector_value ();
> int n = vec.length ();
> for (int i = 0; i < n; i++)
> vec(i)--;
> c.
>
>

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave