[100] 300 [100] 300 Reference, Tutorial by www.msharpmath.com revised on 2013.01.04 cemmath The Simple is the Best Alphabetic Listing of Cross Reference Tags "A." "W." "p." "x." means means means means a a a a "matrix data" "matrix data", and is writable "poly data" (polynomial) "double data" "z." means a "complex data" "v." means a "vertex data" "cs." means a "csys data" For example, the following syntax matrix A.inv is interpreted as A matrix "A" calls a function "inv" to find an inverse, and returns the result as a "matrix". //---------------------------------------------------------------------To go to the end of file, click end of file Click characters below for a quick move. . . . . A I Q Y . . . . . . . . B J R Z . . . . . . C K S . . . . . . D L T . . . . . . E M U . . . . . . F N V . . . . . . G O W . . . . . . H P X . . . . //---------------------------------------------------------------------Green-colored name-tag with no link are units and constants Blue-colored name-tag has linked contents. is an example. A .det determinant 1 Click it for a quick move. Below [100] 300 Reference, Tutorial by www.msharpmath.com //-------------------------------------------------------------// A //-------------------------------------------------------------_alpha = 0.0072973525376 −10 // fine-structure const x .A 10 x .ac 4046.8564224 𝑥 [m^2] = 43560 𝑥 [ft^2], acre acos (x) inverse cosine function acosd (x) inverse cosine function in degree acosh (x) inverse hyperbolic cosine function acot (x) inverse cotangent function acotd (x) inverse cotangent function in degree acoth (x) inverse hyperbolic cotangent function acsc (x) inverse cosecant function acscd (x) inverse cosecant function in degree acsch (x) inverse hyperbolic cosecant function A .add (i,j,s) 𝑥 [m], angstrom elementary row operation, Ri=Ri+Rj*s A .all 0 if any a_ij is zero, column-first operation A .all1 double-returning, equivalent to A.all.all(1) A .any 1 if any a_ij is not zero A .any1 double-returning, equivalent to A.any.any(1) A .apoly ascending polynomial A .area area of three-sides triangle A .asa/asao triangle solution asec (x) inverse secant function asecd (x) inverse secant function in degree asech (x) inverse hyperbolic secant function asin (x) inverse sine function asind (x) inverse sine function in degree asinh (x) inverse hyperbolic sine function atan (x) inverse tangent function atan2 (y,x) inverse tangent function with two arguments 2 [100] 300 Reference, Tutorial by www.msharpmath.com atand (x) inverse tangent function in degree atand2 (y,x) inverse tangent function in degree atanh (x) inverse hyperbolic tangent function x .atm 101.32501 𝑥 [kPa] = 760 𝑥 [mmHg], atmosphere x .atmtec 98.0665 𝑥 [kPa], atm technical [kgf/cm^2] x .au 1.496 × 1011 𝑥 [m], astronomical unit //-------------------------------------------------------------// B //-------------------------------------------------------------.B (a,b) beta function 𝐵(𝑥, 𝑦) dot-only A .backsub 𝑛 × (𝑛 + 1) matrix, backward substitution x .bar 100 𝑥 [kPa], bar x .bbl 0.159 𝑥 [m^3], barrel (petroleum) // _barrel x .bhp 9.810657 𝑥 [kW], boiler horsepower poly .binom (n) binomial polynomial .bisect (a,b, f(x)) bisection to find a root, dot-only .blkdiag (...) equivalent to .diagcat(...), block diagonal x .BTU 1.0550559 𝑥 [kJ] x .BTUph 0.29307108 × 10−3 𝑥 [kW], BTU/h x .BTUpm 17.584265 × 10−3 𝑥 [kW], BTU/min x .BTUps 1.0550559 𝑥 [kW], BTU/s //-------------------------------------------------------------// C //-------------------------------------------------------------_c0 = 2.99792458 x 10^8 // speed of light [m/s] x .c Celsius to other temperatures, return Kelvin x .c2f 1.8𝑥 + 32 [F], Celsius to Fahrenheit x .c2k 𝑥 + 273.15 [K], Celsius to Kelvin x .c2r 1.8𝑥 + 491.67 [R], Celsius to Rankine 3 [100] 300 Reference, Tutorial by www.msharpmath.com x .Cal 4.1868 𝑥 [kJ], kilo calorie x .cal 0.0041868 𝑥 [kJ], calorie x .calph 1.1163 × 10−6 𝑥 [kW], calorie/h x .calpm 0.06978 × 10−3 𝑥 [kW], calorie/min x .calps 0.0041868 𝑥 [kW], calorie/s x .cc 10−6 𝑥 [m^3], milliliter or cubic centimeter cbrt (x) cubic root, x^(1/3) .ceil (x) integer near ∞ x .ceil integer near ∞ A .chol Choleski decomposition Cint Cint(𝑥) = (1/𝑥) ∫0 [1 − cos(𝑡)] /𝑡 2 𝑑𝑡 A .circle a circle passing three points x .cm 0.01𝑥 [m], centimeter x .cm2 10−4 𝑥 [m^2], square centimeter x .cm3 10−6 𝑥 [m^3], milliliter or cubic centimeter x .cmAq 0.0980638 𝑥 [kPa], cm of water x .cmHg 1.3322368 𝑥 [kPa], cm of mercury x .cmps2 0.01 𝑥 [m/s^2] W .col (j) column vector, W(*,j) A .colabs column absolute A .coldel (j) column delete A .coldet (j) j-th column determinant A .colend A(*,end), A.endcol A .colins (j,B) insert B at j-th column A .colskip (k) if k>0, 1,1+k,1+2k ... else ... n-2k,n-k,n A .colswap (i,j) swap i-th and j-th columns A .colunit A.unit, p .compan companion matrix whose eigenvalues are 𝑥 each column is x^T x = 1 the roots of p(x) A .cond condition number, A.norminf*A.inv.norminf 4 [100] 300 Reference, Tutorial by www.msharpmath.com A .conj complex conjugate, equivalent to A .conjtr conjugate transpose, equivalent to .corrcoef (A,B) correlation coefficient cos (x) cosine function cosd (x) cosine function in degree cosh (x) hyperbolic cosine function cot (x) cotangent function cotd (x) cotangent function in degree coth (x) hyperbolic cotangent function .cross (A,B) cross product csc (x) cosecant function cscd (x) cosecant function in degree csch (x) hyperbolic cosecant function A .cumprod cumulative product A .cumsum A~ = A.cumsum p .cumsum ∑ 𝑝 = 𝑝. ~ ,cumulative sum cumtrapz(x,y) cumulative trapezoidal rule ~A A' x .Cup 284.130625 × 10−6 𝑥 [m^3], cup(UK) x .cup 236.588 × 10−6 𝑥 [m^3], cup(US) v .cyl read in cylindrical coordinate z .cyl from cylindrical to rectangular coordinates //-------------------------------------------------------------// D //-------------------------------------------------------------x .day 86400 𝑥 [s] .decimal (x) = x - .round(x) /x/A/p/ .decimal deviation from the closest integer z .deg deg = atan(y/x) * 180/pi csys.deg angles in the degree system .del (f) (a,b,c) gradient at (a,b,c) for f(x,y,z) dot-only 5 (in degree) [100] 300 Reference, Tutorial by www.msharpmath.com .del* (u,v,w)(a,b,c) divergence dot-only .del^ (u,v,w)(a,b,c) curl dot-only .del2 (f)(a,b,c) Laplacian dot-only .del[] gradient dot-only .del[][].x1.x2 ... .xn (f1,f2,...,fm) (c1,c2,...,cn) dot-only .x1.x2 ... .xn (f)(c1,c2,...,cn) p .denom (nmax=10000) returns an integer denominator up to nmax A .det determinant /x/A/p/ .dfact double factorial of x, x(x-2)(x-3)... .diag (A,k=0) diagonal matrix W .diag (k) diagonal vector .diagcat (A1,A2,...) A1 \_ A2 \_ ..., diagonal concatenation A .diagm (k) .diag(A,k) for vectors, otherwise zero off-diags A .diff A` = A.diff, finite-difference p .diff Δ𝑝 = p.′ = p`, finite difference cs .dir direction cosines of a coordinate system .div (a,b) quotient q .divisor (n) divisors, .divisor(10) = [1,2,5,10] x .dL 0.0001𝑥 [m^3], deciliter .dot (A,B) A ** B A .dpoly descending polynomial x .dyne 10−8 𝑥 [kN], dyne // _dyn in a = bq+r dot product dot-only dot-only //-------------------------------------------------------------// E //-------------------------------------------------------------_e = 2.718281828459045 // exp(1) _eps = 2.22e-16 _e0 = 8.854187817 x 10^-12 // electric const [F/m] _eC = 1.602176487 x 10^-19 // elementary charge [C] _eV = 1.7827 x 10^-36 .E1 (x) exponential integral, expint(x) // machine epsilon in Cemmath 6 // electron volt [kg] [100] 300 .Ei (x) Reference, int_-inf^x Tutorial by www.msharpmath.com exp(t)/t dt .e_k (n) k-th unit vector in an n-dimension dot-only A .eig return eigenvalues A .eiginvpow (shift) eigenvalue by inverse power method A .eigpoly eigenpolynomial A .eigpow (shift) eigenvalue by power method A .eigvec (lam) eigenvecter(s) A .end real part of A(end,end) W .entry (matrix) entry of elements, erf (x) error function erfc (x) complementary error function x .erg W.(matrix) 10−10 𝑥 [kJ], erg p .even 𝑎0 + 𝑎2 𝑥 2 + 𝑎4 𝑥 4 + 𝑎6 𝑥 6 + ⋯ exp (x) exponential function expint (x) exponential integral, expint(x)=.E1(x) expm (A) = I + A + A^2/2! + A^3/3! + .... expm1 (x) exp(x)-1 A .ext (m,n,s) extend A more with m rows, n columns .eye (m,n=m) identity matrix //-------------------------------------------------------------// F //-------------------------------------------------------------_F = 96485.33977318 // Faraday const [C/mol] x .f Fahrenheit to other temperatures, return Kelvin x .f2c (𝑥 − 32)/1.8 [C], Fahrenheit to Celsius x .f2k (𝑥 + 459.67)/1.8 [K] , Fahrenheit to Kelvin x .f2r 𝑥 + 459.67 [R] , Fahrenheit to Rankine .fact (x) factorial, .factor (x) factorization, /x/A/p/ .fact factorial of x, x(x-1)(x-2)... x(x-1)(x-2) .. .factor(10) = [ 2,5 ] 7 [100] 300 Reference, Tutorial by www.msharpmath.com A .false index for zero elements A .find A.true, inbdex for nonzero elements .fix (x) integer x .fix integer near 0 A .fliplr reverse columns, flip left-right A .flipud reverse rows, flip upside-down .floor (x) integer near -∞ x .floor integer near -∞ x .Floz 28.4130625 × 10−6 𝑥 [m^3], ounce(UK) x .floz 29.5735 × 10−6 𝑥 [m^3], ounce(US) near 0 poly .format(string) set the format for screen output A .forsub nx(n+1) matrix, forward substitution x .ft 0.3048𝑥 [m], feet x .ft2 0.09290304𝑥 [m^2], square feet x .ft3 0.028316846592 𝑥 [m^3], cubic feet x .ftlbph 0.3766161 × 10−6 𝑥 [kW], ft.lbf/h x .ftlbpm 0.022596965 × 10−3 𝑥 [kW], ft.lbf/min x .ftlbps 1.3558179 × 10−3 𝑥 [kW], ft.lbf/s x .ftph 0.0000846667 𝑥 [m/s], ft/h x .ftphs 8.4666667 × 10−5 𝑥 [m/s^2] x .ftpm 0.00508 𝑥 [m/s], ft/min x .ftps 0.3048 𝑥 [m/s], ft/s x .ftps2 0.3048 𝑥 [m/s^2] //-------------------------------------------------------------// G //-------------------------------------------------------------_G = 6.673 x 10^-11 // gravitational [m^3/(kg.s^2)] _g = 9.80665 _G0 = 7.7480917004 x 10^-5 // conductance quantum [ohm^-1] _gamma = 0.5772156649015329 // earth's gravity [m/s^2] // Euler-Mascheroni constant 8 [100] 300 _gc Reference, Tutorial by www.msharpmath.com = 32.174 // earth's gravity in British unit [ft/s^2] .G (x) .G(x)=(x-1)!, Gamma function Γ(𝑥), dot-only x .g 0.001𝑥 [kg], gram x .Gal 0.00454609 𝑥 [m^3], gallon(UK) // _Gallon x .gal 0.00378541 𝑥 [m^3], gallon(US) // _gallon A .gausselim Gauss-elimination without pivot A .gaussjord Gauss-Jordan elimination with pivot A .gausspivot Gauss-elimination with pivot .gcd (a,b) greatest common divisor dot-only x .Gill 142.0653125 × 10−6 𝑥 [m^3], gill(UK) x .gill 118.2941 × 10−6 𝑥 [m^3], gill(US) x .GJ 106 𝑥 [kJ], giga Joule x .GN 106 𝑥 [kN], giga newton x .GPa 10−6 𝑥 [kPa], giga Pascal x .gpcm3 1000 𝑥 [kg/m^3], gram per cm^3 x .gpL 𝑥 [kg/m^3], gram per liter x .gr 6.479891 × 10−5 𝑥 [kg], grain x .grpGal 0.014253767 𝑥 [kg/m^3], grain/gal(UK) x .grpgal 0.017118069 𝑥 [kg/m^3], grain/gal(US) x .gry 31556952𝑥 [s] // gregorian year x .GW 106 𝑥 [kW], giga Watt //-------------------------------------------------------------// H //-------------------------------------------------------------_h = 6.62606896 x 10^-34 // Planck's const [J.s] .H (x) Heaviside function 𝐻(𝑥) .H_n (x) Hermite polynomial 𝐻𝑛 (𝑥) dot-only .H1_nu (x) Hankel function, 𝐻𝜈 (𝑥) .H2_nu (x) Hankel function, 𝐻𝜈 (𝑥) 9 dot-only (1) dot-only (2) dot-only [100] 300 poly .H (n) Reference, Tutorial by www.msharpmath.com Hermite polynomial 𝐻𝑛 (𝑥) x .ha 1000𝑥 [m^2], hectare matrix .hank (A,B) Hankel matrix A .hess eigenvalue-preserving Hessenberg matrix A .high (m) reshape in m-rows matrix .hilb (n) Hilbert matrix .horzcat (A1,A2, ...) A_1 | A_2 | ... // horizontal concatenation x .hour 3600𝑥 [s] x .hp 0.74569987 𝑥 [kW], horsepower(international) x .hPa 0.1 𝑥 [kPa], hecto Pascal x .hph 2.6845195 × 103 𝑥 [kJ], // _hp*_hr x .hpm 0.73549875 𝑥 [kW], horsepower(metric) x .hr 3600𝑥 [s] hypot (x1,x2,...) .norm2(x1,x2,...)=sqrt(x1^2+x2^2+...) //-------------------------------------------------------------// I //-------------------------------------------------------------.I (m,n=m) identity matrix 𝐈 = (𝛿𝑖𝑗 ) dot-only .I_nu (x) Bessel functions 𝐼_𝜈(𝑥) dot-only A .i (k) 𝑘 = 𝑖 + 𝑚(𝑗 − 1), i of (i,j) for linear index k A .ij (i,j) 𝑘 = 𝑖 + 𝑚(𝑗 − 1), linear index k for (i,j) W .imag imaginary part x .in 0.0254𝑥 [m], inch x .in2 6.4516 × 10−4 𝑥 [m^2], square inch x .in3 16.387064 × 10−6 𝑥 [m^3], cubic inch x .inAq 0.249089 𝑥 [kPa], inch of water x .inHg 3.386389 𝑥 [kPa], inch of mercury x .inphs 7.0555556 × 10−6 𝑥 [m/s^2] x .inps2 0.0254 𝑥 [m/s^2] A .ins (i,j,B) insert 𝐁 at (i,j) position 10 [100] 300 .integ (a,b, f(x)) .interp (X,F) Reference, Tutorial by www.msharpmath.com indefinite integral, dot-only polynomial for interpolation of F(X) A .inv inverse matrix p .inv 1/𝑎0 + (1/𝑎1 )𝑥 + (1/𝑎2 )𝑥 2 + … + (1/𝑎𝑛 )𝑥 𝑛 p .invtay 𝑎0 + 𝑎1 𝑥 + 2! 𝑎2 𝑥 2 + 3! 𝑎3 𝑥 3 + ⋯ + 𝑛! 𝑎𝑛 𝑥 𝑛 A .ipos (x) return an index, 𝑎𝑘 ≤ 𝑥 ≤ 𝑎𝑘+1 p .iratio shows a rational approximation .isprime (x) return (1) if x is a prime number A .isreal returns 1 if A is real, otherwise return 0 A .issquare returns 1 if A is square //-------------------------------------------------------------// J //-------------------------------------------------------------x .J 0.001 𝑥 [kJ], Joule A .j (k) 𝑘 = 𝑖 + 𝑚(𝑗 − 1), j of (i,j) for linear index k .J_nu (x) Bessel functions 𝐽𝜈 (𝑥) dot-only .j_n (x) spherical Bessel functions matrix.jord (a,b) Jordan matrix A .jord (i) eliminate rows above and below a_ii x .jyr 31557600𝑥 [s] // julian year dot-only //-------------------------------------------------------------// K //-------------------------------------------------------------_k = 1.3806504 x 10^-23 // Boltzmann const [J/K] _ke = 8.987551787368176 x 10^9 // Coulomb const [N.m^2/C^2] .K_nu (x) Bessel functions 𝐾𝜈 (𝑥) dot-only A .k (i,j) 𝑘 = 𝑖 + 𝑚(𝑗 − 1), linear index k for (i,j) x .k Kelvin to other temperatures, return [K] x .k2c 𝑥 − 27.15 [C], Kelvin to Celsius 11 [100] 300 Reference, Tutorial by www.msharpmath.com x .k2f 1.8𝑥 − 459.67 [F], Kelvin to Fahrenheit x .k2r 1.8𝑥 [R], Kelvin to Rankine x .kcal 4.1868 𝑥 [kJ], kilo calorie x .kg 𝑥 [kg], kilogram x .kgf 9.80665 × 10−3 𝑥 [kN], kilogram-force x .kgfpcm2 98.0665 𝑥 [kPa], atm technical x .kgpm3 𝑥 [kg/m^3] , kg per cubic meter x .kip 4.4482216 𝑥 [kN] = 1000x [lbf], kip x .kJ 𝑥 [kJ], kilo Joule x .km 1000𝑥 [m], kilometer x .km2 106 𝑥 [m^2], square kilometer x .km3 109 𝑥 [m^3], cubic kilometer x .kmph 0.2777777778 𝑥 [m/s], km per hour //_km/_hr x .kmphs 0.27777777777 𝑥 [m/s^2] x .kmpm 16.666667 𝑥 [m/s], km per min x .kmps 1000 𝑥 [m/s], km per s x .kN 𝑥 [kN], kilo newton x .kn 0.514444 𝑥 [m/s], knot x .kNm 𝑥 [kN.m], kN.m x .kPa 𝑥 [kPa], kilo Pascal .kron (A,B) A ^^ B x .ksf 47.88027943 𝑥 [kPa], kip/ft^2 x .ksi 6894.7572695 𝑥 [kPa], kip/in^2 x .kt 205.2 × 10−6 𝑥 [kg], carat x .kW 𝑥 [kW], kilo Watt x .kWh 3600 𝑥 [kJ], kiloWatt-hour // _kW*_hr kronecker product //-------------------------------------------------------------// L //-------------------------------------------------------------.L_n (x) Laguerre polynomial 𝐿𝑛 (𝑥) dot-only 12 [100] 300 poly.L (n) Reference, Tutorial by www.msharpmath.com Laguerre polynomial x .L 0.001𝑥 [m^3], liter x .lb 0.45359237 𝑥 [kg], pound-mass x .lbm 0.45359237 𝑥 [kg], pound-mass x .lbf 4.4482216 × 10−3 𝑥 [kN], pound-force x .lbft 1.3558179 × 10−3 𝑥 [kN.m], lbf.ft x .lbin 0.11298482 × 10−3 𝑥 [kN.m], lbf.in x .lbpft3 16.018463 𝑥 [kg/m^3], lbm/ft^3 x .lbpGal 99.776372 𝑥 [kg/m^3], lbm/gal(UK) x .lbpgal 119.82648 𝑥 [kg/m^3], lbm/gal(US) x .lbpin3 27679.905 𝑥 [kg/m^3], lbm/in^3 .lcm (a,b) least common multiplier poly.left (string) set the left delimiter for screen output p .len 𝑛 + 1 of n-th degree polynomial A .len/length max(m,n) lnG (x) ln 𝐺(𝑥), gammaln(x) in matlab log (x) natural log with base _e log1p (x) log(1+x) log2 (x) log with base 2 log10 (x) log with base 10 logm (A) inverse of expm (a,b) .logspan (n,g=1) dot-only 10.^( (log10(a),log10(b)).span(n,g=1) ) A .longer max(m,n) A .lu LU decomposition, (𝐋, 𝐔) = 𝐀. lu x .ly 9.4607 × 1015 𝑥 [m], light year x .lyr 31622400𝑥 [s] // leap year //-------------------------------------------------------------// M //-------------------------------------------------------------_mu0 = 1.2566370614 x 10^-6 // magnetic const [N/A^2] 13 [100] 300 Reference, Tutorial by www.msharpmath.com _muB = 9.27400915 // Bohr magneton [J/T], T=tesla _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] A .m m, number of rows x .m 𝑥 [m], meter x .m2 𝑥 [m^2], square meter x .m3 𝑥 [m^3], cubic meter x .ma 320 𝑥 [m/s], speed of sound (mach) (a,h) .march (n,g=1) [ a, a+h(1),... , a+h(1+g+...+g^(n-2)) ] .max (x1,x2, ... , xn) maximum dot-only A .max maximum elements p .max max{𝑎𝑖 } A .max1 max 𝑎𝑖𝑗 , equivalent to A.max.max(1) A .max1k/maxk linear index k for A.max1 poly .maxcol(n=10) set the maximum column length for output A .maxentry absolute maximum element p .maxentry max{|𝑎𝑖 |} A .maxentryk linear index k for A.maxentry x .mb 0.1 𝑥 [kPa], millibar // _mbar A .mean mean elements p .mean ∑𝑛𝑖=0 𝑎𝑖 /(𝑛 + 1) x .mg 10−6 𝑥 [kg], milligram x .mi 1609.344𝑥 [m] = 1760 𝑥 [yd], mile x .mi2 2589988.110336 𝑥 [m^2], square mile x .mi3 4.16818182544058 × 109 𝑥 [m^3], cubic mile .min (x1,x2, ... , xn) minimum x .min 60𝑥 [s] // minute dot-only 14 [100] 300 Reference, Tutorial by www.msharpmath.com A .min minimum elements p .min min{𝑎𝑖 } A .min1 min 𝑎𝑖𝑗 , equivalent to A.min.min(1) A .min1k/mink linear index k for A.min1 A .minentry absolute minimum element p .minentry min{|𝑎𝑖 |} A .minentryk linear index k for A.minentry A .minor (i,j) minor by eliminating i-th row and j-th column x .mipm 26.8224 𝑥 [m/s], mile per min x .mips 1609.344 𝑥 [m/s], mile per s x .MJ 1000 𝑥 [kJ], mega Joule x .mJ 10−6 𝑥 [kJ], mill Joule x .mL 10−6 𝑥 [m^3], milliliter or cubic centimeter x .mm 0.001𝑥 [m], millimeter x .mm2 10−6 𝑥 [m^2], square millimeter x .mm3 10−9 𝑥 [m^3], cubic millimeter x .mmAq 0.00980638 𝑥 [kPa], mm of water x .mmHg 0.133322368 𝑥 [kPa], mm of mercury x .mmps2 0.001 𝑥 [m/s^2] x .MN 1000 𝑥 [kN], mega newton A .mn m*n, total number of elements .mod (a,b) modulus a-b*floor(a/b) p .monic (𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥2 dot-only + … + 𝑎𝑛−1 𝑥 𝑛−1 ) /𝑎𝑛 + 𝑥 𝑛 x .MPa 1000 𝑥 [kPa], mega Pascal x .mph 0.44704 𝑥 [m/s], mile per hour // _mi/_hr x .mphr 0.0002777777778 𝑥 [m/s], meter per hour x .mphs 0.44704 𝑥 [m/s^2] x .mpm 0.016666667 𝑥 [m/s], meter per minute x .mps 𝑥 [m/s], meter per second x .mps2 𝑥 [m/s^2] , meter per square second x .ms 0.001 𝑥 [s] // milli second 15 [100] 300 A .mul (i, s) Reference, Tutorial by www.msharpmath.com elementary row operation, R_i = R_i * s x .mum 10−6 𝑥 [m], micron x .mus 10−6 𝑥 [s] // micro second x .MW 1000 𝑥 [kW], mega Watt x .mW 10−6 𝑥 [kW], milli Watt //-------------------------------------------------------------// N //-------------------------------------------------------------_NA = 6.02214179 x 10^23 // Avogadro's const [mol^-1] x .N 0.001 𝑥 [kN], newton .n_n (x) spherical Bessel functions, dot only A .n 𝑛, number of columns p .n 𝑛, the hightest order of a polynomial x .Ncm 0.01 × 10−3 𝑥 [kN.m], N.cm .ncr (n,r) combination nCr, nextpow2(x) 2^n >= x p .newton (x) 𝑎0 + 𝑎1 (𝑥 − 𝑥1 ) + 𝑎2 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) + ⋯ x .Nm 0.001 𝑥 [kN.m], N.m x .nm 10−9 𝑥 [m], nanometer x .nmi 1852𝑥 [m], nautical mile n!/ ( (n-r)!r! ) A .norm (p) p-norm (vector or matrix) .norm1 (x1,x2,…) |𝑥1 | + |𝑥2 | + |𝑥3 | + ⋯ .norm2 (x1,x2,…) √𝑥12 + 𝑥22 + 𝑥32 + ⋯ .norm22 (x1,x2,…) 𝑥12 + 𝑥22 + 𝑥32 + ⋯ A .norm1 1-norm A .norm2 Frobenius norm, sqrt(A**A) A .norm22 A.norm22 = A.norm2^2 = A**A A .norminf infinite-norm .npr (n,r) permutation nPr, x .ns 10−9 𝑥 [s] // nano second dot-only n!/ ( (n-r)! ) dot-only 16 [100] 300 Reference, Tutorial by www.msharpmath.com //-------------------------------------------------------------// O //-------------------------------------------------------------p .odd 𝑎1 𝑥 + 𝑎3 𝑥 3 + 𝑎5 𝑥 5 + 𝑎7 𝑥 7 + … .off 'dot-off' default mode, both modes work. dot-only .on 'dot-on' activates dot-only dot-only .ones (m,n=m) all-ones matrix cs.org origin of a coordinate system x .oz 0.0283495 𝑥 [kg], ounce-mass x .ozf 0.278014 × 10−3 𝑥 [kN], ounce-force x .ozft 0.084738667 × 10−3 𝑥 [kN.m], ozf.ft x .ozin 7.06156 × 10−6 𝑥 [kN.m], ozf.in x .ozm 0.0283495 𝑥 [kg], ounce-mass x .ozpGal 6.2360182 𝑥 [kg/m^3], ozm/gal(UK) x .ozpgal 7.4891491 𝑥 [kg/m^3], ozm/gal(US) x .ozpft3 1.00115 𝑥 [kg/m^3], ozm/ft^3 x .ozpin3 1729.989044 𝑥 [kg/m^3], ozm/in^3 //-------------------------------------------------------------// P //-------------------------------------------------------------_phi0 v .P = 2.067833667 x 10^-15, magnetic flux quantum [Wb] coordinate 𝜙 in (𝑅, 𝜙, 𝜃) coordinate .P_n (x) Legendre polynomial poly .P (n) Legendre polynomial dot-only x .Pa 0.001 𝑥 [kPa], Pascal A .pass line, parabola, cubic csys.pass180 pass through -x coordinate, 0 <= t < 2*pi csys.pass0 pass through +x coordinate, -pi < t <= pi x .pc 3.0857 × 1016 𝑥 [m], parsec 17 [100] 300 Reference, Tutorial by www.msharpmath.com A .pcent a_ij/sum(a_ij) * 100 x .pdl 0.138255 × 10−3 𝑥 [kN], poundal x .pdlft 0.042140124 × 10−3 𝑥 [kN.m], pdl.ft x .pdlin 3.511677 × 10−6 𝑥 [kN.m], pdl.in A .plot plot A .plu A = PLU decomposition .pm (n) 1 if n is even, otherwise (-1) x .pm 1 if x is even, otherwise (-1) A .pm (-1)^(i+j) a_ij p .pm 𝑎0 − 𝑎1 𝑥 + 𝑎2 𝑥 2 − 𝑎3 𝑥 3 + ⋯ + (−1)𝑛 𝑎𝑛 𝑥 𝑛 .polyfit (X,F, kth) same as .regress(X,F,kth) pow (x,a) x^a p .pow (k) 𝑎0 + 𝑎1 𝑥 𝑘 + 𝑎2 𝑥 2𝑘 + 𝑎3 𝑥 3𝑘 … + 𝑎𝑛 𝑥 𝑛𝑘 pow2 (x) 2^x pow10 (x) 10^x .primes (a,b) prime numbers between a and b A .prod product of elements p .prod ∏𝑛𝑖=0 𝑎𝑖 A .prod1 equivalent to A.prod.prod(1) x .psf 0.04788027943 𝑥 [kPa], lbf/ft^2 .psi (x) 𝜓(𝑛) = −𝛾 + + + + ⋯ + x .psi 6.8947572695 𝑥 [kPa], lbf/in^2 x .Pt 568.26125 × 10−6 𝑥 [m^3], pint(UK) // _Pint x .pt 473.1765 × 10−6 𝑥 [m^3], pint(US) // _pint A .pushd push-down with top row A .pushl push-left with rightmost column A .pushr push-right with leftmost column A .pushu push-up with bottom row x .py 3.30579 𝑥 [m^2], pyung dot-only dot-only 1 1 1 1 1 2 3 𝑛−1 dot only //-------------------------------------------------------------- 18 [100] 300 // Reference, Tutorial by www.msharpmath.com Q //-------------------------------------------------------------A .qr A = QR decomposition x .Qt 1136.5225 × 10−6 𝑥 [m^3], quart(UK) // _Quart x .qt 946.35296 × 10−6 𝑥 [m^3], quart(US) // _quart p .quad (r,s) divisor 𝑥 2 + 𝑟𝑥 + 𝑠 by Bairstow method //-------------------------------------------------------------// R //-------------------------------------------------------------_R = 8.31447247122 // molar gass const [J/(mol.K)] _Rinf = 1.0973731568527 x 10^7 // Rydberg const [m^-1] x .r Rankine to other temperatures, return [K] x .r2c (𝑥 − 491.67)/1.8 [C], Rankine to Celsius x .r2f 𝑥 − 459.67 [F], Rankine to Fahrenheit x .r2k 𝑥/1.8 [K], Rankine to Kelvin v .R coordinate 𝑅 in (𝑅, 𝜙, 𝜃) coordinate v .r coordinate 𝑟 in (𝑟, 𝜃, 𝑧) coordinate z .r |z| = sqrt( x*x + y*y ) csys.rad angles in the radian system .rand (m,n=m) randon number matrix between 0 and 1 A .rank rank of a matrix A A .ratio (denom=10000) shows a rational approximation p .ratio shows a rational approximation A .ratio1 denomenator of a rational approximation A .read (string) read A from a file W .real real part v .rec read in rectangular coordinate csys.rec rectangular coordiante system .regress (X,F, mftn) matrix for regression dot-only .rem (a,b) modulus a-b*fix(a/b) dot-only 19 [100] 300 A .repmat (m,n=m) Reference, Tutorial by www.msharpmath.com repeat a tile-shaped copying A .resize (m,n) resize a matrix by copying A .rev reverse elements, rotating 180 degree p .rev 𝑎𝑛 + 𝑎𝑛−1 𝑥 + 𝑎𝑛−2 𝑥 2 + 𝑎𝑛−3 𝑥 3 + … + 𝑎0 𝑥 𝑛 poly .right (string) set the right delimiter for screen output .roots ( … ) a polynomial with knwon roots A .rot90 rotate 90 degree (counter-clockwise) A .rotm90 rotate -90 degree A .roots (𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) ⋯ (𝑥 − 𝑥𝑛 ) .round (x) integer near 𝑥 /x/A/p/ .round nearest integer to 𝑥 W .row (i) row vector, A(i,*) A .rowabs row absolute A .rowdel (i) row delete A .rowdet (i) i-th row determinant A .rowend A(end,*), A.endrow A .rowins (i,B) insert B at i-th row, A.insrow A .rowskip (k) if k>0, 1,1+k,1+2k ... else ... m-2k,m-k,m A .rowswap (i,j) elementary row operation, swap rows A .rowunit each row is 𝑥 𝑇 𝑥 = 𝑥12 + 𝑥22 + ⋯ + 𝑥𝑛2 = 1 dot-only //-------------------------------------------------------------// S //-------------------------------------------------------------_sigma = 5.6704 x 10^-8 // Stefan-Boltzmann[W/(m^2.K^4)] x .s 𝑥 [s] // second A .sas/saso triangle solution A .sdev standard deviation sec (x) secant function .secant (a,b, f(x)) secant method for root finding secd (x) secant function in degree 20 dot-only [100] 300 Reference, Tutorial by www.msharpmath.com sech (x) hyperbolic secant function p .shift (c) 𝑎0 + 𝑎1 (𝑥 − 𝑐) + 𝑎2 (𝑥 − 𝑐)2 + … + 𝑎𝑛 (𝑥 − 𝑐)𝑛 A .shorter min(m,n) .sign (x) (1) if x > 0, (-1) if x < 0 and (0) if x = 0 x .sign (1) if x>0, (-1) if x<= and (0) if x=0 sin (x) sine function sinc (x) sin(x)/x sind (x) sine function in degree sinh (x) hyperbolic sine function Sint (x) Sint(𝑥) = (1/𝑥) ∫0 sin(𝑡) /𝑡 𝑑𝑡 A .size tuple, number of rows and columns A .skewl (k) lower-triangular w.r.t the skew-diagonal A .skewu (k) upper-triangular w.r.t the skew-diagonal x .slug 14.5939 𝑥 [kg] = 32.174x [lbm], slug x .slugpft3 515.378711 𝑥 [kg/m^3], slug/ft^3 x .slugpin3 890574.42 𝑥 [kg/m^3], slug/in^3 A .solve nx(n+1) matrix, A.axb, solve Ax = b with (A|b) p .solve solution matrix (a,b) .span (n,g=1) [ x_1 = a, x_2, …, x_n = b ],dx_i=g dx_(i-1) v .sph read in spherical coordinate .spline (X,F) 3rd-order spline matrix dot-only .spline1 (X,F) 1st-order spline matrix dot-only .splinebc1 (X,F) 3rd-order spline matrix dot-only .splinebc2 (X,F) 3rd-order spline matrix dot-only A .spl (B) 𝑥 evaluation for matrix A .spldiff differentiate piecewise polynomials A .splint integrate piecewise polynomials A .splplot plot piecewise polynomials sqrt (x) square root sqrtm (A) from A .sss/ssso triangle solution with three sides XX = A 21 [100] 300 Reference, Tutorial by www.msharpmath.com A .stat basic statistics A .std standard deviation (a,b) .step (h) [ a, a+h, a+2h, ... ] W .sub (r)(c) submatrix, W..(r)(c) A .subdel (r)(c) delete submatrix A .sum sum of elements p .sum ∑𝑛𝑖=0 𝑎𝑖 A .sum1 equivalent to A.sum.sum(1) .swap (#1,#2) swap two arguments A .swap (i,j) elementary row operation, swap rows, A.rowswap p .syndiv (c) p %% c = p.syndiv(c) p .syndiv (q) p %% q , dot-only synthetic division by polynomial q //-------------------------------------------------------------// T //-------------------------------------------------------------v .T coordinate 𝜃 in (𝑅, 𝜙, 𝜃) coordinate v .t coordinate 𝜃 in (𝑟, 𝜃, 𝑧) coordinate z .t t = atan(y/x) .T_n (x) Tchebyshev polynmomial poly .T (n) Tchebyshev polynmomial tan (x) tangent function tand (x) tangent function in degree tanh (x) hyperbolic tangent function p .tay 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 /2! + 𝑎3 𝑥 3 /3! + … + 𝑎𝑛 𝑥 𝑛 /𝑛! x .Tbsp 14.20653125 × 10−6 𝑥 [m^3], tablespoon(UK) x .tbsp 14.78676 × 10−6 𝑥 [m^3], tablespoon(US) .tdma (a,b,c,d, na,nb) (in radian) dot-only tridiagonal matrix algorithm 106 𝑥 dot-only x .Therm 0.10550559 × x .therm 0.1054804 × 106 𝑥 [kJ], therm(US) 22 [kJ], therm(Europe) [100] 300 Reference, Tutorial by www.msharpmath.com x .todeg x * 180 / pi, radian to degree matrix.toep (A,B) toeplitz matrix x .torad x * pi / 180, degree to radian x .Ton 907.1847 𝑥 [kg], English ton-mass x .ton 1000𝑥 [kg], ton, metric ton-mass x .Tonf 8.896443239 𝑥 [kN], English ton-force x .tonf 9.80665 𝑥 [kN], ton-force x .torr 0.133322368 𝑥 [kPa], mmHg A .tr transpose A .trace trace trapz(x,y) area by trapezoidal rule A .tril (k) lower-triangular A .triu (k) upper-triangular A .true index for nonzero elements /x/A/p/v/z/ .trun (eps=1.e-30) truncate if |x|<eps /x/A/p/v/z/ .trun 𝑘 truncate with 𝜖 = 10−𝑘 , 𝑘 ≤ 16 x .Tsp 3.5516328125 × 10−6 𝑥 [m^3], teaspoon(UK) x .tsp 4.92892 × 10−6 𝑥 [m^3], teaspoon(US) //-------------------------------------------------------------// U //-------------------------------------------------------------A .unit normalize each column vector (column-first) v .unit normalize a vertex z .unit normalize a complex number p .up (k) 𝑎0 𝑥 𝑘 + 𝑎1 𝑥1+𝑘 + 𝑎2 𝑥 2+𝑘 + … + 𝑎𝑛 𝑥 𝑛+𝑘 //-------------------------------------------------------------// V //-------------------------------------------------------------A .vander (nrow) Vandermonde matrix 23 [100] 300 Reference, Tutorial by www.msharpmath.com A .var variance .vertcat (A1,A2,...) A1 _ A2 _ ... // vertical concatenation //-------------------------------------------------------------// W //-------------------------------------------------------------x .W 0.001 𝑥 [kW], Watt x .week 604800 𝑥 [s] x .Wh 3.6 𝑥 [kJ], Watt-hour // _W*_hr A .wide (n) A..n = A.wide(n), reshape in n-column A .write (string) write A to a file //-------------------------------------------------------------// X //-------------------------------------------------------------A .xrot/xrotd (theta) rotation around the x-axis v .xrot/xrotd (theta) rotation around the x-axis //-------------------------------------------------------------// Y //-------------------------------------------------------------.Y_nu (x) Bessel function 𝑌_𝜈(𝑥) dot-only x .yd 0.9144𝑥 [m] = 3𝑥 [ft], yard x .yd2 0.83612736𝑥 [m^2], square yard x .yd3 0.764554857984 𝑥 [m^3], cubic yard x .ydph 0.000254 𝑥 [m/s], yard/h x .ydpm 0.015424 𝑥 [m/s], yard/min x .ydps 0.9144 𝑥 [m/s], yard/s .ym (year,month) print calendar, .ym(2012,8) x .yr 31536000𝑥 A .yrot/yrotd (theta) dot-only [s] // non-leap year rotation around the y-axis 24 [100] 300 Reference, Tutorial by www.msharpmath.com v .yrot/yrotd (theta) rotation around the y-axis //-------------------------------------------------------------// Z //-------------------------------------------------------------_Z0 = 376.73031346177 // Impedance of vacuum [ohm] .zeros (m,n=m) all-zeros matrix A .zrot/zrotd (theta) rotation around the z-axis (in degree) v .zrot/zrotd (theta) rotation around the z-axis //-------------------------------------------------------------// end of lists //-------------------------------------------------------------- //---------------------------------------------------------------------//---------------------------------------------------------------------poly( a0,a1,a2, …, an ); // list of coefficients poly( [a0,a1,a2,…,an] ); // a single matrix poly( A ); // A is a matrix [an, …,a2,a1,a0].poly; // general order, n >= 4 .[an, …,a2,a1,a0]; // general order, n >= 4 .[ a ] // constant polynomial .[ a,b ] .[ a,b,c ] .[ a,b,c,d ] // 1st-order poly, line y = ax+b // 2nd-order poly, parabola y = ax^2 + bx + c // 3rd-order poly, cubic y = ax^3 + bx^2 + cx +d p = poly[ n+1 ].i ( f(i) ) p = poly[ 0,n ].i ( f(i) ) A general for the coefficient function is 𝑝(𝑥) = 𝑓(𝑎)𝑥 𝑎 + 𝑓(𝑎 + 𝑚)𝑥 𝑎+𝑚 + 𝑓(𝑎 + 2𝑚)𝑥 𝑎+2𝑚 + ⋯ p = poly[ a,b, m=1 ] .i ( f(i) ) 25 [100] 300 Reference, Tutorial by www.msharpmath.com 26 [100] 300 Reference, Tutorial by www.msharpmath.com Description of functions [ A ] ----------------------------------------------------------------Inverse Cosine Function double acos(double x) complex acos(complex z) matrix acos(matrix A) ---------------------------------------------------------------------- acos(𝑥) = cos−1 𝑥 −1 ≤ 𝑥 ≤ 1, 0 ≤ acos(𝑥) ≤ 𝜋 cos−1 𝑧 = −𝑖 log (𝑧 + √𝑧 2 − 1 ) #> acos(0.5); cos(ans); ans = 1.0471976 ans = 0.5 #> acos( 2+0i ); cos(ans); ans = 0 - 1.31696! ans = 2 + 0! #> acos( [ 0.5, 2+0i ] ); cos(ans); ans = [ 1.0472 + i 5.55112e-017 ans = [ 0.5 - i 4.80741e-017 0 - i 1.31696 2 #> plot.x(-1,1) ( acos(x) ); ---------------------------------------------------------------------Inverse Cosine Function in Degree 27 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com double acosd(double x) complex acosd(complex z) matrix acosd(matrix A) ---------------------------------------------------------------------- 180 𝜋 0 ≤ acosd(𝑥) ≤ 180 acosd(𝑥) = acos(𝑥) −1 ≤ 𝑥 ≤ 1, #> acosd(0.5); cosd(ans); ans = 60 ans = 0.5 #> acosd( 2+0i ); cosd(ans); ans = 0 - 75.4561! ans = 2 + 0! #> acosd( [ 0.5, 2+0i ] ); cosd(ans); ans = [ 60 + i 3.18055e-015 ans = [ 0.5 - i 4.80741e-017 0 - i 75.4561 2 ] ] ---------------------------------------------------------------------Inverse Hyperbolic Cosine Function double acosh(double x) complex acosh(complex z) matrix acosh(matrix A) ---------------------------------------------------------------------- acosh(𝑥) = cosh−1 𝑥 = log (𝑥 + √𝑥 2 − 1) 1 ≤ 𝑥 < ∞, −1 cosh 0 ≤ acosh(𝑥) < ∞ 𝑧 = log (𝑧 + √𝑧 2 − 1) The return value of acosh is the positive inverse or the complex inverse with the smallest argument. #> acosh(pi); cosh(ans); ans = 1.8115263 ans = 3.1415927 #> acosh( 0i ); cosh(ans); ans = 0 + 1.5708! ans = 6.12323e-017 + 0! #> acosh( [ pi, 0i ] ); cosh(ans); 28 [100] 300 ans = [ ans = [ Reference, Tutorial by www.msharpmath.com 1.81153 3.14159 0 + i 1.5708 6.12323e-017 #> plot.x(1,5) ( acosh(x) ); ---------------------------------------------------------------------Inverse Cotangent Function double acot(double x) complex acot(complex z) matrix acot(matrix A) ---------------------------------------------------------------------- acot(𝑥) = atan(1/𝑥) 𝜋 𝜋 −∞ < 𝑥 < ∞, − < acot(𝑥) < 2 2 1 𝑖𝑧 + 1 cot −1 𝑧 = 𝑖 log ( ) 2 𝑖𝑧 − 1 #> acot( 2 ); cot(ans); ans = 0.46364761 ans = 2 #> acot( 2i ); cot(ans); ans = 0 - 0.549306! ans = 0 + 2! #> acot( [ 2, 2i ] ); cot(ans); ans = [ 0.463648 + i 5.55112e-017 ans = [ 2 - i 2.77556e-016 29 0 - i 0.54930 ] 0 + i 2 ] ] ] [100] 300 Reference, Tutorial by www.msharpmath.com #> plot.x(-50,-0.01) ( acot(x) ); plot+.x(0.01,50) ( acot(x) ); ---------------------------------------------------------------------Inverse Cotangent Function in Degree double acotd(double x) complex acotd(complex z) matrix acotd(matrix A) ---------------------------------------------------------------------- 180 π − 90 < acotd(𝑥) < 90 acotd(𝑥) = acot(𝑥) −∞ < 𝑥 < ∞, #> acotd( 2 ); cotd(ans); ans = 26.565051 ans = 2 #> acotd( 2i ); cotd(ans); ans = 0 - 31.4729! ans = 0 + 2! #> acotd( [ 2, 2i ] ); cotd(ans); ans = [ 26.5651 + i 3.18055e-015 ans = [ 2 - i 2.77556e-016 0 - i 31.4729 0 + i 2 ---------------------------------------------------------------------Inverse Hyperbolic Cotangent Function double acoth(double x) complex acoth(complex z) matrix acoth(matrix A) ---------------------------------------------------------------------- acoth(𝑥) = atanh(1/𝑥) 𝑥 < −1, 1 < 𝑥, − ∞ < acoth(𝑥) < ∞ 1 𝑧+1 coth−1 𝑧 = log 2 𝑧−1 #> acoth( 2 ); coth(ans); ans = 0.54930614 ans = 2 #> acoth( 0.5+0i ); coth(ans); ans = 0.549306 - 1.5708! ans = 0.5 + 4.59243e-017! 30 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com #> acoth( [ 2, 0.5+0i ] ); coth(ans); ans = [ 0.549306 ans = [ 2 0.549306 - i 1.5708 0.5 + i 4.59243e-017 ] ] #> plot.x(1.01,50) ( acoth(x) ); plot+.x(-50,-1.01) ( acoth(x) ); ---------------------------------------------------------------------Inverse Cosecant Function double acsc(double x) complex acsc(complex z) matrix acsc(matrix A) ---------------------------------------------------------------------- 𝑥 ≤ −1, acsc(𝑥) = asin(1/𝑥) 𝜋 𝜋 1 ≤ 𝑥, − ≤ acsc(𝑥) ≤ 2 2 𝑖 1 csc −1 𝑧 = −𝑖 log ( + √1 − 2 ) 𝑧 𝑧 #> acsc( 2 ); csc(ans); ans = 0.52359878 ans = 2 #> acsc( 0.5+0i ); csc(ans); ans = 1.5708 - 1.31696! ans = 0.5 + 2.65144e-017! #> acsc( [ 2, 0.5+0i ] ); csc(ans); ans = [ 0.523599 + i 5.55112e-017 ans = [ 2 - i 1.92296e-016 31 1.5708 - i 1.31696 0.5 + i 2.65144e-017 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com #> plot.x( 1,50 ) ( acsc(x) ); plot+.x(-50,-1) ( acsc(x) ); ---------------------------------------------------------------------Inverse Cosecant Function in Degree double acscd(double x) complex acscd(complex z) matrix acscd(matrix A) ---------------------------------------------------------------------- 180 𝜋 −90 ≤ acscd(𝑥) ≤ 90 acscd(𝑥) = acsc(𝑥) 𝑥 ≤ −1, 1 ≤ 𝑥, #> acscd( 2 ); cscd(ans); ans = 30 ans = 2 #> acscd( 0.5+0i ); cscd(ans); ans = 90 - 75.4561! ans = 0.5 + 2.65144e-017! #> acscd( [ 2, 0.5+0i ] ); cscd(ans); ans = [ 30 + i 3.18055e-015 ans = [ 2 - i 1.92296e-016 90 - i 75.4561 0.5 + i 2.65144e-017 ---------------------------------------------------------------------Inverse Hyperbolic Cosecant Function double acsch(double x) complex acsch(complex z) matrix acsch(matrix A) ---------------------------------------------------------------------- acsch(𝑥) = asinh(1/𝑥) −∞ < 𝑥 < ∞, − ∞ < asinh(𝑥) < ∞ 32 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com 1 1 csch−1 𝑧 = log ( + √ 2 + 1) 𝑧 𝑧 #> acsch( 0.5 ); csch(ans); ans = 1.4436355 ans = 0.5 #> acsch( 2i ); csch(ans); ans = -5.55112e-017 - 0.523599! ans = -1.92296e-016 + 2! #> acsch( [ 0.5, 2i ] ); csch(ans); ans = [ 1.44364 ans = [ 0.5 -5.55112e-017 - i 0.523599 -1.92296e-016 + i 2 #> plot.x(-5,-.01) ( acsch(x) ); plot+.x(.01,5) ( acsch(x) ); ---------------------------------------------------------------------matrix A.add(double i, double j, double s) ---------------------------------------------------------------------- elementary row operation, 𝑅𝑖 = 𝑅𝑖 + 𝑠𝑅𝑗 𝑅𝑖 𝑅𝑖 + 𝑠𝑅𝑗 ⋯ 𝐀 = [ ] where 𝑅𝑖 is 𝑖-th row. A.add(i,j, s) = [ ⋯ ] 𝑅𝑗 𝑅𝑗 #> A = [ 2,1,4 ; -1,3,5 ]; A = [ 2 [ -1 1 3 #> A.add(2,1, 4); ans = [ 2 [ 7 1 7 4 ] 5 ] // R_2 = R_2 + R_1 * 4 4 ] 21 ] 33 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.all // column-first operation double A.all1 // equivalent to A.all.all(1) ---------------------------------------------------------------------- 0 if any 𝑎𝑖𝑗 is zero, otherwise 1 #> A = [3,4; 0,0]; A = [ 3 [ 0 B = [ 1 [ 4 B = [1,0; 4,0]; // multi-rows (column-first) 4 ] 0 ] 0 ] 0 ] #> A.all; // operation on each column ans = [ 0 0 ] #> B.all; ans = [ #> A(); A().all; ans = [ ans = [ 1 0 ] // single-row with columnwise counting 3 0 ] 0 4 0 ] #> A.all1; // equivalent to A.all.all(1) ans = 0 ---------------------------------------------------------------------matrix A.any // column-first operation double A.any1 // equivalent to A.any.any(1) ---------------------------------------------------------------------- 1 if any 𝑎𝑖𝑗 is not zero, otherwise 0 #> A = [3,4; 0,0]; A = [ 3 [ 0 B = [ 1 [ 4 #> A.any; ans = [ B = [1,0; 4,0]; // multi-rows (column-first) 4 ] 0 ] 0 ] 0 ] 1 1 ] 34 [100] 300 Reference, Tutorial by www.msharpmath.com #> B.any; ans = [ 1 #> A(); A().any; ans = [ ans = [ // single-row 3 0 1 ] #> A.any1; ans = // equivalent to 1 0 ] 4 0 ] A.any.any(1) ---------------------------------------------------------------------poly A.apoly poly A.poly ---------------------------------------------------------------------- convert a matrix into a polynomial by ascending order #> A = (1:8)._3; A = [ 1 [ 2 [ 3 4 5 6 #> A.apoly; // ans = poly( = +8x^7 +4x^3 note that the order is x^7 1 2 3 4 5 6 7 8 ) +7x^6 +6x^5 +5x^4 +3x^2 +2x +1 7 ] 8 ] 0 ] ---------------------------------------------------------------------double A.area ---------------------------------------------------------------------[ a, b, c ] .area #> [ 3, 4, 5 ].area; ans = // area of a three-sides triangle 6 ---------------------------------------------------------------------Inverse Secant Function double asec(double x) complex asec(complex z) matrix asec(matrix A) ---------------------------------------------------------------------- 35 [100] 300 Reference, Tutorial by www.msharpmath.com asec(𝑥) = acos(1/𝑥) 𝑥 ≤ −1, 1 ≤ 𝑥, 0 ≤ asec(𝑥) ≤ 𝜋 1 1 sec −1 𝑧 = −𝑖 log ( + √ 2 − 1) 𝑧 𝑧 #> asec( 2 ); sec(ans); ans = 1.0471976 ans = 2 #> asec( 0.5+0i ); sec(ans); ans = -0 - 1.31696! ans = 0.5 + 0! #> asec( [ 2, 0.5+0i ] ); sec(ans); ans = [ -1.0472 + i 5.55112e-017 ans = [ 2 - i 1.92296e-016 -0 - i 1.31696 0.5 #> plot.x( 1,50 ) ( asec(x) ); plot+.x(-50,-1 ) ( asec(x) ); ---------------------------------------------------------------------Inverse Secant Function in Degree double asecd(double x) complex asecd(complex z) matrix asecd(matrix A) ---------------------------------------------------------------------- 180 𝜋 0 ≤ asec(𝑥) ≤ 180 asecd(𝑥) = asecd(𝑥) 𝑥 ≤ −1, #> asecd( 2 ); ans = ans = 1 ≤ 𝑥, secd(ans); 60 2 #> asecd( 0.5+0i ); secd(ans); 36 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com ans = -0 - 75.4561! ans = 0.5 + 0! #> asecd( [ 2, 0.5+0i ] ); secd(ans); ans = [ -60 + i 3.18055e-015 ans = [ 2 - i 1.92296e-016 -0 - i 75.4561 0.5 ] ] ---------------------------------------------------------------------Inverse Hyperbolic Secant Function double asech(double x) complex asech(complex z) matrix asech(matrix A) ---------------------------------------------------------------------- asech(𝑥) = acosh(1/𝑥) 0 < 𝑥 ≤ 1, 0 ≤ asech(𝑥) < ∞ 1 1 sech−1 𝑧 = log ( + √ 2 − 1) 𝑧 𝑧 #> asech( 0.5 ); sech(ans); ans = 1.3169579 ans = 0.5 #> asech( 2+0i ); sech(ans); ans = -5.55112e-017 - 1.0472! ans = 2 - 1.92296e-016! #> asech( [ 0.5, 2+0i ] ); sech(ans); ans = [ 1.31696 ans = [ 0.5 -5.55112e-017 - i 1.0472 2 - i 1.92296e-016 #> plot.x(0.001,1) ( asech(x) ); ---------------------------------------------------------------------Inverse Sine Function 37 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com double asin(double x) complex asin(complex z) matrix asin(matrix A) ---------------------------------------------------------------------- asin(𝑥) = sin−1 𝑥 𝜋 𝜋 −1 ≤ 𝑥 ≤ 1, − ≤ asin(𝑥) ≤ 2 2 −1 2 sin 𝑧 = −𝑖 log (𝑖𝑧 + √1 − 𝑧 ) #> asin(0.5); sin(ans); ans = 0.52359878 ans = 0.5 #> asin( 2+0i ); sin(ans); ans = 1.5708 + 1.31696! ans = 2 + 8.75243e-016! #> asin( [ 0.5, 2+0i ] ); sin(ans); ans = [ 0.523599 + i 5.55112e-017 ans = [ 0.5 + i 4.80741e-017 1.5708 + i 1.31696 2 + i 8.75243e-016 #> plot.x(-1,1) ( asin(x) ); ---------------------------------------------------------------------Inverse Sine Function in Degree double asind(double x) complex asind(complex z) matrix asind(matrix A) ---------------------------------------------------------------------- 180 𝜋 −1 ≤ 𝑥 ≤ 1, − 90 ≤ asin(𝑥) ≤ 90 asind(𝑥) = asin(𝑥) #> asind(0.5); sind(ans); ans = 30 38 ] ] [100] 300 ans = Reference, Tutorial by www.msharpmath.com 0.5 #> asind( 2+0i ); sind(ans); ans = 90 + 75.4561! ans = 2 + 4.9065e-016! #> asind( [ 0.5, 2+0i ] ); sind(ans); ans = [ 30 + i 3.18055e-015 ans = [ 0.5 + i 4.80741e-017 90 + i 75.4561 2 + i 4.9065e-016 ] ] ---------------------------------------------------------------------Inverse Hyperbolic Sine Function double asinh(double x) complex asinh(complex z) matrix asinh(matrix A) ---------------------------------------------------------------------- asinh(𝑥) = log (𝑥 + √𝑥 2 + 1) −∞ < 𝑥 < ∞, sinh −1 − ∞ < asinh(𝑥) < ∞ 𝑧 = log (𝑧 + √𝑧 2 + 1) #> asinh( 0.5 ); sinh(ans); ans = 0.48121183 ans = 0.5 #> asinh( 2i ); sinh(ans); ans = 1.31696 + 1.5708! ans = 1.06058e-016 + 2! #> asinh( [ 0.5, 2i ] ); sinh(ans); ans = [ 0.481212 ans = [ 0.5 1.31696 + i 1.5708 1.06058e-016 + i 2 #> plot.x( -50,50 ) ( asinh(x) ); ---------------------------------------------------------------------- 39 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com Inverse Tangent Function double atan(double x) complex atan(complex z) matrix atan(matrix A) ---------------------------------------------------------------------- atan(𝑥) = tan−1 𝑥 𝜋 𝜋 −∞ < 𝑥 < ∞, − < atan(𝑥) < 2 2 𝑖 𝑖+𝑧 −1 tan 𝑧 = log 2 𝑖−𝑧 #> atan(1); tan(ans); ans = 0.78539816 ans = 1 #> atan( 0.5i ); tan(ans); ans = 0 + 0.549306! ans = 0 + 0.5! #> atan( [ 1, 0.5i ] ); tan(ans); ans = [ 0.785398 ans = [ 1 0 + i 0.549306 0 + i 0.5 #> plot.x( -20,20 ) ( atan(x) ); ---------------------------------------------------------------------double atand(double x) complex atand(complex z) matrix atand(matrix A) ---------------------------------------------------------------------- 180 𝜋 − 90 < atan(𝑥) < 90 atand(𝑥) = atan(𝑥) −∞ < 𝑥 < ∞, #> atand(1); tand(ans); 40 ] ] [100] 300 ans = ans = Reference, Tutorial by www.msharpmath.com 45 1 #> atand( 0.5i ); tand(ans); ans = 0 + 31.4729! ans = 0 + 0.5! #> atand( [ 1, 0.5i ] ); tand(ans); ans = [ 45 ans = [ 1 0 + i 31.4729 0 + i 0.5 ] ] ---------------------------------------------------------------------Inverse Tangent Function with two arguments double atan2(double y, double x) matrix atan2(double y, matrix x) matrix atan2(matrix y, double x) matrix atan2(matrix y, matrix x) ---------------------------------------------------------------------- 𝑦 𝑥 −∞ < 𝑦 < ∞, − 𝜋 < atan2(𝑦, 𝑥) ≤ 𝜋 atan2(𝑦, 𝑥) = tan−1 −∞ < 𝑥 < ∞, #> atan2( 1, 1 ) ; // first quadrant, ans = 0.78539816 x = 1, y = 1 #> atan2( 1, -1 ); // second quadrant, x = -1, ans = 2.3561945 #> atan2( -1, -1 ); // third quadrant, ans = -2.3561945 y = 1 x = -1, y = -1 #> atan2( -1, 1 ); // fourth quadrant, x = 1, y = -1 ans = -0.78539816 For a pair of rows and columns with the same dimension, operation applies for a pair of elements (𝑥𝑖 , 𝑦𝑖 ) #> atan2( [1,1,-1,-1], [1,-1,-1,1] ); ans = [ 0.785398 2.35619 -2.35619 -0.785398 ] Otherwise, a structure similar to the Kronecker product is obtained 41 [100] 300 Reference, Tutorial by www.msharpmath.com #> atan2( [1;0.5;-0.5;-1], [1,-1,-1,1] ); ans = [ 0.785398 2.35619 2.35619 [ 0.463648 2.67795 2.67795 [ -0.463648 -2.67795 -2.67795 [ -0.785398 -2.35619 -2.35619 0.785398 0.463648 -0.463648 -0.785398 ] ] ] ] ---------------------------------------------------------------------Inverse Tangent Function in Degree double atand2(double y, double x) matrix atand2(double y, matrix x) matrix atand2(matrix y, double x) matrix atand2(matrix y, matrix x) ---------------------------------------------------------------------- 180 π −∞ < 𝑦 < ∞, − 180 < atand2(𝑦, 𝑥) ≤ 180 atand2(𝑦, 𝑥) = atan2(𝑦, 𝑥) −∞ < 𝑥 < ∞, #> atand2( 1, 1 ) ; ans = // first quadrant, x = 1, y = 1 45 #> atand2( 1, -1 ); ans = // second quadrant, 135 x = -1, y = 1 #> atand2( -1, -1 ); // third quadrant, x = -1, ans = -135 y = -1 #> atand2( -1, 1 ); ans = y = -1 // fourth quadrant, -45 x = 1, For a pair of rows and columns with the same dimension, operation applies for a pair of elements (𝑥𝑖 , 𝑦𝑖 ) #> atand2( [1,1,-1,-1], [1,-1,-1,1] ); ans = [ 45 135 -135 -45 ] Otherwise, a structure similar to the Kronecker product is obtained #> atand2( [1;0.5;-0.5;-1], [1,-1,-1,1] ); ans = [ 45 135 135 [ 26.5651 153.435 153.435 [ -26.5651 -153.435 -153.435 [ -45 -135 -135 42 45 26.5651 -26.5651 -45 ] ] ] ] [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------Inverse Hyperbolic Tangent Function double atanh(double x) complex atanh(complex z) matrix atanh(matrix A) ---------------------------------------------------------------------- 1 1+𝑥 atanh(𝑥) = log 2 1−𝑥 −1 < 𝑥 < 1, − ∞ < atanh(𝑥) < ∞ 1 1+𝑧 tanh−1(𝑥) = log 2 1−𝑧 #> atanh( 0.5 ); tanh(ans); ans = 0.54930614 ans = 0.5 #> atanh( 2i ); tanh(ans); ans = 5.55112e-017 + 1.10715! ans = 2.77556e-016 + 2! #> atanh( [ 0.5, 2i ] ); tanh(ans); ans = [ 0.549306 ans = [ 0.5 5.55112e-017 + i 1.10715 2.77556e-016 + i 2 #> plot.x( -0.99, 0.99 ) ( atanh(x) ); [ B ] ---------------------------------------------------------------------Beta Function double .B(double x, double y) // dot-only matrix .B(double x, matrix y) 43 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com matrix .B(matrix x, double y) matrix .B(matrix x, matrix y) ---------------------------------------------------------------------- 𝐵(𝑥, 𝑦) = 1 𝛤(𝑥)𝛤(𝑦) = ∫ 𝑡 𝑥−1 (1 − 𝑡)𝑦−1 𝑑𝑡 𝛤(𝑥 + 𝑦) 0 𝜋 2 = 2 ∫ sin2𝑥−1 𝜃 cos2𝑦−1 𝜃 𝑑𝜃 0 #> .B(3,4); ans = 0.016666667 #> (.G(3)*.G(4) )/.G(3+4); ans = 0.016666667 // from definition ---------------------------------------------------------------------matrix A.backsub ---------------------------------------------------------------------- For an 𝑛 × (𝑛 + 1) matrix, backward substitution is carried out to find a solution vector. 𝑎11 [ 0 0 𝑎12 𝑎22 0 𝑎13 𝑥1 𝑏1 𝑎23 ] [𝑥2 ] = [𝑏2 ] 𝑎33 𝑥3 𝑏3 ⇒ 𝑎11 𝑥1 + 𝑎12 𝑥2 + 𝑎13 𝑥3 𝑎22 𝑥2 + 𝑎23 𝑥3 𝑎33 𝑥3 = 𝑏1 = 𝑏2 = 𝑏3 𝑥3 = 𝑏3 /𝑎33 𝑥2 = (𝑏2 − 𝑎23 𝑥3 )/𝑎22 𝑥1 = (𝑏1 − 𝑎12 𝑥2 − 𝑎13 𝑥3 )/𝑎11 #> A = [ 2,-4,-10,12; 0,-1,15,-16; 0,0,-134,134 ]; A = [ 2 -4 -10 [ 0 -1 15 [ 0 0 -134 #> A.backsub; ans = [ 3 ] [ 1 ] [ -1 ] 12 ] -16 ] 134 ] ---------------------------------------------------------------------poly poly.binom(double n) ---------------------------------------------------------------------- 44 [100] 300 Reference, Tutorial by www.msharpmath.com binomial polynomial of degree 𝑛 1 𝑥 ( ) = 𝑥(𝑥 − 1)(𝑥 − 2) × … × (𝑥 − 𝑛 − 2)(𝑥 − 𝑛 − 1) 𝑛 𝑛! #> for.n(0,5) poly.binom(n).ratio; ans = (1/1) * poly( 1 ) ans = (1/1) * poly( 0 1 ) ans = (1/2) * poly( 0 -1 1 ) ans = (1/6) * poly( 0 2 -3 1 ) ans = (1/24) * poly( 0 -6 11 -6 1 ) ans = (1/120) * poly( 0 24 -50 35 -10 1 ) #> .hold; for.n(1,6) plot.x(0,n) ( poly.binom(n+1)(x) ); plot; ---------------------------------------------------------------------double .bisect(double a,double b,double f(double x)) // dot-only ---------------------------------------------------------------------- In finding a root of 𝑓(𝑥) = 0, it may happen that the given object function 𝑓(𝑥) is not simple. Then, it is recommended to define an objective function 'f(x)' and combine with the dot-only function installed in Cemmath. For a simple case, a root of 𝑒 𝑥 = 5 can be found by #> double fun(x) = exp(x)-5; #> .bisect(0,3, fun); ans = 1.6094379 // dot function in Cemmath Another example is to find 𝑠 in the following equation 𝑠 ∫ 𝑥 sin(𝑥) 𝑑𝑥 = 0 0 If the Umbrella 'int' is utilized, solution can be found by 45 [100] 300 Reference, Tutorial by www.msharpmath.com #> double fn(s) = int.x(0,s) ( x*sin(x) ); #> .bisect(pi,2*pi, fn); ans = 4.4934094 Similarly in the shooting method for ordinary differential equations, solution is frequently found by the dot function '.bisect(a,b,f(x))' . For example, the following system 𝑦 ′′ + 2𝑥𝑦 ′ = 0 } 𝑦(0) = 2, 𝑦(1) = 3 𝑦ex = 2 + erf(𝑥) erf(1) can be solved alternatively by assuming a slope at 𝑥 = 0 and satisfying the objective function 𝑦(1) = 3. The exact slope is known to be 𝑦 ′ (0) = 1 2 2 2 = = 1.33900 ⋯ [ 𝑒 −𝑥 ] erf(1) √𝜋 √𝜋erf(1) 𝑥=0 An example run is shown below. double fn(s) { y1 = ode.x(0,1) ( y'' = -2*x*y', y = 2, y' = s ).return(y).end; return y1-3; } #> .bisect(1,3, fn); ans = 1.3390033 //---------------------- This function can be written by users as follows double user_bisect(a,b, fun(x)) { eps = 1.e-6; ya = fun(a); if( |ya| < eps ) return a; yb = fun(b); if( |yb| < eps ) return b; if( ya*yb > 0 ) { "y(a)y(b) > 0"; return NaN; } iter = 0; while(1) { if( ++iter > 200 ) { "bisection failed"; return NaN; } x = 0.5*(a+b); y = fun(x); [ iter,a,b,x,y ];; // display with double semicolons ;; 46 [100] 300 Reference, Tutorial by www.msharpmath.com if( |y| > inf ) { "bisection diverged"; return NaN; } if( |a-b|/(1.+|a|+|b|) < eps || |y| < eps ) break; if( ya*y > 0 ) a = x; else b = x; } return x; } #> user_bisect(1,3, fn); ans = [ 1 1 ans = [ 2 1 ans = [ 3 1 ans = [ 4 1.25 ans = [ 5 1.25 ans = [ 6 1.3125 ans = [ 7 1.3125 ans = [ 8 1.32813 ans = [ 9 1.33594 ans = [10 1.33594 ans = [11 1.33789 ans = [12 1.33887 ans = [13 1.33887 ans = [14 1.33887 ans = [15 1.33899 ans = [16 1.33899 ans = [17 1.33899 ans = 1.3390045 3 2 1.5 1.5 1.375 1.375 1.34375 1.34375 1.34375 1.33984 1.33984 1.33984 1.33936 1.33911 1.33911 1.33905 1.33902 2 1.5 1.25 1.375 1.3125 1.34375 1.32813 1.33594 1.33984 1.33789 1.33887 1.33936 1.33911 1.33899 1.33905 1.33902 1.339 0.493648 0.120236 -0.0664698 0.0268832 -0.0197933 0.00354493 -0.0081242 -0.00228964 0.000627647 -0.000830994 -0.000101674 0.000262986 8.06564e-005 -1.05087e-005 3.50738e-005 1.22826e-005 8.86954e-007 ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] [ C ] ---------------------------------------------------------------------Cubic Root Function double cbrt (double x) complex cbrt (complex z) matrix cbrt (matrix A) ---------------------------------------------------------------------#> cbrt( 8 ); ans = 2 #> cbrt( 1i ); ans = 0.866025 + 0.5! #> cbrt( [ 8, 1i ] ); 47 [100] 300 Reference, ans = [ Tutorial by www.msharpmath.com 2 0.866025 + i 0.5 ---------------------------------------------------------------------double .ceil (double x) matrix .ceil (matrix A) ---------------------------------------------------------------------- Integer near ∞ #> .ceil(pi); ans = 4 #> .ceil( [ pi, -pi ] ); ans = [ 4 -3 ] ---------------------------------------------------------------------double x.ceil ---------------------------------------------------------------------- Integer near ∞ #> pi.ceil; ans = 4 #> (-pi).ceil; ans = // dot(.) is prior to unary minus -3 #> -pi.ceil; ans = -4 ---------------------------------------------------------------------matrix A.chol ---------------------------------------------------------------------- Choleski decomposition 𝐀 = 𝐔𝑇 𝐔 𝑖−1 𝑖−1 𝑎𝑖𝑗 = ∑ 𝑢𝑘𝑖 𝑢𝑘𝑖 + 𝑢𝑖𝑖 𝑢𝑖𝑖 ⇒ 𝑘=1 𝑘=1 𝑖−1 𝑎𝑖𝑗 = ∑ 𝑢𝑘𝑖 𝑢𝑘𝑗 + 𝑢𝑖𝑖 𝑢𝑖𝑗 𝑢𝑖𝑖 = (𝑎𝑖𝑖 − ∑ 𝑢𝑘𝑖 𝑢𝑘𝑖 )1/2 𝑖−1 ⇒ 𝑘=1 𝑢𝑖𝑗 = (𝑎𝑖𝑗 − ∑ 𝑢𝑘𝑖 𝑢𝑘𝑗 )/𝑢𝑖𝑖 𝑘=1 48 ] [100] 300 Reference, Tutorial by www.msharpmath.com where 𝑗 = 𝑖 + 1, 𝑖 + 2, … , 𝑛 − 1, 𝑛. #> A = [ 2,-1,1; -1,3,-2; 1,-2,5 ]; A = [ 2 -1 1 ] [ -1 3 -2 ] [ 1 -2 5 ] #> A.chol; ans = [ 1.41421 -0.707107 0.707107 ] [ 0 1.58114 -0.948683 ] [ 0 0 1.89737 ] For users convenience, a direct code is presented below. void user_chol(matrix& A) { n = A.m; U = .zeros(n); for.i(1,n) { sum = A(i,i) - U..(1:i-1)(i).norm22; if( sum < 0 ) { "negative diagonal in Choleski";; break; } U(i,i) = sqrt(sum); for.j(i+1,n) U(i,j) = (A(i,j) - U..(1:i-1)(i) ** U..(1:i-1)(j))/U(i,i); U ;; // display U..(i)(i:n) } } #> user_chol(A); // user function user_chol.U = [ 1.41421 -0.707107 [ 0 0 [ 0 0 user_chol.U = [ 1.41421 -0.707107 [ 0 1.58114 [ 0 0 user_chol.U = [ 1.41421 -0.707107 [ 0 1.58114 [ 0 0 listed above 0.707107 ] 0 ] 0 ] 0.707107 ] -0.948683 ] 0 ] 0.707107 ] -0.948683 ] 1.89737 ] 49 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------double Cint(double x) matrix Cint(matrix x) ---------------------------------------------------------------------- 1 𝑥 1 − cos 𝑡 Cint(𝑥) = ∫ 𝑑𝑡 𝑥 0 𝑡2 #> Cint(1); ans = 0.48638538 #> Cint([1,2]); ans = [ 0.486385 0.44867 ] ---------------------------------------------------------------------matrix A.circle ---------------------------------------------------------------------- An equation of a circle passing three points is created by [ x1,y1, x2,y2, x3,y3 ].circle; and is represented as 𝐴(𝑥 2 + 𝑦 2 ) + 𝐵𝑥 + 𝐶𝑦 + 𝐷 = 0 The coefficients are expressed by [ A, B, C, D ] #> [ 1,1, -1,2, 2,4 ].circle; circle (-7)(x^2+y^2) + (9)x + (39)y + (-34) = 0 center = ( 0.642857, 2.78571 ) radius = 1.82108 ans = [ -7 9 39 -34 ] ---------------------------------------------------------------------matrix W.col(double j) // equivalent to W(*,j) ---------------------------------------------------------------------- j-th column vector, both read/write Note that this function allows to modify the original matrix. #> A = (1:12)._4 ; A = [ 1 4 7 50 10 ] [100] 300 [ [ Reference, 2 3 #> A.col(3); ans = [ [ [ Tutorial by www.msharpmath.com 5 6 8 9 11 ] 12 ] 4 5 6 107 108 109 10 ] 11 ] 12 ] 7 ] 8 ] 9 ] #> A.col(3) += 100; A = [ [ [ 1 2 3 ---------------------------------------------------------------------matrix A.colabs ---------------------------------------------------------------------- finds Euclidean norm of each column note: Each column can be normalized by A.unit #> A = [ 1,-3; -2,4 ]; A = [ 1 [ -2 #> A.colabs; ans = [ // -3 ] 4 ] [ sqrt(1+4), sqrt(9+16) ] 2.23607 5 ] #> A().colabs; ans = [ 1 2 3 4 ] ---------------------------------------------------------------------matrix A.coldel(double j) ---------------------------------------------------------------------- deletes j-th column #> A = (1:12)._4; A = [ 1 [ 2 [ 3 #> A.coldel(3); 4 5 6 7 8 9 51 10 ] 11 ] 12 ] [100] 300 Reference, Tutorial by www.msharpmath.com ans = [ [ [ 1 2 3 4 5 6 10 ] 11 ] 12 ] Note that the original matrix is not changed. To modify a matrix itself, assignment should be carried out #> A.col(3) = []; A = [ 1 [ 2 [ 3 // equivalent to A = A.coldel(3); 4 5 6 10 ] 11 ] 12 ] ---------------------------------------------------------------------matrix A.coldet(double j) ---------------------------------------------------------------------- j-th column determinant Consider the Laplace expansion of a determinant 𝑛 𝑎11 𝑎21 det(𝐀) = |𝑎 31 𝑎41 𝑛 𝑎12 𝑎22 𝑎32 𝑎42 𝑎13 𝑎23 𝑎33 𝑎43 det(𝐀) = ∑ 𝐶𝑖𝑗 𝑎𝑖𝑗 = ∑ 𝐶𝑖𝑗 𝑎𝑖𝑗 , 𝑖=1 𝑎14 𝑎24 𝑎34 | 𝑎44 𝐶𝑖𝑗 = (−1)𝑖+𝑗 det(𝐀𝑖𝑗 ) 𝑗=1 where 𝐀𝑖𝑗 is a minor of 𝐀. Then, det(𝐀𝑖𝑗 ) is obtained by A.coldet(j). For example, an equation of a circle passing three points (𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), (𝑥3 , 𝑦3 ) is given as 𝑥 2 + 𝑦 2 𝑥12 + 𝑦12 𝑥22 + 𝑦22 𝑥32 + 𝑦32 𝑥1 𝑥2 𝑥3 | det(𝐀) = | 𝑥 𝑦 𝑦1 𝑦2 𝑦3 1 1 1 1 2 2) (𝑥 (𝑥) (𝑦) (1) = 𝐶11 + 𝑦 + 𝐶21 + 𝐶31 + 𝐶41 =0 For three points (1,1), (−1,2), (2,4), we have 52 [100] 300 Reference, #> A = [ 1, 1+1,1+4,4+16; A = [ 1 [ 1 [ 1 [ 1 #> #> #> #> #> [ [ [ [ A.minor(1,1).det; A.minor(2,1).det; A.minor(3,1).det; A.minor(4,1).det; A.coldet(1); ans = -7 ] -9 ] 39 ] 34 ] Tutorial by www.msharpmath.com 1, 1,-1,2;1, ans ans ans ans 2 1 1 1 1,2,4;1, 1,1,1 ]; 5 -1 2 1 = = = = 20 2 4 1 ] ] ] ] -7 -9 39 34 Therefore, (−1)1+1 (−7)(𝑥 2 + 𝑦 2 ) − (−9)(𝑥) + (39)(𝑦) − (34)(1) = 0 #> [ 1,1, -1,2, 2,4 ].circle; // an easier approach circle (-7)(x^2+y^2) + (9)x + (39)y + (-34) = 0 center = ( 0.642857, 2.78571 ) radius = 1.82108 ans = [ -7 9 39 -34 ] ---------------------------------------------------------------------matrix A.colend // equivalent to A(*,end), A.endcol ---------------------------------------------------------------------- returns an end column #> A = [ 2,1,4; -1,3,2; 0,1,2 ]; A = [ 2 1 [ -1 3 [ 0 1 #> A.endcol; ans = [ 4 ] [ 2 ] [ 2 ] 4 ] 2 ] 2 ] 53 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.colins(double j, matrix B) ---------------------------------------------------------------------- insert 𝐁 at 𝑗-th column of 𝐀 Note that if 𝑗 > 𝐀. 𝑛, horizontal concatenation 𝐀|𝐁 is carried out. #> A = (1:16)._4; A = [ 1 5 9 13 ] [ 2 6 10 14 ] [ 3 7 11 15 ] [ 4 8 12 16 ] #> A.inscol(1, [101:105; 201:205]); ans = [ 101 102 103 104 105 1 5 9 13 ] // starts at column 1 [ 201 202 203 204 205 2 6 10 14 ] [ 0 0 0 0 0 3 7 11 15 ] [ 0 0 0 0 0 4 8 12 16 ] #> A.inscol(2, [101:105; 201:205]'); // column 2 ans [ 1 [ 2 [ 3 [ 4 = 101 102 103 104 201 202 203 204 5 6 7 8 9 10 11 12 [ 0 105 205 0 0 13 14 15 16 ] ] ] ] 0 ] #> A.inscol(100, [101:105; 201:205]'); // out of range makes attaching ans = [ 1 5 9 13 101 201 ] [ 2 6 10 14 102 202 ] [ 3 7 11 15 103 203 ] [ 4 8 12 16 104 204 ] [ 0 0 0 0 105 205 ] ---------------------------------------------------------------------matrix A.colskip(double k) ---------------------------------------------------------------------- 54 [100] 300 Reference, if 𝑘 > 0, 1,1 + 𝑘, 1 + 2𝑘, ⋯ Tutorial by www.msharpmath.com else ⋯ , 𝑛 − 3𝑘, 𝑛 − 2𝑘, 𝑛 − 𝑘, 𝑛 under develpment ---------------------------------------------------------------------matrix A.colswap(double i, double j) ---------------------------------------------------------------------- swap 𝑖-th and 𝑗-th columns #> A = [ 1,2,3,4; 5,6,7,8 ]; A = [ 1 2 [ 5 6 3 7 4 ] 8 ] #> A.colswap(2,4); ans = [ 1 [ 5 3 7 2 ] 6 ] 4 8 ---------------------------------------------------------------------matrix p.compan ---------------------------------------------------------------------- A companion matrix of a polynomial 𝑝(𝑥) = 𝑥 𝑛 + 𝑎𝑛−1 𝑥 𝑛−1 + 𝑎𝑛−2 𝑥 𝑛−2 + ⋯ + 𝑎1 𝑥 + 𝑎0 is determined by using its coefficients to be −𝑎𝑛−1 1 0 𝐀= ⋯ [ 0 −𝑎𝑛−2 0 1 ⋯ 0 ⋯ 0 0 ⋱ ⋯ −𝑎1 ⋯ ⋯ ⋱ 1 −𝑎0 0 ⋯ 0 0 ] %> companion matrix #> p = .[ 1, -10, 23, 6 ]; p = poly( 6 23 -10 1 ) = x^3 -10x^2 +23x +6 #> A = p .compan; ans = [ 10 -23 55 -6 ] [100] 300 Reference, [ [ Tutorial by www.msharpmath.com 1 0 0 1 0 ] 0 ] #> A .eigpoly; ans = poly( 6 23 -10 1 ) = x^3 -10x^2 +23x +6 ---------------------------------------------------------------------double A.cond ---------------------------------------------------------------------- condition number, A.norminf*A.inv.norminf 𝑛 cond(𝐀) = ||𝐀||∞ ||𝐀−1 ||∞ ||𝐀||∞ = max ∑ |𝑎𝑖𝑗 | , 1≤𝑖≤𝑛 #> A = [ 1, 3; 2, 5 ]; A = [ 1 [ 2 #> A.cond ; ans = ans = 𝑗=1 3 ] 5 ] A.norminf * A.inv.norminf; 56 56 #> A.norminf; ans = 7 #> A.inv; A.inv.norminf; ans = [ -5 [ 2 ans = 8 3 ] -1 ] ---------------------------------------------------------------------matrix A.conj // equivalent to ~A = A.conj ---------------------------------------------------------------------- complex conjugate 𝑎1 𝐀 = [𝑎2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎8 𝑎9 𝑎10 𝑎11 ] , 𝑎12 𝑎̅1 ~𝐀 = 𝐀. conj = [𝑎̅2 𝑎̅3 #> A = [ 1+1i, 2-3i; 5-4i, 3+1i; 2+3i, -1+5i ]; 56 𝑎̅4 𝑎̅5 𝑎̅6 𝑎̅7 𝑎̅8 𝑎̅9 𝑎̅10 𝑎̅11 ] 𝑎̅12 [100] 300 Reference, Tutorial by www.msharpmath.com A = [ [ [ 1 + i 1 5 - i 4 2 + i 3 2 - i 3 3 + i 1 -1 + i 5 ] ] ] #> ~A; // ~A = A.conj ans = [ 1 - i 1 [ 5 + i 4 [ 2 - i 3 2 + i 3 3 - i 1 -1 - i 5 ] ] ] ---------------------------------------------------------------------matrix A.conjtr // equivalent to A' = A.conjtr ---------------------------------------------------------------------- conjugate transpose 𝑎1 𝑎 𝐀=[ 2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎8 𝑎9 𝑎10 𝑎11 ] , 𝑎12 𝑎̅1 𝑎 ̅ 𝐀′ = 𝐀. conjtr = [ 4 𝑎̅7 𝑎̅10 𝑎̅2 𝑎̅5 𝑎̅8 𝑎̅11 𝑎̅3 𝑎̅6 𝑎̅9 ] 𝑎̅12 #> A = [ 1+1i, 2-3i; 5-4i, 3+1i; 2+3i, -1+5i ]; A = [ 1 + i 1 2 - i 3 ] [ 5 - i 4 3 + i 1 ] [ 2 + i 3 -1 + i 5 ] #> A'; // A' = A.conjtr ans = [ 1 - i 1 [ 2 + i 3 5 + i 4 3 - i 1 #> A.'; // compare with A.' = A.tr ans = [ 1 - i 1 5 + i 4 [ 2 + i 3 3 - i 1 2 - i 3 -1 - i 5 ] ] 2 - i 3 -1 - i 5 ] ] ---------------------------------------------------------------------double .corrcoff(matrix A, matrix B) ---------------------------------------------------------------------- correlation coefficient #> x = [ 1,2,3,4 ]; y = [ 2,3,5,9 ]; x = [ 1 2 3 57 4 ] [100] 300 y = [ Reference, Tutorial by www.msharpmath.com 2 3 5 9 ] #> p = .polyfit(x,y,1); p = poly( -1 2.3 ) = 2.3x -1 #> .corrcoef( y,p(x) ); // correlation between data and fitting ans = 0.9591663 #> plot.x(1,4) ( p(x) ); plot+(x,y); ---------------------------------------------------------------------Cosine Function double cos(double x) complex cos(complex z) matrix cos(matrix A) ---------------------------------------------------------------------- cos(𝑥 + 2𝜋) = cos(𝑥), cos(𝑖𝑥) = cosh(𝑥) −∞ < 𝑥 < ∞, − 1 ≤ cos(𝑥) ≤ 1 cos(𝑥 + 𝑖𝑦) = cos 𝑥 cosh 𝑦 − 𝑖 sin 𝑥 sinh 𝑦 #> cos( pi/3 ); ans = 0.5 #> cos( 1+2! ); ans = 2.03272 - 3.0519! #> cos( [ pi/3, 1+2i ] ); ans = [ 0.5 2.03272 - i 3.0519 58 ] [100] 300 Reference, Tutorial by www.msharpmath.com #> plot.x( 0,pi ) ( cos(x) ); ---------------------------------------------------------------------Cosine Function in Degree double cosd(double x) complex cosd(complex z) matrix cosd(matrix A) ---------------------------------------------------------------------- 𝜋𝑥 cosd(𝑥) = cos ( ), 180 −∞ < 𝑥 < ∞, cosd(𝑥 + 360) = cosd(𝑥) − 1 ≤ cosd(𝑥) ≤ 1 #> cosd( 60 ); ans = 0.5 #> cosd( 1.todeg+2.todeg! ); ans = 2.03272 - 3.0519! #> cosd( [ 60, 1.todeg+2.todeg! ] ); ans = [ 0.5 2.03272 - i 3.0519 #> plot.x( 0,180 ) ( cosd(x) ); ---------------------------------------------------------------------Hyperbolic Cosine Function double cosh(double x) complex cosh(complex z) 59 ] [100] 300 Reference, Tutorial by www.msharpmath.com matrix cosh(matrix A) ---------------------------------------------------------------------- 𝑒 𝑥 + 𝑒 −𝑥 cosh(𝑥) = , cosh(𝑖𝑥) = cos(𝑥) 2 −∞ < 𝑥 < ∞, 1 ≤ cosh(𝑥) < ∞ cosh(𝑥 + 𝑖𝑦) = cosh 𝑥 cos 𝑦 + 𝑖 sinh 𝑥 sin 𝑦 #> cosh( 1 ); ans = 1.5430806 #> cosh( 2i ); ans = -0.416147 + 0! #> cosh( [ 1, 2i ] ); ans = [ 1.54308 -0.416147 ] #> plot.x( -5,5 ) ( cosh(x) ); ---------------------------------------------------------------------Cotangent Function double cot(double x) complex cot(complex z) matrix cot(matrix A) ---------------------------------------------------------------------- cot(𝑥) = − 𝜋 < 𝑥 < 0, 2 #> cot( pi/4 ); ans = 1 , tan(𝑥) cot(𝑥 + 𝜋) = cot(𝑥) 𝜋 0<𝑥< , 2 1 #> cot( 2i ); ans = 0 - 1.03731! 60 − ∞ < cot(𝑥) < ∞ [100] 300 Reference, Tutorial by www.msharpmath.com #> cot( [ pi/4, 2i ] ); ans = [ 1 0 - i 1.03731 ] #> plot.x( -pi/2+0.01,-0.01 )( cot(x) ); #> plot+.x( 0.01,pi/2-0.01 )( cot(x) ); ---------------------------------------------------------------------Cotangent Function in Degree double cotd(double x) complex cotd(complex z) matrix cotd(matrix A) ---------------------------------------------------------------------- −90 < 𝑥 < 0, 𝜋𝑥 cotd(𝑥) = cot ( ) 180 0 < 𝑥 < 90, − ∞ < cotd(𝑥) < ∞ #> cotd( 45 ); ans = 1 #> cotd( 2.todeg! ); ans = 0 - 1.03731! #> cotd( [ 45, 2.todeg! ] ); ans = [ 1 0 - i 1.03731 #> plot.x( -89,-1 )( cotd(x) ); 61 ] [100] 300 Reference, Tutorial by www.msharpmath.com #> plot+.x( 1,89 )( cotd(x) ); ---------------------------------------------------------------------Hyperbolic Cotangent Function double coth(double x) complex coth(complex z) matrix coth(matrix A) ---------------------------------------------------------------------- 1 tanh(𝑥) coth(𝑥) < −1, coth(𝑥) = −∞ < 𝑥 < ∞, #> coth( 1 ); ans = 1 < coth(𝑥) 1.3130353 #> coth( 2i ); ans = 0 + 0.457658! #> coth( [ 1, 2i ] ); ans = [ 1.31304 0 + i 0.457658 ] #> plot.x( -5,-0.1 )( coth(x) ); #> plot+.x( 0.1,5 )( coth(x) ); ---------------------------------------------------------------------double .cross(matrix A, matrix B) ---------------------------------------------------------------------- cross product 𝐢 𝐣 𝐤 |A1 A2 A3 | = 𝐢(A2 B3 − A3 B2 ) + 𝐣(A3 B1 − A1 B3 ) + 𝐤(A1 B2 − A2 B1 ) B1 B2 B3 #> .cross( [2,1,5], [-1,2,4] ) ; ans = [ -6 -13 62 5 ] [100] 300 Reference, #> .cross( [2,1], [-1,2] ) ; ans = [ 0 Tutorial by www.msharpmath.com -0 5 ] ---------------------------------------------------------------------Cosecant Function double csc(double x) complex csc(complex z) matrix csc(matrix A) ---------------------------------------------------------------------- 1 , csc(𝑥 + 2𝜋) = csc(𝑥) sin(𝑥) −∞ < 𝑥 < ∞, csc(𝑥) ≤ −1, 1 ≤ csc(𝑥) csc(𝑥) = #> csc( pi/6 ); ans = 2 #> csc( 2i ); ans = 0 - 0.275721! #> csc( [ pi/6, 2i ] ); ans = [ 2 0 - i 0.275721 ] #> plot.x( -pi/2+0.01, -0.01 )( csc(x) ); #> plot+.x( 0.01, pi/2-0.01 )( csc(x) ); ---------------------------------------------------------------------Cosecant Function in Degree double cscd(double x) complex cscd(complex z) matrix cscd(matrix A) ---------------------------------------------------------------------- 𝜋𝑥 cscd(𝑥) = csc ( ) , cscd(𝑥 + 360) = cscd(𝑥) 180 −∞ < 𝑥 < ∞, cscd(𝑥) ≤ −1, 1 ≤ cscd(𝑥) 63 [100] 300 Reference, #> cscd( 30 ); ans = Tutorial by www.msharpmath.com 2 #> cscd( 2.todeg! ); ans = 0 - 0.275721! #> cscd( [ 30, 2.todeg! ] ); ans = [ 2 0 - i 0.275721 #> plot.x( -90+1, -1 )( cscd(x) ); #> plot+.x( 1, 90-1 )( cscd(x) ); ---------------------------------------------------------------------Hyperbolic Cosecant Function double csch(double x) complex csch(complex z) matrix csch(matrix A) ---------------------------------------------------------------------- 1 sinh(𝑥) − ∞ < csch(𝑥) < ∞ csch(𝑥) = −∞ < 𝑥 < ∞, #> csch( 0.5 ); ans = 1.9190348 #> csch( 2i ); ans = -0 - 1.09975! #> csch( [ 0.5, 2i ] ); ans = [ 1.91903 -0 - i 1.09975 64 ] ] [100] 300 Reference, Tutorial by www.msharpmath.com #> plot.x( -5,-0.1)( csch(x) ); #> plot+.x( 0.1,5 )( csch(x) ); ---------------------------------------------------------------------matrix A.cumprod // column-first operation ---------------------------------------------------------------------- cumulative product along each column #> A = [ 1,3,2; 5,4,3; 6,7,4 ]; A = [ 1 3 [ 5 4 [ 6 7 2 ] 3 ] 4 ] #> A.cumprod; // multi-rows (column-first) ans = [ 1 3 2 ] [ 5 12 6 ] [ 30 84 24 ] #> A().cumprod; ans = [ // a single row 1 5 30 90 360 2520 5040 15120 ---------------------------------------------------------------------matrix A.cumsum // column-first operation ---------------------------------------------------------------------A~ = A.cumsum, cumulative sum #> A = [ 1,3,2; 5,4,3; 6,7,4 ]; A = [ 1 3 [ 5 4 [ 6 7 2 ] 3 ] 4 ] #> A~; // multi-rows (column-first), equivalent to ans = 65 A~ = A.cumsum 60480 ] [100] 300 [ [ [ Reference, 1 6 12 3 7 14 #> A()~; // a single row ans = [ 1 6 12 #> A~~; Tutorial by www.msharpmath.com // the same as 15 2 ] 5 ] 9 ] 19 26 28 31 35 ] A.cumsum.cumsum ans = [ [ [ 1 7 19 3 10 24 2 ] 7 ] 16 ] ---------------------------------------------------------------------poly p.cumsum ---------------------------------------------------------------------- ∑ p = p. ~ = p. cumsum ,cumulative sum p. cumsum = 𝑝. ~ = ∑ 𝑝(𝑥) = 𝑝(1) + 𝑝(2) + 𝑝(3) + ⋯ + 𝑝(𝑛) #> p = .[1].up(3); p = poly( 0 0 = x^3 // x^3 0 1 ) #> p.~; // Σ 𝑘 3 = (1/4)𝑛2 (𝑛 + 1)2 ans = poly( 0 0 0.25 0.5 0.25 ) = 0.25x^4 +0.5x^3 +0.25x^2 ---------------------------------------------------------------------matrix cumtrapz(matrix x,matrix y) ---------------------------------------------------------------------area by the trapezodial rule 1 [ (𝑥2 − 𝑥1 )(𝑦1 + 𝑦2 ) + (𝑥3 − 𝑥2 )(𝑦2 + 𝑦3 ) + 2 ⋯ + (𝑥𝑛 − 𝑥𝑛−1 )(𝑦𝑛−1 + 𝑦𝑛 )] #> x = 0:0.25:1;; y=1 ./(1+x.^2);; cumtrapz(x,y); ans = [ 0 0.242647 0.460294 #> int.x[5](0,1) ( 1/(1+x^2) ).poly(1).return; ans = 66 0.640294 0.782794 ] [100] 300 [ [ [ [ [ 0 0.242647 0.460294 0.640294 0.782794 Reference, Tutorial by www.msharpmath.com ] ] ] ] ] ---------------------------------------------------------------------complex z.cyl ---------------------------------------------------------------------- convert a complex number from cylindrical to rectangular coordinates #> z = 1 + pi!/6; z = 1 + 0.523599! #> z.cyl; ans = 0.866025 + 0.5! ---------------------------------------------------------------------vertex v.cyl ---------------------------------------------------------------------- read in cylindrical coordinate #> < 1,1,1 > .cyl ; // r = sqrt(x^2+y^2) ans = < 1.414 0.7854 1 > ---------------------------------------------------------------------csys csys.cyl ---------------------------------------------------------------------- a cylindrical coordinate system #> cs1 = csys.cyl; cs1 = 'cyl' local csys org = < 0 dir = [ 1 [ 0 [ 0 #> < 1,sqrt(3) >.cs1; ans = < 2 0 0 > 0 ] 0 ] 1 ] 0 1 0 1.047 0 > ---------------------------------------------------------------------double .decimal (double x) 67 [100] 300 Reference, Tutorial by www.msharpmath.com matrix .decimal (matrix A) ---------------------------------------------------------------------- 𝑥 = 𝑥. round + 𝑥. decimal #> .decimal(pi); ans = 0.14159265 #> .decimal( [pi, _e] ); ans = [ 0.141593 -0.281718 ] ---------------------------------------------------------------------double x.decimal matrix A.decimal poly p.decimal ---------------------------------------------------------------------- deviation from the closest integer Note that x = x.round + x.decimal For a matrix and a polynomial, element-by-element operation is applied. #> pi.decimal; ans = 0.14159265 #> (-pi).decimal; ans = -0.14159265 ---------------------------------------------------------------------double z.deg // both read/write ---------------------------------------------------------------------z.r |z| = sqrt(x*x+y*y) z.t arg = atan(y/x) z.deg deg = atan(y/x)*180/pi #> z = 3+4!; z = 3 + 4! #> z.deg; ans = // read 53.130102 #> z.deg = 90; ans = 90 z; // write z = 3.06152e-016 + 5! 68 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------void csys.deg void csys.rad ---------------------------------------------------------------------angles in the degree system #> csys.deg; < 1, 45, 0, cyl > ; ans = < 0.7071 0.7071 0 > In the above, 360 degree system is activated by "csys.deg". Therefore, writing a vertex in a cylindrical coordinate system by < 𝑟, 𝜃, 𝑧, cyl > interprets 𝜃 to be an angle in degree system. Thus we have 𝜋 1 = 4 √2 𝜋 1 𝑦 = 𝑟 sin 𝜃 = (1) sin 45O = (1) sin = 4 √2 𝑥 = 𝑟 cos 𝜃 = (1) cos 45O = (1) cos #> csys.rad; < 1, pi/4, ans = < 0.7071 0, cyl > ; 0.7071 0 > The default system is the radian coordinate system. [ D ] ---------------------------------------------------------------------vertex .del(double f(x,y,z)) (double a, double b, double c) dot-only ---------------------------------------------------------------------- For a given point (𝑎, 𝑏, 𝑐), the gradient ∇𝑓 is numerically calculated from the following syntax 𝜕𝑓 𝜕𝑓 𝜕𝑓 +𝐣 + 𝐤 𝜕𝑥 𝜕𝑦 𝜕𝑧 𝜕𝑓 1 𝜕𝑓 𝜕𝑓 ∇𝑓 = 𝐞𝑟 + 𝐞𝜃 + 𝐞𝑧 𝜕𝑟 𝑟 𝜕𝜃 𝜕𝑧 𝜕𝑓 1 𝜕𝑓 1 𝜕𝑓 ∇𝑓 = 𝐞𝑟 + 𝐞𝜑 + 𝐞𝜃 𝜕𝑟 𝑟 𝜕𝜑 𝑟 sin 𝜑 𝜕𝜃 ∇𝑓 = 𝐢 69 [100] 300 Reference, Tutorial by www.msharpmath.com .del ( f(x,y,z) ) (a,b,c) .del ( f(x,y,z) ) (a,b,c).cyl .del ( f(x,y,z) ) (a,b,c).sph #> .del( x^3*y^2*z #> .del( x^3*y^2*z )( 2,3,4 ) )( 3,pi/3,4 ) #> .del( x^3*y^2*z )( 3,pi/4,pi/3 ) .sph; ans = < ans = < ans = < 432 118.4 17.44 ; .cyl; 192 75.4 14.8 72 > 29.61 > 7.851 > ---------------------------------------------------------------------double .del*(u,v,w) (double a, double b, double c) dot-only ---------------------------------------------------------------------- The divergence of a vector field 𝐮 = (𝑢, 𝑣, 𝑤) is defined as 𝜕𝑢 𝜕𝑣 𝜕𝑤 + + 𝜕𝑥 𝜕𝑦 𝜕𝑧 1 𝜕 1 𝜕𝑣 𝜕𝑤 ( 𝑟𝑢) + ∇⋅𝐮= + 𝑟 𝜕𝑟 𝑟 𝜕𝜃 𝜕𝑧 1 𝜕 1 𝜕 1 𝜕𝑤 2 ( 𝑟 𝑢) + ( sin 𝜑 𝑣) + ∇⋅𝐮 = 2 𝑟 𝜕𝑟 𝑟 sin 𝜑 𝜕𝜑 𝑟 sin 𝜑 𝜕𝜃 ∇⋅𝐮= .del * ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c) .del * ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c).cyl .del * ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c).sph #> .del*( x*y^2*z, x*x*y, x*z*z ) (2,3,4); #> .del * ( x*y^2*z, x*x*y, x*z*z ) (3,pi/3,4).cyl; #> .del * ( x*y^2*z, x*x*y, x*z*z ) (3,pi/4,pi/3).sph; ans = 56.0000800 ans = ans = 35.7731456 10.2560429 ---------------------------------------------------------------------vertex .del^(u,v,w) (double a, double b, double c) dot-only ---------------------------------------------------------------------- The curl of a vector field 𝐮 = (𝑢, 𝑣, 𝑤) is defined as 70 [100] 300 Reference, Tutorial by www.msharpmath.com 𝐢 𝐣 𝜕 𝜕 ∇×𝐮=| 𝜕𝑥 𝜕𝑦 𝑢 𝑣 𝐞𝑟 𝑟𝐞𝜃 1 𝜕 𝜕 ∇×𝐮= | 𝑟 𝜕𝑟 𝜕𝜃 𝑢 𝑟𝑣 𝐞𝑟 𝑟𝐞𝜑 1 𝜕 𝜕 | ∇×𝐮= 2 𝑟 sin 𝜑 𝜕𝑟 𝜕𝜑 𝑢 𝑟𝑣 𝐤 𝜕 | 𝜕𝑧 𝑤 𝐞𝑧 𝜕 | 𝜕𝑧 𝑤 𝑟sin𝜑𝐞𝜃 𝜕 | 𝜕𝜃 𝑟𝑤 sin𝜑 .del ^ ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c) .del ^ ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c).cyl .del ^ ( u(x,y,z),v(x,y,z),w(x,y,z) ) (a,b,c).sph #> .del ^ ( x*y^2*z, x*x*y, x*z*z ) (2,3,4); #> .del ^ ( x*y^2*z, x*x*y, x*z*z ) (3,pi/3,4).cyl; #> .del ^ ( x*y^2*z, x*x*y, x*z*z ) (3,pi/4,pi/3).sph; ans = < ans = < ans = < 0 0 1.097 2 -12.71 -1.321 -36 > 1.047 > 5.424 > ---------------------------------------------------------------------double .del2(double f(x,y,z)) (double a, double b, double c) dot-only ---------------------------------------------------------------------- The Laplacian of a function 𝑓(𝑥, 𝑦, 𝑧) is defined as 𝜕2𝑓 𝜕2𝑓 𝜕2𝑓 + + 𝜕𝑥 2 𝜕𝑦 2 𝜕𝑧 2 1 𝜕 𝜕𝑓 1 𝜕2𝑓 𝜕2𝑓 ∇2 𝑓 = (𝑟 )+ 2 + 𝑟 𝜕𝑟 𝜕𝑟 𝑟 𝜕𝜃 2 𝜕𝑧 2 1 𝜕 𝜕𝑓 1 𝜕 𝜕𝑓 1 𝜕2𝑓 ∇2 𝑓 = 2 ( 𝑟2 ) + 2 ( sin 𝜑 )+ 2 2 𝑟 𝜕𝑟 𝜕𝑟 𝑟 sin 𝜑 𝜕𝜑 𝜕𝜑 𝑟 sin 𝜑 𝜕𝜃 2 ∇2 𝑓 = .del2 ( f(x,y,z) ) (a,b,c) .del2 ( f(x,y,z) ) (a,b,c).cyl .del2 ( f(x,y,z) ) (a,b,c).sph 71 [100] 300 Reference, Tutorial by www.msharpmath.com #> .del2 ( x^2*y^2*z^3 ) (2,3,4); #> .del2 ( x^2*y^2*z^3 ) (3,pi/3,4).cyl; #> .del2 ( x^2*y^2*z^3 ) (3,pi/4,pi/3).sph; ans = 2528.0051988 ans = ans = 645.6077851 16.1024906 ---------------------------------------------------------------------matrix .del[] .x1.x2. ... .xn(f)(double c1,c2,...,cn) dot-only ---------------------------------------------------------------------gradient ∇𝑓 = 𝐞1 .del[] .x1.x2 𝜕𝑓 𝜕𝑓 𝜕𝑓 + 𝐞2 + ⋯ + 𝐞𝑛 𝜕𝑥1 𝜕𝑥2 𝜕𝑥𝑛 .xn ( f(x1,x2, …,xn) ) (c1,c2, …, cn) ∇𝑓 = 𝐞1 3𝑥12 𝑥22 𝑥3 𝑥42 𝑓 = 𝑥13 𝑥22 𝑥3 𝑥42 + 𝐞2 𝑥13 2𝑥2 𝑥3 𝑥42 + 𝐞3 𝑥13 𝑥22 𝑥42 + 𝐞4 𝑥13 𝑥22 𝑥3 2𝑥4 #> .del[] .x1.x2.x3.x4 ( x1^3*x2^2*x3*x4^2 ) (2,3,4,5); ans = [ 10800 4800 1800 2880 ] ---------------------------------------------------------------------matrix .del[][].x1.x2. ... .xn(f1,f2,...,fm)(c1,c2,...,cn) dot-only ---------------------------------------------------------------------jacobian 𝜕𝑓1 𝜕𝑥 | 1 𝜕𝑓2 𝜕𝑓𝑖 𝐉 = [ ] = 𝜕𝑥1 𝜕𝑥𝑗 ⋮ | 𝜕𝑓 𝑚 𝜕𝑥1 .del[][] .x1.x2 𝜕𝑓1 𝜕𝑥2 𝜕𝑓2 𝜕𝑥2 ⋮ 𝜕𝑓𝑚 𝜕𝑥2 𝜕𝑓1 𝜕𝑥𝑛 | 𝜕𝑓2 ⋯ 𝜕𝑥𝑛 ⋱ ⋮ 𝜕𝑓𝑚 | ⋯ 𝜕𝑥𝑛 ⋯ .xn ( f1,f2,f3, … , fm ) (c1,c2, …, cn) The operator ‘.del[][]’ plays a simple role of differentiation only, as similar to 72 [100] 300 Reference, Tutorial by www.msharpmath.com the operator ‘.del[]’. Consider a vector function of 𝐟 = (𝑥 2 𝑦, 𝑥 + 3𝑦, 𝑒 𝑥 (𝑦 + 1)), and find a Jacobian matrix at point 𝐫 = (2,3). This can be solved by #> .del[][] .x.y ( x*x*y, x+3*y, exp(x)*(y+1) )(2,3); ans = [ 12 4 ] [ [ 1 29.557 3 ] 7.3891 ] Therefore, we get a Jacobian matrix at point r (2,3) 𝐉=[ 12 4 𝜕𝑓𝑖 ]=[ 1 3 ] 𝜕𝑥𝑗 29.557 7.3891 ---------------------------------------------------------------------poly p.denom(double nmax=10000) ---------------------------------------------------------------------returns an integer denominator up to an integer "nmax" 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 nmax = 10000 is used when a value of nmax is not specified. The least common multiplier (lcm) is found up to nmax. #> poly( 1/2, 1/3, 1/4, 1/5 ).denom; ans = 60 #> poly( 1/2, 1/3, 1/4, 1/5 ).ratio; ans = (1/60) * poly( 30 20 15 12 ) ---------------------------------------------------------------------double A.det ---------------------------------------------------------------------- determinant of a matrix #> A = [ 3,4; 1,2 ]; A = [ 3 4 ] 73 [100] 300 [ Reference, 1 Tutorial by www.msharpmath.com 2 ] #> A.det; ans = 2 ---------------------------------------------------------------------double x.dfact matrix A.dfact poly p.dfact ---------------------------------------------------------------------- double factorial of 𝑥 is defined to be 𝑥(𝑥 − 2)(𝑥 − 4)(𝑥 − 6) ⋯ For a matrix and a polynomial, element-by-element dfact is applied. if 𝑥 is not an integer, the Gamma function is used when a factor becomes less than unity. #> 8.dfact; // 8*6*4*2 ans = 384 #> 8.5 .dfact; ans = 550.84547 ---------------------------------------------------------------------matrix .diag(matrix A, double k=0) ---------------------------------------------------------------------#> .diag( [1,2,3,4] ); // [1,2,3,4].diagm(0), diagonal vector ans = [ 1 0 0 0 ] [ 0 2 0 0 ] [ 0 0 3 0 ] [ 0 0 0 #> .diag( [1,2], 1 ); ans = [ 0 1 0 ] [ [ 0 0 0 0 4 ] // [1,2].diagm(1) 2 ] 0 ] #> .diag( [3,4], -2 ); ans = // [3,4].diagm(-2) [ 0 ] 0 0 0 74 [100] 300 [ [ [ 0 3 0 0 0 4 Reference, 0 0 0 Tutorial by www.msharpmath.com 0 ] 0 ] 0 ] ---------------------------------------------------------------------matrix W.diag(double k) // both read/write ---------------------------------------------------------------------diagonal vector #> A = (1:12)._4; A = [ 1 [ 2 [ 3 #> A.diag(0); ans = [ 1 [ 5 [ 9 #> A.diag(1); ans = [ 4 [ 8 [ 12 #> A.diag(-1); ans = [ 2 [ 6 4 5 6 7 8 9 10 ] 11 ] 12 ] ] ] ] ] ] ] ] ] #> A.diag(0) = 0; A = [ 0 4 [ 2 0 [ 3 6 // diagonal vector is modified 7 8 0 10 ] 11 ] 12 ] ---------------------------------------------------------------------matrix .diagcat( ... ) matrix .blkdiag( ... ) ---------------------------------------------------------------------diagonal concatenation, the same as blkdiag in matlab %> block-diagonal matrix #> .diagcat( 5,6,7,8 ); 75 [100] 300 Reference, Tutorial by www.msharpmath.com ans = [ [ [ [ 5 0 0 0 0 6 0 0 0 0 7 0 #> .diagcat ( [1,2], 5, [11,13; 12,14] ); ans = [ 1 2 [ 0 0 [ 0 0 [ 0 0 #> .diagcat ( 2*.I(2), .ones(2) ); ans = [ 2 [ [ [ 0 0 0 0 ] 0 ] 0 ] 8 ] 0 5 0 0 0 11 0 ] 0 ] 13 ] 0 12 14 ] 0 0 0 ] 2 0 0 0 1 1 0 ] 1 ] 1 ] ---------------------------------------------------------------------matrix A.diagm(double k) ---------------------------------------------------------------------.diag(A,k) for vectors, otherwise all-zero off-diagonals #> (1:4).diagm(0); ans = [ 1 0 [ 0 2 [ 0 0 [ 0 0 0 0 3 0 0 0 0 4 #> (1:4).diagm(-1); ans = [ 0 0 [ 1 0 [ 0 2 [ 0 0 [ 0 0 0 0 0 3 0 0 0 0 0 4 #> A = (1:12)._4; A = [ 1 ] ] ] ] 0 0 0 0 0 ] ] ] ] ] 4 7 76 10 ] [100] 300 [ [ Reference, Tutorial by www.msharpmath.com 2 3 5 6 8 9 #> A.diagm(0); ans = [ 1 [ 0 [ 0 0 5 0 0 ] 0 ] 9 ] 11 ] 12 ] ---------------------------------------------------------------------matrix A.diff // column-first operation ---------------------------------------------------------------------A` = A.diff, finite-difference // one-row case, [ 2-1, 5-2, 7-5 ] and reduce 1 column, column-first #> A` ; ans = [ 1 3 2 ] // multi-rows case, [ 3-1, 0-2, 4-5, 9-7 ; ... ] // and reduce 1 row, column-first #> B` ; ans = [ 2 [ -1 -2 4 #> B`` ; ans = [ -1 6 2 ] 5 ] // becomes 1-row after 2 operations -3 6 7 3 ] #> B``` ; ans = [ // differenced on row elements after 3 operations 9 1 -4 ] ---------------------------------------------------------------------poly p.diff ---------------------------------------------------------------------- Δ𝑝 = p.′ = p` = p. diff, finite difference p. diff = p(x) − p(x − 1) #> p = .[1].up(3); p = poly( 0 0 = x^3 // x^3 0 1 ) 77 [100] 300 Reference, Tutorial by www.msharpmath.com #> p`; // x^3 – (x-1)^3 = x^3 – (x^3 -3x^2 +3x -1) ans = poly( 1 -3 3 ) = 3x^2 -3x +1 ---------------------------------------------------------------------matrix cs.dir ---------------------------------------------------------------------#> cs1 = csys.rec ; cs1 = 'rec' local csys org = < 0 0 0 > dir = [ 1 0 0 ] [ 0 1 0 ] [ 0 0 1 ] #> cs1.org ; ans = < // origin of a coordinate system 0 0 0 > #> cs1.dir ; ans = [ [ [ // direction cosines of a coordinate system 1 0 0 0 1 0 0 ] 0 ] 1 ] ---------------------------------------------------------------------double .div(double a, double b) dot-only matrix .div(double a, matrix b) matrix .div(matrix a, double b) matrix .div(matrix a, matrix b) ---------------------------------------------------------------------- quotient 𝑞 in 𝑎 = 𝑏𝑞 + 𝑟 #> .div(13,5); ans = 2 ---------------------------------------------------------------------matrix .divisor(double n) ---------------------------------------------------------------------- integer divisors of an integer 𝑛 #> .divisor(10); ans = [ 1 2 5 10 ] 78 [100] 300 Reference, Tutorial by www.msharpmath.com "complete number search"; // 6 -> [ 1,2,3,6 ] for.n(2,100000) { if( .divisor(n).sum1 == 2*n ) n; } n = 6 n = 28 n = 496 n = 8128 1+2+3 = 6 !!! ---------------------------------------------------------------------double .dot(matrix A, matrix B) dot-only ---------------------------------------------------------------------A ** B = .dot(A,B) dot product of two matrices. dimension allowed. columnwise counting and no matching in #> A = [ 1,2,3,4 ]; B = [ 5,6,7 ]; A = [ 1 2 3 B = [ 5 6 7 ] 4 ] #> .dot(A,B); // (1)(5) + (2)(6) + (3)(7) ans = 38 #> A**B; ans = 38 ---------------------------------------------------------------------poly A.dpoly ---------------------------------------------------------------------- convert a matrix into a polynomial by descending order #> A = (1:8)._3; A = [ 1 4 [ 2 5 [ 3 6 7 ] 8 ] 0 ] #> A.dpoly; ans = poly( 0 8 7 6 5 4 3 2 1 ) = x^8 +2x^7 +3x^6 +4x^5 +5x^4 +6x^3 +7x^2 +8x 79 [100] 300 Reference, Tutorial by www.msharpmath.com [ E ] ---------------------------------------------------------------------double .E1/expint (double x) matrix .E1/expint (matrix A) ---------------------------------------------------------------------∞ −𝑡 𝐸1 (𝑧) = ∫ 𝑧 ∞ −𝑧𝑡 𝑒 𝑑𝑡 = ∫ 𝑡 1 𝑒 𝑡 𝑑𝑡 = −𝛾 − ln 𝑧 + 𝑧 − 𝑧2 𝑧3 𝑧𝑛 + + ⋯ + (−1)𝑛+1 + … 2! 2 3! 3 𝑛! 𝑛 #> plot.x(0.2,1.6) ( 0, .E1(x) ); ---------------------------------------------------------------------double .Ei (double x) // dot only matrix .Ei (matrix A) ---------------------------------------------------------------------𝑥 𝑒𝑡 𝑥2 𝑥3 𝑥𝑛 𝑑𝑡 = 𝛾 + ln 𝑥 + 𝑥 + + + ⋯+ + … 2! 2 3! 3 𝑛! 𝑛 −∞ 𝑡 𝐸𝑖(𝑥 > 0) = ∫ #> plot.x(0.2,1.6) ( 0, .Ei(x) ); ---------------------------------------------------------------------matrix .e_k(double n), double k dot-only 80 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------- 𝑘-th unit vector in an 𝑛-dimensional space 𝒆1 = (1,0,0, … ,0) 𝒆2 = (0,1,0, … ,0) ⋮ 𝒆𝑛 = (0,0,0, … ,1) #> k=2; .e_k(5); k = 2 ans = [ 0 #> for.k(1,5) .e_k(5); ans = [ 1 0 ans = [ 0 1 ans = [ 0 0 ans = [ 0 0 ans = [ 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 ] ] ] ] ] ] ---------------------------------------------------------------------matrix A.eig (matrix X, matrix D) = A.eig // tuple ---------------------------------------------------------------------- return eigenvalues With two l-values, modal matrix (eigenvectors) and spectral matrix (eigenvalues) are obtained. #> A = [ -3,4; 5,-2 ]; A = [ -3 [ 5 #> A.eig; ans = [ [ 4 ] -2 ] 2 ] -7 ] #> (X,D) = A.eig; X = [ 4 [ 5 D = [ 2 -5 ] 5 ] 0 ] 81 [100] 300 [ Reference, 0 Tutorial by www.msharpmath.com -7 ] ---------------------------------------------------------------------double A.eiginvpow(double shift) (matrix lam, matrix x) = A.eiginvpow(double shift) // tuple ---------------------------------------------------------------------- the absolute smallest eigenvalue by inverse power method #> A = [ -3,4; 5,-2 ]; A = [ -3 [ 5 4 ] -2 ] #> A.eiginvpow(0); ans = 1.9999999 #> (lam,x) = A.eiginvpow(0); lam = 1.9999999 x = [ 0.8 ] [ 1 ] User code can be written as follows. (step 1) Set 𝜆(0) = 0, 𝐱 (0) = 1, 𝑘 = 0 (step 2) Solve 𝐀𝐲 (𝑘+1) = 𝐱 (𝑘) to find 𝐲 (𝑘+1) and 𝜆(𝑘+1) 1 (step 3) Modify 𝐱 (𝑘+1) = 𝜆(𝑘+1) 𝐲 (𝑘+1) with assumed eigenvalue 𝜆(𝑘+1) (step 4) Repeat steps 2-3 until convergence. double eiginvpow(matrix A, &x) { // A lam = k = 0 ; x = .ones(A.m,1); while( 1 ) { lamo = lam; y = A \x ; [ ++k, lam = y.maxentry , (x = y / if( lam .-. lamo < 1.e-6 ) break; } return 1 / lam; } #> A = [ -3,4; 5,-2 ] ;; x = [] ;; // #> lam = eiginvpow(A,x); k 1/lamba [ 1 is copied, x is referred, lam )' ] ;; // to display // relative difference declare a matrix x(1) 0.571429 0.75 82 x(2) 1 ] [100] 300 [ [ [ [ [ [ [ [ [ [ 2 3 4 5 6 7 8 9 10 11 Reference, Tutorial by www.msharpmath.com 0.482143 0.505291 0.498504 0.500429 0.499878 0.500035 0.49999 0.500003 0.499999 0.5 0.814815 0.795812 0.8012 0.799657 0.800098 0.799972 0.800008 0.799998 0.800001 0.8 1 1 1 1 1 1 1 1 1 1 ] ] ] ] ] ] ] ] ] ] ---------------------------------------------------------------------poly A.eigpoly ---------------------------------------------------------------------- eigenpolynomial of a matrix A Fadeev-Leverrier method. det(𝐀 − 𝜆𝐈) = 0 → −𝜆 + 𝑐1 𝜆𝑛−1 + ⋯ + 𝑐𝑛−1 𝜆 + 𝑐𝑛 = 0 𝑛 all the coefficients 𝑐𝑖 , 𝑖 = 1,2,3, ⋯ , 𝑛 in the above can be determined recursively from the following relation 𝐁1 = 𝐀, 𝑐1 = trace 𝐁1 1 𝐁2 = 𝐀(𝐁1 − 𝑐1 𝐈), 𝑐2 = 2 trace 𝐁2 ... 1 𝐁𝑖 = 𝐀(𝐁𝑖−1 − 𝑐𝑖−1 𝐈), 𝑐𝑖 = 𝑖 trace 𝐁𝑖 , 𝑖 = 2,3, ⋯ , 𝑛 #> [ 3,2,4; 2,5; 1,-1,2 ] .eigpoly ; ans = poly( 6 23 -10 1 ) = x^3 -10x^2 +23x +6 // unnamed matrix is used By noting that 𝑐𝑛 𝑐𝑛−1 𝑐2 𝑐1 + 𝑛−1 + ⋯ + 2 + − 1 = 0 𝑛 𝜆 𝜆 𝜆 𝜆 user code can be written as poly user_eigpoly(matrix A) { // A is copied n = A.m; c = -poly(1:n+1); // to make c[0] = -1 B = A; 83 [100] 300 Reference, c[1] = B.trace; for.k(2,n) { B = A*(B-c[k-1]*.I(n)) ;; c[k] = B.trace/k ; } return c.rev; Tutorial by www.msharpmath.com // display } #> user_eigpoly( [ 3,2,4; 2,5; 1,-1,2 ] ); user_eigpoly.B = [ -13 -8 -20 ] [ -4 -21 8 ] [ -7 5 -12 ] user_eigpoly.B = [ -6 0 0 ] [ 0 -6 0 ] [ 0 0 -6 ] ans = poly( -6 -23 10 -1 ) = -x^3 +10x^2 -23x -6 ---------------------------------------------------------------------double A.eigpow(shift) (double lam, matrix x) = A.eigpow(shift) // tuple ---------------------------------------------------------------------- the absolute largest eigenvalue by the power method #> A = [-3,6; 2,-2]; A = [ -3 [ 2 #> A.eigpow(0); ans = 6 ] -2 ] // A.eigpow(shift) -6 #> (lam,x) = A.eigpow(0); lam = -6 x = [ 1 ] [ -0.5 ] User code can be written as follows. (step 1) Set 𝜆(0) = 0, 𝐱 (0) = 1, 𝑘 = 0 (step 2) Compute 𝐲 (𝑘+1) = 𝐀𝐱 (𝑘), and search the maximum absolute element 𝑦𝑖 from 𝐲 (𝑘+1) . Then, put 𝜆(𝑘+1) = 𝑦𝑖 . 84 [100] 300 Reference, (step 3) Determine 𝐱 (𝑘+1) = Tutorial by www.msharpmath.com 1 𝐲 (𝑘+1) 𝜆(𝑘+1) (step 4) repeat until convergence double eigpow(matrix A, &x) { // A is copied, x is referred, lam = k = 0 ; x = .ones(A.m,1); while( 1 ) { lamo = lam; y = A * x ; [ ++k, lam = y.maxentry , (x = y / lam )' ] ;; // to display if( lam ~= lamo ) break; // 6 digits identical } return lam; } #> A = [ -3,6; 2,-2 ];; x = [];; // declare a matrix x #> eigpow(A,x); k [ [ [ [ [ [ [ [ [ [ 1 2 3 4 5 6 7 8 9 10 lam x(1) x(2) 1 1 1 1 1 1 1 1 1 1 0 -0.666667 -0.47619 -0.504065 -0.499325 -0.500113 -0.499981 -0.500003 -0.499999 -0.5 3 -3 -7 -5.85714 -6.02439 -5.99595 -6.00068 -5.99989 -6.00002 -6 ] ] ] ] ] ] ] ] ] ] ---------------------------------------------------------------------matrix A.eigvec(lam) ---------------------------------------------------------------------- eigenvecter corresponding to an eigenvalue lam #> A = [ 3,2,4; 2,5; 1,-1,2 ];; #> A.eigvec(6); ans = [ 4 ] [ 8 ] [ -1 ] // lam = 6 #> A = [ -2,0,1; 1,-3,1; 0,0,-3 ]; #> A.eigvec(-3); ans = 85 [100] 300 [ [ [ Reference, 0 1 0 Tutorial by www.msharpmath.com 1 ] 0 ] -1 ] #> A = [ 0,1; 3,0,1; 2 ] ; #> A.eigvec(-1); ans = [ 0.5 ] [ -0.5 ] [ -1 ] #> A = [ 1, -2; 4, -3 ];; #> A .eigvec( -1+2i ); // case of complex eigenvalue ans = [ -0.5 - i 0.5 ] [ -1 ] ---------------------------------------------------------------------double A.end ---------------------------------------------------------------------- real part of A(end,end) #> A = (1:12)._4; A = [ 1 [ 2 [ 3 #> A.end ans = 4 5 6 7 8 9 10 ] 11 ] 12 ] 12 ---------------------------------------------------------------------matrix W.entry(matrix ind) // equivalent to W.(matrix) ---------------------------------------------------------------------- entry of elements, #> A = (1:12)._4*10; A = [ [ [ 10 20 30 40 50 60 70 80 90 100 ] 110 ] 120 ] 86 [100] 300 #> A.( [2,6,10] ); ans = [ Reference, // A.entry( [2,6,10] ), argument is matrix only 20 60 100 ] #> A.( [2,6,10] ) = 0; A = [ 10 40 70 [ [ 0 30 50 0 80 90 // write 0 ] 110 ] 120 ] #> B = [ -1, 2, 0, -3, 4 ] ; B = [ -1 2 0 #> B < 0 ; ans = [ Tutorial by www.msharpmath.com -3 4 ] // true(1) or false(0) 1 0 0 1 0 ] #> B .( B < 0 ) = 10 ; // modify B if b_ij < 0 B = [ 10 2 0 10 4 ] ---------------------------------------------------------------------Error Function double erf (double x) matrix erf (matrix x), for real-part only ---------------------------------------------------------------------- erf(𝑥) = #> erf(0.5); ans = 2 √𝜋 𝑥 2 ∫ 𝑒 −𝑧 𝑑𝑧 0 0.52049988 #> erf( [0.5,0.6] ); ans = [ 0.5205 0.603856 ] ---------------------------------------------------------------------Complementary Error Function double erfc (double x) matrix erfc (matrix x), for real-part only ---------------------------------------------------------------------- erfc(𝑥) = 1 − erf(𝑥) = 1 − #> erfc(1); ans = 0.15729921 87 2 √𝜋 𝑥 2 ∫ 𝑒 −𝑧 𝑑𝑧 0 [100] 300 Reference, #> erfc( [1,2] ); ans = [ 0.157299 Tutorial by www.msharpmath.com 0.00467773 ] ---------------------------------------------------------------------poly p.even ---------------------------------------------------------------------- Even power only for a polynomial 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. even = 𝑎0 + 𝑎2 𝑥 2 + 𝑎4 𝑥 4 + 𝑎6 𝑥 6 + ⋯ #> p = poly( 1,2,3,4,5,6,7 ) ; p = poly( 1 2 3 4 5 6 7 ) = 7x^6 +6x^5 +5x^4 +4x^3 +3x^2 +2x +1 #> p .even; ans = poly( 1 0 3 0 5 = 7x^6 +5x^4 +3x^2 +1 0 7 ) ---------------------------------------------------------------------Exponential Function double exp (double x) complex exp (complex z) matrix exp (matrix A) ---------------------------------------------------------------------- 𝑒𝑥 = 1 + 𝑥 + #> exp(1); ans = 𝑥2 𝑥3 𝑥𝑛 + + ⋯+ + … 2! 3! 𝑛! 2.7182818 #> exp(1+1i); ans = 1.46869 + 2.28736! #> exp( [1, 1+1i] ); ans = [ 2.71828 1.46869 + i 2.28736 ] ---------------------------------------------------------------------double expint/.E1 (double x) matrix expint/.E1 (matrix A) ---------------------------------------------------------------------- 88 [100] 300 Reference, ∞ −𝑡 𝐸1 (𝑧) = ∫ 𝑧 𝑒 𝑡 Tutorial by www.msharpmath.com ∞ −𝑧𝑡 𝑑𝑡 = ∫ 𝑒 𝑡 1 𝑑𝑡 = −𝛾 − ln 𝑧 + 𝑧 − #> .E1(3) ans = 𝑧2 𝑧3 𝑧𝑛 + + ⋯ + (−1)𝑛+1 + … 2! 2 3! 3 𝑛! 𝑛 0.013048381 #> .E1( [3,4] ); ans = [ 0.0130484 0.00377935 ] ---------------------------------------------------------------------Matrix Exponential Function matrix expm (matrix A) ---------------------------------------------------------------------- 𝑒𝐀 = 𝐈 + 𝐀 + #> A = [1,0.3; 0.2,1]; A = [ 1 [ 𝐀2 𝐀3 𝐀𝑛 + +⋯+ + … 2! 3! 𝑛! E = expm(A); 0.3 ] 0.2 1 ] 2.80024 0.549109 0.823664 ] 2.80024 ] E = [ [ Especially, expm(A) can be approximately computed by #> p = poly(0:10).fact .inv ;; #> p[A]; ans = [ 2.80024 0.823664 ] [ 0.549109 2.80024 ] ---------------------------------------------------------------------double expm1 (double x) matrix expm1(matrix A) ---------------------------------------------------------------------- 𝑒𝑥 − 1 = 𝑥 + 𝑥2 𝑥3 𝑥𝑛 + + ⋯+ + … 2! 3! 𝑛! 89 [100] 300 Reference, Tutorial by www.msharpmath.com #> expm1(1.e-30); exp(1.e-30)-1; ans = 1e-030 ans = 0 ---------------------------------------------------------------------matrix A.ext(double m, double n, double s) ---------------------------------------------------------------------- A 𝑝 × 𝑞 matrix becomes a (𝑝 + 𝑚) × (𝑞 + 𝑛) matrix by extending. Elements of new entries are assigned by 𝑠 #> matrix.format("%6g"); #> A = (1:16)._4 ; A = [ [ [ [ 1 2 3 4 5 6 7 8 9 10 11 12 #> A.ext(3,4, 0.); 13 14 15 16 ] ] ] ] // A.extend ans = [ 1 [ 2 [ 3 [ 4 5 6 7 8 9 10 11 12 13 14 15 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [ [ [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ] 0 ] 0 ] 0 0 0 ] ] ] ] [ F ] ---------------------------------------------------------------------Factorial Function double .fact (double x) matrix .fact (matrix A) ---------------------------------------------------------------------#> .fact(5); ans = 120 #> .fact( [5,pi] ); 90 [100] 300 ans = [ Reference, 120 Tutorial by www.msharpmath.com 7.18808 ] ---------------------------------------------------------------------double x.fact matrix A.fact poly p.fact ---------------------------------------------------------------------- factorial of 𝑥 , 𝑥(𝑥 − 1)(𝑥 − 2)(𝑥 − 3) ⋯ #> 5.fact; ans = 120 #> (-0.5).fact; ans = 1.7724537 #> sqrt(pi); ans = // (−1/2)! = √𝜋 1.7724539 For a matrix and a polynomial, element-by-element approach is applied. ---------------------------------------------------------------------matrix factor (double x) ---------------------------------------------------------------------- factorization of an integer #> .factor(2520); // 2520 = (2)(2)(2)(3)(3)(5)(7) ans = [ 2 2 2 3 3 5 7 ] ---------------------------------------------------------------------matrix A.false ---------------------------------------------------------------------- index for zero elements #> A = [ 2,0,3; 0,-1,0; 0,2,4 ]; A = [ 2 0 [ 0 -1 [ 0 2 #> A.false; ans = [ 2 3 4 3 ] 0 ] 4 ] 8 ] 91 [100] 300 #> A.true; ans = [ 1 Reference, 5 6 Tutorial by www.msharpmath.com 7 9 ] ---------------------------------------------------------------------double fix (double x) matrix fix (matrix A) ---------------------------------------------------------------------- integer near zero #> .fix(pi); ans = 3 #> .fix( [pi, -pi] ); ans = [ 3 -3 ] ---------------------------------------------------------------------double x.fix ---------------------------------------------------------------------- integer near zero #> pi.fix; ans = 3 #> (-pi).fix; ans = -3 ---------------------------------------------------------------------matrix A.fliplr ---------------------------------------------------------------------- reverse columns, flip left-right 𝑎1 𝑎 𝐀=[ 2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎8 𝑎9 𝑎10 𝑎11 ] , 𝑎12 𝑎10 𝐀. fliplr = [𝑎11 𝑎12 𝑎7 𝑎8 𝑎9 #> A = [ 1,2,3,4; 5,6,7,8 ]; A = [ 1 2 [ 5 6 3 7 4 ] 8 ] #> A.fliplr; ans = [ [ 2 6 1 ] 5 ] 4 8 3 7 92 𝑎4 𝑎5 𝑎6 𝑎1 𝑎2 ] 𝑎3 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.flipud ---------------------------------------------------------------------- reverse rows, flip upside-down 𝑎1 𝐀 = [𝑎2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎8 𝑎9 𝑎10 𝑎11 ] , 𝑎12 #> A = [ 1,2; 3,4; 5,6 ]; A = [ 1 [ 3 [ 5 2 ] 4 ] 6 ] #> A.flipud; ans = [ [ [ 6 ] 4 ] 2 ] 5 3 1 𝑎3 𝐀. flipud = [𝑎2 𝑎1 𝑎6 𝑎5 𝑎4 𝑎9 𝑎8 𝑎7 𝑎12 𝑎11 ] 𝑎10 ---------------------------------------------------------------------double floor (double x) matrix floor (matrix A) ---------------------------------------------------------------------- integer near −∞ #> floor(pi); ans = #> floor( [pi,-pi] ); ans = [ 3 3 -4 ] ---------------------------------------------------------------------double x.floor ---------------------------------------------------------------------- integer near −∞ #> pi.floor; ans = 3 #> (-pi).floor; 93 [100] 300 ans = Reference, Tutorial by www.msharpmath.com -4 ---------------------------------------------------------------------void poly.format(string fmt) ---------------------------------------------------------------------- set the format for screen output poly.format("") retrieves a default format. #> p = poly( pi, 1.e-5 ) ; p = poly( 3.1416e+000 1.0000e-005) = 1e-005x +3.14159 #> poly.format("%25.16e") ; p ; poly.format(""); p = poly( 3.1415926535897931e+000 1.0000000000000001e-005) = 1e-005x +3.14159 ---------------------------------------------------------------------matrix A.forsub ---------------------------------------------------------------------- For an 𝑛 × (𝑛 + 1) matrix, forward substitution is carried out to find a solution vector. 𝑎11 𝑥1 𝑎21 𝑥1 + 𝑎22 𝑥2 𝑎31 𝑥1 + 𝑎32 𝑥2 + 𝑎33 𝑥3 = 𝑏1 = 𝑏2 = 𝑏3 𝑥1 = 𝑏1 /𝑎11 𝑥2 = (𝑏2 − 𝑎21 𝑥1 )/𝑎22 𝑥3 = (𝑏3 − 𝑎31 𝑥1 − 𝑎32 𝑥2 )/𝑎33 #> A = [ 3,0,0, -6; 4,7,0, 13; 1,5,-4, -3 ]; A = [ 3 0 0 [ 4 7 0 [ 1 5 -4 #> A.forsub; ans = [ [ [ -2 ] 3 ] 4 ] 94 -6 ] 13 ] -3 ] [100] 300 Reference, Tutorial by www.msharpmath.com [ G ] ---------------------------------------------------------------------Gamma Function double .G(double x) // dot-only matrix .G(matrix x) ---------------------------------------------------------------------𝑥 𝛤(𝑥) = (𝑥 − 1)! = ∫ 𝑡 𝑥−1 𝑒 −𝑡 𝑑𝑡 0 #> .G(0.5); sqrt(pi); ans = 1.7724537 ans = 1.7724539 #> .G(6); 5.fact; ans = ans = // G(n+1) = n! 120 120 ---------------------------------------------------------------------matrix A.gausselim ---------------------------------------------------------------------- Gauss-elimination without row-swaps (without pivot) 𝑎11 𝑎12 𝑎13 𝑎14 𝑎15 𝑎16 𝑎21 𝑎22 𝑎23 𝑎24 𝑎25 𝑎26 𝐀=[ 𝑎 𝑎36 ] 31 𝑎32 𝑎33 𝑎34 𝑎35 𝑎41 𝑎42 𝑎43 𝑎44 𝑎45 𝑎46 𝑎11 0 𝐀. gausselim = [ 0 0 𝑎12 ∗ 𝑎22 0 0 𝑎13 ∗ 𝑎23 ∗∗ 𝑎33 0 #> A = [ 1,-2,-5,6; 3,-7,0,2; -5,3,-4,-8 ]; A = [ 1 -2 -5 6 ] [ 3 -7 0 2 ] [ -5 3 -4 -8 ] #> A.gausselim;; ans = [ 1 -2 -5 6 ] 95 𝑎14 ∗ 𝑎24 ∗∗ 𝑎34 ∗∗∗ 𝑎44 𝑎15 ∗ 𝑎25 ∗∗ 𝑎35 ∗∗∗ 𝑎45 𝑎16 ∗ 𝑎26 ∗∗ ] 𝑎36 ∗∗∗ 𝑎46 [100] 300 [ [ 0 0 -1 0 15 -134 Reference, Tutorial by www.msharpmath.com -16 ] 134 ] user-code can be written as follows. matrix gausselim(A) { // copy A n = A.m; for.k(1,n-1) { if( |A(k,k)| < _eps ) break; prow = A.row(k)/A(k,k); for.i(k+1,n) { A.row(i) -= A(i,k)*prow; A(i,k) = 0; A;; // display } cut;; } return A; } #> gausselim(A);; gausselim.A = [ 1 -2 -5 6 ] [ 0 -1 15 -16 ] [ -5 3 -4 -8 ] gausselim.A = [ 1 -2 -5 6 ] [ 0 -1 15 -16 ] [ 0 -7 -29 22 ] -----------------------------------------------------------gausselim.A = [ 1 -2 -5 6 ] [ 0 -1 15 -16 ] [ 0 0 -134 134 ] --------------------------------------------------------------------------------------------------------------------------------matrix A.gaussjord ---------------------------------------------------------------------- Gauss-Jordan elimination with pivot (switching rows) 𝑎11 𝑎12 𝑎13 𝑎14 𝑎15 𝑎16 𝑎21 𝑎22 𝑎23 𝑎24 𝑎25 𝑎26 𝐀=[ 𝑎 𝑎36 ] 31 𝑎32 𝑎33 𝑎34 𝑎35 𝑎41 𝑎42 𝑎43 𝑎44 𝑎45 𝑎46 96 [100] 300 where 𝑎𝑝𝑗 Reference, Tutorial by www.msharpmath.com 1 0 0 0 1 0 𝐀. gaussjord = 0 0 1 [0 0 0 represents a pivoted coefficient. 0 𝑎𝑝5 ∗ 0 𝑎𝑝5 ∗∗ 0 𝑎𝑝5 ∗∗∗ 1 𝑎𝑝5 𝑎𝑝6 ∗ 𝑎𝑝6 ∗∗ 𝑎𝑝6 ∗∗∗ 𝑎𝑝6 ] #> A = [ 1,-2,-5,6; 3,-7,0,2; -5,3,-4,-8 ]; A = [ 1 -2 -5 6 ] [ 3 -7 0 2 ] [ -5 3 -4 -8 ] #> A.gaussjord; ans = [ 1 0 [ 0 1 [ 0 0 0 0 1 3 ] 1 ] -1 ] user-code can be written as follows. matrix gaussjord(A) { // copy A n = A.m; for.k(1,n) { // up to the last line // pivoting procedure below //imax = k; //pmax = |A(k,k)|; //for.i(k+1,n) { // if( pmax < |A(i,k)| ) { pmax = |A(i,k)|; imax = i; } //} imax = A..(k:n)(k).maxentryk +k-1; if( imax != k ) { [ k,imax ];; A = A.swap(imax,k);; } // elimination procedure if( |A(k,k)| < _eps ) continue; // _eps = 2.22e-16 A.row(k) /= A(k,k); for.i(1,n) { if( i == k ) continue; // skip if k=i A.row(i) -= A(i,k)*A.row(k); A(i,k) = 0.; A;; // display } cut;; } return A; 97 [100] 300 Reference, Tutorial by www.msharpmath.com } #> gaussjord(A);; gaussjord.ans = [ 1 3 ] gaussjord.A = [ -5 3 -4 -8 ] [ 3 -7 0 2 ] [ 1 -2 -5 6 ] gaussjord.A = [ 1 -0.6 0.8 1.6 ] [ 0 -5.2 -2.4 -2.8 ] [ 1 -2 -5 6 ] gaussjord.A = [ 1 -0.6 0.8 1.6 ] [ 0 -5.2 -2.4 -2.8 ] [ 0 -1.4 -5.8 4.4 ] -----------------------------------------------------------gaussjord.A = [ 1 0 1.07692 1.92308 ] [ -0 1 0.461538 0.538462 ] [ 0 -1.4 -5.8 4.4 ] gaussjord.A = [ 1 0 1.07692 1.92308 ] [ -0 1 0.461538 0.538462 ] [ 0 0 -5.15385 5.15385 ] -----------------------------------------------------------gaussjord.A = [ 1 0 0 3 ] [ -0 1 0.461538 0.538462 ] [ -0 -0 1 -1 ] gaussjord.A = [ 1 0 0 3 ] [ 0 1 0 1 ] [ -0 -0 1 -1 ] --------------------------------------------------------------------------------------------------------------------------------matrix A.gausspivot ---------------------------------------------------------------------- Gauss-elimination with pivot (row-swaps) 𝑎11 𝑎12 𝑎13 𝑎21 𝑎22 𝑎23 𝐀=[ 𝑎 31 𝑎32 𝑎33 𝑎41 𝑎42 𝑎43 98 𝑎14 𝑎24 𝑎34 𝑎44 𝑎15 𝑎25 𝑎35 𝑎45 𝑎16 𝑎26 𝑎36 ] 𝑎46 [100] 300 where 𝑎𝑝𝑗 Reference, Tutorial by www.msharpmath.com 𝑎𝑝1 𝑎𝑝2 𝑎𝑝3 ∗ ∗ 0 𝑎𝑝2 𝑎𝑝3 𝐀. gausspivot = 0 ∗∗ 0 𝑎𝑝3 0 0 [ 0 represents a pivoted coefficient. 𝑎𝑝4 ∗ 𝑎𝑝4 ∗∗ 𝑎𝑝4 ∗∗∗ 𝑎𝑝4 𝑎𝑝5 ∗ 𝑎𝑝5 ∗∗ 𝑎𝑝5 ∗∗∗ 𝑎𝑝5 𝑎𝑝6 ∗ 𝑎𝑝6 ∗∗ 𝑎𝑝6 ∗∗∗ 𝑎𝑝6 ] #> A = [ 1,-2,-5,6; 3,-7,0,2; -5,3,-4,-8 ]; A = [ 1 -2 -5 6 ] [ 3 -7 0 2 ] [ -5 3 -4 -8 ] #> A.gausspivot; ans = [ -5 [ 0 [ 0 3 -5.2 0 -4 -2.4 -5.15385 -8 ] -2.8 ] 5.15385 ] #> A.rowswap(1,3).add(2,1, 3/5).add(3,1, 1/5).add(3,2, -1.4/5.2); ans = [ -5 3 -4 -8 ] [ 0 -5.2 -2.4 -2.8 ] [ 0 0 -5.15385 5.15385 ] // Gauss elimination with pivot, user code for A.gausspivot matrix gausspivot(A) { // copy A n = A.m; for.k(1,n-1) { // pivoting procedure below //imax = k; //pmax = |A(k,k)|; //for(i = k+1; i <= n; i++) { // if( pmax < |A(i,k)| ) { pmax = |A(i,k)|; imax = i; } //} imax = A..(k:n)(k).maxentryk +k-1; if( imax != k ) { [ k,imax ];; A = A.swap(imax,k);; } // elimination procedure if( |A(k,k)| < _eps ) continue; // _eps = 2.22e-16 prow = A.row(k)/A(k,k); for.i(k+1,n) { if( i == k ) continue; // skip if k=i A.row(i) -= A(i,k)*prow; A(i,k) = 0.; A;; // display } 99 [100] 300 Reference, Tutorial by www.msharpmath.com cut;; } return A; } #> gausspivot(A);; gausspivot.ans = [ 1 3 ] gausspivot.A = [ -5 3 -4 -8 ] [ 3 -7 0 2 ] [ 1 -2 -5 6 ] gausspivot.A = [ -5 3 -4 -8 ] [ 0 -5.2 -2.4 -2.8 ] [ 1 -2 -5 6 ] gausspivot.A = [ -5 3 -4 -8 ] [ 0 -5.2 -2.4 -2.8 ] [ 0 -1.4 -5.8 4.4 ] -----------------------------------------------------------gausspivot.A = [ -5 3 -4 -8 ] [ 0 -5.2 -2.4 -2.8 ] [ 0 0 -5.15385 5.15385 ] --------------------------------------------------------------------------------------------------------------------------------double .gcd (double a, double b) // dot-only matrix .gcd (double a, matrix b) matrix .gcd (matrix a, double b) matrix .gcd (matrix a, matrix b) ---------------------------------------------------------------------- greatest common divisor #> .gcd(24,36); ans = 12 [ H ] ---------------------------------------------------------------------Heaviside Function double .H(double x) // dot-only matrix .H(matrix x) 100 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------- 𝐻(𝑥) = { #> .H(-1); ans = 0 #> .H(3); ans = 1 0, 1, 𝑥<0 𝑥>0 ---------------------------------------------------------------------double .H_n(double x) // dot-only matrix .H_n(matrix x), for real-part only ---------------------------------------------------------------------- evaluation of the Hermite polynomial of degree n #> .H_5(0.3); ans = 31.75776 #> .H_5( [0.3,0.4] ); ans = [ 31.7578 38.0877 ] #>.hold; for.n(1,5) plot.x(-1,1) ( .H_n(x) ); plot; ---------------------------------------------------------------------poly poly.H(double n) ---------------------------------------------------------------------- Hermite polynomial of degree 𝑛 𝐻0 (𝑥) = 1, 𝐻1 (𝑥) = 2𝑥 𝐻𝑛 (𝑥) = 2𝑥𝐻𝑛−1 (𝑥) − 2(𝑛 − 1)𝐻𝑛−2 (𝑥) #> for.n(0,5) poly.H(n); ans = poly( 1 ) = 1 ans = poly( 0 2 ) 101 [100] 300 = ans = = ans = = ans = = ans = = Reference, Tutorial by www.msharpmath.com 2x poly( -2 0 4 ) 4x^2 -2 poly( -0 -12 0 8 ) 8x^3 -12x poly( 12 -0 -48 0 16 ) 16x^4 -48x^2 +12 poly( 0 120 -0 -160 0 32 ) 32x^5 -160x^3 +120x ---------------------------------------------------------------------Hankel function by Bessel Function complex .H1_nu(double x) dot-only complex .H2_nu(double x) dot-only matrix .H1_nu(matrix x), for real-part only matrix .H2_nu(matrix x), for real-part only ---------------------------------------------------------------------(1) 𝐻𝜈 (𝑥) = 𝐽𝜈 (𝑥) + 𝑖 𝑌𝜈 (𝑥) (2) 𝐻𝜈 (𝑥) = 𝐽𝜈 (𝑥) − 𝑖 𝑌𝜈 (𝑥) #> .H1_0.5(1.2); .H2_0.5(1.2); ans = 0.678865 - 0.263929! ans = 0.678865 + 0.263929! #> .J_0.5(1.2); .Y_0.5(1.2); ans = 0.67886517 ans = -0.26392895 #> .H1_0.5( [1.2, 2.4] ); .H2_0.5( [1.2, 2.4] ); ans = [ ans = [ 0.678865 - i 0.263929 0.678865 + i 0.263929 0.347885 + i 0.379782 0.347885 - i 0.379782 ] ] ---------------------------------------------------------------------matrix matrix.hank (matrix A, matrix B) ---------------------------------------------------------------------#> matrix.hank ([3,1,2,0],[0,-1,-2,-3]); ans = [ 3 [ 1 [ 2 [ 0 // hankel 1 2 0 2 0 -1 0 ] -1 ] -2 ] -1 -2 -3 ] 102 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.hess ---------------------------------------------------------------------- eigenvalue-preserving Hessenberg matrix #> A = [ 4,3,-2,1; 3,1,7,9; -2,7,5,4; 1,9,4,2 ]; A = [ 4 3 -2 [ 3 1 7 [ -2 7 5 [ 1 9 4 #> A .hess .trun12; ans = [ 4 [ -3.74166 [ 0 [ 0 1 9 4 2 ] ] ] ] 0 0 9.24686 1.77171 ] ] ] ] // built-in function -3.74166 -1.07143 6.83441 0 0 6.83441 7.29972 9.24686 #> A.eig; A.hess.eig; ans = [ 5.61943 ] [ -8.47695 ] [ -1.2945 ] [ 16.152 ] ans = [ 5.61943 ] [ -8.47695 ] [ -1.2945 ] [ 16.152 ] ---------------------------------------------------------------------matrix A.high(double m) ---------------------------------------------------------------------- reshape in 𝑚 -rows #> A = (1:12)._4; A = [ 1 [ 2 [ 3 4 5 6 7 8 9 #> A.high(5); ans = [ 1 6 11 ] 103 10 ] 11 ] 12 ] [100] 300 [ [ [ [ Reference, 2 3 4 5 Tutorial by www.msharpmath.com 7 8 9 10 12 0 0 0 ] ] ] ] ---------------------------------------------------------------------matrix matrix.hilb (double n) ---------------------------------------------------------------------- 𝑎𝑖𝑗 = 1 𝑖+𝑗 #> matrix.hilb(4); ans = [ [ [ [ 1 0.5 0.333333 0.25 0.5 0.333333 0.25 0.2 0.333333 0.25 0.2 0.166667 0.25 0.2 0.166667 0.142857 ] ] ] ] ---------------------------------------------------------------------matrix .horzcat( ...) ---------------------------------------------------------------------- horizontal concatenation. #> .horzcat( 1,2, [ 3;4;5 ], 6, [-1;-2] ans = [ 1 2 3 [ 0 0 4 [ 0 0 5 ) 6 6 6 -1 ] -2 ] 0 ] Operation is carried out in sequence. Therefore, the first two entries 1 and 2 have all-zeros below. But the fourth entry (6) is extended as a matrix. [ I ] ---------------------------------------------------------------------Identity matrix matrix .I(double m, double n=m) dot-only matrix .eye(double m, double n=m) ---------------------------------------------------------------------- 104 [100] 300 Reference, Tutorial by www.msharpmath.com 𝑎𝑖𝑗 = 𝛿𝑖𝑗 = { 1 𝑖=𝑗 0 𝑖≠𝑗 #> .I(3,4); ans = [ [ [ 1 0 0 0 1 0 0 0 1 0 ] 0 ] 0 ] #> .I(4); ans = [ [ [ [ 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 ] ] ] ] ---------------------------------------------------------------------Bessel Function double .I_(double matrix .I_(double matrix .I_(matrix matrix .I_(matrix nu)(double nu)(matrix nu)(double nu)(matrix x) x) x) x) dot-only ---------------------------------------------------------------------∞ 𝐼𝜈 (𝑥) = ∑ 𝑖=0 1 𝑥 2𝑠+𝜈 ( ) 𝑠! (𝑠 + 𝜈)! 2 #> nu = 1;; #> .I_nu+1/2(0.3); ans = 0.044096517 #> .I_nu+1/2( [0.3,0.4] ); ans = [ 0.0440965 0.0683662 ] #> .I_[1.5;2.5]( [0.3,0.4] ); ans = [ 0.0440965 0.0683662 ] [ 0.00263901 0.00544447 ] #> .hold; for.nu(0,5) plot.x(0,2) ( .I_nu(x)); 105 plot; [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------double A.i(double k) ---------------------------------------------------------------------- i of index (i,j) for linear index 𝑘 = 𝑖 + 𝑚(𝑗 − 1) #> A = (1:12)._4; A = [ 1 [ 2 [ 3 #> i = A.i(8); i = 4 5 6 7 8 9 10 ] 11 ] 12 ] 2 ---------------------------------------------------------------------(double i, double j) = A.ij(double k) // tuple ---------------------------------------------------------------------- index (i,j) for linear index 𝑘 = 𝑖 + 𝑚(𝑗 − 1) #> A = (1:12)._4; A = [ 1 [ 2 [ 3 #> (i,j) = A.ij(8); i = j = #> A.k(2,3); ans = 4 5 6 7 8 9 10 ] 11 ] 12 ] 2 3 8 ---------------------------------------------------------------------matrix W.imag // both read/write ---------------------------------------------------------------------- 106 [100] 300 Reference, Tutorial by www.msharpmath.com imaginary part #> A = [ 1-2i, -3+4i; 2+3i, 5+2i ]; A = [ 1 - i 2 [ 2 + i 3 #> A.imag; ans = [ [ -2 3 -3 + i 4 5 + i 2 ] ] 4 ] 2 ] #> A.imag += 100; // matrix is modified, writable A = [ 1 + i 98 -3 + i 104 [ 2 + i 103 5 + i 102 ] ] ---------------------------------------------------------------------matrix A.ins(double i,double j,matrix B) ---------------------------------------------------------------------- insert 𝐁 at (i,j) position #> A = (1:16)._4; A = [ 1 5 9 13 ] [ 2 6 10 14 ] [ 3 7 11 15 ] [ 4 8 12 16 ] #> A.ins(1,1, [101:105; 201:205]); ans = [ 101 102 103 104 105 [ 201 202 203 204 205 [ 0 0 0 0 0 [ 0 0 0 0 0 [ 0 0 0 0 0 [ 0 0 0 0 0 0 1 2 3 0 4 0 0 0 0 0 0 5 9 13 6 10 14 7 11 15 // [101:105; 201:205] \_ A ] ] ] ] ] 8 12 16 ] #> A.ins(2,2, [101:105; 201:205]'); ans = [ 1 0 0 5 9 13 ] [ 0 101 201 0 0 0 ] 107 [100] 300 [ [ [ [ [ [ 0 0 0 0 2 3 102 103 104 105 0 0 [ 4 0 202 203 204 205 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 10 14 7 11 15 Reference, Tutorial by www.msharpmath.com ] ] ] ] ] ] 0 8 12 16 ] #> A.ins(100,100, [101:105; 201:205]'); ans = [ 1 5 9 13 0 0 ] [ [ [ [ [ 2 3 4 0 0 6 10 14 0 0 7 11 15 0 0 8 12 16 0 0 0 0 0 101 201 0 0 0 102 202 [ 0 0 [ 0 0 [ 0 0 // A \_ [101:105; 201:205]' ] ] ] ] ] 0 0 103 203 ] 0 0 104 204 ] 0 0 105 205 ] ---------------------------------------------------------------------double .integ(double a,double b,double f(double x)) // dot-only ---------------------------------------------------------------------general integration including indefinite integral 𝑏 ∫ 𝑓(𝑥)𝑑𝑥 , −∞ ∞ ∞ ∫ 𝑓(𝑥)𝑑𝑥 , 𝑎 ∫ 𝑓(𝑥)𝑑𝑥 , −∞ 𝑏 ∫ 𝑓(𝑥)𝑑𝑥 𝑎 inf = ∞ //------------------// indefinite integral //------------------double integ(a,b, f(x)) { aa = a; bb = b; flip = 1; if(a > b) { aa = b; bb = a; flip = -1; } (sum, h,r) = (0, 0.05, 1.2); if( aa < -0.5*inf && 0.5*inf < bb ) { // -inf<x<inf a = 0; for[300] { sum += int.x(a,a+h) ( f(x) ) + int.x(-a-h,-a) ( f(x) ); a += h; h *= r; } 108 [100] 300 Reference, Tutorial by www.msharpmath.com } else if( aa < -0.5*inf ) { // -inf<x<bb a = bb; for[300] { sum += int.x(a-h,a) ( f(x) ); a -= h; h *= r; } } else if( 0.5*inf < bb ) { // aa<x<inf a = aa; for[300] { sum += int.x(a,a+h) ( f(x) ); a += h; h *= r; } } else { // aa<x<bb a = b = 0.5*(aa+bb); h = 0.25*(bb-aa); for[30] { sumo = sum; sum += int.x(a,a+h) (f(x)) + int.x(b-h,b) (f(x)); a += h; b -= h; h *= 0.5; if( sum.-.sumo < 1.e-8) break; } } return sum*flip; } ∞ 2 ∫ 𝑒 −𝑥 sin(𝑥 2 ) 𝑑𝑥 = 0 #> double f(x) = exp(-x^2)*sin(x^2); #> integ(0,inf,f); #> .integ(0,inf,f); ans = 0.28518528 ans = 0.28518528 1/2 √𝜋 (√2 − 1) 4 // exercise 9(f) // user function listed above // built-in dot function #> sqrt(pi)/4*sqrt(sqrt(2)-1); ans = 0.28518528 1 ∫ 0 1 √1 − 𝑥 2 #> double f(x) = 1/sqrt(1-x^2); #> integ(0,1,f); #> .integ(0,1,f); ans = 1.5707658 ans = 1.5707658 #> asin(1); ans = 𝑑𝑥 = sin−1(1) // user function listed above // built-in dot function 1.5707963 ---------------------------------------------------------------------poly .interp(matrix X, matrix Y) 109 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------- interpolating polynomial #> x = [ 1,2,3,4 ]; y = [ 10,20,48,100 ]; x = [ 1 2 y = [ 10 20 3 48 4 ] 100 ] #> .interp(x,y); ans(x); // pass all points ans = poly( 12 -6 3 1 ) = x^3 +3x^2 -6x +12 ans = [ 10 20 48 100 ] ---------------------------------------------------------------------matrix A.inv ---------------------------------------------------------------------- inverse matrix, A.inv * A = .I(A) #> A = [ 1+2i, 3-4i; -2+1i, 5+2i ]; A = [ 1 + i 2 [ -2 + i 1 #> A.inv; ans = [ [ 1.7 + i 0.1 0.5 - i 0.5 #> A.inv * A ; ans = [ 1 [ 0 3 - i 4 5 + i 2 ] ] -0.5 + i 1.5 0.5 + i 0.5 ] ] 0 1 ] ] 0 0 ] ] #> A.inv*A - .I(A); ans = [ [ 0 0 ---------------------------------------------------------------------poly p.inv ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. inv = 1/𝑎0 + (1/𝑎1 )𝑥 + (1/𝑎2 )𝑥 2 + … + (1/𝑎𝑛 )𝑥 𝑛 110 [100] 300 Reference, Tutorial by www.msharpmath.com #> .[ 1,2,3,4 ] .inv ; // x^3 + 2x^2 + 3x + 4 ans = poly( 0.25 0.333333 0.5 1 ) = x^3 +0.5x^2 +0.333333x +0.25 ---------------------------------------------------------------------poly p.invtay ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. invtay = 𝑎0 + 𝑎1 𝑥 + 2! 𝑎2 𝑥 2 + 3! 𝑎3 𝑥 3 + ⋯ + 𝑛! 𝑎𝑛 𝑥 𝑛 #> .[ 1,2,3,4 ] .invtay; ans = poly( 4 3 4 6 ) = 6x^3 +4x^2 +3x +4 ---------------------------------------------------------------------double A.ipos(double x) ---------------------------------------------------------------------- returns an index, 𝑎𝑘 ≤ 𝑥 ≤ 𝑎𝑘+1 #> A = [ 1,6,16,25; 3,9,18,30; 4,15,21,40 ]; A = [ 1 6 16 [ 3 9 18 [ 4 15 21 #> A.ipos(8.3); ans = // a(4)=6 <= x <= a(5)=9 4 #> A.ipos(35); ans = // a(11)=30 <= x <= a(12)=40 11 #> A.ipos(-5); ans = ans = A.ipos(100); 0 0 25 ] 30 ] 40 ] // out of range ---------------------------------------------------------------------poly p.iratio ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 shows a rational approximation of each coefficient #> poly(0.1, 0.2, 1.5).iratio ; 111 [100] 300 Reference, Tutorial by www.msharpmath.com ans = poly( 1/10 1/5 3/2 ) ---------------------------------------------------------------------double .isprime (double x) ---------------------------------------------------------------------- return 1 if x is a prime number, otherwise return 0 #> .isprime(37); ans = #> .isprime(36); ans = 1 0 ---------------------------------------------------------------------double A.isreal ---------------------------------------------------------------------- return 1 if a matrix is real, otherwise return 0 #> [ 1+2i ].isreal; ans = ans = [ 3 ].isreal; 0 1 ---------------------------------------------------------------------double A.issquare ---------------------------------------------------------------------- returns 1 if a matrix is square, otherwise return 0 #> [ 1,2; 3,4 ].issquare; ans = 1 ans = 0 [ 1,2,3,4 ].issquare; [ J ] ---------------------------------------------------------------------Bessel Function double .J_(double nu)(double x) dot-only matrix .J_(double nu)(matrix x) matrix .J_(matrix nu)(double x) matrix .J_(matrix nu)(matrix x) ---------------------------------------------------------------------- 112 [100] 300 Reference, Tutorial by www.msharpmath.com ∞ 𝐽𝜈 (𝑥) = ∑ 𝑖=0 (−1)𝑠 𝑥 2𝑠+𝜈 ( ) 𝑠! (𝑠 + 𝜈)! 2 #> nu = 1;; #> .J_nu+1/2(0.3); ans = 0.043309878 #> .J_nu+1/2( [0.3,0.4] ); ans = [ 0.0433099 0.0662131 ] #> .J_[1.5;2.5] ( [0.3,0.4] ); ans = [ 0.0433099 0.0662131 ] [ 0.0026053 0.00532144 ] #> .hold; for.nu(0,5) plot.x(0,15) (.J_nu(x)); plot; ---------------------------------------------------------------------Spherical Bessel Function double .j_(double nu)(double x) dot-only matrix .j_(double nu)(matrix x) matrix .j_(matrix nu)(double x) matrix .j_(matrix nu)(matrix x) ---------------------------------------------------------------------- 𝜋 𝐽 (𝑥) 2𝑥 𝑛+1/2 sin 𝑥 𝑗0 (𝑥) = 𝑥 𝑗𝑛 (𝑥) = √ #> .j_0(0.5); sin(0.5)/0.5; ans = 0.958851 ans = 0.95885108 #> .j_0( [0.5,1] ); ans = [ 0.958851 0.841471 ] 113 [100] 300 Reference, Tutorial by www.msharpmath.com #> .j_[0;1] ( [0.5,1] ); ans = [ 0.958851 0.841471 ] [ 0.162537 0.301169 ] ---------------------------------------------------------------------double A.j(double k) ---------------------------------------------------------------------- j of index (i,j) for linear index 𝑘 = 𝑖 + 𝑚(𝑗 − 1) #> A = (1:12)._4; A = [ 1 [ 2 [ 3 #> j = A.j(8); j = 4 5 6 7 8 9 10 ] 11 ] 12 ] 3 ---------------------------------------------------------------------matrix matrix.jord (double a, double b) matrix matrix.jordan (double a, double b) ---------------------------------------------------------------------#> matrix.jordan(4,1); ans = [ 1 1 0 0 ] [ 0 1 1 0 ] [ [ 0 0 0 0 1 0 1 ] 1 ] ---------------------------------------------------------------------matrix A.jord(double i) ---------------------------------------------------------------------- eliminates rows above and below 𝑎𝑖𝑖 through elementary row operations. #> A = [ 1,-2,-5,6; 3,-7,0,2; -5,3,-4,-8 ]; A = [ 1 -2 -5 114 6 ] [100] 300 Reference, Tutorial by www.msharpmath.com [ 3 -7 [ -5 3 #> A.jord(1); ans = [ 1 -2 [ 0 -1 [ 0 -7 #> A.jord(1).jord(2); ans = [ 1 0 [ 0 1 [ 0 0 #> A.jord(1).jord(2).jord(3); ans = [ 1 0 [ 0 1 [ 0 0 0 -4 2 ] -8 ] -5 15 -29 6 ] -16 ] 22 ] -35 -15 -134 38 ] 16 ] 134 ] 0 0 1 3 ] 1 ] -1 ] [ K ] ---------------------------------------------------------------------Bessel Function double .K_(double nu)(double x) dot-only matrix .K_(double nu)(matrix x) matrix .K_(matrix nu)(double x) matrix .K_(matrix nu)(matrix x) ---------------------------------------------------------------------- 𝐾𝜈 (𝑥) = 𝜋 𝐼−𝜈 (𝑥) − 𝐼𝜈 (𝑥) 2 sin 𝜈𝜋 #> nu = 1;; #> .K_nu+1/2(0.3); ans = 7.3456985 #> .K_nu+1/2( [0.3,0.4] ); ans = [ 7.3457 4.64922 ] #> .K_[1.5;2.5]( [0.3,0.4] ); ans = [ 7.3457 4.64922 ] [ 75.1521 36.1975 ] 115 [100] 300 Reference, Tutorial by www.msharpmath.com #> .hold; for.nu(0,5) plot.x(4,15) (.K_nu+0.5(x)); plot; ---------------------------------------------------------------------double A.k(double i,double j) ---------------------------------------------------------------------- linear index 𝑘 = 𝑖 + 𝑚(𝑗 − 1) for row-column index (i,j) #> A = (1:12)._4; A = [ 1 [ 2 [ 3 4 5 6 #> A.k(2,3); ans = 7 8 9 10 ] 11 ] 12 ] 8 #> (i,j) = A.ij(8); i = j = 2 3 ---------------------------------------------------------------------matrix .kron(matrix A, matrix B) ---------------------------------------------------------------------- A ^^ B = .kron(A,B) A Kronecker product of 𝑚 × 𝑛 matrix 𝐀 and 𝑝 × 𝑞 matrix 𝐁 results in 𝑚 × 𝑛 block-matrix dimension of 𝑚𝑝 × 𝑛𝑞, and its (𝑖, 𝑗) block is 𝑎𝑖𝑗 𝐁. %> Kronecker product, A^^B = .kron(A,B) #> matrix.format("%6g") ; #> A = [ 1, 10, -100; -10, 1, 1000 ];; B = [ 1,2,3; 4,5,6; 7,8,9 ];; #> A ^^ B ; ans = [ 1 2 3 10 20 30 -100 -200 -300 ] [ 4 5 6 40 50 116 60 -400 -500 -600 ] [100] 300 [ [ [ [ 7 -10 -40 -70 8 -20 -50 -80 Reference, 9 -30 -60 -90 Tutorial by www.msharpmath.com 70 1 4 7 80 2 5 8 90 3 6 9 -700 1000 4000 7000 -800 2000 5000 8000 -900 ] 3000 ] 6000 ] 9000 ] Cemmath extends the concept of the Kronecker product to handle various operatations more than multiplication via the upgraded function. For example, the above Kronecker product is the same as #> matrix kronmul(double x) = x * [ 1, 10, -100; -10, 1, 1000 ] ; #> B = [ 1,2,3; 4,5,6; 7,8,9 ];; #> kronmul++(B); // upgraded matrix(double) ans = [ 1 2 3 10 20 30 -100 -200 [ 4 5 6 40 50 60 -400 -500 [ 7 8 9 70 80 90 -700 -800 [ [ [ -10 -40 -70 -20 -50 -80 -30 -60 -90 1 4 7 2 5 8 3 6 9 1000 4000 7000 2000 5000 8000 -300 ] -600 ] -900 ] 3000 6000 9000 another example #> matrix.format("%9g"); #> matrix kron3(double x) = [ x, 1, -sqrt(x); 0, x^2, log(x) ] ; #> kron3++( [2,3;4,5] ); // upgraded matrix(double) ans = [ [ [ [ 2 4 0 0 3 5 0 0 1 1 4 16 1 -1.41421 -1.73205 ] 1 -2 -2.23607 ] 9 0.693147 1.09861 ] 25 1.38629 1.60944 ] [ L ] ---------------------------------------------------------------------double .L_n(double x) dot-only matrix .L_n(matrix x), for real-part only ---------------------------------------------------------------------- Laguerre polynomial 117 ] ] ] [100] 300 #> .L_5(0.3); ans = Reference, Tutorial by www.msharpmath.com -11.19993 #> .L_5( [0.3,0.4] ); ans = [ -11.1999 -36.1702 ] #> .hold; for.n(1,5) plot.x(0,1) ( .L_n(x) ); plot; ---------------------------------------------------------------------poly poly.L(n) ---------------------------------------------------------------------- Laguerre polynomial of degree 𝑛 𝐿0 (𝑥) = 1, 𝐿1 (𝑥) = 1 − 𝑥 𝐿𝑛 (𝑥) = (2𝑛 − 𝑥 − 1)𝐿𝑛−1 (𝑥) − (𝑛 − 1)2 𝐿𝑛−2 (𝑥) #> for.n(0,5) poly.L(n); ans = poly( 1 ) = 1 ans = poly( 1 -1 ) = -x +1 ans = poly( 2 -4 1 ) = x^2 -4x +2 ans = poly( 6 -18 9 -1 ) = -x^3 +9x^2 -18x +6 ans = poly( 24 -96 72 -16 1 ) = x^4 -16x^3 +72x^2 -96x +24 ans = poly( 120 -600 600 -200 25 -1 ) = -x^5 +25x^4 -200x^3 +600x^2 -600x +120 ---------------------------------------------------------------------double .lcm (double a, double b) dot-only matrix .lcm (double a, matrix b) matrix .lcm (matrix a, double b) 118 [100] 300 Reference, Tutorial by www.msharpmath.com matrix .lcm (matrix a, matrix b) ---------------------------------------------------------------------- least common multiplier #> .lcm(24,36); ans = 72 ---------------------------------------------------------------------void poly.left(string fmt) ---------------------------------------------------------------------- set the left delimiter for screen output #> p = poly( 1,2,3 ) ; p = poly( 1 2 3 ) = 3x^2 +2x +1 #> poly.left("[]()left<>--->") ; p = []()left<>---> 1 2 3 ) = 3x^2 +2x +1 p ; ---------------------------------------------------------------------double p.len ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. len = 𝑛 + 1 #> p = (1:5).dpoly; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p.len; ans = 5 ---------------------------------------------------------------------matrix A.len matrix A.length matrix A.longer ---------------------------------------------------------------------return 𝑚𝑎𝑥(𝑚, 𝑛) for an 𝑚 × 𝑛 matrix 𝐀 #> A = (1:6)._3; A = [ 1 [ 2 #> A.len; 3 4 5 ] 6 ] 119 [100] 300 ans = Reference, Tutorial by www.msharpmath.com 3 ---------------------------------------------------------------------double .lnG/logG(double x) // dot-only matrix .lnG/logG(matrix x) ---------------------------------------------------------------------𝑥 ln(𝛤(𝑥)) = ln ∫ 𝑡 𝑥−1 𝑒 −𝑡 𝑑𝑡 0 #> .lnG(0.5); log(sqrt(pi)); ans = 0.57236486 ans = 0.57236494 #> .lnG(6); ans = ans = log(5.fact); 4.7874917 4.7874917 // G(n+1) = n! ---------------------------------------------------------------------Logarithmic Function with base _e double log (double x) matrix log (matrix A) ---------------------------------------------------------------------#> log(2); log(_e); log(10); ans = 0.69314718 ans = 1 ans = 2.3025851 #> log( [2,_e,10] ); ans = [ 0.693147 1 2.30259 ] ---------------------------------------------------------------------double log1p (double x) matrix log1p (matrix A) ---------------------------------------------------------------------- ln(1 + 𝑥) = 𝑥 − 𝑥2 𝑥3 𝑥5 + − +⋯ 2 3 5 #> log1p(1.e-40); log(1+1.e-40); ans = 1e-040 ans = 0 ---------------------------------------------------------------------Logarithmic Function with base 2 120 [100] 300 Reference, Tutorial by www.msharpmath.com double log2 (double x) matrix log2 (matrix A) ---------------------------------------------------------------------#> log2(2); log2(_e); log2(10); ans = 1 ans = 1.442695 ans = 3.3219281 #> log2( [2,_e,10] ); ans = [ 1 1.4427 3.32193 ] ---------------------------------------------------------------------Logarithmic Function with base 10 double log10 (double x) matrix log10 (matrix A) ---------------------------------------------------------------------#> log10(2); log10(_e); log10(10); ans = 0.30103 ans = 0.43429448 ans = 1 #>log10( [2,_e,10] ); ans = [ 0.30103 0.434294 1 ] ---------------------------------------------------------------------matrix logm (matrix A) ---------------------------------------------------------------------- inverse of expm(A) #> A = [1,0.3; 0.2,1]; E = expm(A); L = logm(A); A = [ [ 1 0.2 0.3 ] 1 ] [ 2.80024 0.823664 ] [ 0.549109 L = -0.0309377 0.20415 2.80024 ] E = [ [ 0.306226 ] -0.0309377 ] #> logm(E); ans = 121 [100] 300 [ [ #> expm(L); ans = [ [ Reference, 1 0.2 0.3 ] 1 ] 1 0.3 ] 0.2 1 ] Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix (double a, double b) .logspan (double n, double g=1) ---------------------------------------------------------------------(a,b) .logspan(n,g=1) // 10.^( (log10(a),log10(b)).span(n,g=1) ) %> (a,b) .logspan(n,g=1) has a matrix of dimension 1 x n --------------#> x = (1,10).logspan(11); log10(x); x = [ ans = [ 1 3.16228 10 ] 1.25893 3.98107 1.58489 5.01187 1.99526 6.30957 2.51189 7.94328 0 0.1 0.2 0.3 0.4 0.5 1 ] 0.6 0.7 0.8 0.9 ---------------------------------------------------------------------(matrix L,matrix U) = A.lu // tuple ---------------------------------------------------------------------A = LU decomposition For 𝑎𝑖𝑗 with 𝑖 ≥ 𝑗 (i.e. lower-triangular and diagonal elements), 𝑗−1 𝑗−1 𝑎𝑖𝑗 = ∑ 𝑙𝑖𝑘 𝑢𝑘𝑗 + 𝑙𝑖𝑗 𝑢𝑗𝑗 ⇒ 𝑙𝑖𝑗 = 𝑎𝑖𝑗 − ∑ 𝑙𝑖𝑘 𝑢𝑘𝑗 𝑘=1 𝑘=1 For 𝑎𝑖𝑗 with 𝑖 < 𝑗 (i.e. upper-triangular elements), 𝑖−1 𝑎𝑖𝑗 = ∑ 𝑙𝑖𝑘 𝑢𝑘𝑗 + 𝑙𝑖𝑖 𝑢𝑗𝑗 𝑖−1 ⇒ 𝑘=1 𝑢𝑖𝑗 = (𝑎𝑖𝑗 − ∑ 𝑙𝑖𝑘 𝑢𝑘𝑗 )/𝑙𝑖𝑖 𝑘=1 #> A = [ 2,-1,1; -1,3,-2; 1,-2,5 ]; 122 [100] 300 A = [ [ [ 2 -1 1 -1 3 -2 Reference, Tutorial by www.msharpmath.com 1 ] -2 ] 5 ] #> (L,U) = A.lu; // tuple assignment by LU decomposition L = [ 2 0 0 ] [ -1 2.5 0 ] [ 1 -1.5 3.6 ] U = [ 1 -0.5 0.5 ] [ 0 1 -0.6 ] [ 0 0 1 ] User-own code can be also written as follows. void user_lu(matrix& A) { n = A.m; L = U = .zeros(n); for.s(1,n) { for.i(s,n) L(i,s) = A(i,s) - L..(i)(1:s-1) ** U..(1:s-1)(s); U(s,s) = 1; // horizontal direction for.j(s+1,n) U(s,j) = (A(s,j) - L..(s)(1:s-1) ** U..(1:s-1)(j))/L(s,s); [ L, U ] ;; // display L..(j:n)(j), U..(i)(i:n) } } #> user_lu(A); ans = [ 2 0 [ -1 0 [ 1 0 ans = [ 2 0 [ -1 2.5 [ 1 -1.5 ans = [ 2 0 [ -1 2.5 [ 1 -1.5 0 0 0 1 0 0 -0.5 0 0 0.5 ] 0 ] 0 ] 0 0 0 1 0 0 -0.5 1 0 0.5 ] -0.6 ] 0 ] 0 0 3.6 1 0 0 -0.5 1 0 0.5 ] -0.6 ] 1 ] 123 [100] 300 Reference, Tutorial by www.msharpmath.com [ M ] ---------------------------------------------------------------------double A.m ---------------------------------------------------------------------return 𝑚 for an 𝑚 × 𝑛 matrix 𝐀 #> A = (1:6)._3; A = [ 1 [ 2 ans = A.m; 3 4 5 ] 6 ] 2 ---------------------------------------------------------------------matrix (double a, double h) .march (double n, double g=1) ---------------------------------------------------------------------(a,h) .march(n,g=1) // [ a, a+h(1),... , a+h(1+g+...+g^(n-2)) ] %> (a,h) .march(n,g=1) has a matrix of dimension 1 x n ---------------#> X = (3,1) .march(6) ; // g = 1 by default X = [ 3 4 5 6 7 8 ] #> X = (3,1) .march(6,2) ; X = [ 3 4 6 // g = 2 for increasing 10 18 34 ] #> X = (3,1) .march(6,0.5) ; X = [ 3 4 4.5 4.75 // g = 0.5 for decreasing 4.875 4.9375 ] ---------------------------------------------------------------------double .max(double x1, double x2, ... ) dot-only ---------------------------------------------------------------------- maximum of arguments #> .max(1,2); ans = 2 #> .max(1,2,3); ans = 3 #> .max(1,2,3,4); ans = 4 124 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.max // column-first operation double A.max1 // equivalent to A.max.max(1) ---------------------------------------------------------------------- maximum elements #> A = [ 0,-1,2; 1,2,-4; 5,-3,-4 ] ; A = [ [ [ 0 1 5 -1 2 -3 #> A.max; ans = [ 5 #> A.max.max; ans = [ 5 ] #> A().max; ans = [ 5 ] #> A.max1; ans = 2 ] -4 ] -4 ] 2 2 ] 5 ---------------------------------------------------------------------double p.max ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 returns max{𝑎𝑖 } #> p = .[ -5,2,4 ] ; p = poly( 4 2 -5 ) = -5x^2 +2x +4 #> p .max ; ans = 4 ---------------------------------------------------------------------double A.max1k/maxk ---------------------------------------------------------------------#> A = [ 3,-1,2; 2,4,-3; -5,-3,-2 ] ; 125 [100] 300 Reference, Tutorial by www.msharpmath.com A = [ [ [ #> A.max1k; ans = 3 2 -5 -1 4 -3 2 ] -3 ] -2 ] 5 ---------------------------------------------------------------------void poly.maxcol(double n=10) ---------------------------------------------------------------------- set the maximum column length for screen output "void" means no data is returned by calling this function. The constant coefficient 𝑎0 is exempted from the counting. #> (1:20).dpoly; // 10 terms without a_0 ans = poly( 20 19 18 17 16 15 14 13 12 11 9 8 7 6 5 4 3 2 1 ) = x^19 +2x^18 +3x^17 +4x^16 +5x^15 +6x^14 +7x^13 +8x^12 +9x^11 +10x^10 +11x^9 +12x^8 +13x^7 +14x^6 +15x^5 +16x^4 +17x^3 +18x^2 +19x +20 10 #> poly.maxcol(5); (1:20).dpoly; // note the difference ans = 5 ans = poly( 20 19 18 17 16 15 // 5 terms without a_0 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ) = x^19 +2x^18 +3x^17 +4x^16 +5x^15 +6x^14 +7x^13 +8x^12 +9x^11 +10x^10 +11x^9 +12x^8 +13x^7 +14x^6 +15x^5 +16x^4 +17x^3 +18x^2 +19x +20 ---------------------------------------------------------------------matrix A.maxentry ---------------------------------------------------------------------- absolute maximum element #> A = [ 0,-1,2; 1,2,-4; 5,-3,-4 ] ; A = [ 0 -1 2 ] 126 [100] 300 [ [ Reference, 1 5 Tutorial by www.msharpmath.com 2 -3 #> A.maxentry; ans = -4 ] -4 ] 5 ---------------------------------------------------------------------double p.maxentry ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 returns max{|𝑎𝑖 |} #> p = .[ -5,2,4 ] ; p = poly( 4 2 -5 ) = -5x^2 +2x +4 #> p .maxentry ; ans = -5 ---------------------------------------------------------------------double A.maxentryk ---------------------------------------------------------------------linear index k for A.maxentry #> A = [ 3,-1,2; 2,4,-3; -5,-3,-2 ] ; A = [ [ [ 3 2 -5 -1 4 -3 #> A.maxentryk; ans = #> (i,j) = A.ij(ans); i = j = 2 ] -3 ] -2 ] 3 3 1 ---------------------------------------------------------------------matrix A.mean // column-first operation ---------------------------------------------------------------------- mean elements 127 [100] 300 Reference, Tutorial by www.msharpmath.com #> A = [ 0,-1,2; 1,2,-4; 5,-3,-4 ] ; A = [ 0 -1 [ 1 2 [ 5 -3 #> A.mean; ans = [ 2 #> A.mean.mean; ans = [ -0.222222 ] #> A().mean; ans = [ -0.222222 ] 2 ] -4 ] -4 ] -0.666667 -2 ] ---------------------------------------------------------------------double p.mean ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 returns ∑𝑛 𝑖=0 𝑎𝑖 /(𝑛 + 1) #> p = .[ -5,2,4 ] ; p = poly( 4 2 -5 ) = -5x^2 +2x +4 #> p .mean ; ans = 0.33333333 ---------------------------------------------------------------------double .min(double x1, double x2, ... ) // dot-only ---------------------------------------------------------------------- minimum of arguments #> .min(4,3); ans = 3 #> .min(4,3,2); ans = 2 #> .min(4,3,2,1); ans = 1 128 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.min // column-first operation double A.min1 // equivalent to A.min.min(1) ---------------------------------------------------------------------- minimum elements #> A = [ 0,-1,2; 1,2,-4; 5,-3,-4 ] ; A = [ [ [ 0 1 5 -1 2 -3 2 ] -4 ] -4 ] #> A.min; ans = [ 0 #> A.min.min; ans = [ -4 ] #> A().min; ans = [ -4 ] #> A.min1; ans = -3 -4 ] -4 ---------------------------------------------------------------------double p.min ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 returns min{𝑎𝑖 } #> p = .[ -5,2,4 ] ; p = poly( 4 2 -5 ) = -5x^2 +2x +4 #> p .min ; ans = -5 ---------------------------------------------------------------------double A.min1k/mink ---------------------------------------------------------------------#> A = [ 3,-1,2; 2,4,-3; -5,-3,-2 ] ; A = 129 [100] 300 [ [ [ Reference, 3 2 -5 Tutorial by www.msharpmath.com -1 4 -3 #> A.min1k; ans = 2 ] -3 ] -2 ] 3 ---------------------------------------------------------------------double A.minentry ---------------------------------------------------------------------- absolute minimum element #> A = [ 0,-1,2; 1,2,-4; 5,-3,-4 ] ; A = [ [ [ 0 1 5 -1 2 -3 2 ] -4 ] -4 ] #> A.minentry; ans = ---------------------------------------------------------------------double p.minentry ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 returns min{|𝑎𝑖 |} #> p = .[ -5,2,4 ] ; p = poly( 4 2 -5 ) = -5x^2 +2x +4 #> p .minentry ; ans = 2 ---------------------------------------------------------------------double A.minentryk ---------------------------------------------------------------------linear index k for A.minentry #> A = [ 3,-1,2; 2,4,-3; -5,-3,-2 ] ; A = [ 3 -1 2 ] 130 [100] 300 [ [ Reference, 2 -5 4 -3 #> A.minentryk; ans = #> (i,j) = A.ij(ans); i = j = Tutorial by www.msharpmath.com -3 ] -2 ] 4 1 2 ---------------------------------------------------------------------matrix A.minor(double i, double j) ---------------------------------------------------------------------- eliminates 𝑖-th row and 𝑗-th column 𝑎11 𝐀 = [𝑎21 𝑎31 𝑎12 𝑎22 𝑎32 𝑎13 𝑎 𝑎23 ], 𝐀. minor(2,1) = 𝐀21 = [ 12 𝑎32 𝑎33 #> A = (1:12)._4; A = [ 1 [ 2 [ 3 4 5 6 7 8 9 #> A.minor(2,3); ans = [ 1 [ 3 4 6 10 ] 12 ] 𝑎13 𝑎33 ] 10 ] 11 ] 12 ] ---------------------------------------------------------------------double A.mn ---------------------------------------------------------------------return 𝑚𝑛 for an 𝑚 × 𝑛 matrix 𝐀 #> A = (1:6)._3; A = [ 1 [ 2 ans = A.mn; 3 4 5 ] 6 ] 6 ---------------------------------------------------------------------double .mod (double a, double b) // dot-only 131 [100] 300 Reference, Tutorial by www.msharpmath.com matrix .mod (double a, matrix b) matrix .mod (matrix a, double b) matrix .mod (matrix a, matrix b) ---------------------------------------------------------------------a % b, remainder r of b = aq + r is equal to modulus a-b*floor(a/b) #> -13 % 5 ; ans = ans = 13 % 5 ; 2 3 #> .mod(-13,5); .mod(13,5); ans = 2 ans = 3 // a-b*floor(a/b) #> .rem(-13,5); .rem(13,5); ans = -3 ans = 3 // a-b*fix(a/b) ---------------------------------------------------------------------poly p.monic ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. monic = (𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + … + 𝑎𝑛−1 𝑥 𝑛−1 ) /𝑎𝑛 + 𝑥 𝑛 #> p = poly( 1,2,3,4,5 ) ; p = poly( 1 2 3 4 5 ) = 5x^4 +4x^3 +3x^2 +2x +1 #> p .monic; ans = poly( 0.2 0.4 0.6 0.8 1 ) = x^4 +0.8x^3 +0.6x^2 +0.4x +0.2 ---------------------------------------------------------------------matrix A.mul(double i, double s) ---------------------------------------------------------------------- elementary row operation, R_i = R_i * s 𝑅𝑖 𝑠𝑅𝑖 𝐀 = [ ⋯ ] where 𝑅𝑖 is 𝑖 -th row. A.mul(i,s) = [ ⋯ ] 𝑅𝑗 𝑅𝑗 #> A = [ 2,1,4 ; -1,3,5 ]; 132 [100] 300 Reference, Tutorial by www.msharpmath.com A = [ [ 2 -1 1 3 4 ] 5 ] #> A.mul(2, 7); ans = [ 2 [ -7 1 21 4 ] 35 ] [ N ] ---------------------------------------------------------------------Spherical Bessel Function double .n_(double nu)(double x) dot-only matrix .n_(double nu)(matrix x) matrix .n_(matrix nu)(double x) matrix .n_(matrix nu)(matrix x) ---------------------------------------------------------------------- 𝜋 𝐽 (𝑥) 2𝑥 −𝑛−1/2 cos 𝑥 𝑛0 (𝑥) = − 𝑥 𝑛𝑛 (𝑥) = (−1)𝑛+1 √ #> .n_0(0.5); -cos(0.5)/0.5; ans = -1.7551653 ans = -1.7551651 #> .n_0( [0.5, 1] ); ans = [ -1.75517 -0.540302 ] #> .n_[0;1]( [0.5, 1] ); ans = [ -1.75517 -0.540302 ] [ -4.46918 -1.38177 ] ---------------------------------------------------------------------double p.n ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. n = 𝑛 #> p = (1:5).dpoly; 133 [100] 300 Reference, Tutorial by www.msharpmath.com p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p.n; ans = 4 ---------------------------------------------------------------------double A.n ---------------------------------------------------------------------return 𝑛 for an 𝑚 × 𝑛 matrix 𝐀 #> A = (1:6)._3; A = [ 1 [ 2 ans = A.n; 3 4 5 ] 6 ] 3 ---------------------------------------------------------------------double .ncr (double x, double y) // dot-only matrix .ncr (double a, matrix b) matrix .ncr (matrix a, double b) matrix .ncr (matrix a, matrix b) ---------------------------------------------------------------------- Combination 𝑛 𝐶𝑟 #> .ncr(7,3); ans = = 𝑛! (𝑛 − 𝑟)! 𝑟! // (7)(6)(5)/((3)(2)(1)) 35 ---------------------------------------------------------------------double nextpow2(double x) matrix nextpow2(matrix x) ---------------------------------------------------------------------- nextpow2(𝑥) = 𝑛, #> nextpow2(7); nextpow2(9); ans = 3 ans = 4 #> nextpow2([7,9]); ans = [ 3 // 2^3 = 8 4 ] 134 2𝑛 ≥ |𝑥| [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------double p.newton(matrix x) ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. newton(𝑥) = 𝑎0 + 𝑎1 (𝑥 − 𝑥1 ) + 𝑎2 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) + ⋯ %> 1 + 2(x-3) + 3(x-3)(x-4) + 4(x-3)(x-4)(x-5) #> poly(1,2,3,4) .newton( [3,4,5] ); ans = poly( -209 169 -45 4 ) = 4x^3 -45x^2 +169x -209 #> 1 + 2*[3].roots + 3*[3,4].roots + 4*[3,4,5].roots ; ans = poly( -209 169 -45 4 ) = 4x^3 -45x^2 +169x -209 #> 1 + 2*.[1,-3] + 3*.[1,-3]*.[1,-4] + 4*.[1,-3]*.[1,-4]*.[1,-5] ; ans = poly( -209 169 -45 4 ) = 4x^3 -45x^2 +169x -209 ---------------------------------------------------------------------double .norm1(double x1,x2, … ) // dot-only ---------------------------------------------------------------------- |𝑥1 | + |𝑥2 | + |𝑥3 | + ⋯ + |𝑥𝑛 | #> .norm1( -1,2,-3, -4 ); ans = 10 ---------------------------------------------------------------------double .norm2(double x1,x2, … ) // dot-only double hypot(double x1,x2, … ) ---------------------------------------------------------------------1 (𝑥12 + 𝑥22 + 𝑥32 + ⋯ + 𝑥𝑛2 )2 #> .norm2( -1,2,-3, -4 ); ans = 5.4772256 // equal to "hypot" ---------------------------------------------------------------------double .norm22(double x1,x2, … ) // dot-only ---------------------------------------------------------------------- 𝑥12 + 𝑥22 + 𝑥32 + ⋯ + 𝑥𝑛2 135 [100] 300 Reference, Tutorial by www.msharpmath.com #> .norm22( -1,2,-3, -4 ); ans = 30 ---------------------------------------------------------------------double A.norm(double p) ---------------------------------------------------------------------- p-norm (vector or matrix) ||𝐀||𝑝 = max 𝑥≠0 ||𝐀𝐱||𝑝 ||𝐱||𝑝 available only for a vector. under development ---------------------------------------------------------------------double A.norm1 ---------------------------------------------------------------------- ||𝐀||1 = max ∑𝑚 𝑖=1 |𝑎𝑖𝑗 | 1≤𝑗≤𝑛 // column sum #> A = [ 1,2,3; 4,5,6; 7,8,9 ]; A = [ [ [ #> A.norm1; ans = #> A().norm1 ; ans = 1 4 7 2 5 8 3 ] 6 ] 9 ] // case of matrix 18 // case of vector 45 ---------------------------------------------------------------------double A.norm2 ---------------------------------------------------------------------𝑚 𝑛 1/2 ||𝐀||𝐹 = (∑ ∑ |𝑎𝑖𝑗 |2 ) 𝑖=1 𝑗=1 Frobenius norm, sqrt(A**A) #> A = [ 1,2; 3,4 ]; A = [ 1 2 ] 136 [100] 300 [ Reference, 3 Tutorial by www.msharpmath.com 4 ] #> A.norm2; // sqrt( 1^2 +2^2 + 3^2 + 4^2 ) = sqrt(30) ans = 5.4772256 ---------------------------------------------------------------------double A.norm22 ---------------------------------------------------------------------𝑚 𝑛 ∑ ∑ |𝑎𝑖𝑗 |2 𝑖=1 𝑗=1 A.norm22 = A.norm2 * A.norm2 = A**A #> A = [ 2,1,4 ; -1,3,5 ]; A = [ 1 [ 3 #> A.norm22; ans = // 2 ] 4 ] 1^2 +2^2 + 3^2 + 4^2 = 30 30 ---------------------------------------------------------------------double A.norminf ---------------------------------------------------------------------- ||𝐀||∞ = max ∑𝑛𝑗=1 |𝑎𝑖𝑗 | // row sum 1≤𝑖≤𝑛 infinite-norm #> A = [ 1,2,3; 4,5,6; 7,8,9 ]; A = [ [ [ #> A.norminf; ans = 1 4 7 // case of matrix 2 5 8 3 ] 6 ] 9 ] 24 ---------------------------------------------------------------------double .npr (double x, double matrix .npr (double a, matrix matrix .npr (matrix a, double matrix .npr (matrix a, matrix y) b) b) b) // dot only 137 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------- Permutation 𝑛 𝑃𝑟 #> .npr(7,3); ans = = 𝑛! (𝑛 − 𝑟)! // (7)(6)(5) 210 [ O ] ---------------------------------------------------------------------poly p.odd ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. odd = 𝑎1 𝑥 + 𝑎3 𝑥 3 + 𝑎5 𝑥 5 + 𝑎7 𝑥 7 + … #> p = poly( 1,2,3,4,5,6,7 ) ; p = poly( 1 2 3 4 5 6 7 ) = 7x^6 +6x^5 +5x^4 +4x^3 +3x^2 +2x +1 #> p .odd; ans = poly( 0 2 0 4 = 6x^5 +4x^3 +2x 0 6 ) ---------------------------------------------------------------------void .off // dot-only ---------------------------------------------------------------------.off 'dot-off' default mode both 'ones' and '.ones' work ---------------------------------------------------------------------void .on // dot-only ---------------------------------------------------------------------.on 'dot-on' mode activates dot function only. ---------------------------------------------------------------------- 138 [100] 300 Reference, Tutorial by www.msharpmath.com matrix .ones(double m, double n = m) ---------------------------------------------------------------------- all-ones matrix #> .ones(2,4); ans = [ 1 [ 1 1 1 1 1 #> .ones(3); [ [ [ 1 1 1 1 ] 1 ] 1 ] 1 1 1 1 ] 1 ] ---------------------------------------------------------------------vertex cs.org ---------------------------------------------------------------------- The origin of a coordinate system #> cs1 = csys.rec ; cs1 = 'rec' local csys org = < 0 dir = [ 1 [ [ 0 0 #> cs1.org ; ans = < #> cs1.dir ; ans = [ [ [ 0 0 ] 1 0 0 1 0 0 0 > 0 0 ] 1 ] 0 0 > 0 1 0 0 ] 0 ] 1 ] [ P ] ---------------------------------------------------------------------double .P_n(double x) dot-only matrix .P_n(matrix x), for real-part only ---------------------------------------------------------------------- 139 [100] 300 Reference, Tutorial by www.msharpmath.com Legendre polynomial #> .P_5(0.3); ans = 0.34538625 #> .P_5( [0.3,0.4] ); ans = [ 0.345386 0.27064 ] #> .hold; for.n(1,5) plot.x(-1,1) ( .P_n(x) ); plot; ---------------------------------------------------------------------poly poly.P(double n) ---------------------------------------------------------------------- Legendre polynomial of degree 𝑛 𝑃0 (𝑥) = 1, 𝑃1 (𝑥) = 𝑥 𝑃𝑛 (𝑥) = (1/𝑛)[(2𝑛 − 1)𝑥𝑃𝑛−1 (𝑥) − (𝑛 − 1)𝑃𝑛−2 (𝑥)] This command is for finding a polynomial itself. For evaluation, it is more convenient to use .P_n(x) #> for.n(0,5) poly.P(n); ans = = ans = = ans = = ans = = ans = = ans = = poly( 1 ) 1 poly( 0 1 ) x poly( -0.5 0 1.5 ) 1.5x^2 -0.5 poly( -0 -1.5 0 2.5 ) 2.5x^3 -1.5x poly( 0.375 -0 -3.75 0 4.375x^4 -3.75x^2 +0.375 poly( 0 1.875 -0 -8.75 7.875x^5 -8.75x^3 +1.875x 4.375 ) 0 7.875 ) 140 [100] 300 Reference, Tutorial by www.msharpmath.com #> .hold; for.n(0,5) plot.x(-1,1) ( .P_n(x) ); plot; ---------------------------------------------------------------------matrix A.pass ---------------------------------------------------------------------- a line passing two points a parabola passing three points a cubic passing four points #> [ 1,1, 2,3 ].pass; y = 2x - 1, root = 0.5 #> [ 1,1, 2,3, 3,7 ].pass; y = x^2 - x + 1 roots [ 0.5 - i 0.866025 [ 0.5 + i 0.866025 vertex at ( 0.5, 0.75 ) y_minimum = 0.75 y-axis intersect ( 0, 1 ) symmetry at x = 0.5 directrix at y = 0.5 focus at ( 0.5, 1 ) ] ] #> [ 1,1, 2,3, 3,7, 5,16 ].pass; y = -0.208333x^3 + 2.25x^2 - 3.29167x + 4.25 y = (1/24)*( -5x^3 + 54x^2 - 79x + 102 ) roots [ 9.34253 ] [ 0.728734 - i 1.2855 ] [ 0.728734 + i 1.2855 ] local minima ( 0.826314, 2.9488 ) local maxima ( 6.37369, 20.7312 ) inflection pt. ( 3.6, 11.84 ) y-axis intersect ( 0, 4.25 ) 141 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------void csys.pass0 // pass through -x coordinate, 0 <= t < 2*pi void csys.pass180 // pass through +x coordinate, -pi < t <= pi ---------------------------------------------------------------------#> csys.pass180; < 1, -1, 0 >.cyl ; ans = < 1.414 5.498 0 > #> csys.pass0; ans = < < 1, -1, 0 >.cyl ; 1.414 -0.7854 0 > ---------------------------------------------------------------------matrix A.pcent ---------------------------------------------------------------------a_ij/sum(a_ij) * 100 𝐴 = (𝑎𝑖𝑗 ), #> A = [ 3, 4; 7, 6 ]; A = [ 3 [ 7 #> A.pcent; ans = [ 15 [ 35 #> ans.sum1; ans = 100 𝑎𝑖𝑗 ( ) × 𝟏𝟎𝟎 𝑎11 + 𝑎12 + ⋯ + 𝑎21 + ⋯ + 𝑎𝑚𝑛 4 ] 6 ] 20 ] 30 ] ---------------------------------------------------------------------figure A.plot ---------------------------------------------------------------------plot ---------------------------------------------------------------------(matrix P, matrix L, matrix U) = A.plu // tuple ---------------------------------------------------------------------A = PLU decomposition #> A = [ 0,2,1; 3,2; 4,8,-16 ]; (P,L,U) = A.plu; A = [ 0 2 1 ] [ 3 2 0 ] 142 [100] 300 [ Reference, Tutorial by www.msharpmath.com 4 8 -16 ] 0 0 1 0 1 0 1 ] 0 ] 0 ] 1 0.75 0 0 1 -0.5 0 ] 0 ] 1 ] 4 0 0 8 -4 0 -16 ] 12 ] 7 ] P = [ [ [ L = [ [ [ U = [ [ [ ---------------------------------------------------------------------double .pm (double x) dot-only matrix .pm (matrix A) double x .pm ---------------------------------------------------------------------- cos 𝜋𝑛 = (−1)𝑛 #> .pm(0); .pm(1); .pm(2); ans = 1 ans = -1 ans = 1 ans = 1 #> .pm( [0,1,2] ); ans = [ 2.pm; 1 -1 1 ] ---------------------------------------------------------------------matrix A.pm ---------------------------------------------------------------------- alternating signs (plus and minus) by (−1)𝑖+𝑗 𝑎𝑖𝑗 #> A = (1:20)._5; A = [ 1 5 [ 2 6 [ 3 7 [ 4 8 9 10 11 12 13 14 15 16 17 18 19 20 #> A.pm; ans = 143 ] ] ] ] [100] 300 [ [ [ [ 1 -2 3 -4 -5 6 -7 8 Reference, 9 -10 11 -12 Tutorial by www.msharpmath.com -13 14 -15 16 17 -18 19 -20 ] ] ] ] ---------------------------------------------------------------------poly p.pm ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. pm = 𝑎0 − 𝑎1 𝑥 + 𝑎2 𝑥 2 − 𝑎3 𝑥 3 + ⋯ + (−1)𝑛 𝑎𝑛 𝑥 𝑛 #> p = .[ 1,2,3,4,5 ]; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p .pm ; // actually cos(n*pi) = (-1)^n ans = poly( 5 -4 3 -2 1 ) = x^4 -2x^3 +3x^2 -4x +5 ---------------------------------------------------------------------poly .polyfit(matrix X, matrix Y, double kth) ---------------------------------------------------------------------- polynomial regression #> x = [ 1,2,3,4 ]; y = [ 2,3,5,9 ]; x = [ 1 2 y = [ 2 3 #> .polyfit(x,y,1); ans = poly( -1 2.3 ) = 2.3x -1 #> .polyfit(x,y,2); ans = poly( 2.75 -1.45 0.75 ) = 0.75x^2 -1.45x +2.75 #> .polyfit(x,y,3); ans = poly( 1 1.33333 -0.5 0.166667 ) = 0.166667x^3 -0.5x^2 +1.33333x +1 #> .polyfit(x,y,1).plot(1,4); #> plot+(x,y); 144 3 5 4 ] 9 ] [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------Power of a real number double pow (double a, double b) matrix pow (double a, matrix b) matrix pow (matrix a, double b) matrix pow (matrix a, matrix b) ---------------------------------------------------------------------- pow(𝑎, 𝑏) = 𝑎𝑏 #> pow(3,2); ans = // 3^2 9 ---------------------------------------------------------------------poly p.pow(double k) ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. pow(𝑘) = 𝑎0 + 𝑎1 𝑥 𝑘 + 𝑎2 𝑥 2𝑘 + 𝑎3 𝑥 3𝑘 … + 𝑎𝑛 𝑥 𝑛𝑘 #> p = .[ 1,2,3,4,5 ]; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p .pow(3) ; // replace x by x^3 ans = poly( 5 0 0 4 0 0 3 0 0 0 1 ) = x^12 +2x^9 +3x^6 +4x^3 +5 2 0 ---------------------------------------------------------------------double pow2(double x) matrix pow2(matrix x) ---------------------------------------------------------------------- pow2(𝑥) = 2𝑥 #> pow2(10); 145 [100] 300 ans = Reference, Tutorial by www.msharpmath.com 1024 ---------------------------------------------------------------------double pow10(double x) matrix pow10(matrix x) ---------------------------------------------------------------------- pow10(𝑥) = 10𝑥 #> pow10(2); ans = 100 ---------------------------------------------------------------------matrix .primes (double a, double b) ---------------------------------------------------------------------- primes numbers between a and b #> .primes(10,100); ans = [ 11 13 17 47 53 59 97 ] 19 61 23 67 29 71 31 73 37 79 41 83 43 89 ---------------------------------------------------------------------matrix A.prod // column-first operation double A.prod1 // equivalent to A.prod.prod(1) ---------------------------------------------------------------------- product of elements #> A = [ 1,-1,2; 1,2,-4; 5,-3,-4 ] ; A = [ 1 -1 [ 1 2 [ 5 -3 #> A.prod; ans = [ 5 #> A().prod; ans = [ 960 ] #> A.prod1; ans = 960 2 ] -4 ] -4 ] 6 146 32 ] [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------double p.prod ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. prod = 𝑎0 𝑎1 𝑎2 𝑎3 × … × 𝑎𝑛 #> p = .[ -5,2,4 ] ; p = poly( 4 2 -5 ) = -5x^2 +2x +4 #> p .prod ; ans = -40 ---------------------------------------------------------------------double .psi(double x) dot only matrix .psi(matrix x) ---------------------------------------------------------------------- 1 1 1 1 𝜓(𝑛 > 1) = −𝛾 + + + + ⋯ + 1 2 3 𝑛−1 𝜓(1) = −𝛾, #> .psi( 1:10 ); ans = [ -0.577216 1.50612 1.70612 0.422784 1.87278 0.922784 2.01564 1.25612 2.14064 ---------------------------------------------------------------------matrix A.pushd ---------------------------------------------------------------------- push-down with top row #> A = (1:20)._5; A = [ 1 5 [ 2 6 [ 3 7 [ 4 8 9 10 11 12 13 14 15 16 17 18 19 20 ] ] ] ] #> A.pushd; ans = [ 1 [ 2 [ 3 [ 4 0 0 9 10 0 0 0 13 0 0 0 0 ] ] ] ] 0 5 6 7 ---------------------------------------------------------------------matrix A.pushl 147 2.25175 ] [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------- push-left with rightmost column #> A = (1:20)._5; A = [ 1 5 [ 2 6 [ 3 7 [ 4 8 9 10 11 12 13 14 15 16 17 18 19 20 ] ] ] ] #> A.pushl; ans = [ 1 [ 6 [ 11 [ 16 9 14 19 0 13 18 0 0 17 0 0 0 ] ] ] ] 5 10 15 20 ---------------------------------------------------------------------matrix A.pushr ---------------------------------------------------------------------- push-right with leftmost column #> A = (1:20)._5; A = [ 1 5 [ 2 6 [ 3 7 [ 4 8 #> A.pushr; ans = [ 1 [ 0 [ 0 [ 0 9 10 11 12 13 14 15 16 17 18 19 20 ] ] ] ] 9 6 3 0 13 10 7 4 17 14 11 8 ] ] ] ] 5 2 0 0 ---------------------------------------------------------------------matrix A.pushu ---------------------------------------------------------------------- push-up with bottom row #> A = (1:20)._5; A = [ 1 5 9 13 17 ] 148 [100] 300 [ [ [ 2 3 4 #> A.pushu; ans = [ 1 [ 2 [ 3 [ 4 Reference, Tutorial by www.msharpmath.com 6 7 8 10 11 12 14 15 16 18 ] 19 ] 20 ] 6 7 8 0 11 12 0 0 16 0 0 0 0 0 0 0 ] ] ] ] [ Q ] ---------------------------------------------------------------------(matrix Q, matrix R) = A.qr // tuple ---------------------------------------------------------------------- A = QR decomposition #> A = [ 1,-1,3; 1,-1,-2; 0,1,1 ]; A = [ 1 -1 [ 1 -1 [ 0 1 3 ] -2 ] 1 ] #> (Q,R) = A.qr;; Q.trun10; R.trun10; ans = [ 0.707107 0 0.707107 [ 0.707107 0 -0.707107 [ 0 1 0 ans = [ 1.41421 -1.41421 0.707107 [ 0 1 1 [ 0 0 3.53553 matrix user_qr(matrix& A) { n = A.m; Q = A.unit; S = .I(n); for.i(2,n) { x = Q.col(i-1); S -= x * x.tr; Q.col(i) = ( S * Q.col(i) ).unit.trun12; 149 ] ] ] ] ] ] [100] 300 Reference, Tutorial by www.msharpmath.com } return Q; } #> sqrt(2)*user_qr(A); // user function listed above ans = [ 1 0 1 ] [ 1 0 -1 ] [ 0 1.41421 0 ] ---------------------------------------------------------------------poly p.quad(double r, double s) ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 divisor 𝑥 2 + 𝑟𝑥 + 𝑠 by Bairstow method #> .[1,12,62,172,221] .quad(0,0); // starting from x^2 ans = poly( 13 4 1 ) = x^2 +4x +13 // internal iteration by the Bairstow method is as follows Iter r s [ 0 0 0 ] [ 1 2.08429 3.56452 ] [ 2 3.65639 7.37287 ] [ 3 4.75654 12.2209 ] [ 4 3.70532 12.7884 ] [ 5 3.97725 12.9056 ] [ 6 4.00016 12.9994 ] [ 7 4 13 ] [ 8 4 13 ] [ R ] ---------------------------------------------------------------------double v.R // both read/write double v.P double v.T ---------------------------------------------------------------------%> spherical coordinate -----------------------------------------------#> v = < 1,1,1 > ; v = < 1 1 1 > 150 [100] 300 Reference, Tutorial by www.msharpmath.com #> v.R; v.P; v.T; ans = 1.7320508 ans = 0.95531662 ans = 0.78539816 #> v.P += 1; v.R; v.P; v.T; // only P is changed v = < 1.135 1.135 -0.6497 > ans = 1.7320508 ans = 1.95531662 ans = 0.78539816 ---------------------------------------------------------------------double z.r // both read/write double z.t ---------------------------------------------------------------------z.r |z| = sqrt(x*x+y*y) z.t arg = atan(y/x) z.deg deg = atan(y/x)*180/pi #> z = 3+4!; z = 3 + 4! #> z.r = 10; ans = z; 10 z = 6 + 8! #> z = 3+4!; z = 3 + 4! #> z.t = pi/2; z; ans = 1.5707963 z = 3.06152e-016 + 5! ---------------------------------------------------------------------double v.r // both read/write double v.t ---------------------------------------------------------------------%> cylindrical coordinate ---------------------------------------------#> v = < 1,1,1 > ; v = < 1 1 1 > 151 [100] 300 Reference, Tutorial by www.msharpmath.com #> v.r; v.t; v.z; ans = 1.4142136 ans = 0.78539816 ans = 1 #> v.r += 1; v.r; v.t; v.z; // only r is changed v = < 1.707 1.707 1 > ans = 2.4142136 ans = 0.78539816 ans = 1 ---------------------------------------------------------------------void csys.rad void csys.deg ---------------------------------------------------------------------- angles in the degree system #> csys.deg; < 1, 45, 0, cyl > ; ans = < 0.7071 0.7071 0 > #> csys.rad; < 1, pi/4, ans = < 0.7071 0 > 0, cyl > ; 0.7071 ---------------------------------------------------------------------matrix .rand (double m, double n = m) ---------------------------------------------------------------------- random number matrix #> .rand(3,4); [ [ [ ans = 0.00041 0.18467 0.06334 0.265 0.19169 0.15724 0.11478 0.29358 0.26962 0.24464 ] 0.05705 ] 0.28145 ] ---------------------------------------------------------------------double A.rank ---------------------- ------------------------------------------------ rank of a matrix A 152 [100] 300 Reference, %> rank of a matrix #> A = [ 1,2; -1,-2; 2,4 ]; A = [ 1 [ -1 [ 2 ans = Tutorial by www.msharpmath.com A.rank; 2 ] -2 ] 4 ] 1 #> A = [ 1,-1,2; 2,-2,4 ]; A = [ 1 A.rank; -1 2 ] [ ans = -2 4 ] 2 1 #> A = [ 1,-1,2; 2,-2,3 ]; A = A.rank; [ [ ans = -1 -2 1 2 2 ] 3 ] 2 ---------------------------------------------------------------------matrix A.ratio(double denom=10000) ---------------------------------------------------------------------- shows a rational approximation #> A = matrix.hilb(4); A = [ 1 [ 0.5 [ 0.333333 [ 0.25 #> A.ratio; (1/420) x [ 420 [ 210 [ 140 [ 105 0.5 0.333333 0.25 0.2 0.333333 0.25 0.2 0.166667 0.25 0.2 0.166667 0.142857 ] ] ] ] 210 140 105 84 140 105 84 70 105 84 70 60 ] ] ] ] ---------------------------------------------------------------------poly p.ratio ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 153 [100] 300 Reference, Tutorial by www.msharpmath.com 1 p. ratio = ( ) (𝑏0 + 𝑏1 𝑥 + 𝑏2 𝑥 2 + 𝑏3 𝑥 3 + … + 𝑏𝑛 𝑥 𝑛 ) lcm shows a rational approximation #> p = .[ 1/2, 1/3, 1/4, 1/5 ]; p = poly( 0.2 0.25 0.333333 0.5 ) = 0.5x^3 +0.333333x^2 +0.25x +0.2 #> p .ratio ; ans = (1/60) * poly( 12 15 20 30 ) ---------------------------------------------------------------------double A.ratio1 ---------------------------------------------------------------------- denomenator of a rational approximation #> A = matrix.hilb(4); A = [ 1 0.5 [ 0.5 0.333333 [ 0.333333 0.25 [ 0.25 0.2 #> A.ratio1; ans = #> A.ratio; (1/420) x [ [ [ [ 0.333333 0.25 0.2 0.166667 0.25 0.2 0.166667 0.142857 ] ] ] ] 140 105 84 70 105 84 70 60 ] ] ] ] 420 420 210 140 105 210 140 105 84 ---------------------------------------------------------------------matrix A.read(string fname) ---------------------------------------------------------------------- read A from a file ---------------------------------------------------------------------matrix W.real // both read/write ---------------------------------------------------------------------- real part 154 [100] 300 Reference, Tutorial by www.msharpmath.com #> A = [ 1-2i, -3+4i; 2+3i, 5+2i ]; A = [ 1 - i 2 [ 2 + i 3 #> A.real; ans = [ [ 1 2 -3 + i 4 5 + i 2 ] ] 97 + i 4 105 + i 2 ] ] -3 ] 5 ] #> A.real += 100; A = [ 101 - i 2 [ 102 + i 3 ---------------------------------------------------------------------vertex v.rec ---------------------------------------------------------------------- read in rectangular coordinate #> < 1,1,1 > .rec ; ans = < 1 1 1 > ---------------------------------------------------------------------csys csys.rec ---------------------------------------------------------------------- a rectangular coordinate system #> cs1 = csys.rec ; cs1 = 'rec' local csys org = < 0 dir = [ [ [ 1 0 0 0 0 > 0 1 0 0 ] 0 ] 1 ] ---------------------------------------------------------------------matrix .regress(matrix X, matrix Y, double kth) matrix .regress(matrix X, matrix Y, matrix f(double x)) ---------------------------------------------------------------------- regression curve 155 [100] 300 Reference, 𝑢1 (𝑥1 ) 𝑢1 (𝑥2 ) 𝑢2 (𝑥1 ) 𝑢2 (𝑥2 ) 𝐔=[ ⋮ ⋮ 𝑢𝑟 (𝑥1 ) 𝑢𝑟 (𝑥2 ) Tutorial by www.msharpmath.com ⋯ ⋯ ⋯ ⋯ ⋮ ⋮ ⋯ ⋯ 𝑓1 𝑢1 (𝑥𝑛 ) 𝑎1 𝑓2 𝑢2 (𝑥𝑛 ) 𝑎 ] , 𝐚 = [ 2] , 𝐟 = ⋮ ⋮ ⋮ ⋮ 𝑎𝑟 𝑢𝑟 (𝑥𝑛 ) [𝑓𝑛 ] 𝐔𝐔𝑇 𝐚 = 𝐔𝐟 #> x = [ 1,2,3,4 ]; f = [ 2,3,5,9 ]; x = [ 1 2 f = [ 2 3 3 5 4 ] 9 ] #> matrix mftn(double x) = [ 1 ; exp(0.4*x) ]; // base functions #> a = .regress(x,f, mftn); // built-in dot function a = [ -1.37806 2.0443 ] 𝑓(𝑥) = −1.378061 + 2.044303𝑒 0.4𝑥 #> mftn++(x); // ans = [ 1 [ 1.49182 matrix(double) can be upgraded by ++ postfix 1 2.22554 𝐔=[ #> F = a * mftn++(x); F = [ 1.67168 1 3.32012 1 𝑒 0.4𝑥1 1 𝑒 0.4𝑥2 // fitted values 3.17162 #> .corrcoef(f,F); ans = 0.99357009 1 ] 4.95303 ] 1 𝑒 0.4𝑥3 1 ] 𝑒 0.4𝑥4 5.40926 8.74744 ] // correlation coefficient ---------------------------------------------------------------------double .rem (double a, double b) // dot-only matrix .rem (double a, matrix b) matrix .rem (matrix a, double b) matrix .rem (matrix a, matrix b) ---------------------------------------------------------------------remainder a-b*fix(a/b) #>.rem(-13,5); .rem(13,5); ans = -3 ans = 3 // a-b*fix(a/b) #>.mod(-13,5); ans = // a-b*floor(a/b) .mod(13,5); 2 156 [100] 300 Reference, ans = Tutorial by www.msharpmath.com 3 ---------------------------------------------------------------------matrix A.repmat(double m, double n=m) ---------------------------------------------------------------------- extends a 𝑝 × 𝑞 matrix to 𝑚𝑝 × 𝑛𝑞 matrix by tile-shaped copying #> matrix.format("%5g"); #> A = [ 1,2,3; 4,5 ]; A = [ 1 2 3 ] [ 4 5 0 ] #> A.repmat(3,4); ans = [ 1 2 3 [ 4 5 0 [ 1 2 3 [ 4 5 0 [ 1 2 3 [ 4 5 0 1 4 1 4 1 4 matrix.maxcol(20): 2 5 2 5 2 5 3 0 3 0 3 0 1 4 1 4 1 4 2 5 2 5 2 5 3 0 3 0 3 0 1 4 1 4 1 4 2 5 2 5 2 5 3 0 3 0 3 0 ] ] ] ] ] ] ---------------------------------------------------------------------matrix A.resize(double m, double n) ---------------------------------------------------------------------- A 𝑝 × 𝑞 matrix becomes a 𝑚 × 𝑛 matrix by resizing. #> A = [ 1,2,3; 4,5,6 ]; A = [ [ 1 4 2 5 3 ] 6 ] #> A.resize(4,6); ans = [ [ [ [ 1 4 0 0 2 5 0 0 3 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ] ] ] ] #> A.resize(4,2); ans = [ 1 2 ] 157 [100] 300 [ [ [ 4 0 0 Reference, Tutorial by www.msharpmath.com 5 ] 0 ] 0 ] #> A.resize(1,5); ans = [ 1 2 3 0 0 ] ---------------------------------------------------------------------matrix A.rev ---------------------------------------------------------------------- reverse elements, rotating 180 degree 𝑎1 𝐀 = [𝑎2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎8 𝑎9 𝑎10 𝑎11 ] , 𝑎12 𝑎12 𝐀. rev = [𝑎11 𝑎10 𝑎9 𝑎8 𝑎7 𝑎6 𝑎5 𝑎4 𝑎3 𝑎2 ] 𝑎1 #> A = (1:6)._2; A = [ 1 4 ] [ 2 5 ] [ 3 6 ] #> A.rev; ans = [ 6 [ 5 [ 4 3 ] 2 ] 1 ] ---------------------------------------------------------------------poly p.rev ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 + … p. rev = 𝑎𝑛 + 𝑎𝑛−1 𝑥 + 𝑎𝑛−2 𝑥 2 + 𝑎𝑛−3 𝑥 3 + … + 𝑎0 𝑥 𝑛 #> p = .[ 1,2,3,4,5 ] ; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p .rev ; ans = poly( 1 2 3 4 5 ) = 5x^4 +4x^3 +3x^2 +2x +1 ---------------------------------------------------------------------- 158 [100] 300 Reference, Tutorial by www.msharpmath.com void poly.right(string fmt) ---------------------------------------------------------------------- set the right delimiter for screen output #> p = poly( 1,2,3 ) ; p = poly( 1 2 3 ) = 3x^2 +2x +1 #> poly.right ("[]()left<>--->") ; p ; p = poly( 1 2 3 []()left<>---> = 3x^2 +2x +1 ---------------------------------------------------------------------matrix A.rot90 ---------------------------------------------------------------------- rotate 90 degree (counter-clockwise) 𝑎1 𝐀 = [ 𝑎2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎8 𝑎9 𝑎10 𝑎11 ] , 𝑎12 #> A = [ 1,2; 3,4; 5,6 ]; A = [ 1 [ 3 [ 5 2 ] 4 ] 6 ] #> A.rot90 ; ans = [ [ 4 3 2 1 𝑎10 𝑎7 𝐀. rot90 = [ 𝑎 4 𝑎1 𝑎11 𝑎8 𝑎5 𝑎2 𝑎12 𝑎9 𝑎6 ] 𝑎3 6 ] 5 ] ---------------------------------------------------------------------matrix A.rotm90 ---------------------------------------------------------------------- 𝑎1 𝐀 = [𝑎2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎8 𝑎9 #> A = [ 1,2; 3,4; 5,6 ]; A = [ 1 𝑎10 𝑎11 ] , 𝑎12 𝑎3 𝑎6 𝐀. rotm90 = [ 𝑎 9 𝑎12 2 ] 159 𝑎2 𝑎5 𝑎8 𝑎11 𝑎1 𝑎4 𝑎7 ] 𝑎10 [100] 300 [ [ #> A.rotm90; ans = [ [ Reference, 3 5 4 ] 6 ] 5 6 3 4 Tutorial by www.msharpmath.com 1 ] 2 ] ---------------------------------------------------------------------poly .roots (double x1,x2, ..., xn) dot-only poly .roots (matrix A); ---------------------------------------------------------------------- polynomial with given roots #> A = [ 1+1!; 1-1! ]; A = [ [ // 1! = 1i = 1j 1 + i 1 1 - i 1 for digits ] ] #> .roots(A); // complex roots ans = poly( 2 = x^2 -2x +2 -2 1 ) ---------------------------------------------------------------------poly A.roots ---------------------------------------------------------------------(x-x1)(x-x2) ... (x-xn) #> A = [ 0,1,2,3 ]; A = [ 0 1 2 3 ] #> A.roots; ans = poly( 0 -6 11 -6 1 ) = x^4 -6x^3 +11x^2 -6x ---------------------------------------------------------------------double .round (double x) matrix .round (matrix A) ---------------------------------------------------------------------- integer near 𝑥 160 [100] 300 #> .round(pi); ans = Reference, Tutorial by www.msharpmath.com 3 #> .round( [pi, -pi] ); ans = [ 3 -3 ] ---------------------------------------------------------------------double x.round matrix A.round poly p.round ---------------------------------------------------------------------- integer near 𝑥 #> pi.round; ans = #> (-pi).round; ans = 3 -3 For a polynomial and a matrix, element-by-element operation is applied. ---------------------------------------------------------------------matrix W.row(i) // equivalent to W(i,*) ---------------------------------------------------------------------- i-th row vector Note that this function allows to modify the original matrix. #> A = (1:12)._4 ; A = [ 1 [ 2 [ 3 #> A.row(2); ans = [ #> A.row(2) += 100; A = [ 1 [ 102 [ 3 4 5 6 2 7 8 9 5 4 105 6 10 ] 11 ] 12 ] 8 7 108 9 161 11 ] 10 ] 111 ] 12 ] [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.rowabs ---------------------------------------------------------------------- finds Euclidean norm of each row #> A = [ 1,2; 3,4 ]; A = [ 1 [ 3 2 ] 4 ] #> A.rowabs; // [ sqrt(1+4); sqrt(9+16) ] ans = [ 2.23607 ] [ 5 ] ---------------------------------------------------------------------matrix A.rowdel(double i) ---------------------------------------------------------------------- delete 𝑖-th row #> A = [ 1,2; 3,4; 5,6 ]; A = [ 1 [ 3 [ 5 2 ] 4 ] 6 ] #> A.delrow(2); ans = [ 1 [ 5 2 ] 6 ] ---------------------------------------------------------------------matrix A.rowdet(i) ---------------------------------------------------------------------- i-th row determinant Consider the Laplace expansion of a determinant 𝑎11 𝑎21 𝐀 = [𝑎 31 𝑎41 𝑎12 𝑎22 𝑎32 𝑎42 162 𝑎13 𝑎23 𝑎33 𝑎43 𝑎14 𝑎24 𝑎34 ] 𝑎44 [100] 300 Reference, 𝑛 Tutorial by www.msharpmath.com 𝑛 det(𝐀) = ∑ 𝐶𝑖𝑗 𝑎𝑖𝑗 = ∑ 𝐶𝑖𝑗 𝑎𝑖𝑗 , 𝑖=1 𝐶𝑖𝑗 = (−1)𝑖+𝑗 det(𝐀𝑖𝑗 ) 𝑗=1 Then, det(𝐀𝑖𝑗 ) is obtained by A.rowdet(i). For example, an equation of a circle passing three points (𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), (𝑥3 , 𝑦3 ) is given as 𝑥2 + 𝑦2 𝑥 𝑦 1 𝑥 2 + 𝑦12 𝑥1 𝑦1 1| det(𝐀) = || 12 𝑥2 + 𝑦22 𝑥2 𝑦2 1| 𝑥32 + 𝑦32 𝑥3 𝑦3 1 = 𝐶11 (𝑥 2 + 𝑦 2 ) + 𝐶12 (𝑥) + 𝐶13 (𝑦) + 𝐶14 (1) = 0 For three points (1,1), (−1,2), (2,4), we have #> A = [ 1,1,1,1; 1+1,1,1,1; 1+4,-1,2,1; 4+16,2,4,1 ]; A = [ 1 1 1 [ 2 1 1 [ 5 -1 2 [ 20 2 4 #> #> #> #> #> A.minor(1,1).det; A.minor(1,2).det; A.minor(1,3).det; A.minor(1,4).det; A.rowdet(1); ans = [ ans ans ans ans -7 = = = = 1 1 1 1 ] ] ] ] -7 -9 39 34 -9 39 34 ] Therefore, (−1)1+1 (−7)(𝑥 2 + 𝑦 2 ) − (−9)(𝑥) + (39)(𝑦) − (34)(1) = 0 #> [ 1,1, -1,2, 2,4 ].circle; // an easier approach circle (-7)(x^2+y^2) + (9)x + (39)y + (-34) = 0 center = ( 0.642857, 2.78571 ) radius = 1.82108 ans = [ -7 9 39 163 -34 ] [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.rowend ---------------------------------------------------------------------- the last row vector A(end,*), A.endrow #> A = [ 1,2; 3,4; 5,6 ]; A = [ 1 [ 3 [ 5 #> A.endrow; ans = [ 2 ] 4 ] 6 ] 5 6 ] ---------------------------------------------------------------------matrix A.rowins(double i, matrix B) ---------------------------------------------------------------------- insert B at i-th row, A.insrow #> matrix.format("%6g"); #> A = (1:16)._4 ; A = [ 1 5 9 13 ] [ [ [ 2 3 4 6 7 8 10 11 12 14 ] 15 ] 16 ] #> A.insrow(1, [101:105; 201:205]); [ [ [ [ [ [ ans = 101 102 201 202 1 5 2 6 3 7 4 8 103 203 9 10 11 104 204 13 14 15 12 16 105 205 0 0 0 // [101:105; 201:205] _ A ] ] ] ] ] 0 ] #> A.insrow(2, [101:105; 201:205]'); ans = [ 1 5 9 13 ] [ [ [ 101 102 103 201 202 203 0 0 0 0 ] 0 ] 0 ] 164 [100] 300 [ [ [ [ [ 104 105 2 3 4 204 205 6 7 8 0 0 10 11 12 Reference, 0 0 14 15 16 Tutorial by www.msharpmath.com ] ] ] ] ] #> A.insrow(100, [101:105; 201:205]'); ans = [ 1 5 9 13 ] [ 2 6 10 14 ] [ 3 7 11 15 ] [ [ [ [ [ 4 101 102 103 104 8 201 202 203 204 12 0 0 0 0 [ 105 205 0 16 0 0 0 0 // A _ [101:105; 201:205]' ] ] ] ] ] 0 ] ---------------------------------------------------------------------matrix A.rowskip/skip(double k) ---------------------------------------------------------------------- skip rows by hopping k rows 𝑅1 , 𝑅1+𝑘 , 𝑅1+2𝑘 , ⋯ ⋯ , 𝑅𝑚−2𝑘 , 𝑅𝑚−𝑘 , 𝑅𝑚 if 𝑘 > 0 if 𝑘 < 0 #> A = (1:50)';; #> A.rowskip(10); // from first-row, skip forward ans = [ 1 ] [ 11 ] [ 21 ] [ 31 ] [ 41 ] #> A.rowskip(-10); ans = [ 10 ] [ 20 ] [ 30 ] [ 40 ] [ 50 ] // from end-row, skip backward 165 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.rowswap/swap(double i,double j) ---------------------------------------------------------------------- swap rows as an elementary operation #> A = [ 1,2; 3,4; 5,6; 7,8 ]; A = [ 1 2 ] [ 3 4 ] [ 5 6 ] [ 7 8 ] #> A.rowswap(2,4); ans = [ 1 2 ] [ 7 8 ] [ 5 6 ] [ 3 4 ] ---------------------------------------------------------------------matrix A.rowunit ---------------------------------------------------------------------- each row is normalized to be unity. #> A = [ 1,2; 4,3; 2,7 ]; A = [ 1 [ 4 [ 2 #> A.rowunit; ans = [ 0.447214 [ 0.8 [ 0.274721 2 ] 3 ] 7 ] 0.894427 ] 0.6 ] 0.961524 ] [ S ] ---------------------------------------------------------------------matrix A.sdev // column-first operation ---------------------------------------------------------------------- standard deviation 166 [100] 300 Reference, #> A = [ 1,2; 4,3; 2,7 ]; A = [ 1 [ 4 [ 2 #> A.sdev; ans = [ 1.24722 #> A().sdev; ans = [ 1.95078 ] #> A.sdev.sdev; ans = [ Tutorial by www.msharpmath.com 2 ] 3 ] 7 ] 2.16025 ] // note that data are different from A() 0.456514 ] ---------------------------------------------------------------------Secant Function double sec(double x) complex sec(complex z) matrix sec(matrix A) ---------------------------------------------------------------------- 1 , sec(𝑥 + 2𝜋) = sec(𝑥) cos(𝑥) −∞ < 𝑥 < ∞, sec(𝑥) ≤ −1, 1 ≤ sec(𝑥) sec(𝑥) = #> sec( pi/3 ); ans = 2 #> sec( 2i ); ans = 0.265802 + 0! #> sec( [ pi/3, 2i ] ); ans = [ 2 0.265802 167 ] [100] 300 Reference, Tutorial by www.msharpmath.com #> plot.x( 0,pi/2-0.1 )( sec(x) ); #> plot+.x( pi/2+0.1, pi )( sec(x) ); ---------------------------------------------------------------------double .secant(double a, double b, doubel f(double x)) dot-only ---------------------------------------------------------------------- root-finding by the secant method #> double f(x) = exp(x)-5; #> .bisect(3,4, f); // .bisect needs to know f(a)f(b) < 0 .bisect(a,b, f(x)), f(a)*f(b) > 0 ans = 1.#INF #> .secant(4,5, f); // .secant starts with no condition but may fail ans = 1.6094379 ---------------------------------------------------------------------Secant Function in Degree double secd(double x) complex secd(complex z) matrix secd(matrix A) ---------------------------------------------------------------------- πx secd(𝑥) = sec ( ) , secd(𝑥 + 360) = secd(𝑥) 180 −∞ < 𝑥 < ∞, secd(𝑥) ≤ −1, 1 ≤ secd(𝑥) #> secd( 60 ); ans = 2 #> secd( 2.todeg! ); ans = 0.265802 + 0! #> secd( [ 60, 2.todeg! ] ); ans = [ 2 0.265802 ] 168 [100] 300 Reference, Tutorial by www.msharpmath.com #> plot.x( 0,89 )( secd(x) ); #> plot+.x( 91,180 )( secd(x) ); ---------------------------------------------------------------------Hyperbolic Secant Function double sech(double x) complex sech(complex z) matrix sech(matrix A) ---------------------------------------------------------------------- 1 cosh(𝑥) 0 < sech(𝑥) ≤ 1 sech(𝑥) = −∞ < 𝑥 < ∞, #> sech( pi ); ans = 0.086266738 #> sech( 2i ); ans = -2.403 + 0! #> sech( [ pi, 2i ] ); ans = [ 0.0862667 -2.403 ] #> plot.x( -5,5 ) ( sech(x) ); ---------------------------------------------------------------------poly p.shift(double c) 169 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. shift(𝑐) = 𝑎0 + 𝑎1 (𝑥 − 𝑐) + 𝑎2 (𝑥 − 𝑐)2 + … + 𝑎𝑛 (𝑥 − 𝑐)𝑛 replace x in p(x) by x-c %> p(x) = x^4 + 2x^3 + 3x^2 + 4x + 5 %> q(x) = x^4 - 6x^3 + 15x^2 - 16x + 9 #> p = .[ 1,2,3,4,5 ]; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> q = p.shift(2); // replace x by x-2 q = poly( 9 -16 15 -6 1 ) = x^4 -6x^3 +15x^2 -16x +9 #> q( .[1,2] ); // confirm p(x) = q(x+2) ans = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 ---------------------------------------------------------------------double A.shorter ---------------------------------------------------------------------return 𝑚𝑖𝑛(𝑚, 𝑛) for an 𝑚 × 𝑛 matrix 𝐀 #> A = (1:6)._3; A = [ 1 [ 2 3 4 5 ] 6 ] #> A.shorter; // compare with A.longer = 3 ans = 2 ---------------------------------------------------------------------double .sign (double x) matrix .sign(matrix x) ---------------------------------------------------------------------(1) if x>0, (-1) if x<= and (0) if x=0 #> .sign(-3); .sign(0); .sign(5); ans = -1 170 [100] 300 Reference, ans = ans = Tutorial by www.msharpmath.com 0 1 ---------------------------------------------------------------------double x.sign ---------------------------------------------------------------------(1) if x>0, (-1) if x<= and (0) if x=0 #> ( pi).sign; #> ( 0).sign; #> (-pi).sign; ans = ans = ans = 1 0 -1 ---------------------------------------------------------------------Sine Function double sin(double x) complex sin(complex z) matrix sin(matrix A) ---------------------------------------------------------------------- −∞ < 𝑥 < ∞, #> sin( pi/6 ); ans = − 1 ≤ sin(𝑥) ≤ 1 0.5 #> sin( 2i ); ans = 0 + 3.62686! #> sin( [ pi/6, 2i ] ); ans = [ #> plot.x( -pi/2,pi/2 0.5 0 + i 3.62686 ] ) ( sin(x) ); ---------------------------------------------------------------------double sinc(double x) matrix sinc(matrix A) ---------------------------------------------------------------------- 171 [100] 300 Reference, Tutorial by www.msharpmath.com sin 𝑥 𝑥 #> sinc ( 0 ); ans = 1 #> sinc ( [ 0, 1 ] ); ans = [ 1 0.841471 ] ---------------------------------------------------------------------Sine Function in Degree double sind(double x) complex sind(complex z) matrix sind(matrix A) ---------------------------------------------------------------------- 𝜋𝑥 sind(𝑥) = sin ( ) 180 −∞ < 𝑥 < ∞, − 1 ≤ sind(𝑥) ≤ 1 #> sind( 30 ); ans = 0.5 #> sind( 2.todeg! ); ans = 0 + 3.62686! #> sind( [ 30, 2.todeg! ] ); ans = [ 0.5 0 + i 3.62686 #> plot.x( -90,90 ) ( sind(x) ); ---------------------------------------------------------------------Hyperbolic Sine Function double sinh(double x) complex sinh(complex z) matrix sinh(matrix A) ---------------------------------------------------------------------- 172 [100] 300 Reference, Tutorial by www.msharpmath.com 𝑒 𝑥 − 𝑒 −𝑥 2 − ∞ < sinh(𝑥) < ∞ sinh(𝑥) = −∞ < 𝑥 < ∞, #> sinh( pi ); ans = 11.548739 #> sinh( 2i ); ans = -0 + 0.909297! #> sinh( [ 0.5, 2i ] ); ans = [ 0.521095 -0 + i 0.909297 ] #> plot.x( -5,5 ) ( sinh(x) ); ---------------------------------------------------------------------double Sint(double x) matrix Sint(matrix x) ---------------------------------------------------------------------- 1 𝑥 sin 𝑡 Sint(𝑥) = ∫ 𝑑𝑡 𝑥 0 𝑡 #> Sint(1); ans = 0.94608307 #> Sint([1,2]); ans = [ 0.946083 0.802706 ] ---------------------------------------------------------------------(double i, double j) = A.size // tuple ---------------------------------------------------------------------double tuple, number of rows and columns #> A = (1:12)._4; A = 173 [100] 300 [ [ [ Reference, 1 2 3 Tutorial by www.msharpmath.com 4 5 6 #> (m,n) = A.size; m = n = #> A.m; A.n; ans = ans = 7 8 9 10 ] 11 ] 12 ] 3 4 3 4 ---------------------------------------------------------------------matrix A.skewl(double k) ---------------------------------------------------------------------- lower-triangular with respect to the skew-diagonal #> A = (1:20)._4; A = [ 1 6 [ 2 7 [ 3 8 [ 4 9 [ 5 10 11 12 13 14 15 16 17 18 19 20 ] ] ] ] ] #> A.skewl(0); ans = [ 0 [ 0 [ 0 [ 4 [ 5 0 12 13 14 15 16 17 18 19 20 ] ] ] ] ] 0 0 8 9 10 ---------------------------------------------------------------------matrix A.skewu(double k) ---------------------------------------------------------------------- upper-triangular with respect to the skew-diagonal #> A = (1:20)._4; A = [ 1 6 [ 2 7 [ 3 8 [ 4 9 [ 5 10 11 12 13 14 15 16 17 18 19 20 ] ] ] ] ] 174 [100] 300 #> A.skewu(0); ans = [ 1 [ 2 [ 3 [ 4 [ 0 6 7 8 0 0 Reference, Tutorial by www.msharpmath.com 11 12 0 0 0 ] ] ] ] ] 16 0 0 0 0 ---------------------------------------------------------------------matrix p.solve ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 Returns roots of a polynomial in a matrix form #> p = .[ 1,2,3,4,5 ] ; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p.solve; ans = [ -1.28782 [ -1.28782 [ 0.287815 [ 0.287815 + + i i i i 0.857897 0.857897 1.41609 1.41609 ] ] ] ] ---------------------------------------------------------------------matrix A.solve matrix A.axb ---------------------------------------------------------------------- nx(n+1) matrix solve Ax = b with (A|b) #> A = [ 1,-2,-5,6; 3,-7,0,2; -5,3,-4,-8 ]; A = [ 1 -2 -5 [ 3 -7 0 [ -5 3 -4 #> A.solve; // A.gausspivot.backsub; ans = [ 3 ] [ 1 ] [ -1 ] 175 6 ] 2 ] -8 ] [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix (double a, double b) .span (double n, double g=1) ---------------------------------------------------------------------(a,b) .span(n,g=1) // [ x_1 = a, x_2, …, x_n = b ], dx_i = g dx_(i-1) %> (a,b) .span(n,g=1) has a matrix of dimension 1 x n --------------#> (0,15) .span(6) ; // g = 1 (by default) for uniform span ans = [ 0 3 6 9 12 15 ] #> X = (0,15) .span(5,2) ; X = [ 0 1 3 // g = 2 for increasing, dX = [ 1 2 4 8 ] 7 15 ] #> X = (0,15) .span(5,0.5) ; X = [ 0 8 12 // g = 0.5 for decreasing, dX = [ 8 4 2 1 ] 14 15 ] // g = -2 for symmetric increasing, dX = [ 1 2 4 4 2 1 ] #> X = (1,15) .span(7,-2) ; X = [ 1 2 4 8 12 14 15 ] // g = -0.5 for symmetric decreasing, dX = [ 4 2 1 2 4 ] #> X = (1,14) .span(6,-0.5) ; X = [ 1 5 7 8 10 14 ] ---------------------------------------------------------------------vertex v.sph ---------------------------------------------------------------------- read in spherical coordinate #> < 1,1,1 > .sph ; // R = sqrt(x^2+y^2+z^2) ans = < 1.732 0.9553 0.7854 > ---------------------------------------------------------------------csys csys.sph ---------------------------------------------------------------------- a spherical coordinate system #> cs1 = csys.sph ; cs1 = 'sph' local csys 176 [100] 300 org = < dir = [ [ [ Reference, 0 Tutorial by www.msharpmath.com 0 1 0 0 0 > 0 ] 0 ] 1 ] 0 1 0 ---------------------------------------------------------------------matrix A.spl(double x) matrix A.spl(matrix B) matrix A.spldiff matrix A.splint figure A.splplot ---------------------------------------------------------------------A.spl(x) evaluation for double A.spl(B) evaluation for matrix differentiate piecewise polynomials integrate piecewise polynomials plot piecewise polynomials Piecewise continuous polynomials are defined with a number of polynomials and corresponding intervals 𝑥11 𝑥21 𝐀=[ ⋮ 𝑥𝑚1 𝑥12 𝑥22 ⋮ 𝑥𝑚2 𝑎10 𝑎20 ⋮ 𝑎𝑚0 𝑎11 𝑎21 ⋮ 𝑎𝑚1 ⋯ ⋯ ⋮ ] ⋯ 𝑥11 ≤ 𝑥 ≤ 𝑥12 ∶ 𝑝1 (𝑥) = 𝑎10 + 𝑎11 𝑥 + 𝑎12 𝑥 2 + … + 𝑎1𝑛 𝑥 𝑛 + … 𝑥21 ≤ 𝑥 ≤ 𝑥22 ∶ 𝑝2 (𝑥) = 𝑎20 + 𝑎21 𝑥 + 𝑎22 𝑥 2 + … + 𝑎2𝑛 𝑥 𝑛 + … ⋮ 𝑥𝑚1 ≤ 𝑥 ≤ 𝑥𝑚2 ∶ 𝑝𝑚 (𝑥) = 𝑎𝑚0 + 𝑎𝑚1 𝑥 + 𝑎𝑚2 𝑥 2 + … + 𝑎𝑚𝑛 𝑥 𝑛 + … Combination of these piecewise polynomials can be expressed by the following matrix 𝑥11 𝑥21 𝐀=[ ⋮ 𝑥𝑚1 𝑥12 𝑥22 ⋮ 𝑥𝑚2 𝑎10 𝑎20 ⋮ 𝑎𝑚0 𝑎11 𝑎21 ⋮ 𝑎𝑚1 ⋯ ⋯ ⋮ ] ⋯ This special representation by a matrix can be handled in various ways 177 [100] 300 A.spl(x) A.spl(B) A.spldiff A.splint A.splplot ; // // // // // Reference, Tutorial by www.msharpmath.com evaluation for double evaluation for matrix differentiate piecewise polynomials integrate piecewise polynomials plot piecewise polynomials #> x = (0:9).tr; #> [ x, x+1, x+1 ].splplot; // spline plot Data set { (𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), … , (𝑥𝑛 , 𝑦𝑛 )} can be connected by lines and represented by piecewise continuous polynomials as follows. #> x = [ 1, 2, 4, 5 ];; #> y = [ 3, 5, 4, 7 ];; #> P = .spline1(x,y); // spline1 for the 1st-order splines P = [ 1 2 1 2 ] [ [ 2 4 4 5 6 -8 1 ≤ 𝑥 ≤ 2 ∶ 𝑝1 (𝑥) = 1 + 2𝑥 2 ≤ 𝑥 ≤ 4 ∶ 𝑝2 (𝑥) = 6 − 0.5𝑥 4 ≤ 𝑥 ≤ 5 ∶ 𝑝3 (𝑥) = −8 + 3𝑥 #> P.splplot; // plot piecewise polynomials 178 -0.5 ] 3 ] [100] 300 #> P.spl(2.5); ans = Reference, Tutorial by www.msharpmath.com // p2(2.5) = 6 - 0.5(2.5) 4.75 #> P.spl( [ 2.5, 3, 4.5 ] ); ans = [ 4.75 #> Q = P.splint; Q = [ 1 [ 2 [ 4 // evaluation by matrix 4.5 5.5 ] 2 4 5 𝑥 -2 -7 21 1 6 -8 1 ] -0.25 ] 1.5 ] 𝑥 1 ≤ 𝑥 ≤ 2 ∶ 𝑞1 (𝑥) = ∫ 𝑝1 (𝑧)𝑑𝑧 = ∫ 1 + 2𝑧 𝑑𝑧 = − 2 + 𝑥 + 𝑥 2 1 𝑥 1 1 2 ≤ 𝑥 ≤ 4 ∶ 𝑞2 (𝑥) = ∫ 𝑝2 (𝑧)𝑑𝑧 + 𝑞1 (2) = −7 + 6𝑥 − 𝑥 2 4 2 𝑥 3 4 ≤ 𝑥 ≤ 5 ∶ 𝑞3 (𝑥) = ∫ 𝑝3 (𝑧)𝑑𝑧 + 𝑞2 (4) = 21 − 8𝑥 + 𝑥 2 2 4 #> Q.spl(4.5) - Q.spl(2.5); ans = 8.9375 4 4.5 4 4.5 1 3 ∫ 𝑝2 (𝑥)𝑑𝑥 + ∫ 𝑝3 (𝑥) 𝑑𝑥 = [−7 + 6𝑥 − 𝑥 2 ] + [21 − 8𝑥 + 𝑥 2 ] 4 2 2.5 4 2.5 4 = 8.9375 For spline interpolation using 3rd-degree polynomials, example is #> x = [ 1, 2, 4, 5 ];; #> y = [ 3, 5, 4, 7 ];; #> P = .spline(x,y); // 1st and 2nd columns for interval P = 179 [100] 300 [ [ [ 1 2 4 2 4 5 Reference, 1 -10.5 89.5 Tutorial by www.msharpmath.com 0.625 17.875 -57.125 2.0625 -6.5625 12.1875 -0.6875 ] 0.75 ] -0.8125 ] 1 ≤ 𝑥 ≤ 2 ∶ 𝑝1 (𝑥) = 1 + 0.625𝑥 + 2.0625𝑥 2 − 0.6875𝑥 3 2 ≤ 𝑥 ≤ 4 ∶ 𝑝2 (𝑥) = −10.5 + 17.875𝑥 − 6.5625𝑥 2 + 0.75𝑥 3 4 ≤ 𝑥 ≤ 5 ∶ 𝑝3 (𝑥) = 89.5 − 57.125𝑥 + 12.1875𝑥 2 − 0.8125𝑥 3 #> P.splplot; // .spline(x,y).splplot; #> [ x', y' ].plot+ ; #> P.splint.spl(4.5) - P.splint.spl(2.5) ; ans = 8.5068359 4 4.5 ∫ 𝑝2 (𝑥)𝑑𝑥 + ∫ 𝑝3 (𝑥) 𝑑𝑥 2.5 4 4 = ∫ −10.5 + 17.875𝑥 − 6.5625𝑥 2 + 0.75𝑥 3 𝑑𝑥 2.5 4.5 + ∫ 89.5 − 57.125𝑥 + 12.1875𝑥 2 − 0.8125𝑥 3 𝑑𝑥 4 = 8.5068359 #> P.splint.splplot; #> P.spldiff.splplot; // .spline(x,y) .splint .splplot; integrate // .spline(x,y) .spldiff .splplot; differentiate 180 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.spline (matrix x, matrix y) matrix A.spline1(matrix x, matrix y) matrix A.splinebc1(matrix x, matrix y) matrix A.splinebc2(matrix x, matrix y) ---------------------------------------------------------------------- Piecewise continuous polynomials are defined with a number of polynomials and corresponding intervals #> x = [ 1, 2, 4, 5 ];; #> y = [ 3, 5, 4, 7 ];; #> P = .spline1(x,y); P = [ 1 [ 2 [ // spline1 for the 1st-order splines 4 2 4 1 6 2 ] -0.5 ] 5 -8 3 ] 1 ≤ 𝑥 ≤ 2 ∶ 𝑝1 (𝑥) = 1 + 2𝑥 2 ≤ 𝑥 ≤ 4 ∶ 𝑝2 (𝑥) = 6 − 0.5𝑥 4 ≤ 𝑥 ≤ 5 ∶ 𝑝3 (𝑥) = −8 + 3𝑥 #> P = .spline(x,y); P = [ 1 2 [ 2 4 [ 4 // 1st and 2nd columns for interval 5 1 -10.5 0.625 17.875 2.0625 -6.5625 -0.6875 ] 0.75 ] 89.5 -57.125 12.1875 -0.8125 ] 1 ≤ 𝑥 ≤ 2 ∶ 𝑝1 (𝑥) = 1 + 0.625𝑥 + 2.0625𝑥 2 − 0.6875𝑥 3 2 ≤ 𝑥 ≤ 4 ∶ 𝑝2 (𝑥) = −10.5 + 17.875𝑥 − 6.5625𝑥 2 + 0.75𝑥 3 4 ≤ 𝑥 ≤ 5 ∶ 𝑝3 (𝑥) = 89.5 − 57.125𝑥 + 12.1875𝑥 2 − 0.8125𝑥 3 #> P = .splinebc1(x,y); 181 [100] 300 Reference, Tutorial by www.msharpmath.com P = [ [ [ 1 2 4 2 4 5 1 0.911111 1.63333 -0.544444 ] -7.06667 14.1 -5.23333 0.6 ] 70.6667 -45.5111 9.83333 -0.655556 ] #> P = .splinebc2(x,y); P = [ 1 2 1 1.11111 1.33333 -0.444444 ] [ 2 4 -4.66667 11.5 -4.33333 0.5 ] [ 4 5 58.6667 -38.1111 8.33333 -0.555556 ] ---------------------------------------------------------------------Square Root Function double sqrt (double x) complex sqrt (complex z) matrix sqrt (matrix A) ---------------------------------------------------------------------#> sqrt( 4 ); ans = 2 #> sqrt( 1i ); ans = 0.707107 + 0.707107! #> sqrt( [ 4, 1i ] ); ans = [ 2 0.707107 + i 0.707107 ---------------------------------------------------------------------matrix sqrtm (matrix A) ---------------------------------------------------------------------- 𝐗𝐗 = 𝐀, #> A = [ 1,0.3; 0.2,1 ]; A = [ [ sqrt(A); 1 0.2 0.3 ] 1 ] 1 0.447214 0.547723 ] 1 ] 0.992355 0.10077 0.151156 ] 0.992355 ] 𝐗 = √𝐀 X = sqrtm(A); ans = [ [ X = [ [ 182 ] [100] 300 #> X^2 ; ans = [ [ 1 0.2 Reference, Tutorial by www.msharpmath.com 0.3 ] 1 ] ---------------------------------------------------------------------Triangle solutions matrix A.sss matrix A.sas matrix A.asa matrix A.ssso matrix A.saso matrix A.asao ---------------------------------------------------------------------lowercase a, b, c for the sides uppercase A, B, C for the angles S = Area, rI = radius of incircle, rO = radius of circumcircle [ a, b, c ] .sss [ b, C, A ] .asa [ a, b, C ] .sas // two sides and in-between angle // one side and two end-angles // two sides and in-between angle [ a, b, c ] .ssso [ a, b, C ] .saso [ b, C, A ] .asao #> [ 3, 4, 5 ].sss; (a,b,c) = (3, 4, 5); (A,B,C) = (36.8699, 53.1301, 90); (S,rI,rO) = (6, 1, 2.5); ans = [ 3 4 [ 36.8699 53.1301 [ 6 1 5 ] 90 ] 2.5 ] #> plot.asa(2,60,30); 183 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.stat ---------------------------------------------------------------------- basic statistics #> A = [ 1,2; 4,3; 2,7 ]; A = [ 1 [ 4 [ 2 2 ] 3 ] 7 ] #> A.stat; 6 // entry ncount 3.16667 // mean value 19 // sum 1 // minimum 7 // maximum 1.95078 // standard deviation ans = [ 6 ] [ 3.16667 ] [ 19 ] [ 1 ] [ 7 ] [ 1.95078 ] ---------------------------------------------------------------------matrix A.std // column-first operation ---------------------------------------------------------------------- standard deviation #> A = [ 1,2; 4,3; 2,7 ]; 184 [100] 300 Reference, Tutorial by www.msharpmath.com A = [ [ [ 1 4 2 2 ] 3 ] 7 ] #> A.std; ans = [ 1.52753 #> A().std; ans = [ 2.13698 ] 2.64575 ] ---------------------------------------------------------------------matrix (double a, double b) .step (double h) ---------------------------------------------------------------------(a,b) .step(h) // [ a, a+h, a+2h, ... ] a : h : b #> 1 : 5 ; ans = [ 1 2 3 4 5 ] #> 5 : 1 ; ans = [ 5 4 3 2 1 ] #> 1 : 0.2: 2 ; ans = [ 1 // equivalent to (1,2).step(0.2); 1.2 1.4 1.6 1.8 2 ] ---------------------------------------------------------------------matrix W.sub(rows)(columns) // equivalent to W..(rows)(columns) ---------------------------------------------------------------------- Submatrix %> A ..(rows)(columns) // rows and columns are double, matrix or mixed. #> x = 1:6 ;; #> A = [ x+10; x+20; x+30; x+40; x+50; x+60 ]; A = [ [ [ [ [ 11 21 31 41 51 12 22 32 42 52 13 23 33 43 53 14 24 34 44 54 15 25 35 45 55 16 26 36 46 56 [ 61 62 63 64 65 66 ] 185 ] ] ] ] ] [100] 300 #> A ..( 2,1, ans = [ 22 [ 12 [ 42 [ 32 [ 62 Reference, Tutorial by www.msharpmath.com [4,3], 6 )( 2, [5,4], 1 ); 25 15 45 35 24 14 44 34 21 11 41 31 65 64 61 ] // A .sub (rows)(columns) ] ] ] ] #> A ..( 3, [5] )(); ans = [ 31 32 33 34 35 36 ] [ 53 54 55 56 ] 51 // A .sub (rows)() 52 #> A ..( 2,1, [4,3], 6 )( 2, [5,4], 1 ) += (1:50)*100; // note the order A = [ 1711 212 13 1214 715 16 ] [ [ [ [ [ 1621 1931 1841 51 2061 122 432 342 52 562 23 33 43 53 63 1124 1434 1344 54 1564 #> A = .zeros(5);; #> A..(1,2)(1,2) += .ones(2); A = [ 1 1 0 0 [ [ [ [ 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 625 935 845 55 1065 26 36 46 56 66 ] ] ] ] ] // connecting one point 0 ] 0 0 0 0 ] ] ] ] ] ] ] ] #> A..(2,3)(2,3) += 3*.ones(2); A = [ [ [ [ 1 1 0 0 1 4 3 0 0 3 3 0 0 0 0 0 0 0 0 0 [ 0 0 0 0 0 ] #> A..(3,5)(3,5) += 7*.ones(2); A = 186 [100] 300 [ [ [ [ [ 1 1 0 0 0 1 4 3 0 0 Reference, 0 3 10 0 7 0 0 0 0 0 Tutorial by www.msharpmath.com 0 0 7 0 7 ] ] ] ] ] ---------------------------------------------------------------------matrix A.subdel(rows)(columns) ---------------------------------------------------------------------- delete submatrix #> X = [ 11:14; 21:24; 31:34; 41:44; 51:54 ]; X = [ 11 12 [ 21 22 [ 31 32 [ [ 13 23 33 14 ] 24 ] 34 ] 41 51 42 52 43 53 44 ] 54 ] 31 41 32 42 33 43 34 ] 44 ] #> A.subdel(1,2,5)(); ans = [ [ #> A.subdel()(2,4); ans = [ [ [ [ [ 11 21 31 41 51 13 23 33 43 53 ] ] ] ] ] // equivalent to A = A.subdel(1,2,5)(2,4); #> A = X;; A..(1,2,5)(2,4) = []; A = [ 31 33 [ 41 43 ] ] ---------------------------------------------------------------------matrix A.sum // column-first operation double A.sum1 // equivalent to A.sum.sum(1) 187 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------- sum of elements #> A = [ 1,2; 4,3; 2,7 ]; A = [ 1 [ 4 [ 2 #> A.sum; ans = [ 7 #> A().sum; ans = [ 19 ] #> A.sum1; ans = 2 ] 3 ] 7 ] 12 ] 19 ---------------------------------------------------------------------double p.sum ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. sum = 𝑎0 + 𝑎1 + 𝑎2 + 𝑎3 + … + 𝑎𝑛 #> p = .[ 1,2,3,4,5 ] ; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p .sum ; // sum of all coefficients ans = 15 ---------------------------------------------------------------------dot-only Function void .swap (double &x, double &y) dot-only void .swap (complex &x, complex &y) void .swap (matrix &x, matrix &y) void .swap (poly &x, poly &y) void .swap (vertex &x, vertex &y) ---------------------------------------------------------------------- swap two arguments #> a = 2; b = 3; .swap(a,b); a; b; a = 2 188 [100] 300 b = a = b = Reference, Tutorial by www.msharpmath.com 3 3 2 #> z = 2i; w = 3i; .swap(z,w); z; w; z w z w = = = = 0 0 0 0 + + + + 2! 3! 3! 2! #> p = .[1,2]; p = poly( 2 = x +2 q = poly( 4 = 3x +4 p = poly( 4 = 3x +4 q = poly( 2 = x +2 q = .[3,4]; 1 ) 3 ) 3 ) 1 ) #> A = [1,2]; B = [3,4]; A B A B = = = = [ [ [ [ .swap(A,B); 1 3 3 1 2 4 4 2 #> u = < 1,2 >; v = < 3,4 >; u v u v = = = = < < < < .swap(p,q); p ; q; 1 3 3 1 A ; B; ] ] ] ] .swap(u,v); u; v; 2 4 4 2 0 0 0 0 > > > > ---------------------------------------------------------------------matrix A.swap (double i, double j) matrix A.rowswap(double i, double j) ---------------------------------------------------------------------- elementary row operation, swap rows 𝑅𝑖 𝑅𝑗 𝐀 = [ ⋯ ] where 𝑅𝑘 is 𝑘-th row. A.swai(i,j) = [ ⋯ ] 𝑅𝑗 𝑅𝑖 189 [100] 300 Reference, Tutorial by www.msharpmath.com #> A = [ 2,1,4 ; -1,3,5 ]; A = [ 2 [ -1 1 3 4 ] 5 ] #> A.swap(1,2); ans = [ -1 [ 2 3 1 5 ] 4 ] ---------------------------------------------------------------------poly p.syndiv(double c) matrix p.syndiv(poly q) ---------------------------------------------------------------------- p %% q , synthetic division by polynomial q 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 First Description (1): p.syndiv(c) is equal to p %% c A synthetic division by a factor (𝑥 − 𝑐) is defined as 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 𝑝(𝑥) = 𝑝(𝑥) %% 𝑐 = 𝑏0 + 𝑏1 (𝑥 − 𝑐) + 𝑏2 (𝑥 − 𝑐)2 + 𝑏3 (𝑥 − 𝑐)3 + … + 𝑏𝑛 (𝑥 − 𝑐)𝑛 the coefficients are determined by comparing two expressions such that 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + … + 𝑎𝑛 𝑥 𝑛 = 𝑏0 + 𝑏1 (𝑥 − 𝑐) + 𝑏2 (𝑥 − 𝑐)2 + ⋯ + 𝑏𝑛 (𝑥 − 𝑐)𝑛 An example is 𝑥 3 + 4𝑥 2 + 3𝑥 + 2 = (𝑥 + 3)3 − 5(𝑥 + 3)2 + 6(𝑥 + 3) + 2 %> synthetic division by a factor x-c #> p = .[1,4,3,2]; p = poly( 2 3 4 1 ) = x^3 +4x^2 +3x +2 190 [100] 300 Reference, Tutorial by www.msharpmath.com #> p.syndiv(-3) ; // equivalent to p %% -3; ans = poly( 2 6 -5 1 ) = x^3 -5x^2 +6x +2 #> ans ( .[1,3] ); // ans = p %% -3 ; from the above command ans = poly( 2 3 4 1 ) = x^3 +4x^2 +3x +2 Second Description (2): p.syndiv(q) is equal to p %% q The synthetic division by higher degrees can be also treated by an operator ‘%%’. For example 𝑥 5 + 8𝑥 4 + 6𝑥 3 + 3𝑥 2 − 10𝑥 + 2 −6𝑥 + 4 −9𝑥 − 8 = 2 + 2 + (𝑥 + 6) 2 2 2 (𝑥 + 𝑥 + 1) (𝑥 + 𝑥 + 1) (𝑥 + 𝑥 + 1) This has a nature similar to synthetic division 𝑥 5 + 8𝑥 4 + 6𝑥 3 + 3𝑥 2 − 10𝑥 + 2 = (𝑥 + 6)(𝑥 2 + 𝑥 + 1)2 + (−9𝑥 − 8)(𝑥 2 + 𝑥 + 1) + (−6𝑥 + 4) An operator ‘%%’ for this case can be applied %> synthetic division by a polynomial #> p = .[ 1,8,6,3,-10,2 ]; p = poly( 2 -10 3 6 8 1 ) = x^5 +8x^4 +6x^3 +3x^2 -10x +2 #> q = .[1,1,1]; // equal to q = poly( 1 1 1 ) = x^2 +x +1 q = poly(1,1,1); #> A = p %% q ; // ascending poly A = [ 4 -6 ] [ -8 -9 ] [ 6 1 ] 191 [100] 300 Reference, Tutorial by www.msharpmath.com #> -p + A.row(1).apoly+ A.row(2).apoly*q + A.row(3).apoly*q^2; ans = poly( 0 ) = 0 𝑥 5 + 8𝑥 4 + 6𝑥 3 + 3𝑥 2 − 10𝑥 + 2 = (4 − 6𝑥) + (−8 − 9𝑥)(𝑥 2 + 𝑥 + 1) + (6 + 𝑥)(𝑥 2 + 𝑥 + 1)2 The final command confirms that the partial fraction is correct. Note that a matrix is returned by 𝑝%%𝑞 instead of polynomial array. [ T ] ---------------------------------------------------------------------double .T_n(double x) dot-only matrix .T_n(matrix x), for real-part only ---------------------------------------------------------------------- Tchebyshev polynomial of degree n 𝑇0 (𝑥) = 1, 𝑇1 (𝑥) = 𝑥, #> .T_5(0.3); ans = 𝑇𝑛 (𝑥) = 2𝑥𝑇𝑛−1 (𝑥) − 𝑇𝑛−2 (𝑥) 0.99888 #> .T_5( [0.3,0.4] ); ans = [ 0.99888 0.88384 ] #> .hold; for.n(1,5) plot.x(-1,1) ( .T_n(x) ); plot; ---------------------------------------------------------------------poly poly.T(double n) ---------------------------------------------------------------------- 192 [100] 300 Reference, Tutorial by www.msharpmath.com Tchebyshev polynomial of degree n 𝑇0 (𝑥) = 1, 𝑇1 (𝑥) = 𝑥, 𝑇𝑛 (𝑥) = 2𝑥𝑇𝑛−1 (𝑥) − 𝑇𝑛−2 (𝑥) This command is for finding a polynomial itself. For evaluation, it is more convenient to use .T_n(x) #> for.n(0,5) poly.T(n); ans = poly( 1 ) = 1 ans = poly( 0 1 ) = x ans = poly( -1 0 2 ) = 2x^2 -1 ans = poly( -0 -3 0 4 ) = 4x^3 -3x ans = poly( 1 -0 -8 0 8 ) = 8x^4 -8x^2 +1 ans = poly( 0 5 -0 -20 0 16 ) = 16x^5 -20x^3 +5x #> .hold; for.n(0,5) plot.x(-1,1) ( .T_n(x) ); plot; ---------------------------------------------------------------------Tangent Function double tan(double x) complex tan(complex z) matrix tan(matrix A) ---------------------------------------------------------------------- tan(𝑥 + 𝜋) = tan(𝑥) 𝜋 𝜋 − < 𝑥 < , − ∞ < tan(𝑥) < ∞ 2 2 #> tan( pi/4 ); 193 [100] 300 Reference, ans = Tutorial by www.msharpmath.com 1 #> tan( 2i ); ans = 0 + 0.964028! #> tan( [ pi/4, 2i ] ); ans = [ 1 0 + i 0.964028 ] #> plot.x( -pi/2+0.1,pi/2-0.1 ) ( tan(x) ); ---------------------------------------------------------------------Tangent Function in Degree double tand(double x) complex tand(complex z) matrix tand(matrix A) ---------------------------------------------------------------------- 𝜋𝑥 tand(𝑥) = tan ( ), 180 −90 < 𝑥 < 90 tand(𝑥 + 180) = tand(𝑥) − ∞ < tand(𝑥) < ∞ #> tand( 45 ); ans = 1 #> tand( 2.todeg! ); ans = 0 + 0.964028! #> tand( [ 45, 2.todeg! ] ); ans = [ 1 0 + i 0.964028 ] 194 [100] 300 Reference, Tutorial by www.msharpmath.com #> plot.x( -85,85 ) ( tand(x) ); ---------------------------------------------------------------------Hyperbolic Tangent Function double tanh(double x) complex tanh(complex z) matrix tanh(matrix A) ---------------------------------------------------------------------- 𝑒 𝑥 − 𝑒 −𝑥 𝑒 𝑥 + 𝑒 −𝑥 2 − 1 < tanh(𝑥) < 1 tanh(𝑥) = −∞ < 𝑥 < ∞, #> tanh( pi ); ans = 0.99627208 #> tanh( 2i ); ans = 0 - 2.18504! #> tanh( [ pi, 2i ] ); ans = [ 0.996272 0 - i 2.18504 ] #> plot.x( -5,5 ) ( tanh(x) ); ---------------------------------------------------------------------poly p.tay ---------------------------------------------------------------------- 195 [100] 300 Reference, Tutorial by www.msharpmath.com 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 p. tay = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 /2! + 𝑎3 𝑥 3 /3! + … + 𝑎𝑛 𝑥 𝑛 /𝑛! #> p = .[ 1,2,3,4,5 ] ; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p .tay ; ans = poly( 5 4 1.5 0.333333 0.0416667 ) = 0.0416667x^4 +0.333333x^3 +1.5x^2 +4x +5 #> p .tay.iratio ; ans = poly( 5 4 3/2 1/3 1/24 ) ---------------------------------------------------------------------matrix .tdma(matrix a,matrix b,matrix c,matrix d, double na,nb) dot-only ---------------------------------------------------------------------- tridiagonal matrix algorithm . 0 0 x1 d1 a1 c1 0 . b a c . . 0 0 x2 d 2 2 2 2 0 b3 a3 c3 . 0 0 x3 d 3 . . . . 0 . ... ... ... . . . ... ... ... 0 . . . . . bn 1 an 1 cn 1 . . . 0 0 0 . . bn an xn d n (Tri-Diagonal Matrix Algorithm) 𝑎𝑖 𝑥𝑖 + 𝑏𝑖 𝑥𝑖−1 + 𝑐𝑖 𝑥𝑖+1 = 𝑑𝑖 , 𝑖 = 1,2,3, ⋯ , 𝑛, 𝑏1 = 𝑐𝑛 = 0 𝑐1 𝑑1 , 𝑄1 = 𝑎1 𝑎1 𝑐𝑖 𝑑𝑖 − 𝑏𝑖 𝑄𝑖−1 𝑃𝑖 = − , 𝑄𝑖 = , 𝑖 = 2,3,4, ⋯ , 𝑛 𝑎𝑖 + 𝑏𝑖 𝑃𝑖−1 𝑎𝑖 + 𝑏𝑖 𝑃𝑖−1 𝑥𝑛 = 𝑄𝑛 𝑥𝑖 = 𝑃𝑖 𝑥𝑖+1 + 𝑄𝑖 , 𝑖 = 𝑛 − 1, 𝑛 − 2, ⋯ ,3,2,1 𝑃1 = − //---------------------------------------------------------------------// tridiagonal matrix algorithm (TDMA) 196 [100] 300 Reference, Tutorial by www.msharpmath.com // a(i)*x(i) + b(i)*x(i-1) + c(i)*x(i+1) = d(i), b(1) = c(n) = 0 //---------------------------------------------------------------------matrix user_tdma(a,b,c,d, double na,nb) { n = a.mn; P = Q = x = .zeros(1,n); P(na) = -c(na)/a(na); Q(na) = d(na)/a(na); for.i(na+1,nb) { // for(i = na+1; i <= nb; i++) den = 1/( a(i) + b(i)*P(i-1) + _eps ); P(i) = -c(i) * den; Q(i) = (d(i) - b(i) * Q(i-1))*den; } x(nb) = Q(nb); for.i(nb-1,na,-1) x(i) = P(i)*x(i+1)+Q(i); // for(i = nb-1; i >= na; i--) return x; } // #> #> #> #> #> #> #> An example is shown below n = 6; a = b = c = d = .zeros(1,n);; // create matrices a = 2; b = 1; c = 1; for.i(1,n) d(i) = i;; x = .tdma(a,b,c,d, 1,n).trun12; n = 6 a = [ 2 2 2 2 2 2 ] b = [ 1 1 1 1 1 1 ] c = [ 1 1 1 1 1 1 ] ans = [ 0 1 0 2 0 3 ] ---------------------------------------------------------------------double x.todeg ---------------------------------------------------------------------- x * 180 / pi, radian to degree #> pi.todeg; ans = 180 ---------------------------------------------------------------------matrix matrix.toep (matrix A, matrix B) ---------------------------------------------------------------------#> matrix.toep ([1,0,-1,-2],[1,2,4,8]); // toeplitz 197 [100] 300 Reference, Tutorial by www.msharpmath.com ans = [ [ [ [ 1 0 -1 -2 2 1 0 -1 4 2 1 0 8 4 2 1 ---------------------------------------------------------------------double x.torad ---------------------------------------------------------------------- x * pi / 180, degree to radian #> 180.torad; ans = 3.1415927 ---------------------------------------------------------------------matrix A.tr equivalent to A.' = A.tr ---------------------------------------------------------------------- transpose, [𝐀𝑇 ]𝑖,𝑗 = [𝐀]𝑗,𝑖 𝑎1 𝑎 𝐀=[ 2 𝑎3 𝑎4 𝑎5 𝑎6 𝑎7 𝑎8 𝑎9 𝑎10 𝑎11 ] 𝑎12 𝑎1 𝑎4 𝐀.′ = 𝐀. tr = [ 𝑎 7 𝑎10 𝑎2 𝑎5 𝑎8 𝑎11 𝑎3 𝑎6 𝑎9 ] 𝑎12 #> A = [ 1+1i, 2-3i; 5-4i, 3+1i; 2+3i, -1+5i ]; A = [ 1 + i 1 2 - i 3 ] [ 5 - i 4 3 + i 1 ] [ 2 + i 3 -1 + i 5 ] #> A.'; // A.' = A.tr ans = [ 1 - i 1 [ 2 + i 3 5 + i 4 3 - i 1 #> A'; // compare with A' = A.conjtr ans = [ 1 - i 1 5 + i 4 [ 2 + i 3 3 - i 1 2 - i 3 -1 - i 5 ] ] 2 - i 3 -1 - i 5 ] ] ---------------------------------------------------------------------double A.trace ---------------------------------------------------------------------- 198 ] ] ] ] [100] 300 Reference, Tutorial by www.msharpmath.com 𝑛 ∑ 𝑎𝑖𝑖 𝑖=0 #> A = (1:12)._4; A = [ 1 [ 2 [ 3 #> A.trace; ans = 4 5 6 7 8 9 10 ] 11 ] 12 ] 15 ---------------------------------------------------------------------double trapz(matrix x,matrix y) ---------------------------------------------------------------------area by the trapezodial rule 1 [ (𝑥2 − 𝑥1 )(𝑦1 + 𝑦2 ) + (𝑥3 − 𝑥2 )(𝑦2 + 𝑦3 ) + 2 ⋯ + (𝑥𝑛 − 𝑥𝑛−1 )(𝑦𝑛−1 + 𝑦𝑛 )] #> x = 0:0.01:1;; y=1 ./(1+x.^2);; trapz(x,y); ans = 0.785394 #> int.x(0,1) ( 1/(1+x^2) ); pi/4; ans = 0.78539816 ans = 0.78539816 ---------------------------------------------------------------------matrix A.tril(double k) ---------------------------------------------------------------------- lower-triangular 𝑎11 𝑎12 𝐀 = [𝑎21 𝑎22 𝑎31 𝑎32 #> A = (1:12)._4; A = [ 1 [ 2 [ 3 𝑎13 𝑎23 𝑎33 𝑎14 𝑎24 ] , 𝑎34 𝑎11 𝐀. tril(0) = [𝑎21 𝑎31 4 5 6 7 8 9 #> A.tril(0); 199 0 𝑎22 𝑎32 10 ] 11 ] 12 ] 0 0 𝑎33 0 0] 0 [100] 300 Reference, Tutorial by www.msharpmath.com ans = [ [ [ 1 2 3 0 5 6 0 0 9 0 ] 0 ] 0 ] #> A.tril(-1); ans = [ 0 [ 2 [ 3 0 0 6 0 0 0 0 ] 0 ] 0 ] ---------------------------------------------------------------------matrix A.triu(double k) ---------------------------------------------------------------------- upper-triangular 𝑎11 𝑎 𝐀 = [ 21 𝑎31 𝑎12 𝑎22 𝑎32 𝑎13 𝑎23 𝑎33 𝑎14 𝑎24 ] , 𝑎34 𝑎11 𝐀. triu(0) = [ 0 0 𝑎12 𝑎22 0 #> A = (1:12)._4; A = [ 1 [ 2 [ 3 4 5 6 7 8 9 10 ] 11 ] 12 ] #> A.triu(0); ans = [ 1 [ 0 [ 0 4 5 0 7 8 9 10 ] 11 ] 12 ] #> A.triu(-1); ans = [ 1 [ 2 [ 0 4 5 6 7 8 9 10 ] 11 ] 12 ] 𝑎13 𝑎23 𝑎33 ---------------------------------------------------------------------matrix A.true matrix A.find ---------------------------------------------------------------------- index for nonzero elements 200 𝑎14 𝑎24 ] 𝑎34 [100] 300 Reference, Tutorial by www.msharpmath.com #> A = [ 2,0,3; 0,-1,0; 0,2,4 ]; A = [ 2 0 [ 0 -1 [ 0 2 3 ] 0 ] 4 ] #> A.true; ans = [ 1 5 6 7 #> A.false; ans = [ 2 3 4 8 ] 9 ] ---------------------------------------------------------------------double x.trun(eps=1.e-30) matrix poly vertex A.trun(eps=1.e-30) p.trun(eps=1.e-30) v.trun(eps=1.e-30) double x.trunk matrix A.trunk poly p.trunk vertex v.trunk ϵ = 10−𝑘 ϵ = 10−𝑘 ϵ = 10−𝑘 ϵ = 10−𝑘 ---------------------------------------------------------------------- truncate 𝑥, i.e. make 𝑥 = 0 if |𝑥| < 𝜖 only a sample case of a matrix is considered below. All other types can be done in an element-by-element manner. #> [ 0.001, 2.e-10, 3.e-13, 5.e-17 ].trun; ans = [ 0.001 2e-010 3e-013 5e-017 ] #> [ 0.001, 2.e-10, 3.e-13, 5.e-17 ].trun2; ans = [ 0 0 0 0 ] #> [ 0.001, 2.e-10, 3.e-13, 5.e-17 ].trun10; ans = [ 0.001 2e-010 0 0 ] #> [ 0.001, 2.e-10, 3.e-13, 5.e-17 ].trun16; ans = [ 0.001 2e-010 3e-013 0 ] [ U ] 201 [100] 300 Reference, Tutorial by www.msharpmath.com ---------------------------------------------------------------------matrix A.unit matrix A.colunit ---------------------------------------------------------------------makes 2 2 2 2 𝑘-th column to be 𝑎1𝑘 + 𝑎2𝑘 + 𝑎3𝑘 + ⋯ + 𝑎𝑚𝑘 = 1, 𝐱 𝑇 𝐱 = 1 #> A = [ 1,2,3,4; 5,6,4,8 ]; A = [ 1 2 [ 5 6 #> A.unit; // the same as A.colunit ans = [ 0.196116 0.316228 [ 0.980581 0.948683 3 4 4 ] 8 ] 0.6 0.8 0.447214 ] 0.894427 ] ---------------------------------------------------------------------complex z.unit ---------------------------------------------------------------------- normalize a complex number #> z = 3+4! ; z = 3 + 4! #> z.unit ; ans = 0.6 + 0.8! ---------------------------------------------------------------------vertex v.unit ---------------------------------------------------------------------- normalize a vertex #> v = < 1,1,1 >; v = < #> v.unit; ans = < 1 1 1 > 0.5774 0.5774 0.5774 > ---------------------------------------------------------------------poly p.up(double k) ---------------------------------------------------------------------- 𝑝(𝑥) = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3 + … + 𝑎𝑛 𝑥 𝑛 202 [100] 300 Reference, Tutorial by www.msharpmath.com p. up(𝑘) = 𝑎0 𝑥 𝑘 + 𝑎1 𝑥 1+𝑘 + 𝑎2 𝑥 2+𝑘 + … + 𝑎𝑛 𝑥 𝑛+𝑘 #> p = .[ 1,2,3,4,5 ]; p = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> p .up(3) ; // replace x by x^3 ans = poly( 0 0 0 5 4 3 2 1 ) = x^7 +2x^6 +3x^5 +4x^4 +5x^3 [ V ] ---------------------------------------------------------------------matrix A.vander(double nrow) ---------------------------------------------------------------------- Vandermonde matrix 1 1 1 ⋯ 1 1 𝑥1 𝑥2 𝑥3 ∙ 𝑥𝑛−1 𝑥𝑛 ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ ∙ 𝑟 𝑥𝑛𝑟 ] [ 𝑥1𝑟 𝑥2𝑟 𝑥3𝑟 ∙ 𝑥𝑛−1 #> x = [ 1,2,3,4 ]; x = [ 1 #> x.vander(3); ans = [ 1 [ 1 [ 1 2 1 2 4 3 1 3 9 4 ] 1 ] 4 ] 16 ] ---------------------------------------------------------------------matrix A.var // column-first operation ---------------------------------------------------------------------- variance #> A = [ 1,2; 4,3; 2,7 ]; A = [ 1 [ 4 [ 2 2 ] 3 ] 7 ] 203 [100] 300 Reference, #> A.var; ans = [ 2.33333 #> A().var; ans = [ 4.56667 ] Tutorial by www.msharpmath.com 7 ] ---------------------------------------------------------------------matrix .vertcat( ...) ---------------------------------------------------------------------- vertical concatenation #> .vertcat( 1,2,[3,4,5], 6,[ -1,-2] ); ans = [ 1 0 [ 2 0 [ 3 4 [ 6 6 [ -1 -2 0 0 5 6 0 ] ] ] ] ] [ W ] ---------------------------------------------------------------------matrix A.wide(double n) // equivalent to A..n = A.wide(n) ---------------------------------------------------------------------- reshape in n-column #> A = (1:20)._3; A = [ 1 8 [ 2 9 [ 3 10 [ 4 11 [ 5 12 [ 6 13 [ 7 14 15 16 17 18 19 20 0 #> A .wide(4); ans = [ 1 [ 2 [ 3 13 14 15 7 8 9 ] ] ] ] ] ] ] 19 ] 20 ] 0 ] 204 [100] 300 [ [ [ 4 5 6 10 11 12 Reference, 16 17 18 Tutorial by www.msharpmath.com 0 ] 0 ] 0 ] ---------------------------------------------------------------------void A.write(string fmt) ---------------------------------------------------------------------- write A to a file [ X ] ---------------------------------------------------------------------double z.x // both read/write double z.y ---------------------------------------------------------------------- z.x z.y real part of a complex number imaginary part of a complex number #> z = 3+4!; z = 3 + 4! #> z.x = 21; z; ans = z = 21 + 4! 21 #> z.y = 39; z; ans = z = 21 + 39! 39 ---------------------------------------------------------------------double v.x // both read/write double v.y double v.z ---------------------------------------------------------------------%> rectangular coordinate ---------------------------------------------#> v = < 1,1,1 > ; v.x; v.y; v.z; v = < 1 1 1 > 205 [100] 300 #> v.x; v.y; v.z; ans = ans = ans = Tutorial by www.msharpmath.com 1 1 1 #> v.x += 1 ; v = < Reference, // only x is changed 2 1 1 > ---------------------------------------------------------------------matrix A.xrot(double theta) matrix A.yrot(double theta) matrix A.zrot(double theta) matrix matrix matrix A.xrotd(double theta) A.yrotd(double theta) A.zrotd(double theta) in degree ------------------------------- --------------------------------------- tensor-related functions for stress analysis rotation around the x-axis rotation around the y-axis rotation around the z-axis σ11 σ 𝛔 = [𝐢 𝐣 𝐤] [ 21 σ31 𝛔 = [ 𝐞1 𝐞2 σ12 σ22 σ32 σ13 𝐢 σ23 ] [ 𝐣 ], σ33 𝐤 σ11 𝐞3 ]𝐐𝑇 [σ21 σ31 σ11 𝐒̃ = 𝐐𝑇 [σ21 σ31 // s σ12 σ22 σ32 = ( ij+ji ), pure shear stress #> X = [ 0,1,0; 1,0,0; 0,0,0 ]; X = [ 0 1 [ 1 0 [ 0 0 0 ] 0 ] 0 ] 206 σ12 σ22 σ32 𝐞1 𝐢 𝐞 [ 𝐣 ] = 𝐐 [ 2] 𝐞3 𝐤 σ13 𝐞1 σ23 ] 𝐐 [𝐞2 ] σ33 𝐞3 σ13 σ23 ] 𝐐 σ33 [100] 300 Reference, Tutorial by www.msharpmath.com // snew = ( uu-vv ), tension and compression #> X.zrotd(45).trun10; ans = [ 1 0 0 ] [ 0 -1 0 ] [ 0 0 0 ] ---------------------------------------------------------------------vertex v.xrot(double theta) vertex v.yrot(double theta) vertex v zrot(double theta) vertex v.xrotd(double theta) in degree vertex v.yrotd(double theta) vertex v.zrotd(double theta) ------------------------------- --------------------------------------- rotation around the x-axis rotation around the y-axis rotation around the z-axis #> v = < 1,1,1 > ; v.zrotd(45).trun10; v = < ans = < 1 0 1 1.414 1 > 1 > [ Y ] ---------------------------------------------------------------------Bessel Function double .Y_(double nu)(double x) dot-only matrix .Y_(double nu)(matrix x) matrix .Y_(matrix nu)(double x) matrix .Y_(matrix nu)(matrix x) ---------------------------------------------------------------------under test ---------------------------------------------------------------------void .ym(double year, double month) dot-only ---------------------------------------------------------------------- print calendar 207 [100] 300 #> .ym(2012,8); 2012 8 Sun Mon __ __ 05 06 12 13 19 20 26 27 Reference, Tue __ 07 14 21 28 Wed 01 08 15 22 29 Tutorial by www.msharpmath.com Thr 02 09 16 23 30 Fri 03 10 17 24 31 Sat 04 11 18 25 [ Z ] ---------------------------------------------------------------------matrix .zeros(double m, double n = m) ---------------------------------------------------------------------- all-zeros matrix #> .zeros(2,4); ans = [ 0 [ 0 0 0 0 0 #> .zeros(3); ans = [ 0 [ 0 [ 0 0 0 0 0 ] 0 ] 0 ] 0 ] 0 ] ---------------------------------------------------------------------end of file ---------------------------------------------------------------------- 208