string: pass tests
[scilab.git] / scilab / modules / string / tests / nonreg_tests / bug_14099.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2016 - Samuel GOUGEON
4 //
5 //  This file is distributed under the same license as the Scilab package.
6 // =============================================================================
7 // <-- CLI SHELL MODE -->
8 // <-- NO CHECK REF -->
9 // <-- Non-regression test for bug 14099 -->
10 //
11 // <-- Bugzilla URL -->
12 // http://bugzilla.scilab.org/14099
13 //
14 // <-- Short Description -->
15 // string(polynomial) and string(rational) had badly formatted outputs
16 //  and were not vectorized
17 //
18 // * Coefficients equal to +1|-1 were not masked: 1x^n  =>  x^n
19 // * Coefficients with imaginary parts were output as "%i*##": =>"##i"
20 // * Pure imaginary coefficients had parentheses: (%i*3)z^n  => 3iz^n
21 // * format("e") was as well applied to exponents: 3.4D+04x^2.0D+00 => 3.4D+04x^2
22
23 format("v", 10);
24
25 // Single polynomials
26     // Constantes
27 assert_checkequal(string(0*%z), "0");
28 assert_checkequal(string(0*%i*%z), "0");
29 assert_checkequal(string(3+0*%z), "3");
30 assert_checkequal(string(-3+0*%z), "-3");
31
32 assert_checkequal(string(3*%i+0*%z), "3i");
33 assert_checkequal(string(-3*%i+0*%z), "-3i");
34
35 assert_checkequal(string(1-3*%i+0*%z), "1-3i");
36 assert_checkequal(string(-1+3*%i+0*%z), "-1+3i");
37 assert_checkequal(string(-1-3*%i+0*%z), "-1-3i");
38
39 assert_checkequal(string(-3*%i*%z), "-3iz");
40 assert_checkequal(string((1-3*%i)*%z), "(1-3i)z");
41 assert_checkequal(string((-1+3*%i)*%z), "(-1+3i)z");
42 assert_checkequal(string((-1-3*%i)*%z), "(-1-3i)z");
43
44 assert_checkequal(string(-3*%i*%z^13), "-3iz¹³");
45 assert_checkequal(string((1-3*%i)*%z^13), "(1-3i)z¹³");
46 assert_checkequal(string((-1+3*%i)*%z^13), "(-1+3i)z¹³");
47 assert_checkequal(string((-1-3*%i)*%z^13), "(-1-3i)z¹³");
48
49 p = (1-%i) - %i*%z -3*%i*%z^8 + 4*%z^15 - %i*%z^18;
50 assert_checkequal(string(p), "1-i -iz -3iz⁸ +4z¹⁵ -iz¹⁸");
51
52 p = - %i*%z -3*%i*%z^8 + 4*%z^15 - %i*%z^18;
53 assert_checkequal(string(p), "-iz -3iz⁸ +4z¹⁵ -iz¹⁸");
54
55 // Matrix of polynomials
56 ps = "[0*z, 3+0*%i*z; -z, -%i*z;  -z^17, -%i*z^17;  "+..
57      "1-z+5*z^3+41*z^20, (1+(1-%i)*z)^3-1;  "+..
58      "-z+z^3+3*z^4, (%i-2)-3*z-3*%i*z^13+(1+7*%i)*z^20; ]";
59 z = poly(0,"x");
60 p = evstr(ps);
61 refS = ["0"                   "3"
62         "-x"                 "-ix"
63         "-x¹⁷"                 "-ix¹⁷"
64         "1 -x +5x³ +41x²⁰"    "(3-3i)x -6ix² -(2+2i)x³"
65         "-x +x³ +3x⁴"        "-2+i -3x -3ix¹³ +(1+7i)x²⁰"
66         ];
67 assert_checkequal(string(p), refS);
68
69 x = poly(0,"x");
70 p = "[64.692+38.966*x-36.580*x^2+90.044*x^3-93.111*x^4;64.381-74.963*x+52.75*x^2-1.8822*x^3+32.721*x^4]";
71 p = evstr(p);
72 format(6);
73 refS = ["64.69 +38.97x -36.58x² +90.04x³ -93.11x⁴"
74         "64.38 -74.96x +52.75x² -1.882x³ +32.72x⁴"
75        ];
76 assert_checkequal(string(p), refS);
77
78 p = p/3+p*%i/4;
79 format(5);
80 refS = ["21.6+16.2i +(13+9.74i)x -(12.2+9.14i)x² +(30+22.5i)x³ -(31+23.3i)x⁴"
81         "21.5+16.1i -(25+18.7i)x +(17.6+13.2i)x² -(0.63+0.47i)x³ +(10.9+8.18i)x⁴"
82         ];
83 assert_checkequal(string(p), refS);
84
85 // with %nan and %inf
86 p = poly([%nan 2 3 -%nan 8],"x","coeff");
87 p = p+%i*p/2;
88 //""""+string(p)+""""
89 refS = "Nan+Nani +(2+i)x +(3+1.5i)x² -(Nan+Nani)x³ +(8+4i)x⁴";
90 assert_checkequal(string(p), refS);
91
92 // with format("e")
93 format("e",8);
94 ps = "[0*z, 3+0*%i*z; -z, -%i*z;"+..
95      "1+5*z^3-%pi*z^17,  -z^17; "+..
96      "(1+(1-%i)*z)^3-1+%e*%i*z^11, -%i*z^17]";
97 z = poly(0,"x");
98 p = evstr(ps);
99 refS = ["0"                                                                        "3.0D+00"
100         "-1.0D+00x"                                                                "-1.0D+00ix"
101         "1.0D+00 +5.0D+00x³ -3.1D+00x¹⁷"                                           "-1.0D+00x¹⁷"
102         "(3.0D+00-3.0D+00i)x -6.0D+00ix² -(2.0D+00+2.0D+00i)x³ +2.7D+00ix¹¹"       "-1.0D+00ix¹⁷"
103         ];
104 assert_checkequal(string(p), refS);
105
106 // With a name of variable longer than 1 character:
107 x = poly(0,"ABC");
108 p = (2-%i+x).^[2 4 ; 1 3];
109 format("v",10);
110 //""""+string(p)+""""
111 refS = ["3-4i +(4-2i)ABC +ABC²"  "-7-24i +(8-44i)ABC +(18-24i)ABC² +(8-4i)ABC³ +ABC⁴"
112         "2-i +ABC"               "2-11i +(9-12i)ABC +(6-3i)ABC² +ABC³"
113        ];
114 assert_checkequal(string(p), refS);
115