[Top][All Lists]

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

Re: Dyn. Linked FAQ (Was .oct-files/ library-wrapper)

From: Pascal Fleury
Subject: Re: Dyn. Linked FAQ (Was .oct-files/ library-wrapper)
Date: Sat, 20 Jan 2001 01:46:01 +0900

Douglas Eck wrote:

> I have started a FAQ for writing Dyn. Linked (.oct) files in octave 2.1.
> I wasn't going to share it yet, because it's very sketchy. Also,
> isn't in FAQ mode yet... just a wrapper .html page and
> a few source files.
> Maybe it will be useful.
> Comments are greatly appreciated from everyone .
> See
> -Doug

Hi All !

Having asked a very similar question a while ago on this list, and maybe to
reuse Paul's answers I got at that time, I repost what he wrote, and which
turned out to be very helpful to me as a starter. Otherwise, ov-base.h is
the file to print and to take close to your bedlamp!

Paul Kienzle wrote:

> Pascal,
> No, there is no good API definition.  All of the internals of Octave are
> available to your function.  This is a mixed blessing.  On the one hand,
> You can do absolutely anything, but on the other, modularity quickly gets
> broken.  As far as I'm concerned Octave is not yet mature enough to have
> a stable API.  At some point it would be worthwhile going through all the
> DLD functions, seeing what they use, clean up the interface and only make
> those bits visible.
> Now, to answer your questions.
> You let the octave interpreter know about your functions using
> DEFUN_DLD (name, args, nargout, "documentation string")
> {
>    octave_value_list retval;
>    int nargin = args.length();
>    // function body
>    return retval;
> }
> The actual arguments are of type octave_value, and can be accessed
> as args(0), args(1), ... args(nargin-1).  In practice, you will want
> to manipulate the values as a specific type.  First you must test
> that it is of the correct type, and then extract the value of that
> type.  For example,
>    if (args(0).is_real_scalar())  {
>       double v = args(0).double_value();
>       // action to take when processing double argument
>    }
> The best source of information for useful operations on octave values
> is in the file src/ov-base.h.
> Usually you will be dealing with matrices in your octave file.  Liboctave
> defines lots of operations on matrices, which are described (or at least
> listed) in doc/liboctave.  You have the usual element-wise operators,
> as well as matrix product, quotient, solvers, inverse, transpose, lu
> decomposition, qr decomposition, etc.  The matrix types are Matrix,
> RowVector, ColumnVector and DiagMatrix, plus Complex versions of this.
> Most reasonable operations between these various types are defined.
> There are also charMatrix and boolMatrix types with a different set
> of operators.
> All matrix elements can be accessed using subscripts as in x(i,j).
> An element can either be assigned to or returned from.  You can force
> bounds checking by using x.checkelem(i,j) as your element reference.
> You can avoid bounds checking by using x.elem(i,j) as your element
> reference.  Note that arrays in liboctave are copy-on-modify.  If you
> assign one array to another, it simply copies a pointer to the data and
> increments a reference count.  When you assign to an element of an array
> which is referenced from two places, it makes a copy of the entire array.
> x.xelem(i,j) does not check if it is a copy before operating.  If you are
> passing the array to a FORTRAN or C subroutine, you can pass
> if the function will not modify the array, or x.fortran_vec() if you
> want to modify the array.  fortran_vec() will make a unique copy of the
> array if it is referenced from more than one place.  Note that the data
> is stored in FORTRAN rather than C array format.  In C, treat it as a
> pointer to a vector, and reference the elements of the vector as rows*j+i.
> The return values are easier since the octave_value constructor is
> overloaded to automatically convert the basic types into octave values.
> You simply assign retval(i) with the ith return value.  You don't need
> to assign more than nargout values to retval since the interpreter will
> simply throw away the rest.
> In the Octave 2.1.x series, you can have multiple DEFUN_DLDs in the
> same .oct file.  One of them must have the name of the oct file since
> that's how the interpreter finds the correct file to load.  However,
> once it is loaded, all DEFUN_DLD routines in the file are available.
> The .oct file remains loaded until the DEFUN_DLD's are explicitly
> cleared. [true?]  In practice, this means that you name the octave
> file after your initialization procedure, and everything works fine.
> I usually find that the interpreter crashes if I recompile the .oct file
> out from under it, but I haven't tried it on recent versions of octave.
> In Octave 2.0.x, each DEFUN_DLD needs to be in its own file of the same
> name as the function.
> I hope this helps.
> Paul Kienzle
> address@hidden

Paul Kienzle wrote:

> On Tue, Nov 14, 2000 at 11:13:09AM +0900, Pascal Fleury wrote:
> > Hi again!
> >
> > Thanks for the information. The pointer to files were very helpful.
> >
> > I have another question, though. I am trying to return an Octave
> structure. I
> > tried to use the type octave_struct, which is itself an octave_value,
> but when
> > I try to insert it into an octave_value_list, Octave crashes. Attached
> is the
> > file I try to compile.
> > So: is this a known "feature" ? Is my way of inserting values into a
> struct
> > wrong ?
> >
> > (The information I am tryin to pass along is the description of the
> sound file
> > I am trying to read, which has no inherent ordering, and is thus best
> accessed
> > by name -> the octave structure is perfect for this.)
> >
> > Thanks,
> > Pascal

> I've never done this, but it would appear that the solution to your
> problem
> can be found on the help-octave mailing list, Oct 3, 2000:
>         user-defined Octave C++ functions
>   DEFUN_DLD (test_struct, args, ,
>     "test_struct")
>   {
>     ColumnVector tmat (3);
>     for (int i = 0; i < 3; i++)
>       tmat(i) = i;
>     Octave_map table;
>     table["strfield"] = "test this";
>     table["matfield"] = tmat;
>     return octave_value (table);
>   }
> - Paul

Attachment: p.fleury.vcf
Description: Card for Pascal Fleury

reply via email to

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