[100] 209 complex, FastRead-Tutorial by www.msharpmath.com
[100] 209 revised on 2012.10.28 / cemmathThe Simple is the Best
complex
In the following, z = x + y! where x is the real part and y is the imaginary part.
//------
// Basic usage
//------
1i, 1j, 1! // sqrt(-1+0!), 'i' or 'j' works only after digits 0-9
complex(x,y) // x+y!
complex.polar(r,t) // x+y!, x = r cos(t), y = r sin(t)
(r+t!) .cyl // x+y!, x = r cos(t), y = r sin(t)
In the above, a postfix operator ! is of higher priority than basic operations (+-*/ ).
#> 1i ; // 'i' after digits
ans = 0 + 1!
#> 1j ; // 'j' after digits
ans = 0 + 1!
#> 1! ; // postfix '!' works both after digits and variables.
ans = 0 + 1!
#> ( 1 + pi / 2! ) .cyl ; // ! in denomenator, negative y direction
ans = 6.12323e-017 - 1!
#> ( 1 + pi! / 2 ) .cyl ; // ! in numerator, positive y direction
ans = 6.12323e-017 + 1!
#> ( 1 + pi! / 2 ) .cyl .trun10 ; // for truncation, see below
ans = 0 + 1!
//------
// Elements (read and write)
//------
z .x // x, real part
z .y // y, imaginary part
z .r, z .abs // |z| = sqrt(x*x+y*y)
z .t, z .arg // phase part in radian, tan^-1(y/x)
z .deg // phase part in degree
Each element can be modified individually. Modifying 'x' and 'y' is as follows.
#> z = 3+4i ;
z = 3 + 4!
#> z.x = 21 ; z; // x is modified, y unchanged
ans = 21
z = 21 + 4!
#> z.y = 39 ; z; // y is modified, x unchanged
ans = 39
z = 21 + 39!
Modifying 'r', 't' and 'deg' is as follows.
#> z = (5+1!).cyl ;; // x = 5 cos(1), y = 5 sin(1)
#> z; |z|; z.r; z.t; z.deg;
z = 2.70151 + 4.20735!
ans = 5
ans = 5
ans = 1
ans = 57.29578
// r is modified, t unchanged
#> w = z;; w.r += 1;; w; w.r; w.t;
w = 3.24181 + 5.04883!
ans = 6
ans = 1
// t is modified, r unchanged
#> w = z;; w.t += 0.2;; w; w.r; w.t;
w = 1.81179 + 4.6602!
ans = 5
ans = 1.2
// deg is modified, r unchanged
#> w = z;; w.deg += 20;; w; w.r; w.deg;
w = 1.09959 + 4.87759!
ans = 5
ans = 77.29578
The difference between |z| and z.r lies in read/write, i.e.,
|z| is read-only
z.r is good for both read and write
//------
// Unary operations for 'complex'
//------
|z| //sqrt(x*x+y*y)
!z // true if |z| == 0, false if |z| != 0
z! // (x+y!)! = -y + x!
~z // a conjugate, z = x-y!
//------
// Binary operations for 'complex'
//------
z is 'complex'
s is 'complex' or 'double' // i.e. scalar
The smallest argument is used for multi-valued operations, if any.
z + s, s + z // binary operation with either 'double' or 'complex'
z - s, s - z
z * s, s * z
z / s, s / z
z \ s, s \ z
z ^ s, s ^ z // complex power can be multi-valued.
z == s, s == z
z != s, s != z
z += s // z = z + s
z -= s // z = z - s
z *= s // z = z * s
z /= s // z = z / s
z ^= s // z = z ^ s
//------
// Member functions for 'complex'
//------
z .conj // a conjugate, z = x-y! (the same as ~z)
z .unit // a normalized complex number, z / |z|
z .polar/cyl // from the rectangular to polar coordinate
z .trun(eps=1.e-30) // truncate if |x|<eps, |y|<eps
z .trun1 // truncate with eps=10^-1,
z .trun2 // truncate with eps=10^-2,
...
z .trun16 // truncate with eps=10^-16
//------
// Mapping
//------
Mapping of complex functions is possible by
plot .x[n=31,g=1](xa,xb) .y[n=31,g=1] (ya(x),yb(x))
( <opt>, complex function )
%> mapping
#> plot.x[41](-pi,pi).y[21](0,pi/2) ( sin(x+y!) );
%> Riemann surface, w = z^(1/3)
#> plot .r[21](0,1).t[61](0,6*pi)
( < w=(r^(1/3)+t!/3).cyl >, w.y ).cyl;
%> mapping of a polar region
#> zo = 0.2+0.3!;
#> r = (0.1,1).span(11);;
#> t = (0,2*pi).span(41);;
#> plot[r][t] ( (r+t!).cyl );
#> plot[r][t] ( < z=(r+t!).cyl >, (z-zo) / (~zo*z-1) );
//------
// complex array
//------
An array of complex numbers is defined according to the following syntax
#> complex Z[7], W[21];
where Z and W are the names of arrays. The dimension of an array can be a variable of type 'double'.
#> n=10; complex Z[n];
n = 10
#> t = pi/5; for.i(0,4) Z[i] = (1+i*t!).cyl;;
t = 0.62831853
#> Z;
Z = complex [10]
[0] = 1 + 0!
[1] = 0.809017 + 0.587785!
[2] = 0.309017 + 0.951057!
[3] = -0.309017 + 0.951057!
[4] = -0.809017 + 0.587785!
[5] = 0 + 0!
[6] = 0 + 0!
[7] = 0 + 0!
[8] = 0 + 0!
[9] = 0 + 0!
//------
// Output format
//------
The screen output can be controlled by
complex.format(string); // default is null string ""
#> complex.format(" %15.4g real, imag %-15.4g"); i = 1!;
i = 0 real, imag 1
#> complex.format(""); i = 1!;
i = 0 + 1!
//------
// end of file
//------
1