[Scilab-users] display of complex/not real numbers, again

Stéphane Mottelet stephane.mottelet at utc.fr
Fri Sep 13 10:41:34 CEST 2019


If Windows users want to test the patch, I have prepared a Windows build 
of patched Scilab at the folowing URL :

http://www.utc.fr/~mottelet/scilab/download/master/scilab-branch-master-6cdd3ce0d62c632cd428b71440b0371a7731dbae_x64.exe

S.

Le 12/09/2019 à 18:55, Stéphane Mottelet a écrit :
> I prefer the display after applying 
> https://antispam.utc.fr/proxy/2/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/codereview.scilab.org/#/c/20981/:
>
> --> x0=%pi/4;h=%eps/2
>  h  =
>
>    1.110D-16
>
> --> cos(x0+%i*h)
>  ans  =
>
>    0.7071068 - 7.850D-17i
>
> However, we could discuss if the arbitrary switch to "e" mode is 
> desirable or not, but since Scilab 6.0 we have got used to this 
> display mixing "v" and "e" mode...
>
> BTW, this patch also fixes the more general problem of ambiguous 
> display of quantities like (below is the display after the patch)
>
> --> 1+%eps
>  ans  =
>
>    1.0000000
>
> Currently Scilab makes users believe that a complex/non real number is 
> real by hiding small non-zero real parts, and makes users believe that 
> a non-integer number is integer by hiding zeros in the fractional 
> part. Each year I have to warn my students, and I am really getting 
> upset about this. The aforementionned patch also fixes that.
>
> S.
>
>
> Le 12/09/2019 à 11:59, Stéphane Mottelet a écrit :
>>
>> Le 12/09/2019 à 11:55, Antoine ELIAS a écrit :
>>> Hello Stéphane,
>>>
>>> In Scilab 6.0.2 without format("e", 24)
>>>
>>> --> h = %eps/128, x0=%pi/4
>>>  h  =
>>>    1.735D-18
>>>
>>>  x0  =
>>>    0.7853982
>>>
>>> --> (cos(x0+h)-cos(x0-h))/2/h
>>>  ans  =
>>>    0.
>>>
>>> --> cos(x0+%i*h)
>>>  ans  =
>>>    0.7071068
>>>
>>> --> imag(cos(x0+%i*h))/h
>>>  ans  =
>>>   -0.7071068
>>>
>>> --> -sin(x0)
>>>  ans  =
>>>   -0.7071068
>>>
>>> It seems to be close of Matlab's outputs, no ?
>>
>> No, Scilab display is singularly different:
>>
>> --> cos(x0+%i*h)
>>  ans  =
>>    0.7071068
>>
>> the above has an imaginary part, which is quite small, but essential 
>> in the computation. Matlab is quite explicit here:
>>
>> >> cos(x0+i*h)
>> ans =
>>    0.7071 - 0.0000i
>>
>>
>>> I probably not understand your problem ...
>>>
>>> Antoine
>>> Le 12/09/2019 à 10:26, Stéphane Mottelet a écrit :
>>>> Hello all,
>>>>
>>>> The subject has been already discussed a lot but I would like it to 
>>>> be discussed again because I now have a real rationale to promote a 
>>>> change in the way complex numbers with small imaginary part are 
>>>> displayed.
>>>>
>>>> I don't know if some of you were aware of the clever technique of 
>>>> complex-step derivative approximation, but until yesterday I was 
>>>> not (see e.g. 
>>>> https://antispam.utc.fr/proxy/2/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/antispam.utc.fr/proxy/2/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/mdolab.engin.umich.edu/sites/default/files/Martins2003CSD.pdf). 
>>>> Roughly speaking, using the extension of a real function x->f(x) to 
>>>> the complex plane allows to compute an approximation of the 
>>>> derivative f'(x0) at a real x0 without using a substraction, like 
>>>> in the central difference formula (f(x0+h)-f(x0-h))/2/h which is 
>>>> subject to substractive cancelation when h is small. In Scilab most 
>>>> operators and elementary functions are already complex-aware so 
>>>> this is easy to illustrate the technique. For example let us 
>>>> approximate the derivative of x->cos(x) at x=%pi/4, first with the 
>>>> central difference formula, then with the complex step technique:
>>>>
>>>> --> format("e",24)
>>>>
>>>> --> h=%eps/128, x0=%pi/4
>>>>  h  =
>>>>
>>>>    1.73472347597680709D-18
>>>>
>>>>  x0  =
>>>>
>>>>    7.85398163397448279D-01
>>>>
>>>>
>>>> --> (cos(x0+h)-cos(x0-h))/2/h
>>>>  ans  =
>>>>
>>>>    0.00000000000000000D+00
>>>>
>>>>
>>>> --> imag(cos(x0+%i*h))/h
>>>>  ans  =
>>>>
>>>>   -7.07106781186547462D-01
>>>>
>>>>
>>>> --> -sin(x0)
>>>>  ans  =
>>>>
>>>>   -7.07106781186547462D-01
>>>>
>>>> You can see the pathological approximation with central difference 
>>>> formula and the perfect (up to relative machine precision) 
>>>> approximation of complex-step formula.
>>>>
>>>> However, the following is a pity:
>>>>
>>>>
>>>> --> cos(x0+%i*h)
>>>>  ans  =
>>>>
>>>>    7.07106781186547573D-01
>>>>
>>>> We cannot see the imaginary part although seeing the latter is 
>>>> fundamental in the complex-step technique. We have to force the 
>>>> display like this, and frankly I don't like having to do that with 
>>>> my students:
>>>>
>>>> --> imag(cos(x0+%i*h))
>>>>  ans  =
>>>>
>>>>   -1.22663473334669916D-18
>>>>
>>>> I hope that you will find that this example is a good rationale to 
>>>> change the default display of Scilab. To feed the discussion, here 
>>>> is how Matlab displays things, without having to change the default 
>>>> settings:
>>>>
>>>>
>>>> >> h=eps/128, x0=pi/4
>>>> h =
>>>>    1.7347e-18
>>>> x0 =
>>>>     0.7854
>>>>
>>>> >> (cos(x0+h)-cos(x0-h))/2/h
>>>> ans =
>>>>      0
>>>>
>>>> >> cos(x0+i*h)
>>>> ans =
>>>>    0.7071 - 0.0000i
>>>>
>>>> >> imag(cos(x0+i*h))/h
>>>> ans =
>>>>    -0.7071
>>>>
>>>> >> -sin(x0)
>>>> ans =
>>>>    -0.7071
>>>>
>>>>
>>>
>>> _______________________________________________
>>> users mailing list
>>> users at lists.scilab.org
>>> https://antispam.utc.fr/proxy/2/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/antispam.utc.fr/proxy/2/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users 
>>>
>>
-- 
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet




More information about the users mailing list