Why msharpmath - msharpmath, The Simple is the Best

advertisement
2013. 03. 12
Brief Guide to
msharpmath (cemmsharp)
Lecture by
www.msharpmath.com
Prof. Charn-Jung Kim
Seoul National University
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
/ 70
Why msharpmath ?
Need a paradigm shift :
grammar-based to human thinking
Find a root of
MATLAB
grammar-based
function f = myfun(x)
f = x^2 – 3*x + 2
>> fzero( @myfun, 0 )
School of Mechanical and Aerospace Engineering
Seoul National University
x 2  3x  2
M#Math
human-thinking
#> solve .x ( x^2 = 3*x – 2 );
The Simple is the Best
www.msharpmath.com
2 / 70
How to download msharpmath
• HomePage : www.msharpmath.com
Click here
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
3 / 70
msharpmath Screen
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
4 / 70
Mathematics Calculator
#> 3 + 4 * 5
ans =
23
#> ans/1000 + ans*100
ans =
2300.023
cursor in msharpmath
#> 3 + 4 * 5
default variable name
for results
Variable “ans” can be
used in subsequent
commands
ans =
23
#> ans / 1000 + ans * 100
ans =
2300.023
Variable “ans” is changed to a new value
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
5 / 70
■ 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 !!!
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
6 / 70
■ 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 !!!
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
7 / 70
■ 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 =
2

1
2 x 1
x
-0.0089769673
e  x  y sin( x  y )dydx
expression-look-alike !!!
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
8 / 70
double
● data type : double (real numbers)
msharpmath (m#math)
MATLAB
double (real number)
complex
poly (polynomial)
matrix
vertex
signal
image
...
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
real numbers are treated
by 1 x 1 matrix
The Simple is the Best
www.msharpmath.com
9 / 70
Operators for double (1)
double
|a|
-a
!a
// absolute
// unary minus
// 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
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
10 / 70
Operators for double (2)
double
a
a
a
a
a
a
a
+
*
/
\
^
%
b
b
b
b
b
b
b
//
//
//
//
//
//
//
#> 2 + 3
ans =
#> 2 - 3
ans =
#> 2 * 3
ans =
addition
subtraction
multiplication
right division
left division
power
remainder, equal to .mod(a,b)
5
#> 2 / 3
ans =
0.66666667
-1
#> 2 \ 3
ans =
1.5
6
#> 2 ^ 3
ans =
8
#> 13 % 5
ans =
3
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
11 / 70
Operators for double (3)
double
Compound operations
a
a
a
a
a
a
+=
-=
*=
/=
^=
%=
b
b
b
b
b
b
//
//
//
//
//
//
a
a
a
a
a
a
=
=
=
=
=
=
a+b
a-b
a*b
a/b
a^b
a%b
#> a = 3
a =
3
#> a += 4
a =
7
#> a ^= 2
a =
49
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
12 / 70
poly
● data type : poly (polynomial)
msharpmath (m#math)
MATLAB
double (real number)
complex
poly (polynomial)
matrix
vertex
signal
image
...
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
polynomials are
treated by functions
such as
polyval(p,x)
polyder(p,x)
The Simple is the Best
www.msharpmath.com
13 / 70
poly
ascending poly
poly( a0, a1, a2, a3, … , an )
[ a0, a1, a2, a3, … , an ] .apoly
p( x)  a0  a1 x  a2 x2  a3 x3 
 an xn
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
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
14 / 70
descending poly
poly
[
.[
.[
.[
.[
.[
an, … , a3, a2,
an, … , a3, a2,
a ]
//
a, b ]
//
a, b, c ]
//
a, b, c, d ] //
a1, a0 ]
a1, a0 ]
constant
1st-order
2nd-order
3rd-order
#> .[ 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 )
School of Mechanical and Aerospace Engineering
Seoul National University
.dpoly
Note : a leading dot for
descending polynomial !!!
polynomial
polynomial, ax+b
polynomial, ax^2+bx+c
polynomial, ax^3+bx^2+cx+d
#> .[]
ans = poly( 0)
= 0
#> p = q = r = .[]
p = poly( 0)
= 0
q = poly( 0)
= 0
r = poly( 0)
= 0
.[] is a zero polynomial
The Simple is the Best
www.msharpmath.com
15 / 70
Operations for poly (1)
poly
-p
p’
p~
p.’
p.~
//
//
//
//
//
unary minus
dp(x)/dx,
int_0^x p(x) dx,
p(x)-p(x-1),
p(1)+p(2)+…+p(n),
#> .[1,0,0,0]'
ans = poly( 0
= 3x^2
0
#> .[1,0,0,0].'
ans = poly( 1 -3
= 3x^2 -3x +1
#> .[1,0,0,0]~
ans = poly( 0
= 0.25x^4
0
derivative
integration
finite difference
cumulative sum
d 3
x  3x 2
dx
3 )
x3  ( x  1)3  3x2  3x  1
3 )
0
0
0.25 )
#> .[1,0,0,0].~
ans = poly( 0 0 0.25 0.5 0.25 )
= 0.25x^4 +0.5x^3 +0.25x^2
School of Mechanical and Aerospace Engineering
Seoul National University

x
0
x3 dx 
1 4
x
4
13  23  33 
1

 x3   x( x  1) 
2

The Simple is the Best
www.msharpmath.com
2
16 / 70
Operations for poly (2)
poly
p
p
p
p
q
p
P
+ q
- q
* q
/ q
\ p
% q
%% q
//
//
//
//
//
//
//
addition
subtraction
multiplication
quotient(right division)
quotient(left division)
remainder
synthetic division
p( x)  3x2  4x  5
q ( x)  x  2
#> 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( -2
= 3x -2
#> p - q
ans = poly( 3 3 3 )
= 3x^2 +3x +3
#> p % q
ans = poly( 9 )
= 9
#> p * q
ans = poly( 10 13 10 3 )
= 3x^3 +10x^2 +13x +10
p( x)  q( x)(3x  2)  9
School of Mechanical and Aerospace Engineering
Seoul National University
3 )
The Simple is the Best
www.msharpmath.com
17 / 70
Operations for poly (3)
poly
p(x)
p(z)
p(q)
p(A)
p[A]
//
//
//
//
//
double
complex
polynomial
matrix (element-by-element)
matrix polynomial, a0 I+a1 A+a2 A^2 + …
#> p = .[1,0,0]
p = poly( 0
= x^2
0
p ( x)  x 2
1 )
(3)2  9
#> p(3)
ans =
9
#> p(3+1i)
ans = 8 + 6!
(3  i)2  9  6i  1  8  6i
#> p( .[3,4] )
ans = poly( 16 24 9 )
= 9x^2 +24x +16
(3x  4)2  9 x2  24 x  16
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
18 / 70
Operations for poly (4)
poly
p(x)
p(z)
p(q)
p(A)
p[A]
//
//
//
//
//
double
complex
polynomial
matrix (element-by-element)
matrix polynomial, a0 I+a1 A+a2 A^2 + …
p ( x)  x 2
#> p( [1,2;3,4] )
ans =
[
1
[
9
#> p[ [1,2;3,4] ]
ans =
[
7
[
15
4 ]
16 ]
10 ]
22 ]
School of Mechanical and Aerospace Engineering
Seoul National University
12
 2
3
1
3

2 2  1 4 


42  9 16 
2
2
1

3
4 

2  1
4  3
2  7

4  15
The Simple is the Best
www.msharpmath.com
10 
22 
19 / 70
Member functions
poly
p.solve
p.pow(k)
p.shift(k)
p.up(k)
p.newton(u)
//
//
//
//
//
roots
replace x by x^k, p[i*k]=p[i]
replace x by x-k
multiply by x^k, p[i+k]=p[i]
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( x)  x2 ,
#> p.shift(2)
ans = = x^2 -4x +4
p( x  2)  ( x  2)2
#> p.up(3)
ans = x^5
p( x) x3  x2 x3
School of Mechanical and Aerospace Engineering
Seoul National University
p( x3 )  ( x3 )2
The Simple is the Best
www.msharpmath.com
20 / 70
poly
exercise for poly
(3x2  5x  7)3  (5x  4 x2 )( x3  6 x2  15x  24)  32( x  17)3
#> .[3,-5,7]^3 - poly(0,5,4)*.[1,-6,15,24] + 32*.[1,-17]^3
ans = poly( -156873 26889 -837 -753 433
= 27x^6 -139x^5 +433x^4 -753x^3 -837x^2
+26889x -156873
School of Mechanical and Aerospace Engineering
Seoul National University
-139
27 )
The Simple is the Best
www.msharpmath.com
21 / 70
complex
● data type : complex
msharpmath (m#math)
MATLAB
double (real number)
complex
poly (polynomial)
matrix
vertex
signal
image
...
matrix
School of Mechanical and Aerospace Engineering
Seoul National University
complex numbers are
treated by 1 x 1 matrix
The Simple is the Best
www.msharpmath.com
22 / 70
Creating complex numbers
complex
1i, 1j, 1!
x+y!
(r+t!).cyl
#> 1i
ans =
// i or j works only after digits 0-9
// postfix ! for pure imaginary part
// r cos(t) + r sin(t) !
‘i’ after digits
0 + 1!
#> 1j
ans =
0 + 1!
#> 1!
ans =
0 + 1!
#> 3+pi!
ans =
3 + 3.14159!
#> (3+pi!).cyl
ans = -3 + 3.67394e-016!
School of Mechanical and Aerospace Engineering
Seoul National University
‘j’ after digits
‘!’ after both digits and
variables
In polar coordinate
The Simple is the Best
www.msharpmath.com
23 / 70
Elements of complex
complex
z = x+y!
|z|
~z
z.x, z.real
z.y, z.imag
z.r, z.abs
z.t, z.arg
z.deg
//
//
//
//
//
//
//
absolute value
conjugate, x-y!
real part, x
imaginary part, x
equal to |z|=sqrt(x*x+y*y)
phase part in radian, tan^-1(y/x)
phase part in degree
#> z = 3+4i
z = 3 + 4!
#> z.r
ans =
5
#> |z|
ans =
#> z.t
ans =
0.92729522
#> z.deg
ans =
53.130102
#> ~z
ans =
#> z.y
ans =
5
3 - 4!
4
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
24 / 70
Be cautious with
complex
A function “sqrt” returns
‘double’ for ‘double’,
‘complex’ for ‘complex’
#> sqrt(-1)
ans =
1 
sqrt(-1)
NaN  sqrt(1)
i  1  sqrt(1  0i)
-1.#IND
#> sqrt(-1+0i)
ans = 6.12323e-017 + 1!
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
25 / 70
matrix
● data type : matrix
msharpmath (m#math)
MATLAB
double (real number)
complex
poly (polynomial)
matrix
vertex
signal
image
...
matrix
Matrix Laboratory
All the data are
processed based on matrix
 a11
a
A   21


 am1
School of Mechanical and Aerospace Engineering
Seoul National University
a12
a22
am 2
a1n 
a2 n 


amn 
The Simple is the Best
www.msharpmath.com
26 / 70
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
[
4
5
0
[
6
0
0
[
2
4
6
[
8
10
0
[
12
0
0
-1
-4
-6
0
0
0
;
,
All zeros are assigned
for undeclared elements
-2
-5
-0
0
0
0
-3
-0
-0
0
0
0
]
]
]
]
]
]
School of Mechanical and Aerospace Engineering
Seoul National University
Matrices can be also
elements of a matrix
The Simple is the Best
www.msharpmath.com
27 / 70
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
1.2
1.4
1.6
#> 1 : 0.2 : 2
ans = [
1.8
School of Mechanical and Aerospace Engineering
Seoul National University
// [ a,a-1,a-2, … ] if a > b
2 ]
The Simple is the Best
www.msharpmath.com
28 / 70
Matrix by built-in functions
matrix
.I(m,n=m)
or
.ones(m,n=m)
.zeros(m,n=m)
#> .I(2,3)
ans =
[
1
0
[
0
1
#> .ones(4)
ans =
[
1
1
[
1
1
[
1
1
[
1
1
#> .zeros(A)
ans =
[
0
0
[
0
0
[
0
0
.eye(m,n=m)
equal to eye(2,3)
0 ]
0 ]
equal to ones(4,4)
1
1
1
1
1
1
1
1
]
]
]
]
Same size with A
0 ]
0 ]
0 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
29 / 70
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 ];
A =
[
1
2 ]
[
3
4 ]
b =
[
5 ]
[
6 ]
b = [ 5; 6 ]
#> A \ b
ans =
[
-4 ]
[ 4.5 ]
School of Mechanical and Aerospace Engineering
Seoul National University
Ax = b,
1
3

2   x1  5

4   x2  6
x = A1b  A \ b
The Simple is the Best
www.msharpmath.com
30 / 70
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
[
9
10
8 ]
11 ]
#> A ^ 5
runtime error#90700: matrix not square
#> A = 5
A =
[
5
[
5
5
5
5 ]
5 ]
1
4

 a11
a
 21
 a11
a
 21
2
5
3
1

5

4
6 

a12
a22
a13 
5

a23 
a12
a22
a13  5

a23  5
3  5

6  5
2
5
5
5
5
5
5
5 
5
5
in MATLAB, A = 5 results in a scalar (1 x 1 matrix)
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
31 / 70
matrix
.*
./
.\
.^
.‘
//
//
//
//
//
Element-by-element operations
element-by-element
element-by-element
element-by-element
element-by-element
pure transpose
multiplication
right division
left division
power
#> A = [ 1,2; 3,4 ]; B = [ 5,6; 7,8 ]
A =
[
1
2 ]
[
3
4 ]
B =
[
5
6 ]
[
7
8 ]
#> A .* B
ans =
[
5
[
21
12 ]
32 ]
#> A .\ B
ans =
[
5
[ 2.33333
3 ]
2 ]
#> A ./ B
ans =
[
0.2 0.333333 ]
[ 0.428571
0.5 ]
#> B .^ A
ans =
[
5
[
343
36 ]
4096 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
32 / 70
Dot and Kronecker Products
matrix
A ** B
A ^^ B
// dot product, .dot(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 ]
#> B ** B
[
7
8
9 ]
ans =
285
#> A ^^
ans
[
1
[
4
[
7
[ -10
[ -40
[ -70
[ 1000
[ 4000
[ 7000
B
=
2
3
5
6
8
9
-20 -30
-50 -60
-80 -90
2000 3000
5000 6000
8000 9000
10
40
70
1
4
7
0
0
0
20
50
80
2
5
8
0
0
0
30 -100 -200 -300 ]
60 -400 -500 -600 ]
90 -700 -800 -900 ]
3
0
0
0 ]
6
0
0
0 ]
9
0
0
0 ]
0
0
0
0 ]
0
0
0
0 ]
0
0
0
0 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
33 / 70
Concatenations
matrix
A | B
A _ B
A \_ B
// horizontal concatenation, .horzcat(A,B,C,…)
// vertical concatenation,
.vertcat(A,B,C,…)
// diagonal concatenation,
.diagcat(A,B,C,…)
#> A = [ 1,2,3 ]; B = [ -4,5 ]
A = [
1
2
B = [
-4
5 ]
#> A | B
ans =
[
1
2
#> A _ B
ans =
[
1
[
-4
2
5
3 ]
0 ]
#> A \_ B
ans =
[
1
[
0
2
0
3
0
3 ]
3
0
-4
School of Mechanical and Aerospace Engineering
Seoul National University
-4
5 ]
0 ]
5 ]
The Simple is the Best
www.msharpmath.com
34 / 70
Set operations
matrix
A ++ B
A -- B
A /\ B
// set union
// set difference
// 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
#> A ++ B
ans =
[
2
3
#> A -- B
ans =
[
4
6 ]
#> B -- A
ans =
[
1
8
[
2
3 ]
4
6
1
8
9 ]
9 ]
9 ]
#> A /\ B
ans =
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
35 / 70
Compound assignments
matrix
A +=
A -=
A *=
A /=
A |=
A _=
A\_=
B
B
B
B
B
B
B
//
//
//
//
//
//
//
A
A
A
A
A
A
A
=
=
=
=
=
=
=
A +
A A *
A /
A |
A _
A\_
B
B
B
B = A(B^-1)
B, horizontal concatenation
B, vertical concatenation
B, diagonal concatenation
A .*= B
A ./= B
A .^= B
// A = A .* B
// A = A ./ B
// A = A .^ B
A ++= B
A --= B
A /\= B
// A = A ++ B, set union
// A = A –- B, set difference
// A = A /\ B, set intersection
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
36 / 70
Access to elements (1)
matrix
A(i,j), A(k)
A{i,j), A{k}
A[i,j], A[k]
// real element
// imaginary element
// 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
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
37 / 70
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 ]
School of Mechanical and Aerospace Engineering
Seoul National University
#> A.diag(0) -= 20
A =
[
-18
5
[
3
-14
[
4
7
8 ]
9 ]
-10 ]
#> A.row(2) *= 100
A =
[
-18
5
[
300 -1400
[
4
7
8 ]
900 ]
-10 ]
The Simple is the Best
www.msharpmath.com
38 / 70
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
#> A.( [2,6,10] ) = 0
A =
[
10
40
70
[
0
50
80
[
30
0
90
60
100 ]
2nd, 6th, and 10th elements
0 ]
110 ]
120 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
39 / 70
Access to elements (4)
matrix
A..(rows)(columns) // submatrix, A.sub(rows)(columns)
#> x = 1:6
x = [
1
#> A = [ x+10; x+20;
A =
[
11
12
[
21
22
[
31
32
[
41
42
[
51
52
[
61
62
2
3
4
5
6 ]
x+30; x+40; x+50; x+60 ]
13
23
33
43
53
63
14
24
34
44
54
64
15
25
35
45
55
65
16
26
36
46
56
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 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
40 / 70
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
[
31
32
13
33
#> A.col(3) = []
A =
[
11
12
[
31
32
14 ]
34 ]
14 ]
34 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
41 / 70
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
[
4
5
#> A.resize(4,6)
ans =
[
1
2
[
4
5
[
0
0
[
0
0
#> A.ext(3,2,
ans =
[
1
[
4
[
-1
[
-1
[
-1
]
3 ]
6 ]
3
6
0
0
0
0
0
0
0
0
0
0
3
6
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
0
0
0
0
]
]
]
]
#> A.resize(4,2)
ans =
[
1
2
[
4
5
[
0
0
[
0
0
]
]
]
]
-1)
2
5
-1
-1
-1
]
]
]
]
]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
42 / 70
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
[
2
5
[
3
6
7 ]
8 ]
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,
ans =
[
1
[
2
[
3
]
]
]
]
[11,12])
4
5
6
11
0
0
12
0
0
7 ]
8 ]
9 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
43 / 70
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 = [ 11,12,13; 21,22,23 ]
A =
[
11
12
13 ]
[
21
22
23 ]
#> A.m;
ans
ans
ans
A.n; A.mn;
=
=
=
#> A = [ 1,2; 4,3 ]
A =
[
1
2 ]
[
4
3 ]
#> A.det
ans =
2
3
6
#> A.inv
ans =
[
-0.6
[
0.8
School of Mechanical and Aerospace Engineering
Seoul National University
-5
0.4 ]
-0.2 ]
The Simple is the Best
www.msharpmath.com
44 / 70
■ Tuples
(a1,a2,a3,…,an)
#> (a,b) = (1,2)
a =
b =
#> (b,a) = (a,b)
b =
a =
// n-tuple
1
2
Swap values by tuples
1
2
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
45 / 70
■ Logical (true or false), conditional
0
// false only for the zero
Non-zero // true for all non-zeros
a
a
a
a
&&
||
!&
!|
b
b
b
b
//
//
//
//
and,
or,
exclusive and,
exclusive or,
.and(a,b)
.or(a,b)
.xand(a,b)
.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 Best
www.msharpmath.com
46 / 70
■ relational
a
a
a
a
a
a
> b
< b
>= b
<= b
== b
!= b
#> 3 > 2
ans =
#> 3 < 2
ans =
#> 3 >= 2
ans =
#> 3 <= 2
ans =
#> 3 == 2
ans =
#> 3 != 2
ans =
//
//
//
//
//
//
greater than
less than
greater than or equal to
less than or equal to
equal to
not equal to
1
0
1
0
0
1
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
47 / 70
■ relational operators for matrices only
A .mn. B
// true if A and B are of same dimension
A .eq. b
A .ne. b
// true if all a_ij == b_ij
// true if all a_ij != b_ij
A
A
A
A
.gt.
.lt.
.ge.
.le.
B
B
B
B
//
//
//
//
true
true
true
true
if
if
if
if
all
all
all
all
a_ij
a_ij
a_ij
a_ij
> b_ij
< b_ij
>= b_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 Best
www.msharpmath.com
48 / 70
■ 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)
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
49 / 70
■ 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 Best
www.msharpmath.com
50 / 70
■ ++, -Prefix : Variable is increased/decreased ‘before use’
++a // a = a+1
--a // a = a-1
#> a = 1; b = ++a;
a =
b =
#> a; b;
a =
b =
1
2
2
2
Postfix : Variable is increased/decreased ‘after use’
a++
// a = a+1
a-// a = a-1
#> a = 1; b = a++;
a =
b =
#> a; b;
a =
b =
1
1
2
1
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
51 / 70
■ if-else
if(cond) stmt; // if cond == true, stmt is executed
if(cond) stmt1; // if cond == true, stmt1 is executed
else
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 =
s =
s =
2nd clause is executed
2
3
30
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
52 / 70
■ while
while(cond) stmt; // stmt while “cond” is true
i = 0;
while( ++i < 5 ) i;
i
i
i
i
i
=
=
=
=
=
0
1
2
3
4
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
53 / 70
■ 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 =
a =
5
25
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
54 / 70
■ for (1)
for[number] stmt; // stmt “number” times
Pibonachi sequence, x(n+2)=x(n)+x(n+1)
#> s = 0;
s
s
s
s
s
s
s
s
s
s
s
=
=
=
=
=
=
=
=
=
=
=
for[10] s += 1;
0
1
2
3
4
5
6
7
8
9
10
#> a = b = 1; for[10] { (a,b) = (b,a+b);; b; }
a
b
b
b
b
b
b
b
b
b
b
=
=
=
=
=
=
=
=
=
=
=
1
2
3
5
8
13
21
34
55
89
144
Note : double semi-colons ;; suppress
the screen output
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
55 / 70
■ 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.j(12,3,-2) j;
#> for.k(3,7) k^2;
ans
ans
ans
ans
ans
=
=
=
=
=
j
j
j
j
j
9
16
25
36
49
=
=
=
=
=
12
10
8
6
4
#> for(j=12; j>=3; j-=2 ) j;
j
j
j
j
j
School of Mechanical and Aerospace Engineering
Seoul National University
=
=
=
=
=
12
10
8
6
4
The Simple is the Best
www.msharpmath.com
56 / 70
■ break
break;
// is used to exit from ‘while’, ‘for’, ‘switch’
i = 0;
while(1) { i; if( ++i > 5 ) break; }
i
i
i
i
i
i
i
=
=
=
=
=
=
=
0
0
1
2
3
4
5
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
57 / 70
■ continue
continue;
// is used to move to the starting position
i = 0;
while( ++i < 5 ) { if( i == 3 ) continue; i; }
i
i
i
i
=
=
=
=
0
1
2
4
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
58 / 70
■ goto
goto target; … target: stmt
// target must start with a string
a = 1;
a =
c =
goto skip;
b = 2;
skip: c = 3;
1
3
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
59 / 70
■ switch-case-default
switch(n) {
case n1: stmt: break;
case n2: stmt;
default: stmt; // optional
}
#> (a,b,c) = (0,0,0)
a =
b =
c =
0
0
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 =
b =
#> a;
a
b
c
b; c;
=
=
=
School of Mechanical and Aerospace Engineering
Seoul National University
5
20
0
20
5
The Simple is the Best
www.msharpmath.com
60 / 70
■ 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) ; // derivative
ans =
-2.8288575
#> fn''(3) ; // 2nd-order derivative
ans =
-2.4033449
#> fn~(0,3); // integration over (0,3)
ans =
3.1110975
#> fn++( [1,2,3] ) ; // upgrade for matrix
ans = [
0.841471
1.81859
0.42336 ]
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
61 / 70
■ 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
matrix
matrix
vertex
vertex
sol
a
b
u
v
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
62 / 70
■ 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 Best
www.msharpmath.com
63 / 70
■ Function (4) upgrade, matrix F(double x)
x
ut ( x)   1 
 x 2 
#> matrix ut(double x) = [ x; 1; x^2 ];
#> ut( 3 );
ans =
[
[
[
3 ]
1 ]
9 ]
#> ut++( [ 3,4,5 ] );
ans =
[
3
[
1
[
9
4
1
16
#> ut++( [ 3,4; 5,6 ] );
ans =
[
3
[
5
[
1
[
1
[
9
[
25
4
6
1
1
16
36
5 ]
1 ]
25 ]
]
]
]
]
]
]
School of Mechanical and Aerospace Engineering
Seoul National University
 3 4  


5
6

3 4   
ut ( 
)   ... 

5 6  ... 




The Simple is the Best
www.msharpmath.com
64 / 70
■ 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 =
y =
3
5
#> swap1(x,y); x; y;
x =
3
y =
5
// Nothing happens to x and y.
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
65 / 70
■ Function (6), referring arguments
A variable can be refered by a prefix '&‘ , and the original variables are
delivered inside a function.
void swap2(&x,&y) {
t = x; x = y; y = t;
}
#> x = 3; y = 5;
x =
y =
3
5
#> swap2(x,y); x; y;
x =
5
y =
3
// Note that x and y are changed.
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
66 / 70
■ 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];
An = matrix [4]
[0] =
[
0
[
0
[
0
[1] =
[ undefined ]
[2] =
[
1
[
5
[3] =
[ undefined ]
array(An);
An;
0
0
0
0 ]
0 ]
0 ]
2
6
3
7
School of Mechanical and Aerospace Engineering
Seoul National University
4 ]
8 ]
The Simple is the Best
www.msharpmath.com
67 / 70
■ 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 Best
www.msharpmath.com
68 / 70
■ Function (9), dynamic default assignments
Arguments of a function can be expressed by the arguments defined
before.
double area( a, b=a ) = a*b;
// b is expressed by a
#> area ; double area( a, b=a )
#> area(3);
ans =
#> area(3,4);
ans =
9
12
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
69 / 70
■ Function (10), function arguments
A special grammar is innovated for a function argument as follows.
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 =
81
ans =
0.17923371
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
70 / 70
Msharpmath, The Simple is the Best
Thank you !
School of Mechanical and Aerospace Engineering
Seoul National University
The Simple is the Best
www.msharpmath.com
71 / 70
Download