Brief Guide to msharpmath ( cemmsharp )
-
Upload
ali-gentry -
Category
Documents
-
view
20 -
download
2
description
Transcript of Brief Guide to msharpmath ( cemmsharp )
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 70
Prof. Charn-Jung KimSeoul National University
Brief Guide tomsharpmath (cemmsharp)
Lecture by
www.msharpmath.com
2013. 03. 12
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 702
Why msharpmath ?
function f = myfun(x)f = x^2 – 3*x + 2>> fzero( @myfun, 0 )
Need a paradigm shift :
grammar-based to human thinking
#> solve .x ( x^2 = 3*x – 2 );
M#Mathhuman-think-ing
MATLABgrammar-based
Find a root of 2 3 2x x
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 703
How to download msharpmath
• HomePage : www.msharpmath.com
Click here
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 704
msharpmath Screen
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 705
Mathematics Calculator
#> 3 + 4 * 5
ans = 23
#> ans / 1000 + ans * 100
ans = 2300.023
cursor in msharpmath
default variable name for results
Variable “ans” can be used in subsequent
commands
Variable “ans” is changed to a new value
#> 3 + 4 * 5 ans = 23
#> ans/1000 + ans*100 ans = 2300.023
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 706
■ Unit conversion
x.f // Fahrenheit to other temperaturesx.c // Celsius to other temperaturesx.ft // feet in meterx.acre// acre in square meterand many more
#> 77.f
25 [C]77 F536.67 R298.15 K ans = 298.15
#> 1.ft * 1 ans = 0.3048
#> 1.acre * 1 ans = 4046.8564
expression-look-alike !!!
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 707
■ Root finding
solve.x{a} ( f(x)=g(x) ) // a root of f(x)=g(x) near asolve.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 !!!
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 708
■ 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
2 2 1
1sin( )
x x y
xe x y dydx
expression-look-alike !!!
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 709
● data type : double (real numbers)
msharpmath (m#math)
double (real number)complexpoly (polynomial)matrixvertexsignalimage...
MATLAB
matrix
real numbers are treated by 1 x 1 matrix
double
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7010
Operators for double (1)
|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
double
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7011
Operators for double (2)
a + b // additiona - b // subtractiona * b // multiplicationa / b // right divisiona \ b // left divisiona ^ b // powera % b // remainder, equal to .mod(a,b)
#> 2 / 3 ans = 0.66666667
#> 2 \ 3 ans = 1.5
#> 2 ^ 3 ans = 8
#> 13 % 5 ans = 3
#> 2 + 3 ans = 5
#> 2 - 3 ans = -1
#> 2 * 3 ans = 6
double
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7012
Operators for double (3)
a += b // a = a+ba -= b // a = a-ba *= b // a = a*ba /= b // a = a/ba ^= b // a = a^ba %= b // a = a%b
#> a = 3 a = 3
#> a += 4 a = 7
#> a ^= 2 a = 49
Compound operations
double
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7013
● data type : poly (polynomial)
msharpmath (m#math)
double (real number)complexpoly (polynomial)matrixvertexsignalimage...
MATLAB
matrix
polynomials are treated by functionssuch as
polyval(p,x)polyder(p,x)
poly
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7014
ascending poly
p(x) = a0 + a1 x + a2 x^2 + a3 x^3 + … + an x^n
poly( a0, a1, a2, a3, … , an )
[ a0, a1, a2, a3, … , an ] .apoly
2 30 1 2 3( ) n
np x a a x a x a x a x
#> 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
poly
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7015
[ 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
#> .[ 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 )
descending poly
Note : a leading dot for descending polynomial !!!
#> .[] ans = poly( 0) = 0#> p = q = r = .[] p = poly( 0) = 0 q = poly( 0) = 0 r = poly( 0) = 0
.[] is a zero polynomial
poly
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7016
-p // unary minusp’ // dp(x)/dx, derivative p~ // int_0^x p(x) dx, integration p.’ // p(x)-p(x-1), finite differencep.~ // 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 (1)
23 3 3 3 11 2 3 ( 1)
2x x x
3 23dx x
dx
3 3 2( 1) 3 3 1x x x x
3 4
0
1
4
xx dx x
poly
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7017
p + q // additionp - q // subtractionp * q // multiplicationp / q // quotient(right division)q \ p // quotient(left division)p % q // remainderP %% q // synthetic division
#> p / q ans = poly( -2 3 ) = 3x -2
#> p % q ans = poly( 9 ) = 9
#> 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
Operations for poly (2)
2( ) 3 4 5p x x x ( ) 2q x x
( ) ( )(3 2) 9p x q x x
#> p = .[3,4,5] p = poly( 5 4 3 ) = 3x^2 +4x +5#> q = .[1,2] q = poly( 2 1 ) = x +2
poly
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7018
p(x) // doublep(z) // complexp(q) // polynomialp(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 (3)
2( )p x x
2 2(3 4) 9 24 16x x x
2(3 ) 9 6 1 8 6i i i
2(3) 9
poly
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7019
p(x) // doublep(z) // complexp(q) // polynomialp(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 ]
Operations for poly (4)
2( )p x x
21 2 1 2 1 2 7 10
3 4 3 4 3 4 15 22
2 2
2 2
1 41 2
9 163 4
poly
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7020
p.solve // rootsp.pow(k) // replace x by x^k, p[i*k]=p[i]p.shift(k) // replace x by x-kp.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].solveans = [ -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
Member functions
2 3 3 2( ) , ( ) ( )p x x p x x
poly
2( 2) ( 2)p x x
3 2 3( )p x x x x
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7021
#> .[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
exercise for poly
2 3 2 3 2 3(3 5 7) (5 4 )( 6 15 24) 32( 17)x x x x x x x x
poly
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7022
● data type : complex
msharpmath (m#math)
double (real number)complexpoly (polynomial)matrixvertexsignalimage...
MATLAB
matrix
complex numbers are treated by 1 x 1 matrix
complex
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7023
Creating complex numbers
1i, 1j, 1! // i or j works only after digits 0-9x+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 vari-ables
In polar coordinate
complex
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7024
Elements of complex
z = x+y! |z| // absolute value~z // conjugate, x-y!z.x, z.real // real part, xz.y, z.imag // imaginary part, xz.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
complex
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7025
Be cautious with sqrt(-1)
A function “sqrt” returns ‘double’ for ‘double’, ‘complex’ for ‘complex’
1
#> sqrt(-1) ans = -1.#IND
#> sqrt(-1+0i) ans = 6.12323e-017 + 1!
1 sqrt( 1 0i)i NaN sqrt( 1)
complex
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7026
● data type : matrix
msharpmath (m#math)
double (real number)complexpoly (polynomial)matrixvertexsignalimage...
MATLAB
matrix
Matrix Laboratory
All the data are pro-cessed based on matrix
11 12 1
21 22 2
1 2
n
n
m m mn
a a a
a a a
a a a
A
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7027
Matrix by a pair of brackets []
// separate rows by a semi-colon ;// separate elements by a comma ,
[] // 0 x 0 matrix
[ a11,a12,…,a1n; a21,a22,… ; … ; a_m1,…, a_mn ]
#> 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 ele-ments of a matrix
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7028
Matrix by a colon operator
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
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7029
Matrix by built-in functions
.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
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7030
Operations for matrices
+ // 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 ]
1
2
1 2 5,
3 4 6
x
x
Ax = b
1 \ x = A b A b
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7031
Operations with scalars
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 ]
1 2 3 1 2 3 5 5 55
4 5 6 4 5 6 5 5 5
11 12 13
21 22 23
11 12 13
21 22 23
5
5 5 5
5 5 5
a a a
a a a
a a a
a a a
in MATLAB, A = 5 results in a scalar (1 x 1 matrix)
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7032
Element-by-element operations
.* // 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 3 ] [ 2.33333 2 ]
#> B .^ A ans = [ 5 36 ] [ 343 4096 ]
#> A .* B ans = [ 5 12 ] [ 21 32 ]
#> A ./ B ans = [ 0.2 0.333333 ] [ 0.428571 0.5 ]
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7033
Dot and Kronecker Products
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
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7034
Concatenations
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 ]
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7035
Set operations
A ++ B // set unionA -- B // set differenceA /\ 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 ]
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7036
Compound assignments
A += B // A = A + BA -= B // A = A - BA *= B // A = A * BA /= B // A = A / B = A(B^-1)A |= B // A = A | B, horizontal concatenationA _= B // A = A _ B, vertical concatenationA\_= B // A = A\_ B, diagonal concatenation
A .*= B // A = A .* BA ./= B // A = A ./ BA .^= B // A = A .^ B
A ++= B // A = A ++ B, set unionA --= B // A = A –- B, set differenceA /\= B // A = A /\ B, set intersection
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7037
Access to elements (1)
A(i,j), A(k) // real elementA{i,j), A{k} // imaginary elementA[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
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7038
Access to elements (2)
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 ]
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7039
Access to elements (3)
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
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7040
Access to elements (4)
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 ] [ 21 22 23 24 25 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 = [ 22 25 24 21 ] [ 12 15 14 11 ] [ 42 45 44 41 ] [ 32 35 34 31 ] [ 62 65 64 61 ]
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7041
Deleting elements
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 ]
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7042
Extending matrices (1)
A.resize(m,n) // new m x n dimension, while copyingA.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 ]
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7043
Extending matrices (2)
A.insrow(i,B) // insert rowA.inscol(j,B) // insert columnA.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 ]
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7044
Member functions
A.m // m of an m x n matrixA.n // n of an m x n matrixA.mn // mn of an m x n matrixA.det // determinantA.rank // rank of a matrixA.inv // inverse of a matrix, A^(-1)A.eig // eigenvalues of a matrixand many more
#> 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
#> 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 ]
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7045
■ 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
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7046
■ Logical (true or false), conditional
0 // false only for the zeroNon-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 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7047
■ relational
a > b // greater thana < b // less thana >= b // greater than or equal toa <= b // less than or equal toa == b // equal toa != 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
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7048
■ 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_ijA .ne. b // true if all a_ij != b_ij
A .gt. B // true if all a_ij > b_ijA .lt. B // true if all a_ij < b_ijA .ge. B // true if all a_ij >= b_ijA .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
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7049
■ 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 errorsince it tries to refer 6-th ele-ment (not existing)
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7050
■ 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
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7051
■ ++, --
Prefix : Variable is increased/decreased ‘before use’++a // a = a+1--a // a = a-1
#> a = 1; b = ++a; a = 1 b = 2#> a; b; a = 2 b = 2
Postfix : Variable is increased/decreased ‘after use’a++ // a = a+1a-- // a = a-1
#> a = 1; b = a++; a = 1 b = 1#> a; b; a = 2 b = 1
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7052
■ if-else
if(cond) stmt; // if cond == true, stmt is executed
if(cond) stmt1; // if cond == true, stmt1 is executedelse stmt2; // if cond == false, stmt2 is executed
x = 2; s = 3;
if (x == 1) s += 10;else if(x == 2) s *= 10;else if(x == 3) s /= 10;
x = 2 s = 3 s = 30
2nd clause is executed
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7053
■ while
while(cond) stmt; // stmt while “cond” is true
i = 0;while( ++i < 5 ) i;
i = 0 i = 1 i = 2 i = 3 i = 4
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7054
■ do-while
do { stmt_list } while(cond); // run at least once// then, stmt while “cond” is true
a = 5;do { a = a*a; } while( a < -1 );
a = 5 a = 25
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7055
■ for (1)
for[number] stmt; // stmt “number” times
#> s = 0; for[10] s += 1;
s = 0 s = 1 s = 2 s = 3 s = 4 s = 5 s = 6 s = 7 s = 8 s = 9 s = 10
#> a = b = 1; for[10] { (a,b) = (b,a+b);; b; }
a = 1 b = 2 b = 3 b = 5 b = 8 b = 13 b = 21 b = 34 b = 55 b = 89 b = 144
Pibonachi sequence, x(n+2)=x(n)+x(n+1)
Note : double semi-colons ;; suppress the screen output
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7056
■ for (2)
for.n(a,b,h=1) stmt1; // stmt1 for n=a,a+h,a+2h,…for(init; cond; expr) stmt2; // the same as C-language
#> for.k(3,7) k^2;
ans = 9 ans = 16 ans = 25 ans = 36 ans = 49
#> for.j(12,3,-2) j;
j = 12 j = 10 j = 8 j = 6 j = 4
#> for(j=12; j>=3; j-=2 ) j;
j = 12 j = 10 j = 8 j = 6 j = 4
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7057
■ break
break; // is used to exit from ‘while’, ‘for’, ‘switch’
i = 0;while(1) { i; if( ++i > 5 ) break; }
i = 0 i = 0 i = 1 i = 2 i = 3 i = 4 i = 5
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7058
■ continue
continue; // is used to move to the starting position
i = 0;while( ++i < 5 ) { if( i == 3 ) continue; i; }
i = 0 i = 1 i = 2 i = 4
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7059
■ goto
goto target; … target: stmt// target must start with a string
a = 1; goto skip; b = 2; skip: c = 3;
a = 1 c = 3
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7060
■ switch-case-default
switch(n) { case n1: stmt: break; case n2: stmt; default: stmt; // optional}
#> (a,b,c) = (0,0,0) a = 0 b = 0 c = 0
#> n = 4; n = 4
switch(n) { case 1: a = 10; break; default: c = 5; // continue to the next line case 2: b = 20; break; case 3: c = 100;}
c = 5 b = 20
#> a; b; c; a = 0 b = 20 c = 5
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7061
■ Function (1)
double f(x) = x ;double f(double x) = x ;double f(double x) { return x; } // standard C grammar
#> double fn(x) = |x|*sin(x) ;#> fn ;double fn(x)
#> fn(3) ;ans = 0.42336002
#> fn'(3) ; // derivativeans = -2.8288575
#> fn''(3) ; // 2nd-order derivativeans = -2.4033449
#> fn~(0,3); // integration over (0,3)ans = 3.1110975
#> fn++( [1,2,3] ) ; // upgrade for matrixans = [ 0.841471 1.81859 0.42336 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7062
■ Function (2) arguments
double f(a,b,c)double f(double a,b,c)double f(double a, double c, double c) // C language
Repeating arguments of same types can be declared by a leading declaration
matrix sol(a,b, vertex u,v)
has data types
matrix solmatrix amatrix bvertex uvertex v
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7063
■ Function (3) return type
matrix test(double n) { // matrix test(n) assumes n is also a matrix (a,b,c) = (0,0,0); switch(n) { case 1: a = 10; break; // case must be followed by an integer only default: c = 5; case 2: b = 20; break; case 3: c = 100; } return [a,b,c];}
#> test(1);ans = [ 10 0 0 ]
#> test(2);ans = [ 0 20 0 ]
#> test(3);ans = [ 0 0 100 ]
#> test(4);ans = [ 0 20 5 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7064
■ Function (4) upgrade, matrix F(double x)
#> matrix ut(double x) = [ x; 1; x^2 ];
#> ut( 3 );ans =[ 3 ][ 1 ][ 9 ]
#> ut++( [ 3,4,5 ] );ans =[ 3 4 5 ][ 1 1 1 ][ 9 16 25 ]
#> ut++( [ 3,4; 5,6 ] );ans =[ 3 4 ][ 5 6 ][ 1 1 ][ 1 1 ][ 9 16 ][ 25 36 ]
2
1)(
x
x
xut
...
...65
43
)65
43(ut
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7065
■ Function (5), copying arguments
Arguments are by default copied inside of a function, and thus original variables are unchanged.
void swap1(x,y) { t = x; x = y; y = t; }
#> x = 3; y = 5;x = 3y = 5
#> swap1(x,y); x; y;x = 3y = 5
// Nothing happens to x and y.
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7066
■ Function (6), referring arguments
A variable can be refered by a prefix '&‘ , and the original vari-ables are delivered inside a function.
void swap2(&x,&y) { t = x; x = y; y = t; }
#> x = 3; y = 5;x = 3y = 5
#> swap2(x,y); x; y;x = 5y = 3
// Note that x and y are changed.
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7067
■ Function (7), array arguments
A prefix '*' is attached to array arguments and data delivery is by reference.
void array(matrix *A){ A[0] = .zeros(3); A[2] = [ 1,2,3,4; 5,6,7,8 ];}
#> matrix An[4]; array(An); An;An = matrix [4][0] =[ 0 0 0 ][ 0 0 0 ][ 0 0 0 ][1] =[ undefined ][2] =[ 1 2 3 4 ][ 5 6 7 8 ][3] =[ undefined ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7068
■ Function (8), recurrence
A function can call itself inside its definition, and this is the case of recurrence function.
%> factorial function
double fac(n) { if( n == 1 ) return 1; return n*fac(n-1);}
#> fac(5);ans = 120
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7069
■ Function (9), dynamic default assignments
Arguments of a function can be expressed by the arguments de-fined before.
double area( a, b=a ) = a*b; // b is expressed by a
#> area ; double area( a, b=a )
#> area(3);ans = 9
#> area(3,4);ans = 12
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7070
■ Function (10), function arguments
A special grammar is innovated for a function argument as fol-lows.
double ffx(x) = x^2;double ggx(x) = x*sin(x);double pftn(f(x),x) = f(x)^2; // function argument f(x)
#> pftn(ffx,3); pftn(ggx,3);ans = 81ans = 0.17923371
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Bestwww.msharpmath.com / 7071
Msharpmath, The Simple is the Best
Thank you !