0
Completat

CAST method may also be useful to others

john doe fa 9 anys actualitzat fa 9 anys 9

current status:


>> a = mp(mprandn(2,1))
b = [0, 1]
a =
0.4334207802637199612271739466794224
-0.2701469591393686343297786869883695
b =
0 1


>> cast(a,'double')
ans =
0.43342
-0.27015
>> cast(a,'like',b)
ans =
0.43342
-0.27015
>> cast(b,'mp')
Error using cast
Unsupported data type for conversion: 'mp'.


>> cast(b,'like',a)
Error using cast
Unsupported data type for conversion: 'mp'.


>> cast(a,'ss')
Error using cast
Unsupported data type for conversion: 'ss'.


with a CAST method like below ...


function r = cast(A,newclass,varargin) %#ok<INUSL>
narginchk(2,3);
if strcmp(newclass,'like')
narginchk(3,3); newclass = class(varargin{1});
end
try r = eval([newclass,'(A)']);
catch, r = eval([newclass,'(double(A))']); end
end


... the new status:


>> cast(b,'mp')
Error using cast
Unsupported data type for conversion: 'mp'.


>> cast(b,'like',a)
ans =
0 1


>> cast(a,'ss')
ans =

d =
u1
y1 0.4334
y2 -0.2701

Static gain.


Of course, my version is incomplete, but enough for me :)

By the way, on what grounds 'mp' inherits not 'double' ?


Under review

@"By the way, on what grounds 'mp' inherits not 'double' ?"


This is very good question. I am asking it myself everyday.

From one point of view it would make existing code porting to mp much easier at some cases.


However, in the same time, this might cause hard-to find errors of accuracy loss.

For example, MATLAB scripts include many use cases of:


zero(m,n,class(a))

ones(..,class(a))

eps(class(a))


If class(a) return "double" for mp-objects - all the commands will create double objects silently, which is obviously will lead to hard-to-find errors. Especially with "eps".


Perfect way would be to overload the built-ins "zeros, ones, eps, etc." completely, for all arguments (not only mp). And add support for 'mp' class to all these functions. However MATLAB screams on this overloads.


Do you know, or could you suggest on how to overload all the system functions, so that MATLAB will not go crazy with all the warnings?


***

Cast is in my TODO list - thank you for sharing your code!


I will make this thread public, if no objections.


There are no independent solutions but solutions to problems.


Some answers to your questions, apparently, they are trivial and therefore I am afraid I do not understand exactly the problem; so please expose some code and error/warning messages from Matlab.

So certainly the odds of finding solutions will grow.

@"By the way, on what grounds 'mp' inherits not 'double' ?"


Would it be more useful? Could you please show code example which can benefit from this?


Now mp-numbers have 'mp' class, different from 'double'.


Thanks to this difference, now it is easy to detect cases like zeros(m,n,class(a)). When a have 'mp' type - MATLAB screams with error, and we can fix this code.


If mp-objects would have 'double' class - MATLAB would create array of zeros in 'double' precision.

To overcome this, we would need to provide overloads for built-in functions - zeros, ones.

Which will return 'mp'-objects even for plain integer parameters. E.g zeros(10) will return 'mp'-matrix.


I don't know reliable way of such global overloading built-ins, ones, zeros, eps, etc.

Do you know?


Yes, I know about @double directory and placing all overloads there. But is it reliable?

As it turned out, MATLAB allows overloading zeros, ones, etc. in recent versions :).

http://www.mathworks.com/help/matlab/matlab_oop/class-support-for-array-creation-functions.html


This changes everything.

the words "... apparently, they are trivial and ..." was based, among other things, on page content indicated by you :)

Of course, I have nothing against :)

Completat

I have added both types of cast to mp class - cast(A,'mp') and cast(A,'like',B).

But I prefer to avoid intermediate conversion to double like this:

try r = eval([newclass,'(A)']);
catch, r = eval([newclass,'(double(A))']); end    %<-----

Also I added test matrices - hilb, invhilb, hadamard, pascal, magic, rosser, wilkinson. Now all of them can create mp-matrices with last parameter classname='mp':

>> magic(3,'mp')

Would greatly appreciate if you would test the new version. Let me know if you find anything strange