0

Fixed

# COMPLEX (& perhaps ISREAL) method - wrong behavior

>> c1 = complex(12)

c1 =

12 + 0i

>> isreal(c1)

ans =

0

>> c2 = complex(mp(12))

c2 =

12

>> isreal(c2)

ans =

1

Служба підтримки клієнтів працює на UserEcho

new stuff:>> complex(0,pi*rand(0,1))

ans =

Empty matrix: 0-by-1

>> complex(0,mp(pi)*rand(0,1))

Error using mp/complex (line 2354)Input arrays must have the same size.

Thank you very much for excellent report(s)!

The second issue (complex(0,mp(pi)*rand(0,1))) has been fixed in our trunk - will be available in a few days.

Corner cases with empty inputs are nightmare - code becomes macaroni type.

But the first issue (complex(mp(12))) is not a bug. It is by design :).

Toolbox automatically detects if numbers are complex or real. There is no point to push the user to do this manually.

I believe Matlab allows real numbers (without imaginary part) to assign as complex for historical reasons. Probably they needed to pre-allocate planar memory or similar.

But there is no practical reason for this right now, especially for toolbox where memory architecture is very different. So that toolbox just does nothing if user assigns pure real number as complex - this doesn't influence subsequent math functionality.

Or do you know the case when it is needed from mathematical point of view? I might miss something here.

Under assumption

paraphrasing laws of robotics, I could formulate the following:

1. A custom toolbox function may not harm a Matlab function.

2. A custom toolbox function may not behave different from homonymous Matlab function under the same interface as the Matlab function.

From Matlab documentation:

More:

Matlab say:

You, as an author, say it's an improvement; I, as a user do not share your opinion. :)

Not for historical reasons but for mathematical reasons; any real number is a complex number but not any complex number is a real number.

For this reason "double" class has "isreal" method but not "

~~iscomplex~~" method :).The solution is very handy; can be added alongside/inside ;) the property ID another boolean property ...

How about influence, I find huge! Try something like;

I hope you have not done so already! :))

Here I am totally agree with you ! :)

Thank you very much for your insights!

Toolbox does follow MATLAB syntax and semantic to the maximum possible extent.

Along the way I add/remove some features (which do not harm the whole idea).

I thought about this particular feature of having real number as complex - I couldn't come up with any mathematical code which can benefit from this. Why it is needed?

I would understand if MATLAB would allow +/- zeros - then this feature would be vital for branch cuts, etc.

But now,when there is only one zero - this feature is pointless from computational point of view.

Could you please educate me with some computational/math code where this might be important?

I haven't seen one so far (possibly working with complex infinities?)

Other math systems have no such feature as well (as far as I know). Usually complexity of numbers are automatically detected and treated as such.

I would greatly appreciate example of real-life code where storage format of a real number is important.

In reality, complex(12) setups the storage format of a number, not the semantic - since it is treated as real number in all mathematical operations anyway. Thus I concluded it was related to legacy storage format in MATLAB.

Please teach me where my reasoning is wrong.

Not crossed my mind for a moment the idea of transforming complex numbers with zero imaginary part in pairs (Re, Im) in memory.

The idea is to

CONCEPTUAL(not in computer memory) treat complex numbers with zero imaginary part as complex numbers and not as real numbers, just as it does Matlab.Already we have:

The problem is not as complex numbers with zero imaginary part receive not the same memory space as those with non-zero imaginary part but as 'Isreal' method results are different from those legitimately expect and which I rely.

For my part I wish you to make them fit in a bit :), but 'isreal' must return the same result as in Matlab.

Without change than the minimum can solve the problem (the problem is not mathematics but programming problem).

In addition, small changes in the constructor and 'complex', 'isreal', 'disp' and 'display' methods.that is all!

Imagine what misfortune can happen if I rely the execution of a vital procedures on the result of test 'isreal(var)'

in the actual conditions.

Complexity for a number, defined as belonging to the set of complex numbers, it is not a matter of ... lottery, if it happens to have nonzero imaginary part then is complex, otherwise it's real. No, it is not so!

Complexity is an attribute (true/false) and its modification is only done algorithmically, not sifting through the numbers to find those with non-zero imaginary part.

This is true for pure theoretical & rigorous world. But for computations, there are a lot of cases when algorithms change numbers from real to complex automatically. All matrix functions detect if input is pure real (even if it is stored as complex) to use real-tailored algorithms since they are way faster. Same for routines which might generate complex outputs, e.g. sqrt(-1), eig, etc.

The system which rigorously treats reals as subset of complex numbers just doesn't exist in practice (I think). Instead complex-rules are applied only where necessary, depending of situation. I tried to investigate this in short notes here: http://www.advanpix.com/2015/10/19/devnotes-1-multiplicative-operations-with-complex-infinity

I promise that I will implement this storage format as soon as I see its importance in some application :).

(The only important case I see is complex infinities, but again MATLAB converts real to complex automatically in this case - so should do the toolbox)

I'm afraid I was not clear enough.

The format is NOT a problem. Period.

The problem,

minimum minimorum, is that a number (array) that intentionally was created as COMPLEX is reported by 'isreal' as real (NOT COMPLEX), contrary to documented behavior of the Matlab.You are absolutely right.

I changed semantic of 'isreal' on purpose, with the idea to improve MATLAB.

Now 'isreal' checks if array is pure real (imag(x)==0), independently of how it was created.

Back then I saw no serious consequences for this change. Probably I need to re-think this.

All things electronic, energy, radio, etc. mean complex numbers ...

Of course I meant the software, including programming languages and systems like MATLAB, Maple and similar.

If you treat the calculations reliability in complex numbers as a whim of mine, you're wrong.

Ask any other user Matlab (including Advanpix Toolbox) from the control systems, process simulation, energy transport, communications radio plus an infinite number of other high-tech fields if they need reliable computing with complex numbers.

Topic closed (for me at least!).

Also whole family of subs*** methods should be changed, and others. Plus overhead of detecting the pure reals before every operation (one pass over array) and many other complications.

Anyway it is doable of course, but the question is do we need this at all :).

Let me summarize the reasoning.

Fact 1.Storing real numbers as complex with zero imaginary part has no any influence on mathematical functionality of MATLAB itself:

As you see, zero-imaginary part is not carried around. So that MATLAB itself doesn't support rigorous handling of complex numbers. This is very justified strategy, any mathematical software follows it. Toolbox follows this as well. Automatically detect where switching to complex is needed (sqrt(-1), eig, etc.), otherwise rely on reals for speed.

Fact 2.The MATLAB doesn't support signed zeros - this is the only case where storing reals as complex numbers would have mathematical sense (to work with branch cuts, etc.)

Conclusion.This feature was designed to optimize storage format, so that you can pre-allocate complex array before computations to avoid reallocations:

The only function which is affected by this functionality is isreal.

As follows from the above - isreal is designed to just check the storage format of a number.

Fact 3.All these attempts to optimize storage format, pre-allocation, etc. for doubles - has very little sense for multi-precision numbers/arrays.

It is long story why - but in short it is because MATLAB doesn't allow creating objects of custom type and keeping them inside MEX, storing just pointers to the objects as property field.

As a result object data have to be moved MEX<-->MATLAB on every operation. In this case pre-allocation hurts a lot - actually it is better to keep size of the objects small (grow as needed) to minimize memory to be transferred.

Toolbox design.- All complex-related functionality in toolbox is done exactly as in MATLAB, with the exception of isreal.

- We use different storage format, and we can use isreal for more meaningful purpose.

Now it checks if input has zero imaginary part. In fact

it generates exactly the same output as original isreal in ALL cases, except when real is stored as complex- which is ignored by mathematical functions in MATLAB anyway, as you see above.Critics.In all years of translating MATLAB codes to multiprecision I haven't encountered the case where toolbox version of 'isreal' affected functionality. But there is always a possibility of mistake.

Therefore I would greatly appreciate if you would show computational code where this meaning of 'isreal' might be harmful.

Just want to let you know, that I have extended toolbox with support of "shallow" complex numbers:

But, in comparison to MATLAB, toolbox supports complex numbers much more correctly, including branch cuts, signed zeros as imaginary part, etc.

If you are interested in details, I wrote short post with examples for demonstration:

http://www.advanpix.com/2016/04/28/branch-cuts-and-signed-zeros-in-matlab/