help-octave
[Top][All Lists]
Advanced

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

Re: Techniques in using liboctave and liboctinterp from C++ program


From: Jaroslav Hajek
Subject: Re: Techniques in using liboctave and liboctinterp from C++ program
Date: Fri, 27 Jun 2008 21:27:32 +0200

On Fri, Jun 27, 2008 at 9:22 PM, John Swensen <address@hidden> wrote:
>
> On Jun 27, 2008, at 9:44 AM, Jaroslav Hajek wrote:
>
>> On Fri, Jun 27, 2008 at 3:01 PM, John Swensen <address@hidden>
>> wrote:
>>>
>>> On Jun 26, 2008, at 1:56 PM, dbateman wrote:
>>>
>>>>
>>>>
>>>>
>>>> John Swensen wrote:
>>>>>
>>>>> I have started using the datatypes and operations of Octave from C++
>>>>> for my research and have a few questions.
>>>>>
>>>>> 1) Can builtin functions and DLD-FUNCTIONS (e.g. find, eig, conv2,
>>>>> etc) be used directly from C++?
>>>>>
>>>>
>>>> You can always use feval to call an m-file or oct-file. You can't
>>>> really
>>>> call an oct-file directly from C++. What you can have for example is
>>>> if the
>>>> oct-file calls another C++ function, you might declare that function
>>>> extern
>>>> and call it as well, though the oct-file would have to be loaded first
>>>>
>>>>
>>>>
>>>>> 2) In the documentation, is there a list of overloaded operators for
>>>>> octave_value objects of different types?  For example, I was trying
>>>>> to
>>>>> do the following:
>>>>> Matrix A(768,1024);
>>>>> Matrix B(1,1);
>>>>> Matric C = A*B;
>>>>> This, however, didn't work.  Instead, I had to declare B as a scalar
>>>>> double as follows:
>>>>> Matrix A(768,1024);
>>>>> double b;
>>>>> Matrix C = A*B;
>>>>> This isn't necessarily a problem, but it would be nice if there was a
>>>>> list of possible overloads somewhere.
>>>>>
>>>>
>>>> That's not so easy for two reasons. Firstly someone has to do the
>>>> work of
>>>> documenting them. However, the second reason is harder. Having
>>>> documented
>>>> these functions they then define an API to the internals of Octave,
>>>> something that has never really been stable. Therefore some thought
>>>> needs to
>>>> go into this process of what functionality to document in an API.
>>>>
>>>>
>>>>
>>>>> 3) I often do image thresholding after I have processed it a bit.  In
>>>>> the Octave interpreter I can simply type 'im2 = im1>0.5'.  I tried
>>>>> doing the following, but it didn't work:
>>>>> Matrix A(768,1024);
>>>>> double B = 0.5;
>>>>> Matrix C = A>B;
>>>>> If I could find out where that bit of the interpreter was
>>>>> implemented,
>>>>> I could figure it out, but I searched through the sources a bit and
>>>>> couldn't find anything.
>>>>>
>>>>
>>>> I don't have a copy of Octave with me, but check the file
>>>> src/OPERATORS/op-m-m.cc for the gt function and see what it does.
>>>>
>>>> D.
>>>> --
>>>> View this message in context:
>>>> http://www.nabble.com/Techniques-in-using-liboctave-and-liboctinterp-from-C%2B%2B-program-tp18137086p18139690.html
>>>> Sent from the Octave - General mailing list archive at Nabble.com.
>>>>
>>>> _______________________________________________
>>>> Help-octave mailing list
>>>> address@hidden
>>>> https://www.cae.wisc.edu/mailman/listinfo/help-octave
>>>
>>> So I have kindof muddled my way through the operator business.  Once I
>>> got my head wrapped around all the #define's, it makes a lot of
>>> sense.  However, the comparator operators are still not working.  I
>>> can get it to compile fine, but every time I try to do a compare, I
>>> get a "fatal: T& Array<T>::checkelem (10, -1, -1): range error" error
>>> message.
>>>
>>> Here is a very simple example that causes the problem:
>>> // compile with 'mkoctfile --link-stand-alone test.cpp -o test'
>>> #include "octave/config.h"
>>> #include "octave.h"
>>> #include <octave/ov.h>
>>>
>>> // Standard C++ includes
>>> #include <iostream>
>>> #include <string>
>>> #include <vector>
>>> #include <map>
>>> using namespace std;
>>>
>>> int main (int argc, char* argv[])
>>> {
>>>  octave_value tmp1(0.5);
>>>  octave_value tmp2(1.0);
>>>  octave_value tmp3(0.5);
>>>
>>>  tmp1 == tmp2;
>>>
>>>  return 0;
>>> }
>>>
>>> I'm assuming this is some sort of indexing error, since the
>>> checkelem() function in Array.h appears to be checking indices.  What
>>> I don't understand is why it thinks these two scalar octave_values are
>>> somehow 3-dimensional.  Why isn't it using the checkelem() that
>>> appears to be tailored for single dimensional octave_values?
>>>
>> I believe it is not, you just see a random error. I get a segfault
>> with the same program.
>> The problem is that you invoke binary ops on octave_value objects, but
>> the typeinfo instance is not initialized. You can do this manually by
>> calling the instance_ok static method. Your program still won't work
>> as the types and ops must be registered first, but at least it will
>> give you a proper error message. See install_types() and install_ops()
>> for registering all common types and ops. I believe this also
>> eliminates the need to initialize typeinfo manually.
>>
>>
>> cheers
>>
>>
>>> John Swensen
>>> _______________________________________________
>>> Help-octave mailing list
>>> address@hidden
>>> https://www.cae.wisc.edu/mailman/listinfo/help-octave
>>>
>>
>>
>>
>> --
>> RNDr. Jaroslav Hajek
>> computing expert
>> Aeronautical Research and Test Institute (VZLU)
>> Prague, Czech Republic
>> url: www.highegg.matfyz.cz
>
> That was exactly what I needed.  But, instead of trying to figure out all
> that needs initializing, I simply called octave_main() with the embedded
> flags set and then was able to do operations on Matrix and ColVectors and
> scalars just as I wanted to.  Thanks!
>

Just to clarify: if you use the liboctave classes (Matrix,
ColumnVector etc) directly, there's no  need to initialize anything.
It's only the run-time type identification of octave_value class that
needs it.



> John Swensen
>



-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

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