Problem creating compiled oct file

View: New views
6 Messages — Rating Filter:   Alert me  

Problem creating compiled oct file

by babelproofreader :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have written a function that by necessity uses a loop for recursive calculations and also has loops within the main loop and as a result it is very slow. Despite much investigation I have not been able to find out how to vectorise this function and so have decided to compile an oct file as I believe the loop structures can be easily written in c++. However I am having problems just passing arguments to the function and retrieving the output value. My code so far, in a file called "loop.cc", is

#include <octave/oct.h>
#include <octave/dColVector.h>
     
DEFUN_DLD (loop, args, , "Help String")
{
octave_value retval;
double numbers = args(0);
// Do something
octave_stdout << "The input values are " << numbers << "\n";
retval = numbers;
return retval;
}  

The input, args(0), is a column vector of values, but when I try compiling I get the error message

loop.cc: In function ‘octave_value_list Floop(const octave_value_list&, int)’:
loop.cc:7: error: cannot convert ‘octave_value’ to ‘double’ in initialization

Can anyone suggest how I can pass a column vector of values (consisting of positive and negative numbers with up to 6 decimal places) as an argument to the function? Also, in anticipation of problems in returning calculated values, what would be the correct syntax for the function to return 2 separate column vectors after calculations based on the input?

Re: Problem creating compiled oct file

by Søren Hauberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

lør, 03 10 2009 kl. 03:54 -0700, skrev babelproofreader:

> DEFUN_DLD (loop, args, , "Help String")
> {
> octave_value retval;
> double numbers = args(0);
> // Do something
> octave_stdout << "The input values are " << numbers << "\n";
> retval = numbers;
> return retval;
> }  
>
> The input, args(0), is a column vector of values, but when I try compiling I
> get the error message

C++ has a much more strict type system, so you can't expect it to work
out types for you at run-time. You need to do something like

  ColumnVector c = args (0).column_vector_value ();
  octave_stdout << c (0) << std::endl;

To see which member functions the 'octave_value' class has, you can look
at

http://octave.sourceforge.net/doxygen/html/classoctave__value.html

To get some more knowledge about the C++ API, you might want to read the
Appendix in the Octave manual.

Søren

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Problem creating compiled oct file

by babelproofreader :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

C++ has a much more strict type system, so you can't expect it to work
out types for you at run-time. You need to do something like

  ColumnVector c = args (0).column_vector_value ();
  octave_stdout << c (0) << std::endl;

What syntax would I use to access the individual elements of c?

Re: Problem creating compiled oct file

by Carlo de Falco-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 3 Oct 2009, at 16:47, babelproofreader wrote:

>
> C++ has a much more strict type system, so you can't expect it to work
> out types for you at run-time. You need to do something like
>
>  ColumnVector c = args (0).column_vector_value ();
>  octave_stdout << c (0) << std::endl;
>
> What syntax would I use to access the individual elements of c?

That's already shown in Søren's example: "c (0)" stands for the first  
element of c
the example below shows how you would print all elements of c

for (ii = octave_idx_type (0); ii<c.length (); ii++)
    octave_stdout << c (ii) << std::endl;

you would use exactly the same syntax to assign element values

ColumnVector d(c);
for (ii = octave_idx_type (0); ii<c.length (); ii++)
    d(c.length () - ii - 1) = c(ii);


For more on writing oct files see here:

http://www.gnu.org/software/octave/doc/interpreter/Oct_002dFiles.html#Oct_002dFiles

or here:

http://octave.sourceforge.net/coda/index.html

HTH,
c.



_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Problem creating compiled oct file

by babelproofreader :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I have written a function that by necessity uses a loop for recursive calculations and also has loops within the main loop and as a result it is very slow. Despite much investigation I have not been able to find out how to vectorise this function and so have decided to compile an oct file as I believe the loop structures can be easily written in c++. However I am having problems just passing arguments to the function and retrieving the output value. My code so far, in a file called "loop.cc", is....

My immediate problem is now solved and I post the code below in the hope that it will help others who may search this forum.

#include <octave/oct.h>
#include <octave/dColVector.h>
     
DEFUN_DLD (basicloop, args, , "Help String")
{
octave_value retval;

 ColumnVector c = args(0).column_vector_value ();       // This is the input argument, in column vector form
 ColumnVector d(c);                                                      // This will be the output column vector returned to Octave by "retval"
 int ii;                                                                           // Declare the loop counter ii as integer type

 for (ii = octave_idx_type (0); ii<c.length (); ii++)       // Start the loop
     {
     d(c.length () - ii - 1) = c(ii) + 0.5;                          // Do something in the loop calculation
     }
 
 retval = d;                                                            // Assign the output column vector to the return value

return retval;                                                        // Return the output to Octave
}


Re: Problem creating compiled oct file

by Carlo de Falco-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 4 Oct 2009, at 17:22, babelproofreader wrote:

> int ii;
> // Declare the loop counter ii as integer type
>
> for (ii = octave_idx_type (0); ii<c.length (); ii++)       // Start  
> the
> loop

It is better, for code portability, to use "octave_idx_type" rather  
than "int" as the type for vector indices so you could change the  
above to

> for (octave_idx_type ii (0); ii<c.length (); ii++)

note that this also changes the scope of the variable ii so that it is  
only visible inside the "for" loop, I usually find this convenient as  
I tend to use the same name for all counters...

c.
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave