octave-maintainers
[Top][All Lists]
Advanced

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

Re: Explanation of Octave operator overloading


From: John Swensen
Subject: Re: Explanation of Octave operator overloading
Date: Fri, 05 May 2006 09:49:03 -0600
User-agent: Thunderbird 1.5.0.2 (Windows/20060308)

David Bateman wrote:
> John Swensen wrote:
>> David Bateman wrote:
>>  
>>> John Swensen wrote:
>>>    
>>>> Could someone give a quick tutorial on how the operator overloads work
>>>> in Octave (and maybe add it as a section in the Da Coda Al Fine
>>>> section
>>>> of the wiki)?
>>>>
>>>> For example, I have defined a new type that derives from
>>>> octave_base_value called octave_socket.  The problem is that I want to
>>>> be able to compare it to numerical values.  I have already tried
>>>> simply
>>>> overloading the operator< to compare against
>>>> octave_value.int_value() as
>>>> well as just a plain int.
>>>> I see there are a bunch of macros called OCTAVE_INSTALL_*_*_*_CMP_OPS
>>>> and assume I have to do something with these, but haven't been able to
>>>> figure out what.
>>>>
>>>> Thanks.
>>>>
>>>> John Swensen
>>>>
>>>>         
>>> A good little example is in
>>> octave-forge/extra/linear-algebra/ov-re-tri.cc which overloads the
>>> assign, ldiv, hermitian and transpose operators for a triangular
>>> matrix type. The reason its good is that it is tiny. In particular
>>> check the install_tri_ops functions, the functions that are defined
>>> immediately above this for the overloaded functions, and finally note
>>> that install_tri_ops is called immediately after the type is
>>> registered in the replacement chol.cc function. Note that I intend to
>>> delete this code in octave-forge now that the code to treat positive
>>> definite matrices is in octave itself, so rescuing it and using it as
>>> an example in da coda or del segna is probably a good idea. In fact if
>>> you are really motivated, rescuing both da coda and del segna, and
>>> this code and writing a chapter on oct-files for the octave 3.0 manual
>>> might be even better :-)
>>>
>>> Regards
>>> David
>>>
>>>
>>>     
>> Thanks for pointing me in the right direction.  That example gave me
>> exactly what I needed.  I did find, however, that for simple comparison
>> operations, there are macros to make it *really* easy.  The DEFBINOP_OP
>> macro allows you to simply implements an accesor function in the
>> user-defined type to return some integer value that can then be compared
>> against other octave types.  In my case, I have a octave_socket class.
>> I used the DEFBINOP_OP macro as follow:
>> DEFBINOP_OP (lt, scalar, scalar, <)
>> DEFBINOP_OP (le, scalar, scalar, <=)
>> DEFBINOP_OP (eq, scalar, scalar, ==)
>> DEFBINOP_OP (ge, scalar, scalar, >=)
>> DEFBINOP_OP (gt, scalar, scalar, >)
>> DEFBINOP_OP (ne, scalar, scalar, !=)
>>
>> I then had to overload the scalar_value() method in my user-defined type
>> and now I can compare against integers with all the operators.
>>
>> Thanks again!
>>
>> - John Swensen
>>
>>
>>   
>
> Oh, well no octave 3.0 manual section on oct-files :-).. Other
> interesting stuff can be found in the src/OPERATORS directory of
> octave itself..
>
> D.
>
>
I am willing to work on a oct file section for the next manual.  I have
found the oct file API to be particularly easy to use (as compared to
the C/C++ API of the most popular brand).  And as I mentioned in my
fundable.org request, I should have plenty of time between when I take
the voluntary layoff at work and when I start school in the fall.

John Swensen



reply via email to

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