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: Tue, 05 Feb 2013 00:02:22 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120825 Thunderbird/15.0

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?
>



reply via email to

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