[100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

[100] 009 revised on 2012.10.30 cemmath The Simple is the Best

Chapter 9 Scalars

9-1 Class ‘double’ // Physical constants // Summary of Unit constants in Cemmath 9-2 Class ‘complex’ 9-3 Complex Functions

In this chapter, scalars are interpreted to be data types of ‘double’ and ‘complex’. As a matter of fact, all the real numbers are stored as ‘double’ in Cemmath.

Section 9-1 Class ‘double’

The data type ‘double’ is an intrinsic method of storing real numbers in Cemmath. In this section, we introduce class functions and member functions, and discuss them for the case of ‘double’.

■ Definitions. The key word specifying a certain data type such as ‘double’ is called the class. A class creates an object the type of which is inherited from the class. In some senses, the class in Cemmath is similar to the class in C++. The most notable distinction between the two is that all the classes defined in Cemmath are of built-in nature in contrast to the class in C++. Nevertheless, due to similar structures, we will stick to use the term class from now on, since Cemmath strives to take advantage of OOP (object-oriented- programing) technique.

A function following a class keyword is defined to be a class function. A syntax of the class function is

1 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

class . function

For example, the class ‘double’ has a single class-function ‘format’

double.format(string) // set the format for screen output where ‘string’ is an argument of the class function ‘format’. From the standpoint of memory usage, the class function can be considered to handle static members, since the action on the class function applies to all the objects.

Meanwhile, a class can create its objects, for example, by

double a,b,c; where a,b,c are double objects. Then, member function is defined to be a function working for each object. The syntax of member function is

object . function

In principle, both the class function and the member function cannot be commuted. This means that a class function cannot be called by an object and vice versa. The interesting features of class functions and member functions will be gradually addressed in the following.

■ Class Functions. For the class ‘double’, a single class function is defined

double.format(string) // set the format for screen output

By typing a command

%> default format (%15.8g) #> sqrt(2);

We can see the output on the screen as ans = 1.4142136

2 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

This default output format, "%15.8g", can be changed, for example, by

%> changing format #> double.format("%25.8e"); sqrt(2); #> double.format("<< %-15.8g >>"); sqrt(2); #> double.format("%15.8g"); sqrt(2);

Then, newly displayed results are ans = 1.41421356e+000 ans = << 1.4142136 >> ans = 1.4142136

■ Pre-defined constants. A few variables are declared as pre-defined constants. Listed are pi = 3.14159... _e = 2.71828 ... _gamma = 0.5787215 ... // Euler-Mascheroni constant _eps = 2.22e-16 // machine epsilon for double precision inf = 1.e60 // a large number in Cemmath NaN = 1.#IND // not a number

■ Member Functions. A few member functions

x.ratio(denom=10000) rational approximation of a real number (denom up to 10000) x.sqrt sqrt(x) x.dbrt x^(1/3) x.round the closet integer x.decimal deviation from the closest integer x.divisor integer divisors of an integer x.isprime return 1 if prime number, otherwise return 0 x.factor return a matrix with prime numbers factoring a number x.pm , i.e. return (1) if even, and return (-1) if odd x.fact factorial of x, x(x-1)(x-2)… x.dfact double factorial of x, x(x-2)(x-3)… x.trun(eps=1.e-30) // truncate if |x|

3 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

x.trun1,trun2,...,trun16 // truncate with are implemented for ‘double’. It should be noted that will be used to declare a complex number , i.e.

Let us approximate by a rational number. This can be attempted by a member function ‘ratio’

%> rational approximation #> pi.ratio(100); #> pi.ratio; ans = (22/7) - 0.00126449 // up to denom <= 100 ans = (355/113) - 2.66764e-007 // up to denom <= 10000 where the integer denominators are limited by the argument of ‘ratio’. ◀

Deviation from the closest integer can be of interest. In this regard, member functions ‘round’ and ‘decimal’ determine the closest integer and the deviation

%> deviation from integer #> sqrt(3).round; #> sqrt(3).decimal ; #> sqrt(3).round + sqrt(3).decimal; ans = 2 ans = -0.26794919 ans = 1.7320508

Note that a relation ‘ x = x.round + x.decimal ’ holds always. ◀

Try to find divisors of a number 360. This can be easily done by

%> divisors #> 360.divisor; ans = [ 1 2 3 4 5

4 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

6 8 9 10 12 15 18 20 24 30 36 40 45 60 72 90 120 180 360 ] which lists a total of 24 divisors. ◀

Whether an integer is a prime number can be examined by ‘.isprime’ and prime numbers factoring an integer can be found by ‘.factor’

%> prime number #> 37.isprime; #> 36.factor; ans = 1 ans = [ 2 2 3 3 ]

Truncation can be imposed by a member function ‘trun’, ‘trun1’, ‘trun2’, ... , ‘trun16’. An integer from 1 to 16 after ‘trun’ means that trunx = trun(1.e-x). For example,

%> truncation #> a = 2.e-4; #> a.trun3; // zero if |a| < 10^-3 #> a.turn16; // zero if |a| < 10^-16 ans = 0.0002 ans = 0 ans = 0.0002

■ Unary Operations. By denoting ‘x’ to be a ‘double’ number, unary operations for ‘double’ numbers are

|x| // absolute -x // unary minus x! // complex number, !x // 1 if x == 0, otherwise 0 !!x // 1 if x != 0, otherwise 0

■ Binary Operations. Basic operations between ‘double’ numbers are

5 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

a + b a .+ b // addition a - b a .- b // subtraction a * b a .* b // multiplication a / b a ./ b // right division a \ b a . \ b // left division a % b a .% b // integer remainder a ^ b a .^ b // power

In the above, operator ‘\’ is called the left-division, whereas operator ‘/’ is called the right-division. The meanings of operations are straightforward, and thus detailed discussion is omitted. One thing worthy of note is the use of dot ‘.’ in front of mathematical operators. It designates that the operation of interest is performed on element-by-element basis. Although there is no difference for the case of ‘double’, the use of dot ‘.’ in front of mathematical operators will be useful for matrix data.

For two integers and, a quotient and a remainder are defined

An example is that . For user’s convenience, a syntax is innovated

(q,r) = a /% b ; // a = bq + r where ‘/%’ is introduced as a compound operator in Cemmath. This can be confirmed by

%> compound operator #> (q1,r1) = 13 /% 5 ; // equivalent to 13 = 5 q1 + r1 q1 = 2.0000000 r1 = 3.0000000

■ Relative difference. In Cemmath, all numbers are stored as double- precision accuracy (about 16 digits are effective). It may happen that two large numbers are identical except the last digit, but their difference is still very large. For example,

a = 123456789 x 10^50; b = 123456788 x 10^50;

6 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

gives a-b= 10^50. In order to properly handle ‘effective digits’ difference between two numbers, a relative difference and its operator is defined as x .-. y // |x-y| / ( 1 + |x|+|y|)

In the followins, a few examples are listed.

#> x = 1; y = 2; x - y; x.-.y; |x-y|/(1+|x|+|y|); x = 1 y = 2 ans = -1 // x - y ans = 0.25 // x .-. y ans = 0.25 // |x-y|/(1+|x|+|y|)

// x, y are small #> x = 1.23456789e-7; y = 1.2e-7; x - y; x .-. y; x = 1.2345679e-007 y = 1.2e-007 ans = 3.456789e-009 // x - y ans = 3.4567882e-009 // x .-. y

// 7 digits identical #> x = 1.23456789e+1; y = 1.23456709e+1; x - y; x .-. y; x = 12.345679 y = 12.345671 ans = 8e-006 // x - y ans = 3.1138886e-007 // x .-. y

// 7 digits identical #> x = 1.23456789e+7; y = 1.23456709e+7; x - y; x .-. y; x = 12345679 y = 12345671 ans = 8 // x - y ans = 3.2400009e-007 // x .-. y

■ Approximate equality. Any real number x with a limited number of digits cannot be equal to 'pi'. In this regard, an approximate equal operator is introduced to be

7 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

x ~= y // x .-. y < eps, i.e. true if |x-y| / ( 1 + |x|+|y|) < eps where eps = 1.e-6 is by default specified. Then, the above operation states that two real numbers are approximately equal if they are equal up to the 6 digits, or if the difference is smaller than eps=1.e-6.

This 'eps' criterion is set by default to be eps=1.e-6, but it can be changed at any time by

#> ~=(1.e-3) ; // newly-specified eps for ~=

A few examples with eps=1.e-6 are

// difference is too small #> x = 1.23456789e-7; y = 1.2e-7; x .-. y; x ~= y; x = 1.2345679e-007 y = 1.2e-007 ans = 3.4567882e-009 // x .-. y ans = 1 // x ~= y

#> x = 1.23456789e+1; y = 1.23456709e+1; x .-. y; x ~= y; x = 12.345679 y = 12.345671 ans = 3.1138886e-007 // x .-. y ans = 1 // x ~= y

// though x-y = 8 #> x = 1.23456789e+7; y = 1.23456709e+7; x .-. y; x ~= y; x = 12345679 y = 12345671 ans = 3.2400009e-007 // x .-. y ans = 1 // x ~= y

// absolutely false ! #> x = 10; y = 20; x .-. y; x ~= y; x = 10 y = 20 ans = 0.32258065 // x .-. y ans = 0 // x ~= y

8 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

■ Number system. Together with the decimal system, other number systems with base can be handled by x .. p // double x in decimal, p is a base, returns 'string' s .. p // string s with base p to decimal system, returns 'double' s..p..q // write inside (base p), read outside (base q)

For practical reason, the base in the number system is bounded in an interval of . An example is

#>for(n=1; n<=20; n++) 2623..n; // base n is from 2 to 16 only 101000111111 [2] 101000111111 [2] 10121011 [3] 220333 [4] 40443 [5] 20051 [6] 10435 [7] 5077 [8] 3534 [9] 2623 [10] 1A75 [11] 1627 [12] 126A [13] D55 [14] B9D [15] A3F [16] A3F [16] A3F [16] A3F [16] A3F [16]

A few more examples are as follows.

#> 75 .. 2 ; // into the binary system 100 1011 [2] #> "1001011" ..2 ; // into the decimal system ans = 75 #> "1001011" ..2 + 1 ; // mathematical operation

9 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

ans = 76 #> "1001011" ..2 + "1001011"..2 ; // mathematical operation ans = 150 #> "1001011" ..2 * "1001011"..2 ; 75*75; // mathematical operation ans = 5625 ans = 5625

#> "A3F"..16 ; // from 16 to 10, hexa to deci ans = 2623

#> 2623 .. 16 ; // from 10 to 16, deci to hexa A3F [16]

#>"3F.4CE"..16 ; ans = 63.30029296875

#> 63.30029296875 ..16 ; 3F.4CE [16]

#> 63.30029296875 ..2 ; 11 1111.0100 1100 111 [2]

#>("3f.4ce" ..16) ..2 ; // write inside, read outside 11 1111.0100 1100 111 [2]

//------// Physical constants //------// Each constant starts with a single '_' (underline) //------_alpha = 0.0072973525376 // fine-structure const _c0 = 2.99792458 x 10^8 // speed of light [m/s] _eC = 1.602176487 x 10^-19 // elementary charge [C] _eV = 1.7827 x 10^-36 // electron volt [kg] _e0 = 8.854187817 x 10^-12 // electric const [F/m] _F = 96485.33977318 // Faraday const [C/mol] _g = 9.80665 // earth's gravity [m/s^2] _gc = 32.174 // earth's gravity in British unit [ft/s^2] _G = 6.673 x 10^-11 // gravitational const [m^3/(kg.s^2)] _G0 = 7.7480917004 x 10^-5 // conductance quantum [ohm^-1] _h = 6.62606896 x 10^-34 // Planck's const [J.s]

10 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

_k = 1.3806504 x 10^-23 // Boltzmann const [J/K] _ke = 8.987551787368176 x 10^9 // Coulomb const [N.m^2/C^2] _muB = 9.27400915 // Bohr magneton [J/T], T=tesla _mu0 = 1.2566370614 x 10^-6 // magnetic const [N/A^2] _m_e = 9.10938215 x 10^-31 // electron mass [kg] _m_m = 1.8835313 x 10^-28 // muon mass [kg] _m_n = 1.674927211 x 10^-27 // neutron mass [kg] _m_p = 1.672621637 x 10^-27 // proton mass [kg] _m_t = 3.16777 x 10^-27 // tau mass [kg] _m_u = 1.660538782 x 10^-27 // atomic mass [kg] _NA = 6.02214179 x 10^23 // Avogadro's const [mol^-1] _phi0 = 2.067833667 x 10^-15 // magnetic flux quantum [Wb], _R = 8.31447247122 // molar gass const [J/(mol.K)] _Rinf = 1.0973731568527 x 10^7 // Rydberg const [m^-1] _sigma = 5.6704 x 10^-8 // Stefan-Boltzmann[W/(m^2.K^4)] _Z0 = 376.73031346177 // Impedance of vacuum [ohm]

#> _F - _NA*_eC ; // relation check, _F =_NA*_eC ans = -9.167706593871117e-010

//------// Summary of Unit constants in Cemmath //------Unit constants are expressed in reference units,

[kg] [s] [m] [m^2] [m^3] [m/s] [m/s^2] [K] [kN] [kJ] [kW] [kPa] [kN.m] [kg/m^3]

The followings are only for named units. Other derived units are calculated by using unit constants. For example,

#> 5 * _oz/_in^3 ; // ounce-mass per cubic inch ans = 8649.9452 results in a conversion into [kg/m^3]. //------_A = 10^-10 [m] // Angstrom _acre = 4046.8564224 [m^2] // acre, or _ac _atm = 101.32501 [kPa] // atmosphere _atmtec = 98.0665 [kPa] // atmosphere technical, kgf/cm^2 _au = 1.496 x 10^11 [m] // astronomical unit

11 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

_bar = 100 [kPa] // bar _bbl = 0.159 [m^3] // barrel (petroleum), or_barrel _bhp = 9.810657 [kW] // boiler horse power _BTU = 1.0550559 [kJ] // British Thermal Unit _cal = 0.0041868 [kJ] // calorie _Cal = 4.1868 [kJ] // kilo calorie _cc = 10^6 [m^3] // cubic centimeter, _mL _cm = 0.01 [m] // centimeter _cm2 = 10^-4 [m^2] // square centimeter _cm3 = 10^-6 [m^3] // milliliter or cubic centimeter _cmAq = 0.0980638 [kPa] // cm of water _cmHg = 1.3332237 [kPa] // cm of mercury _cup = 236.588 x 10^-6 [m^3] // cup _Cup = 284.130625 x 10^-6 [m^3] // cup(UK) _c0 = 2.9979246 x 10^8 [m/s] // speed of light _day = 86400 [s] // day _dL = 0.0001 [m^3] // deci-liter _dyne = 10^-8 [kN] // dyne, or _dyn _erg = 10^-10 [kJ] // erg _floz = 29.5735 x 10^-6 [m^3] // fluid oz _Floz = 28.4130625 x 10^-6 [m^3] // ounce(UK) _ft = 0.3048 [m] // feet _ft2 = 0.09290304 [m^2] // square feet _ft3 = 0.028316846592 [m^3] // cubic feet _gal = 0.00378541 [m^3] // gallon _Gal = 0.00454609 [m^3] // gallon (UK) _gill = 118.2941 x 10^-6 [m^3] // gill _Gill = 142.0653125 x 10^-6 [m^3] // gill(UK) _GJ = 10^6 [kJ] // giga Joule _GN = 10^6 [kN] // giga newton _GPa = 10^6 [kPa] // giga Pascal _gr = 6.479891 x 10^5 [kg] // grain _gram = 0.001 [kg] // gram _GW = 10^6 [kW] // giga Watt _gyr = 31556952 [s] // gregorian year _ha = 10000 [m^2] // hectare _hp = 0.74569987 [kW] // horse power (internat'l) _hPa = 0.1 [kPa] // hecto Pascal _hph = 2684.5195 [kJ] // horsepower-hour, _hp*_hr _hpm = 0.73549875 [kW] // horse power (metric) _hr = 3600 [s] // hour, or _hour

12 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

_in = 0.0254 [m] // inch _in2 = 6.4516 x 10^-4 [m^2] // square inch _in3 = 16.387064 x 10^-6 [m^3] // cubic inch _inAq = 0.249089 [kPa] // inch of water _inHg = 3.386389 [kPa] // inch of mercury _J = 0.001 [kJ] // Joule _jyr = 31557600 [s] // julian year _kcal = 4.1868 [kJ] // kilo calorie _kg = 1 [kg] // kilogram _kgf = 0.00980665 [kN] // kilogram-force _kip = 4.4482216 [kN] // 1000 * _lbf _kJ = 1 [kJ] // kilo Joule _km = 1000 [m] // kilometer _km2 = 10^6 [m^2] // square kilometer _km3 = 10^9 [m^3] // cubic kilometer _kmph = 0.27777778 [m/s] // kilometer per hour, or _kmphr _kN = 1 [kN] // kilo newton _knot = 0.514444 [m/s] // knot, or_kn _kPa = 1 [kPa] // kilo Pascal _ksi = 6894.7573 [kPa] // 1000 * _psi _ksf = 47.880279 [kPa] // 1000 * _psf _kt = 0.0002052 [kg] // carat, or _carat _kW = 1 [kW] // kilo Watt _kWh = 3600 [kJ] // _kW*_hr _L = 0.001 [m^3] // liter _lb = 0.45359237 [kg] // pound-mass, or _lbm _lbf = 0.0044482216 [kN] // pound-force _ly = 9.4607 x 10^15 [m] // light year _lyr = 31622400 [s] // leap year _m = 1 [m] // meter _m2 = 1 [m^2] // square meter _m3 = 1 [m^3] // cubic meter _mach = 320 [m/s] // mach, typical value 320 m/s _mb = 0.1 [kPa] // milli bar, or _mbar _mg = 10^-6 [kg] // milligram _mi = 1609.344 [m] // mile _mi2 = 2589988.110336 [m^2] // square mile _mi3 = 4.16818182544058 x 10^9 [m^3] // cubic mile _min = 60 [s] // minute _mJ = 10^-6 [kJ] // milli Joule _MJ = 1000 [kJ] // mega Joule

13 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

_mL = 10^-6 [m^3] // milli-liter, _cc = _cm^3 _mm = 0.001 [m] // millimeter _mm2 = 10^-6 [m^2] // square millimeter _mm3 = 10^-9 [m^3] // cubic millimeter _mmAq = 0.00980638 [kPa] // mm of water _mmHg = 0.13332237 [kPa] // mm of mercury, _torr _MN = 1000 [kN] // mega newton _MPa = 1000 [kPa] // mega Pascal _mph = 0.44704 [m/s] // mile per hour, _mi/_hr _ms = 0.001 [s] // milli second _mum = 10^-6 [m] // micrometer, micron _mus = 10^-6 [s] // micro second _MW = 1000 [kW] // mega Watt _mW = 10^-6 [kW] // milli Watt _N = 0.001 [kN] // newton _nm = 10^-9 [m] // nano meter _nmi = 1852 [m] // nautical mile _ns = 10^-9 [s] // nano second _oz = 0.0283495 [kg] // ounce-mass, or _ozm _ozf = 0.000278014 [kN] // ounce-force _Pa = 0.001 [kPa] // Pascal _pc = 3.0857 x 10^16 [m] // parsec _pdl = 0.000138255 [kN] // poundal _psi = 6.8947573 [kPa] // _lbf/_in^2 _psf = 0.047880279 [kPa] // _lbf/_ft^2 _pt = 473.1765 x 10^-6 [m^3] // pint _Pt = 568.26125 x 10^-6 [m^3] // pint(UK) //_Pint _py = 3.30579 [m^2] // pyung _qt = 946.35296 x 10^-6 [m^3] // quart _Qt = 1136.5225 x 10^-6 [m^3] // quart(UK) //_Quart _s = 1 [s] // second _slug = 14.5939 [kg] // slug _stone = 6.3502932 [kg] // stone _tbsp = 14.78676 x 10^-6 [m^3] // tablespoon _Tbsp = 14.20653125 x 10^-6 [m^3] // tablespoon(UK) _therm = 105480.4 [kJ] // (US) _Therm = 105505.59 [kJ] // (Europe) _ton = 1000 [kg] // ton _Ton = 907.1847 [kg] // English ton _tonf = 9.80665 [kN] // ton-force _Tonf = 8.8964432 [kN] // English ton-force

14 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

_torr = 0.13332237 [kPa] // torr, mmHg _tsp = 4.92892 x 10^-6 [m^3] // teaspoon _Tsp = 3.5516328125 x 10^-6 [m^3] // teaspoon(UK) _W = 0.001 [kW] // Watt _week = 604800 [s] // week _Wh = 3.6 [kJ] // _W*_hr _yd = 0.9144 [m] // yard _yd2 = 0.83612736 [m^2] // square yard _yd3 = 0.764554857984 [m^3] // cubic yard _yr = 31536000 [s] // non-leap year

Section 9-2 Class ‘complex’

Complex numbers are handled by class ‘complex’ in Cemmath. Because complex numbers have their own data type, conversion from ‘complex’ to ‘double’ is not allowed due to loss of imaginary part.

■ Imaginary Number An imaginary number can be declared by

1i, 1j, 1! // where character 'i' or 'j' can be attached only to digits 0-9. An exclamation mark '!' can also be attached to a variable. For example,

x + y! // x + y ( r + t! ).cyl // x + y, x = r cos(t), y = r sin(t)

■ Class Functions. The class functions for ‘complex’ are listed below.

complex.format(string) // set the format for screen output complex.polar(r,t) // x + y, x = r cos(t), y = r sin(t)

As a beginning step, the imaginary number is created by

#> i = 1! ; #> i = complex.polar(1,pi/2); #> i = (1+pi!/2).cyl;

15 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

where the results are i = 0 + 1! i = 6.12323e-017 + 1! i = 6.12323e-017 + 1!

The first approach is the most simplest among others. Nevertheless,

#> j = sqrt(-1); j = -1.#IND will not work, since ‘sqrt’ returns ‘complex’ only when its argument is ‘complex’. In particular, we can use a simplified syntax

complex() or 0! complex(x) or x+0! which corresponds to .

The default format for ‘complex’ is printing the real and imaginary parts together

complex.format(" %g + %g!");

Users can modify the output format, for example

#> complex.format(" %15.4e real, %15.4e imag ") ; #> i = 1!; changes the output format i = 0.0000e+000 real, 1.0000e+000 imag

■ Elements of ‘complex’. Cemmath provides five elements for ‘complex’

z.x/real // x z.y/imag // y |z|, z.r, z.abs // |z| = sqrt(x*x+y*y)

16 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

z.t, z.arg // arg = atan(y/x) (in radian) z.deg // deg = atan(y/x)*180/pi (in degree) where z is a priori defined complex object.

Suppose that . Then, we can change individually both the real and imaginary parts by

#> z = 3+4!; #> z.x = 21; z; #> z.y = 39; z; which results in z = 3 + 4! ans = 21 z = 21 + 4! ans = 39 z = 21 + 39!

The magnitude and argument of a complex number are defined as

In this sense, the magnitude of a complex number can be changed by

#> z = 3+4!; #> z.r = 10; z; z = 3 + 4! ans = 10 z = 6 + 8!

Note that the argument has not been changed. In a similar way, the argument can be changed by

#> z = 3+4!; #> z.t = pi/2; z; z = 3 + 4! ans = 1.5707963

17 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

z = 3.06152e-016 + 5!

It can be noticed that the magnitude is kept the same in this case.

■ Compound Operations for Elements of ‘complex’. Cemmath provides compound operations for the five elements for ‘complex’

z.x += d z.x -= d z.x *= d z.x /= d z.x ^= d z.y += d z.y -= d z.y *= d z.y /= d z.y ^= d z.r += d z.r -= d z.r *= d z.r /= d z.r ^= d z.t += d z.t -= d z.t *= d z.t /= d z.t ^= d z.deg += d z.deg -= d z.deg *= d z.deg /= d z.deg ^= d

Examples are

#> z = (5+1!).cyl ;; |z|; z.r; z.t; z.deg; ans = 5 ans = 5 ans = 1 ans = 57.29578

#> w = z;; w.r += 1;; w.r; w.t; ans = 6 ans = 1

#> w = z;; w.t += 0.2;; w.r; w.t; ans = 5 ans = 1.2

#> w = z;; w.deg += 20;; w.r; w.deg; ans = 5 ans = 77.29578

■ Unary Operations and Member Functions. Four unary operations and a few member functions are defined as listed below

|z| sqrt(x*x+y*y) -z -z = -x -y! ~z ~z = x -y! z! z! = -y + x!

18 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

!z true if |z| == 0, false if |z| != 0

z.conj z.conj = x-y! (the same as ~z) z.unit returns a normalized complex number, z / |z| z.cyl/polar from polar to rectangular coordinates z.trun(eps=1.e-30) // truncate with x and y, if |x|

Example runs are

#> z = 3+4! ; z = 3 + 4! #> |z| ; ans = 5 #> z! ; ans = -4 + 3! #> !z ; ans = 0 #> ~z ; ans = 3 - 4! #> z.conj ; ans = 3 - 4! #> z.unit ; ans = 0.6 + 0.8! #> z ; z = 3 + 4! #> complex(1,pi/6).polar ; ans = 0.866025 + 0.5!

■ Binary Operations. Binary operations between complexs and binary operations between complex and double are

z == w z == d d == z z != w z != d d != z

z + w z + d d + z z - w z - d d - z z * w z * d d * z z / w z / d d / z

z \ w z \ d d \ z

z ^ w z ^ d d ^ z

It is emphasized here that the complex power is actually a multi-valued function, for example

19 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

In this regard, the complex power in Cemmath takes a single representative value, normally evaluated with a minimum argument.

■ Compound Assignments. Compound assignments relevant to ‘complex’ are as follow, where w can be either complex or double.

z += w // z = z + w z -= w // z = z - w z *= w // z = z * w z /= w // z = z / w z ^= w // z = z ^ w

■ Complex array. Complex arrays are defined complex Z[n],W[n], ... ;

%> array of complex #> n = 10; complex Z[n]; n = 10

#> for.k(0,4) Z[k] = k + k*k!; ans = 0 + 0! ans = 1 + 1! ans = 2 + 4! ans = 3 + 9! ans = 4 + 16!

#> Z; Z = complex [10] [0] = 0 + 0! [1] = 1 + 1! [2] = 2 + 4! [3] = 3 + 9! [4] = 4 + 16! [5] = 0 + 0! [6] = 0 + 0!

20 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

[7] = 0 + 0! [8] = 0 + 0! [9] = 0 + 0!

Section 9-3 Complex Functions

■ Mapping . Complex functions are expressed as

where . To understand the mapping from the -plane to the -plane, two constant lines in the the -plane (normally and) are selected, and the corresponding curves in the -plane are examined. For instance, consider a complex function, i.e.

This means that a constant line corresponds to a hyperbola in the -plane, and a constant line corresponds to an ellipse in the -plane.

The syntax for the mapping between complex planes is

plot.x[n=31,g=1](xa,xb).y[n=31,g=1](ya(x),yb(x)) ( <>, complex function )

The above case can be confirmed by

%> mapping of complex function #> plot.x[41](-pi,pi).y[21](0,pi/2) ( x+y! ); #> plot.x[41](-pi,pi).y[21](0,pi/2) ( << z=x+y! >>, sin(z) ); where the result is shown in Figure 1.

21 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

-plane -plane

Figure 1 Plot of complex function (not-scaled)

■ Mapping from a polar domain. Complex functions can be also expressed in terms of the polar coordinates, i.e.

To illustrate this mapping, consider the following rational function

where . Then, we write

%> 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) ); and get the results shown in Figure 2.

22 [100] 009 Chapter 9 Scalars, Tutorial by www.msharpmath.com

-plane -plane

Figure 2 Mapping in the polar coordinate

■ Multi-Valued Complex Function. Complex functions can be multi- valued, and an example is presented with by

#> plot .r[21](0,1).t[61](0,6*pi) ( << w = (r.cbrt + t!/3).cyl >>, w.y ).cyl;

The Riemann surface is shown in Figure 3 from two different points of view.

Figure 3 Multi-valued function , imaginary part

//------// end of file //------

23