help-octave
[Top][All Lists]
Advanced

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

Re: arrays of objects.


From: ernst
Subject: Re: arrays of objects.
Date: Fri, 08 Feb 2013 14:49:11 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120825 Thunderbird/15.0

On 02/05/2013 11:05 AM, Juan Pablo Carbajal wrote:
> On Tue, Feb 5, 2013 at 10:21 AM, Juan Pablo Carbajal
> <address@hidden> wrote:
>> On Tue, Feb 5, 2013 at 12:02 AM, ernst <address@hidden> wrote:
>>> Hi Juan,
>>>
>>> if you don't mind that the example I give you is not useful,
>>> i can give you a quite simple one:
>>>
>>> constructor:
>>>
>>> function num = pn(dNum)
>>>
>>>   switch (nargin)
>>>     case 0
>>>       dNum = 0.0;
>>>     case 1
>>>       % nothing to do
>>>     otherwise
>>>       print_usage();
>>>   endswitch
>>>
>>>   if ~isa(dNum, 'double')
>>>      error(strcat('invalid type: ',class(dNum)));
>>>    endif
>>>   num.dNum = dNum;
>>>   num = class(num, 'pn');
>>> endfunction
>>>
>>> As you can see, i just wrap a double in a class object.
>>>
>>> The converse, is also easy:
>>>
>>> function dNum = doubleValue(num)
>>>    dNum = num.dNum;
>>> endfunction
>>>
>>>
>>> With this setting, you can reproduce what I wrote before (see below),
>>> right?
>>>
>>> greetings, Ernst
>>>
>>>
>>>> On Mon, Feb 4, 2013 at 10:25 PM, ernst <address@hidden> wrote:
>>>>> Hi all,
>>>>> I want to make octave use another kind of number
>>>>> (just think of intervals, rationals, continued fractions.....) almost
>>>>> like using the builtin types.
>>>>> To this end, i have a class pn and constructor invocation like pn(3.4).
>>>>>
>>>>> This already works quite well, I can do some basic programming works in
>>>>> octave
>>>>> using my own numbers.
>>>>> I can even use vectors and matrices: [pn(2) pn3); pn(4) pn(5)] works.
>>>>>
>>>>> BUT: I have problems with higher dimensional objects.
>>>>> One example is arrayfun:
>>>>> On the one hand side,  a=arrayfun(@pn,rand(2,3,4)) seem to work,
>>>>> on the other hand, arrayfun(@doubleValue,a) does not work,
>>>>> raising
>>>>>
>>>>> "error: can't perform indexing operations for class type",
>>>>>
>>>>> although doubleValue(pn(3)) does the correct conversion to double as
>>>>> expected.
>>>>>
>>>>> I tried with arrayfun with doubles and with chars, both work well.
>>>>> Does it make sense, not to allow general classes?
>>>>> How does Matlab react?? (i dont have one, because it is too expensive).
>>>>>
>>>>> The alternative would be, to provide array-indexing for my class, but i
>>>>> think, this makes no sense,
>>>>> because i just want to use the indexing provided by octave.
>>>>> I have the idea it must be like for java or c, where array indexing
>>>>> is not needed to be implemented for each type.
>>>>>
>>>>> Help very much apprechiated.
>>>>>
>>>>> greetings,
>>>>>
>>>>> Ernst
>>>>> _______________________________________________
>>>>> Help-octave mailing list
>>>>> address@hidden
>>>>> https://mailman.cae.wisc.edu/listinfo/help-octave
>>>> Can't tell what is the issue without more information, but based on my
>>>> own experinece with cellfun, the old class style is not woring very
>>>> well inside those functions. Is as if one would need to declare these
>>>> functions "friend functions" as in C++.
>>>>
>>>> Though I can be completely off, not knowing exactly what is your
>>>> issue. Maybe you can upload your code to Agora?
>>>>
>> Hi ernst,
>>
>> People around here like that answers are posted below the previous
>> message and not on top.
>>
>> The problem you hit on is not the one I was referring to. As I
>> understand it, in your case, you should get an "invalid argument"
>> error rather than the one you get (more cryptic). Why? Since array fun
>> expects an array of type (from the help) logic, numeric, string,
>> structure or cell and you are passing an array of classes. For
>> example, this also would not work
>>  arrayfun(@length,[pn(1); pn(2)])
>> or any other execution of arrayfun with an array of classes.
>>
>> It is ok that arrayfun is not working in your case, cause the use you
>> want is not implemented. Still there is the problem of the cryptic
>> error message.
>>
>> If you have the time you could report the "Crpytic error message" when
>> passing an array of classes to arrayfun in the bug tracker, so this
>> issue is not lost. It is very basic to fix, so I will do it asap.
>>
>> Cheers
>
>

Hi Juan,

> For
> example, this also would not work
>  arrayfun(@length,[pn(1); pn(2)])
> or any other execution of arrayfun with an array of classes.
Right, and as you may expect, the 'cryptic error' comes up again.

"error: can't perform indexing operations for class type",

> It is ok that arrayfun is not working in your case, cause the use you
> want is not implemented. Still there is the problem of the cryptic
> error message.

I think, if octave is OO, then there are more classes to come and the
point in arrays and so is indexing,
no matter what the content is.
One cannot implement a separate array for each classtype, right?

Anyway, octave is a matlab clone and so, the measure is, what matlab does.
I have a documentation, that arrayfun is also restricted to certain
types of arrays.
So, all ok. 


I now try to reimplement arrayfun for at least my class.

Greetings,

Ernst


reply via email to

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