720 likes | 868 Views
2013. 03. 12. Brief Guide to msharpmath ( cemmsharp ). Lecture by www.msharpmath.com. Prof. Charn -Jung Kim Seoul National University. Why msharpmath ?. Need a paradigm shift : grammar-based to human thinking. Find a root of. MATLAB grammar-based. M#Math human-thinking.
E N D
2013. 03. 12 Brief Guide tomsharpmath (cemmsharp) Lecture by www.msharpmath.com Prof. Charn-Jung Kim Seoul National University
Why msharpmath ? Need a paradigm shift : grammar-based to human thinking Find a root of MATLAB grammar-based M#Math human-thinking function f = myfun(x) f = x^2 – 3*x + 2 >> fzero( @myfun, 0 ) #> solve .x ( x^2 = 3*x – 2 );
How to download msharpmath • HomePage : www.msharpmath.com Click here
Mathematics Calculator #> 3 + 4 * 5 ans = 23 #> ans/1000 + ans*100 ans = 2300.023 cursor in msharpmath #>3 + 4 * 5 ans = 23 #>ans / 1000 + ans * 100 ans = 2300.023 default variable name for results Variable “ans” can be used in subsequent commands Variable “ans” is changed to a new value
■ Unit conversion x.f// Fahrenheit to other temperatures x.c// Celsius to other temperatures x.ft// feet in meter x.acre// acre in square meter and many more #> 77.f 25 [C] 77 F 536.67 R 298.15 K ans = 298.15 #> 1.ft * 1 ans = 0.3048 #> 1.acre * 1 ans = 4046.8564 expression-look-alike !!!
■ Root finding solve.x{a} ( f(x)=g(x) ) // a root of f(x)=g(x) near a solve.x.y {a,b} ( u(x,y)=0, v(x,y)=0 ) solve.x.y.z {a,b,c} ( u(x,y,z)=0, v(x,y,z)=0, w(x,y,z)=0 ) #> solve.x ( exp(x) = x+3 ) ans = 1.5052415 #> solve.x ( exp(x) = x+3 ).span(-10,10) ans = [ -2.94753 -5.66165e-008 ] [ 1.50524 -1.89955e-008 ] #> solve.a.b ( a+b = 3, a*b = 2 ) ans = [ 2 ] [ 1 ] expression-look-alike !!!
■ Integration int.x(a,b) ( f(x) ) int.x(a,b).y(u(x),v(x)) ( f(x,y) ) int.x(a,b).y(u(x),v(x)).z(p(x,y),q(x,y)) ( f(x,y,z) ) #> int.x(1,2).y(x,2*x+1) ( exp(-x-y)*sin(x+y) ) ans = -0.0089769673 expression-look-alike !!!
● data type : double(real numbers) double msharpmath (m#math) double(real number) complex poly (polynomial) matrix vertex signal image ... MATLAB matrix real numbersare treated by 1 x 1 matrix
Operators for double (1) double |a| // absolute -a // unary minus !a // return 1 if a=0, otherwise return 0 #> a = -1.7 a = -1.7 #> |a| ans = 1.7 #> -a ans = 1.7 #> !a ans = 0 #> !0 ans = 1
Operators for double (2) double a + b // addition a - b // subtraction a * b // multiplication a / b // right division a \ b // left division a ^ b // power a % b // remainder, equal to .mod(a,b) #> 2 + 3 ans = 5 #> 2 - 3 ans = -1 #> 2 * 3 ans = 6 #> 2 / 3 ans = 0.66666667 #> 2 \ 3 ans = 1.5 #> 2 ^ 3 ans = 8 #> 13 % 5 ans = 3
Operators for double (3) double Compound operations a += b // a = a+b a -= b // a = a-b a *= b // a = a*b a /= b // a = a/b a ^= b // a = a^b a %= b // a = a%b #> a = 3 a = 3 #> a += 4 a = 7 #> a ^= 2 a = 49
● data type : poly(polynomial) poly msharpmath (m#math) double (real number) complex poly (polynomial) matrix vertex signal image ... MATLAB matrix polynomialsare treated by functions such as polyval(p,x) polyder(p,x)
ascending poly poly poly( a0, a1, a2, a3, … , an ) [ a0, a1, a2, a3, … , an ] .apoly p(x) = a0 + a1 x + a2 x^2 + a3 x^3 + … + an x^n #> poly( 1,2,3,4,5 ) ans = poly( 1 2 3 4 5 ) = 5x^4 +4x^3 +3x^2 +2x +1 #> [ 1,2,3,4,5 ].apoly ans = poly( 1 2 3 4 5 ) = 5x^4 +4x^3 +3x^2 +2x +1
descending poly poly [ an, … , a3, a2, a1, a0 ] .dpoly .[ an, … , a3, a2, a1, a0 ] .[ a ] // constant polynomial .[ a, b ] // 1st-order polynomial, ax+b .[ a, b, c ] // 2nd-order polynomial, ax^2+bx+c .[ a, b, c, d ] // 3rd-order polynomial, ax^3+bx^2+cx+d Note : a leading dot for descending polynomial !!! #> .[ 1,2,3,4,5 ] ans = poly( 5 4 3 2 1 ) = x^4 +2x^3 +3x^2 +4x +5 #> .[ 1,3,2 ] y = x^2 + 3x + 2 roots [ -1 ] [ -2 ] vertex at ( -1.5, -0.25 ) y_minimum = -0.25 y-axis intersect ( 0, 2 ) symmetry at x = -1.5 directrix at y = -0.5 focus at ( -1.5, 0 ) #> .[] ans = poly( 0) = 0 #> p = q = r = .[] p = poly( 0) = 0 q = poly( 0) = 0 r = poly( 0) = 0 .[] is a zero polynomial
Operations for poly (1) poly -p // unary minus p’ // dp(x)/dx, derivative p~ // int_0^x p(x) dx, integration p.’ // p(x)-p(x-1), finite difference p.~ // p(1)+p(2)+…+p(n), cumulative sum #> .[1,0,0,0]' ans = poly( 0 0 3 ) = 3x^2 #> .[1,0,0,0].' ans = poly( 1 -3 3 ) = 3x^2 -3x +1 #> .[1,0,0,0]~ ans = poly( 0 0 0 0 0.25 ) = 0.25x^4 #> .[1,0,0,0].~ ans = poly( 0 0 0.25 0.5 0.25 ) = 0.25x^4 +0.5x^3 +0.25x^2
Operations for poly (2) poly p + q // addition p - q // subtraction p * q // multiplication p / q // quotient(right division) q \ p // quotient(left division) p % q // remainder P %% q // synthetic division #> p = .[3,4,5] p = poly( 5 4 3 ) = 3x^2 +4x +5 #> q = .[1,2] q = poly( 2 1 ) = x +2 #> p + q ans = poly( 7 5 3 ) = 3x^2 +5x +7 #> p - q ans = poly( 3 3 3 ) = 3x^2 +3x +3 #> p * q ans = poly( 10 13 10 3 ) = 3x^3 +10x^2 +13x +10 #> p / q ans = poly( -2 3 ) = 3x -2 #> p % q ans = poly( 9 ) = 9
Operations for poly (3) poly p(x) // double p(z) // complex p(q) // polynomial p(A) // matrix (element-by-element) p[A] // matrix polynomial, a0 I+a1 A+a2 A^2 + … #> p = .[1,0,0] p = poly( 0 0 1 ) = x^2 #> p(3) ans = 9 #> p(3+1i) ans = 8 + 6! #> p( .[3,4] ) ans = poly( 16 24 9 ) = 9x^2 +24x +16
Operations for poly (4) poly p(x) // double p(z) // complex p(q) // polynomial p(A) // matrix (element-by-element) p[A] // matrix polynomial, a0 I+a1 A+a2 A^2 + … #> p( [1,2;3,4] ) ans = [ 1 4 ] [ 9 16 ] #> p[ [1,2;3,4] ] ans = [ 7 10 ] [ 15 22 ]
Member functions poly p.solve// roots p.pow(k) // replace x by x^k, p[i*k]=p[i] p.shift(k) // replace x by x-k p.up(k) // multiply by x^k, p[i+k]=p[i] p.newton(u) // a0+a1(x-u1)+a2(x-u1)(x-u2)+… #> .[1,2,5].solve ans = [ -1 - i 2 ] [ -1 + i 2 ] #> p = .[1,0,0]; p.pow(3); p = x^2 ans = x^6 #> p.shift(2) ans = = x^2 -4x +4 #> p.up(3) ans = x^5
exercise for poly poly #> .[3,-5,7]^3 - poly(0,5,4)*.[1,-6,15,24] + 32*.[1,-17]^3 ans = poly( -156873 26889 -837 -753 433 -139 27 ) = 27x^6 -139x^5 +433x^4 -753x^3 -837x^2 +26889x -156873
● data type : complex complex msharpmath (m#math) double (real number) complex poly (polynomial) matrix vertex signal image ... MATLAB matrix complexnumbers are treated by 1 x 1 matrix
Creating complex numbers complex 1i, 1j, 1! // i or j works only after digits 0-9 x+y! // postfix ! for pure imaginary part (r+t!).cyl// r cos(t) + r sin(t) ! #> 1i ans = 0 + 1! #> 1j ans = 0 + 1! #> 1! ans = 0 + 1! #> 3+pi! ans = 3 + 3.14159! #> (3+pi!).cyl ans = -3 + 3.67394e-016! ‘i’ after digits ‘j’ after digits ‘!’ after both digits and variables In polar coordinate
Elements of complex complex z = x+y! |z| // absolute value ~z // conjugate, x-y! z.x, z.real// real part, x z.y, z.imag// imaginary part, x z.r, z.abs // equal to |z|=sqrt(x*x+y*y) z.t, z.arg // phase part in radian, tan^-1(y/x) z.deg // phase part in degree #> z = 3+4i z = 3 + 4! #> |z| ans = 5 #> ~z ans = 3 - 4! #> z.y ans = 4 #> z.r ans = 5 #> z.t ans = 0.92729522 #> z.deg ans = 53.130102
Be cautious with sqrt(-1) complex A function “sqrt” returns ‘double’ for ‘double’, ‘complex’ for ‘complex’ #> sqrt(-1) ans = -1.#IND #> sqrt(-1+0i) ans = 6.12323e-017 + 1!
● data type : matrix matrix msharpmath (m#math) double (real number) complex poly (polynomial) matrix vertex signal image ... MATLAB matrix Matrix Laboratory All the data are processed based on matrix
Matrix by a pair of brackets [] matrix [] // 0 x 0 matrix [ a11,a12,…,a1n; a21,a22,… ; … ; a_m1,…, a_mn ] // separate rows by a semi-colon ; // separate elements by a comma , #> A = [ 1,2,3; 4,5; 6 ] A = [ 1 2 3 ] [ 4 5 0 ] [ 6 0 0 ] #> [ A,-A; 2*A ] ans = [ 1 2 3 -1 -2 -3 ] [ 4 5 0 -4 -5 -0 ] [ 6 0 0 -6 -0 -0 ] [ 2 4 6 0 0 0 ] [ 8 10 0 0 0 0 ] [ 12 0 0 0 0 0 ] All zeros are assigned for undeclared elements Matrices can be also elements of a matrix
Matrix by a colon operator matrix a:b // [ a,a+1,a+2, … ] if a < b a:h:b // [ a,a+h,a+2h, … ] #> 1:5 ans = [ 1 2 3 4 5 ] #> 5:1 ans = [ 5 4 3 2 1 ] #> 1 : 0.2 : 2 ans = [ 1 1.2 1.4 1.6 1.8 2 ] // [ a,a-1,a-2, … ] if a > b
Matrix by built-in functions matrix .I(m,n=m) or .eye(m,n=m) .ones(m,n=m) .zeros(m,n=m) #> .I(2,3) ans = [ 1 0 0 ] [ 0 1 0 ] #> .ones(4) ans = [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] [ 1 1 1 1 ] #> .zeros(A) ans = [ 0 0 0 ] [ 0 0 0 ] [ 0 0 0 ] equal to eye(2,3) equal to ones(4,4) Same size with A
Operations for matrices matrix + // addition - // subtraction * // multiplication / // right division, A/B = A*B^-1 \ // left division, A\B = A^-1*B ^ // power, A^3 = A*A*A ‘ // complex conjugate transpose #> A = [ 1,2; 3,4 ]; b = [ 5; 6 ] A = [ 1 2 ] [ 3 4 ] b = [ 5 ] [ 6 ] #> A \ b ans = [ -4 ] [ 4.5 ]
Operations with scalars matrix Scalar is expanded as a matrix #> A = [ 1,2,3; 4,5,6 ] A = [ 1 2 3 ] [ 4 5 6 ] #> A + 5 ans = [ 6 7 8 ] [ 9 10 11 ] #> A ^ 5 runtime error#90700: matrix not square #> A = 5 A = [ 5 5 5 ] [ 5 5 5 ] in MATLAB, A = 5 results in a scalar (1 x 1 matrix)
Element-by-element operations matrix .* // element-by-element multiplication ./ // element-by-element right division .\ // element-by-element left division .^ // element-by-element power .‘ // pure transpose #> A = [ 1,2; 3,4 ]; B = [ 5,6; 7,8 ] A = [ 1 2 ] [ 3 4 ] B = [ 5 6 ] [ 7 8 ] #> A .* B ans = [ 5 12 ] [ 21 32 ] #> A ./ B ans = [ 0.2 0.333333 ] [ 0.428571 0.5 ] #> A .\ B ans = [ 5 3 ] [ 2.33333 2 ] #> B .^ A ans = [ 5 36 ] [ 343 4096 ]
Dot and Kronecker Products matrix A ** B // dot product, .dot(A,B) A ^^ B // Kronecker product, .kron(A,B) #> A = [ 1,10,-100; -10,1;1000 ]; B = [ 1,2,3; 4,5,6; 7,8,9 ] A = [ 1 10 -100 ] [ -10 1 0 ] [ 1000 0 0 ] 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 60 -400 -500 -600 ] [ 7 8 9 70 80 90 -700 -800 -900 ] [ -10 -20 -30 1 2 3 0 0 0 ] [ -40 -50 -60 4 5 6 0 0 0 ] [ -70 -80 -90 7 8 9 0 0 0 ] [ 1000 2000 3000 0 0 0 0 0 0 ] [ 4000 5000 6000 0 0 0 0 0 0 ] [ 7000 8000 9000 0 0 0 0 0 0 ] #> B ** B ans = 285
Concatenations matrix A | B // horizontal concatenation, .horzcat(A,B,C,…) A _ B // vertical concatenation, .vertcat(A,B,C,…) A \_ B // diagonal concatenation, .diagcat(A,B,C,…) #> A = [ 1,2,3 ]; B = [ -4,5 ] A = [ 1 2 3 ] B = [ -4 5 ] #> A | B ans = [ 1 2 3 -4 5 ] #> A _ B ans = [ 1 2 3 ] [ -4 5 0 ] #> A \_ B ans = [ 1 2 3 0 0 ] [ 0 0 0 -4 5 ]
Set operations matrix A ++ B // set union A -- B // set difference A /\ B // set intersection #> A = [ 2,3,4,3,4,6 ]; B = [ 1,2,3,2,3,8,8,9 ] A = [ 2 3 4 3 4 6 ] B = [ 1 2 3 2 3 8 8 9 ] #> A ++ B ans = [ 2 3 4 6 1 8 9 ] #> A -- B ans = [ 4 6 ] #> B -- A ans = [ 1 8 9 ] #> A /\ B ans = [ 2 3 ]
Compound assignments matrix A += B // A = A + B A -= B // A = A - B A *= B // A = A * B A /= B // A = A / B = A(B^-1) A |= B // A = A | B, horizontal concatenation A _= B // A = A _ B, vertical concatenation A\_= B // A = A\_ B, diagonal concatenation A .*= B // A = A .* B A ./= B // A = A ./ B A .^= B // A = A .^ B A ++= B // A = A ++ B, set union A --= B // A = A –- B, set difference A /\= B // A = A /\ B, set intersection
Access to elements (1) matrix A(i,j), A(k) // real element A{i,j), A{k} // imaginary element A[i,j], A[k] // complex element #> A = [ 1, 3-4! ; -2+5i, 6; 7,-8i ] A = [ 1 3 - i 4 ] [ -2 + i 5 6 ] [ 7 -0 - i 8 ] #> A(2,1); A{2,1}; A[2,1]; ans = -2 ans = 5 ans = -2 + 5! This is a unique feature of msharpmath compared with MATLAB and similar interactive languages
Access to elements (2) matrix A.row(i), A(i,*) // i-th row vector (1 x n matrix) A.col(j), A(*,j) // j-th column vector (m x 1 matrix) A.diag(k) // diagonal vector (k=0 where i=j) #> A = [ 2,5,8; 3,6,9; 4,7,10 ] A = [ 2 5 8 ] [ 3 6 9 ] [ 4 7 10 ] #> A.col(3) ans = [ 8 ] [ 9 ] [ 10 ] #> A.diag(0) -= 20 A = [ -18 5 8 ] [ 3 -14 9 ] [ 4 7 -10 ] #> A.row(2) *= 100 A = [ -18 5 8 ] [ 300 -1400 900 ] [ 4 7 -10 ]
Access to elements (3) matrix A.(matrix) // entry of elelements, A.entry(matrix) #> A = [ 10,40,70,100; 20,50,80,110; 30,60,90,120 ] A = [ 10 40 70 100 ] [ 20 50 80 110 ] [ 30 60 90 120 ] #> A.( [2,6,10] ) ans = [ 20 60 100 ] #> A.( [2,6,10] ) = 0 A = [ 10 40 70 0 ] [ 0 50 80 110 ] [ 30 0 90 120 ] 2nd, 6th, and 10th elements
Access to elements (4) matrix A..(rows)(columns) // submatrix, A.sub(rows)(columns) #> x = 1:6 x = [ 1 2 3 4 5 6 ] #> A = [ x+10; x+20; x+30; x+40; x+50; x+60 ] A = [ 11 12 13 14 15 16 ] [ 2122 23 2425 26 ] [ 31 32 33 34 35 36 ] [ 41 42 43 44 45 46 ] [ 51 52 53 54 55 56 ] [ 61 62 63 64 65 66 ] #> A ..( 2,1,[4,3],6) ( 2,[5,4],1 ) ans = [ 22252421] [ 12 15 14 11 ] [ 42 45 44 41 ] [ 32 35 34 31 ] [ 62 65 64 61 ]
Deleting elements matrix A.row(i) = [] // A = A.rowdel(i) A.col(j) = [] // A = A.coldel(i) A..(rows)(columns) = [] // A = A.subdel(rows)(columns) #> A = [ 11,12,13,14; 21,22,23,24; 31,32,33,34 ] A = [ 11 12 13 14 ] [ 21 22 23 24 ] [ 31 32 33 34 ] #> A.row(2) = [] A = [ 11 12 13 14 ] [ 31 32 33 34 ] #> A.col(3) = [] A = [ 11 12 14 ] [ 31 32 34 ]
Extending matrices (1) matrix A.resize(m,n) // new m x n dimension, while copying A.ext(m,n,s) // extend m rows, n columns and assign s #> A = [ 1,2,3; 4,5,6 ] A = [ 1 2 3 ] [ 4 5 6 ] #> A.resize(4,6) ans = [ 1 2 3 0 0 0 ] [ 4 5 6 0 0 0 ] [ 0 0 0 0 0 0 ] [ 0 0 0 0 0 0 ] #> A.ext(3,2, -1) ans = [ 1 2 3 -1 -1 ] [ 4 5 6 -1 -1 ] [ -1 -1 -1 -1 -1 ] [ -1 -1 -1 -1 -1 ] [ -1 -1 -1 -1 -1 ] #> A.resize(4,2) ans = [ 1 2 ] [ 4 5 ] [ 0 0 ] [ 0 0 ]
Extending matrices (2) matrix A.insrow(i,B) // insert row A.inscol(j,B) // insert column A.ins(i,j,B) // insert matrix #> A = (1:9)._3 A = [ 1 4 7 ] [ 2 5 8 ] [ 3 6 9 ] #> A.insrow(2, [11,12,13,14] ) ans = [ 1 4 7 0 ] [ 11 12 13 14 ] [ 2 5 8 0 ] [ 3 6 9 0 ] #> A.inscol(3, [11,12]) ans = [ 1 4 11 12 7 ] [ 2 5 0 0 8 ] [ 3 6 0 0 9 ]
Member functions matrix A.m// m of an m x n matrix A.n// n of an m x n matrix A.mn // mn of an m x n matrix A.det // determinant A.rank// rank of a matrix A.inv // inverse of a matrix, A^(-1) A.eig // eigenvalues of a matrix and many more #> A = [ 1,2; 4,3 ] A = [ 1 2 ] [ 4 3 ] #> A.det ans = -5 #> A.inv ans = [ -0.6 0.4 ] [ 0.8 -0.2 ] #> A = [ 11,12,13; 21,22,23 ] A = [ 11 12 13 ] [ 21 22 23 ] #> A.m; A.n; A.mn; ans = 2 ans = 3 ans = 6
■ Tuples (a1,a2,a3,…,an) // n-tuple #> (a,b) = (1,2) a = 1 b = 2 #> (b,a) = (a,b) b = 1 a = 2 Swap values by tuples
■ Logical (true or false), conditional 0 // false only for the zero Non-zero // true for all non-zeros a && b // and, .and(a,b) a || b // or, .or(a,b) a !& b // exclusive and, .xand(a,b) a !| b // exclusive or, .xor(a,b) #> [ 0 && 0, 0 && 1, 1 && 0, 1 && 1 ] ans = [ 0 0 0 1 ] #> [ 0 || 0, 0 || 1, 1 || 0, 1 || 1 ] ans = [ 0 1 1 1 ] #> [ 0 !& 0, 0 !& 1, 1 !& 0, 1 !& 1 ] ans = [ 1 0 0 1 ] #> [ 0 !| 0, 0 !| 1, 1 !| 0, 1 !| 1 ] ans = [ 0 1 1 0 ]
■ relational a > b // greater than a < b // less than a >= b // greater than or equal to a <= b // less than or equal to a == b // equal to a != b // not equal to #> 3 > 2 ans = 1 #> 3 < 2 ans = 0 #> 3 >= 2 ans = 1 #> 3 <= 2 ans = 0 #> 3 == 2 ans = 0 #> 3 != 2 ans = 1
■ relational operators for matrices only A .mn. B // true if A and B are of same dimension A .eq. b // true if all a_ij == b_ij A .ne. b // true if all a_ij != b_ij A .gt. B // true if all a_ij > b_ij A .lt. B // true if all a_ij < b_ij A .ge. B // true if all a_ij >= b_ij A .le. B // true if all a_ij <= b_ij #> A = [ 1,2,3 ]; B = [ 4,5,6 ]; A = [ 1 2 3 ] B = [ 4 5 6 ] #> A .mn. B ans = 1 #> A .mn. B' ans = 0
■ Array Data can be declared as an array. Access to its elements is done by [] double x[n] is equal to x[0],x[1],..., x[n-1] Be careful !!! Index starts at 0 #> double x[5] #> x[0] = 3 ans = 3 #> x[1] = 5 ans = 5 #> x x = double [5] [0] = 3 [1] = 5 [2] = 2.0694473e+161 [3] = 1.8875371e+219 [4] = 3.7699368e-317 Note that x[5] causes an error since it tries to refer 6-th element (not existing)
■ ternary operation, a ? b : c a ? b : c // b if a is true, otherwise c #> 3 ? 10 : 20; ans = 10 #> 0 ? 10 : 20; ans = 20 #> (a,b) = (2,3); .max(a,b); .min(a,b); a = 2 b = 3 ans = 3 ans = 2 #> a > b ? a : b b = 3 #> a < b ? a : b a = 2