[Top][All Lists]

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

mkoctfile compilation issues/questions

From: John W. Eaton
Subject: mkoctfile compilation issues/questions
Date: Sat, 14 Feb 2004 19:52:40 -0600

On 14-Feb-2004, Charles R. Wright <address@hidden> wrote:

| I'm trying to create a loadable function and I'm using the code
| attached below as a proving ground before I get in deep.  What I'd
| like to do is make the DEFUN_DLD-created function be a wrapper and
| call another function that does all the work.
| Also, please point me to some good examples for using matrices and
| vectors within a function.  Naturally, I'd like to use the classes
| provided by Octave.  I've been looking at octave-forge, but haven't
| come across anything good for that yet.

There are a number of examples inside the Octave sources, I think.

| P.S. I'm working on a Vitierbi decoder - looks like octave-forge could use 
| one.  The .m file is dog-slow.  I'm hoping the .oct version will be lots 
| faster!

If you could post the .m file code, people might be able to help you
write it so that it is reasonably efficient without having to convert
it to C/C++.

|     printf("Number of input args: %d\n", args.length());

Since this is C++, you probably want to use

  std::cout << "Number of input args: << args.length () << std::endl;

instead of using the C library functions.  Or, it might be better to

  octave_stdout << "Number of input args: << args.length () << std::endl;

if you want the output to go through Octave's pager.

| Array *
| testFunc()
| {
|     int * foo;
|     Array * testArray = new Array * [1];
|     return foo;
| }

The Array class is a template, so you should write Array<double> or
something similar.  If you are trying to return the new Array object,
then why are you returning foo, which is a pointer to an integer (and
one that points to no particular int value).  Also, you probably don't
want to use new.  Octave's Array class manages memory with reference
counting, so it is relatively cheap to return a copy by value.  But,
if you are going to return the Array to Octave or do any arithmetic
with it, you should probably use a ColumnVector, RowVector, or Matrix
object instead.  I might write your function something like this:

// Testing how to write code for dynamically loadable functions
// Charles R. Wright
// 2004-02-11

#include <octave/oct.h>

test_func (int n, double val)
  // Create a ColumnVector of length N with elements initialized to VAL.
  return ColumnVector (n, val);

DEFUN_DLD (octest, args, ,
  "Cut-and-try function for dynamic loading in Octave")
  octave_value_list retval;

  int nargin = args.length ();

  octave_stdout << "Number of input args: " << nargin << std::endl;

  if (nargin > 0)
      octave_value invec = args(0);

      octave_stdout << "Size of input arg 0: "
                    << invec.rows () << " rows "
                    << invec.columns () << " cols" << std::endl;

  ColumnVector foo = test_func (12, 3.0);

  for (int k = 0; k < foo.length (); ++k)
      octave_stdout << "foo[" << k << "] = " << foo(k) << std::endl;

  // Return foo (octave_value_list objects are automatically resized).

  retval(0) = foo;

  return retval;

But please, only go the C++ route if you really must have the speed
and there is no reasonable way to write the function in the scripting
language so that it is fast enough.  It is generally much easier to
maintain the code if it is written in the scripting language.



Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:
How to fund new projects:
Subscription information:

reply via email to

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