python 수학이해하기

Embed Size (px)

Citation preview

PowerPoint

Python mathematicsMoon Yong Joon1

1. 2. 3. 4.

2

5. 6. 7. / 8.

3

9. 10. / 11.

4

1.

Moon Yong Joon5

Moon Yong Joon6

/() /() /() /() /() /() /() /() /() /() /() /() /() /() /() /() /() /() /() /() /() /( ) /() /()7

8

9

Moon Yong Joon10

11

, , , ,

12

13

(, integer) (1, 2, 3, ...) (1, 2, 3, ...) 0 . , , , . 14

15

(, rational number) ( 0 ) . (0 ) (, , , ) '' 16

17

(, irrational number) . , .18

19

[, real number] , . . , (x2 ) . .20

21

(, complex number) .a+bi a, b i i2=-1 . a , b . . 0 . 22

(, complex number) 23

,24

() , . , 1 .

25

: () 0 . 7 -7 7 0 7 -7. n ., , , . 26

: (,reciprocal) (-, : multiplicative inverse) 1, . x 1/x x -1 . 1 27

28

0 , a , a (-1)

absolute value a , a . |a| .

29

30

(, ratio) . (, ratio) . . 100 .31

Pythonsympy, , ,

Moon Yong Joon32

Symbolic mathematics33

sympy SymPy symbolic mathematics python . SymPy a full-featured computer algebra system (CAS) 34

sympy SymPy symbolic mathematics 35

Sympy pi (evalf)

sympy Sympy 36

Symbol name symbol

Symbol 37

sympy SymPy symbolic mathematics 38

Sympy pi (evalf)

39

: sympy(, integer) (1, 2, 3, ...) (1, 2, 3, ...) 0 . 40

: sympy(, rational number) (p) (q) 41

1 : sympy [, real number] 42

2 : sympySympify sympy 43

: sympySympify sympy 44

45

/pi sympy oo , pi 46

Sympy symbol 47

symbol Sympy x = Symbol(x) a, b, c = symbols(a, b, c) 48

symbol : symbols 49

symbol : symbols cls Function 50

51

expand : expand 52

expand : expand 53

force=True

54

Solve : solve 55

Pythonnumberdata type

Moon Yong Joon56

Numeric Type57

Numberic Typesintfloat long(2.x)complex 1 context id

58

, operatorOperationResultNotesx+ysum ofxandyx-ydifference ofxandyx*yproduct ofxandyx/yquotient ofxandyx//y(floored) quotient ofxandyx%yremainder ofx/y-xxnegated+xxunchangedabs(x)absolute value or magnitude ofxint(x)xconverted to integerlong(x)xconverted to long integerfloat(x)xconverted to floating pointcomplex(re,im)a complex number with real partre, imaginary partim.imdefaults to zero.c.conjugate()conjugate of the complex numbercdivmod(x,y)the pair(x//y,x%y)pow(x,y)xto the poweryx**yxto the powery

59

Numeric Type(int)60

- int int operator

61

int int

real : int bit_length() : bit denominator : numerator :

62

long python3 NotesPython 2Python 3x=1000000000000Lx=1000000000000x=0xFFFFFFFFFFFFLx=0xFFFFFFFFFFFFlong(x)int(x)type(x)islongtype(x)isintisinstance(x,long)isinstance(x,int)

63

Numeric Type(float)64

- float float operator

65

float float real : float hex() : 16 fromhex() : hex() float is_integer() : 0 true as_integer_ratio() : denominator : , numerator :

66

Numeric Type(complex)67

- complex float operator

68

complex complex real : float imag: conjugate () :

69

70

fractions fractions . Float float

71

fractions fractions . Float float

72

/

Moon Yong Joon73

74

, . an , a , n .

75

76

77

78

x () x , sqrt(x) " x"( x) .

79

x x1/2

80

x +/-

81

x

x x x 82

x,y

83

Python/

Moon Yong Joon84

85

a, b math.pow ** 86

87

a b math.sqrt root2 **/pow 88

Moon Yong Joon89

90

(, numeral system) , .91

92

(, binary) . 0 1 .93

(octal number system , ) 8 . 0 7 . 3 94

(, ) 10 . 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 .95

(, hexadecimal) 16 . 0 9 A F , . 4 , 2 2 .96

Python

Moon Yong Joon97

98

bin, oct, hex , , 16 99

Bit 100

: and, or, xorOperatorDescription& Binary AND | Binary OR ^ Binary XOR

101

: ~, OperatorDescription~ Binary Ones Complement1 > Binary Right Shift .

102

Moon Yong Joon103

104

PEMDAS 105

106

,S* ,S a,b a*b=b*a , (, commutative law)

.

, , ., , 107

:

108

109

( , associated law) , .

.

, . . . 110

111

112

S S * , S a, b, c a * (b + c) = (a * b) + (a * c) , (b + c) * a = (b * a) + (c * a) S * .

. , , , , . , . , . * 113

114

115

/, , . .12 ={1,2,3,6,12}16 ={1,2,4,8,16}18 ={1,2,3,6,9,18} 12 16 1,2,4 412, 16, 18 1,2 2 1

() 8 ={1,2,4,8}, 9 ={1,3,9} 8 9 1 8 9

116

1, (relatively prime) . 1 .

117

118

(, substitution) (, change of variables) 119

120

(Rationalization) , 121

python operator Moon Yong Joon122

123

special methodpython type class special method

SpecialMethod

special method 124

OperatorDescription0( )Parentheses (grouping)1f(args...)Function call2x[index:index]Slicing3x[index]Subscription4x.attributeAttribute reference5**Exponentiation()6~xBitwise not7+x, -xPositive, negative8*, /, %Multiplication, division, remainder9+, -Addition, subtraction10Bitwise shifts11&Bitwise AND12^Bitwise XOR13|Bitwise OR14in, not in, is, is not, =,, !=, ==Comparisons, membership, identity15notxBoolean NOT16andBoolean AND17orBoolean OR18lambdaLambda expression

125

126

OperationFunctionMethodNegation (Arithmetic)-aneg(a)x.__neg__()Positive+apos(a)x.__pos__()

127

128

129

: OperationFunctionMethodadditionx+yadd(a,b)x.__add__(y)subtractionx-ysub(a,b)x.__sub__(y)multiplicationx*ymul(a,b)x.__mul__(y)divisionx/ydiv(a,b)x.__div__(y)divisionx/ytruediv(a,b)x.__truediv__(y)floor divisionx // yfloordiv(a,b)x.__floordiv__(y)modulo (remainder)x%ymod(a,b)x.__mod__(y)floor division&modulodivmod(x,y)N/Ax.__divmod__(y)raise to powerx**ypow(a,b)x.__pow__(y)

130

: operator Operator import

131

: int methodInt class special method

132

: python

133

: OperationFunctionMethodadditionx+yadd(a,b)y.__radd__(x)subtractionx-ysub(a,b)y.__rsub__(x)multiplicationx*ymul(a,b)y.__rmul__(x)divisionx/ydiv(a,b)y.__rdiv__(x)divisionx/ytruediv(a,b)y.__rtruediv__(x)floor divisionx // yfloordiv(a,b)y.__rfloordiv__(x)modulo (remainder)x%ymod(a,b)y.__rmod__(x)floor division&modulodivmod(x,y)N/Ay.__rdivmod__(x)raise to powerx**ypow(a,b)y.__rpow__(x)

134

135

136

Operationleft bit-shiftxyy bit : (x // (2** y) )bitwiseandx&yx y (1 0) 0 bitwisexorx^yx y bitwiseorx|yx y 1 1 0 0 Bitwise Inversion~a a

137

python ,operator int OperationFunctionMethodleft bit-shiftxyrshift(a,b)x.__rshift__(y)bitwiseandx&yand_(a,b)x.__and__(y)bitwisexorx^yxor(a,b)x.__xor__(y)bitwiseorx|yor_(a,b)x.__or__(y)Bitwise Inversion~ainvert(a)x.__invert__()

138

139

augmented operator140

augmented operatorpython

X += YX = X+ Y

141

OperationFunctionMethodadditionx+=yiadd(a,b)x.__iadd__(y)subtractionx-=yisub(a,b)x.__isub__(y)multiplicationx*=yimul(a,b)x.__imul__(y)divisionx/=yidiv(a,b)x.__idiv__(y)divisionx/=yitruediv(a,b)x.__itruediv__(y)floor divisionx //= yifloordiv(a,b)x.__ifloordiv__(y)modulo (remainder)x%=yimod(a,b)x.__imod__(y)raise to powerx**=yipow(a,b)x.__ipow__(y)

142

OperationFunctionMethodleft bit-shiftx=yirshift(a,b)x.__irshift__(y)bitwiseandx&=yiand_(a,b)x.__iand__(y)bitwisexorx^=yixor(a,b)x.__ixor__(y)bitwiseorx|=yior_(a,b)x.__ior__(y)

143

144

boolean OperationSyntaxFunctionMethodand Logical ANDa and b NA NAor Logical ORaor b NA NANegation (Logical)notanot_(a) NA

145

146

/

Moon Yong Joon147

148

(, equation) / . , . 149

X 2,3

. , a0xna1xn-1an-1xan x an, a0xna1xn-1an-1xan0 x an .150

, , , 151

152

(Linear equation) 1 .153

a0x1+a1x0

a0 = a a1 = b n=1

x .154

155

(, quadratic equation), 2 156

a0x2+a1x1+a2x0

a0 = a a1 = b a2 = c n=2

157

158

(, inequality) . (, inequality sign) .159

a, b, c (,transitive relation) 160

Pythonsympy

Moon Yong Joon161

162

: +/- subs, evalf, replace 163

: *, / subs, evalf, replace 164

165

2 expand solve 166

Python

Moon Yong Joon167

168

python OperationFunctionMethodOrderingabgt(a,b)x.__gt__(y)

169

/

Moon Yong Joon170

171

(-, multiplication) . .172

.173

174

175

(, factorization)

176

177

178

179

(,binomial theorem) x+y (x+y)n , xkyn-k

180

: (a+b)3 (a+b)3=(a+b)(a+b)(a+b) =aaa+aab+aba+abb+baa+bab+bba+bbb =a3+3a2b+3ab2+b3 =a3+3C1a2b+3C2ab2+b3

3C1 = 3P1/1! = 3!/2!/1! = 3 3C2 = 3P2/2! = 3!/1!/2! = 3 181

: (a+b)3

182

Pythonsympy/

Moon Yong Joon183

184

cancel : cancel 185

186

factor factor expand solve 187

factor : modulus modulus 188

factor : (Gau, Gaussian integer) .(a+bi)(a-bi)=a2+b2 189

= (0+i)(0 i)= -i*I= -(i)**2= 1X**2+1

factor : extension 190

2.

Moon Yong Joon191

Moon Yong Joon192

193

(, : morphism) . map , (function) (morphism) .194

?(, function) f:XY X Y .

195

(, codomain) () .196

/ (, domain) . (, range) "" 197

198

?(injective) (one-to-one) (injection) (one-to-one function)

199

? (, ) (surjective) X Y (onto) . (surjection) .

200

?f:XY f (bijective) . (bijection) (one-to-one correspondence) . one-to-one & onto .

201

() , . . , .202

203

f:XY f -1: Y X.

204

. 205

=

206

function composition (, function composition) . (composite function) .

207

.208

.209

Python mappingdata type

210

mapping Type211

Mapping -dictionary Key/Value containerName 1Name 2container::Dictionary Type

212

Mapping - dict dict

213

Mapping dict type

214

Hash 215

key hash mapping hash

tuple mutable hash 216

dict 217

Map - dict . key/value

218

dict 219

Mapping - Accessing Elements Key/Value Key index

220

Mapping - Updating Elements key key

key : key : 221

Mapping - Delete Elements , , dict instance

Dict

222

dict 223

Mapping dict Dictionary , , dictionary instance Function Descriptioncmp(dict1, dict2)Compares elements of both dict.len(dict)Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.str(dict)Produces a printable string representation of a dictionarytype(dict)Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type.dict(mapping)Converts a map into list.

224

Mapping -dict class dict

225

dict 226

Mapping -dictionary Method Descriptiondict.clear()Removes all elements of dictionarydictdict.copy()Returns a shallow copy of dictionarydictdict.fromkeys()Create a new dictionary with keys from seq and valuessettovalue.dict.get(key, default=None)Forkeykey, returns value or default if key not in dictionarydict.has_key(key)Returnstrueif key in dictionarydict,falseotherwisedict.items()Returns a list ofdict's (key, value) tuple pairsdict.keys()Returns list of dictionary dict's keysdict.setdefault(key, default=None)Similar to get(), but will set dict[key]=default ifkeyis not already in dictdict.update(dict2)Adds dictionarydict2's key-values pairs todictdict.values()Returns list of dictionarydict's values dict.iteritems()Iterable items

227

dict.get() dict KeyError get()

Key default

228

dict.setdefault() dict default

229

dict.get/setdefault() dict [] get

230

dict.update() dict dict

231

iteritems/iterkeys/itervalues dict iterable

232

233

keys,items NotesPython 2Python 3a_dictionary.keys()list(a_dictionary.keys())a_dictionary.items()list(a_dictionary.items())a_dictionary.iterkeys()iter(a_dictionary.keys())[iforiina_dictionary.iterkeys()][iforiina_dictionary.keys()]min(a_dictionary.keys())no change

234

keys, values, items list dict

235

dict.key/values/items() python list dict_keys, values,items list()

236

dict.key/values/items: for python 3. for

237

dict.key: set python 3. set

238

viewitems/keys/values 239

dict.viewitems() dict view 3

240

viewitems/viewkeys() dict viewitems/viewkeys set (3 keys()/items() )

241

dict comprehension 242

Dict Comprehension

A = { for (k,v) in sequence if }

243

Dict Comprehension : dict comprehension

A = { for i in sequence if }244

dict Comprehension : dict for

A = { for i in sequence for j in sequence if }

245

Pythonsympy

Moon Yong Joon246

plotting247

1 1 plot 248

2 2 plot 249

250

1,2 plot 251

plot line_color 252

x 253

1 : x 1 plot (x,-5,5) x 254

Plot 1 2 x 255

Plot 1 2 x x 256

y 257

1 : y 1 plot ylim=(y,-5,5) y 258

259

:legend legend260

1 (title, xlabel, ylabel) 261

show 262

sympy plot . show=False 263

Show 264

show sympy plot . show=False 265

Multi plot 266

plot 1 plot plot 267

plot : 2 plot 268

plot plot 269

Multi plot 270

append plot append plot 271

extend plot extend plot 272

3.

Moon Yong Joon273

Numpy classMoon Yong Joon274

ndarray matrix 275

ndarray matrix matrix MATLAB ndarraymatrix 2 * numpy.multiply() numpy.dot()

276

vector : ndarray Array vector

277

ndarray matrix Matrix dot/* , ndarry */multiply

278

Moon Yong Joon279

280

// number, vector list(row or column), matrix array( rows, columns) vector Matrix

281

vertor ndarray 1xN, Nx1, N 1 ,

scalarvector, , , , ()1N N , ,

282

// //

283

(magnitude) (direction)

tail head

284

285

||v|| = sqrt(v0^2 + v1^2 + v2^2... + vn^2)

b= (6,8) |b| = ( 62+ 82) = ( 36+64) = 100 = 10

286

Vector (Magnitude) x y

287

288

(unit vector) 1

1 (hat)

sclae 289

0 ~ 1

290

291

: +The vector (8,13) and the vector (26,7) add up to the vector (34,20)Example: add the vectorsa= (8,13) andb= (26,7)c=a+bc= (8,13) + (26,7) = (8+26,13+7) = (34,20)

ababc292

Vector : + edfed

293

: -

Example: subtractk= (4,5) fromv= (12,2)a=v+ ka= (12,2) + (4,5) = (12,2) + (4,5) = (124,25) = (8,3)294

Vector : - edg-e-e

295

:

m = [7,3]

A = 3m= [21,9]296

Vector : d3d

297

298

vs Inner product, dot product, scalar productOuter product, vector product, cross product.(Dot)X(cross) n 3 a1 b1 + a2 b2 + . + an bn (a2 b3 a3 b2, a3 b1 a1 b3, a1 b2 a2 b1)|a||b| cos |a||b| sin n scalar vector

299

Moon Yong Joon300

301

(Inner Product) cos

a b= |a| |b| cos()Where:|a| : vectora |b| : vectorb : aandb

a b= ax bx+ ay by

302

: 2 a b= |a| |b| cos()a b= 10 13 cos(59.5)a b= 10 13 0.5075...a b= 65.98... = 66 (rounded)

a b= ax bx+ ay bya b= -6 5 + 8 12a b= -30 + 96a b= 66

303

3 1Dot

a b= ax bx+ ay by+ az bza b= 9 4 + 2 8 + 7 10a b= 36 + 16 + 70a b= 122304

3 2

a |a| = (42+ 82+ 102) = (16 + 64 + 100) = 180b |b| = (92+ 22+ 72) = (81 + 4 + 49) = 134 a b= 9*4+ 2*8+ 7*10 = 36+16+70 = 122

a b= |a| |b| cos()

122 = 180 134 cos()cos() = 122 / (180 134)cos() = 0.7855... = cos-1(0.7855...) = 38.2...305

(dot) (dot)

306

vdot: vector (2) (dot)

307

Vector product() 308

a b a b . c a b

Vector productCross product

309

: 2 cross v = [a1,a2] u = [b1,b2] a1 a2 b1 b2a1*b2 a2*b1Example: The cross product ofa= (2,3) andb= (5,6)c= a1b2 a2b1= 26 35 = 3

Answer:a b=-3

310

: 3 cross v = [a1,a2,a3] u = [b1,b2,b3] a2 a3 a1 a2

b2 b3 b1 b2

x : a2*b3 a3*b2y : a3*b1 a1*b2z : a1*b2 a2*b1

Example: The cross product ofa= (2,3,4) andb= (5,6,7)cx= aybz azby= 37 46 = 3cy= azbx axbz= 45 27 = 6cz= axby aybx= 26 35 = 3Answer:a b=(3,6,3)311

(cross) 2 3

312

Inner/outer 313

inner A = [[a1,b1] B = [[a2,b2]]numpy.inner(A,B) array([[a1*a2 + b1*b2]])[[1*4+0*1]]104110414

=.314

Inner

315

dot/inner: (2) 2

316

outer A = [[a1,b1]] B = [[a2,b2]]numpy.outer(A,B) array([[a1*a2 , a1*b2][ b1*a2, b1*b2]]) [[1*4,1*1] [0*4+0*1]]104110414100

=

317

outer Dot

318

Pythonmatrix class vector Moon Yong Joon319

320

Vector : + edfed

321

Vector : - edg-e-e

322

Vector : d3d

323

324

Vector (Magnitude) x y

325

vector 326

Vector : (dot) (dot)

327

vector 328

Vector : (cross) (cross) 2 array 3 matrix

329

Moon Yong Joon330

331

,

332

Diagonal matrix333

A(aij)(i, j1, 2, 3,, n) aij aij=0(ij) A () aij(ij) aij(ij) 0

334

Identity matrix335

import numpy as np

a = np.array([[1,0],[0,1]])b = np.array([[4,1],[3,2]])print(np.dot(b,a))print(np.dot(a,b))[[4 1] [3 2]][[4 1] [3 2]]336

Triangular matrix337

(Upper triangular matrix) (lower triangular matrix) .

Upper triangular matrixlower triangular matrix338

339

(+/-/*)

+-*/=+-*/+-*/

+-*/+-*/

+-*/+-*/1 2 34 5 61 2 34 5 6+=1+1 2+2 3+34+4 5+5 6+6=2 4 68 10 12340

+/-/*

341

(transpose)342

: . T A .

343

python T numpy transpose

344

dot 345

dot vs inner (2)Dot inner dotinner N*M M*N , N*M N*M N*M . M*N N*NN*M N*M N*N

346

dot 1*p, p*1 A B A B

1*pP111347

dot A B A B

2333348

dot : 2A = [[a1,b1],[c1,d1]] B = [[a2,b2],[c2,d2]] numpy.dot(A,B) array([[a1*a2 + b1*c2, a1*b2 + b1*d2], [c1*a2 + d1*c2, c1*b2 + d1*d2]) [[1*4+ 0*2, 1*1+0*2],[0*4+1*2, 0*1+1*2]]10014122100141224122

=.349

dot Numpy.dot

350

351

(det) , - , -

352

(det) : 2

3 12 2

det= 3*2 1*2= 4

353

(det) : 3

3 1 3 3 12 2 3 2 21 1 1 1 1= 3*2*1 3*2*1 + 1*3*1 3*3*1 + 3*2*1 1*2*1= 6 6 + 3 -9 + 6 -2= 15 17= -2

354

(det) : 3n

355

minor determinant356

2i ,j : Mij 2 11 2M112M12M21-1 -121 1

+

+M22 22 357

3i ,j : Mij

3 1 3 2 2 3 1 1 1 M112*1 -3*1-1M12M132*1 -3*1-12*1 -2*1 02 3 1 1 2 3 1 1 2 2 1 1

= 3M11+(-1)* 1M12 + 3M13 = -3+1+0 = -2

+

+358

359

360

(cofactor)

3 1 3 2 2 3 1 1 1 m112 31 12-3-1+-1m122 31 12-3-1-1m132 21 12-20+0m211 31 11-3-2-2m223 31 13-30+0m233 11 13-12--2m311 32 33-6-3+-3m323 32 39-63--3m333 12 26-24+4

361

(adj)

-1 2 -3 1 0 -3 0 -2 4-1 1 0 2 0 -2-3 -3 4

T 362

(inv) 2

[[ 0.66666667 -0.33333333] [-0.33333333 0.66666667]]

1/3 * 2 -1-1 22 11 2

-1A1=1/det(A) * CT363

(inv) 3

[[ 0.5 -1. 1.5] [-0.5 0. 1.5] [ 0. 1. -2. ]]

- 0.5 * -1 2 -3 1 0 -3 0 -2 43 1 3 2 2 3 1 1 1

-1

A1=1/det(A) * CT364

(inv)

365

Dot 366

Dot A B A B

n*m n*m n*n 367

dot n*m 2

368

dot : 2 a(2,2) b(2,2) n*m, m*n = n*n

369

cross product370

cross A = [[a1,b1],[c1,d1]] B = [[a2,b2],[c2,d2]] numpy.cross(A,B) = A.T * Barray([[a1*b2 - c1*a2 , b1*d2 d1*c2])[[1*1- 0*4,0*2-1*2]]100142121-2=371

Cross n*m 2

372

Inner 373

inner A = [[a1,b1],[c1,d1]] B = [[a2,b2],[c2,d2]] numpy.inner(A,B) array([[a1*a2 + b1*b2, a1*c2 + b1*d2], [c1*a2 + d1*b2, c1*c2 + d1*d2])[[1*4+0*1,1*2+0*2],[0*4+1*1, 0*2+1*2]]10014122100141224212

=.374

Inner : 2 a(2,2) b(2,2) out.shape = a.shape[:-1] + b.shape[:-1]

375

Inner : 3 a(2,3,2) b(2,2) out.shape = a.shape[:-1] + b.shape[:-1]

376

outer product377

outer 1 1 104110414100

=

222*2378

outer: 1 Out out[i,j]=a[i]*b[j]

379

outer: 2 5*5

380

outer: 3

381

tensordot382

tensordotTensordot axes 0 tensor product

383

tensordotTensordot axes 0 tensor product =10014122

10014122412241224122=42120000000042122,22,22,2,2,2384

tensordot: 1 2 2 4

385

tensordot: 2 axes = 0 tensor product, axes = 1 tensor dot product, axes = 2 tenser double contraction

386

387

Trace : 3 3(2,2,2) 1 0213465701

388

trace

389

Python matrix class Moon Yong Joon390

391

n [2] "nn" (,

392

Numpy matrix

393

394

: dot()N*M M* N dot M*M ( xij )( yij )=(kxikykj)

a1a2a3a4

b1b2b3b4

a1*b1+a1*b3a2*b2+a2*b4a3*b1+a3*b3a4*b2+a4*b4

=.395

: dot dot

396

: cross cross

397

: +/-N*M N*M +/- N*M a1a2a3a4

b1b2b3b4

a1 +/- b1a2 +/- b2a3+/- b3a4 +/- b4

=+/-398

: +/- +/-

399

: (k) N*M a1a2a3a4

k* a1k * a2k* a3k* a4

=k400

: k

401

: (transpose)N*M M*N a1a2a3a4

a1 a3a2 a4

=T402

: (transpose)N*M M*N T, transpose

403

matmulMatrix dot

404

Matmul: N*m, M*n 1

405

matrix_power matrix_power dot

406

matrix_power: dot

407

pythonNumpy linalg Moon Yong Joon408

Matrix and vector products409

dot(a,b[,out])n n*m m*l production( n*l) vdot(a,b)Vector produtioninner(a,b)N Inner product ( ).outer(a,b[,out])2 .matmul(a,b[,out]) Matrix product (dot )tensordot(a,b[,axes])Compute tensor dot product along specified axes for arrays >= 1-D.linalg.matrix_power(M,n)Raise a square matrix to the (integer) powern.cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None) einsum(subscripts,*operands[,out,dtype,...])Evaluates the Einstein summation convention on the operands.kron(a,b)Kronecker product of two arrays.

410

Decompositions411

linalg.cholesky(a)Cholesky decomposition.linalg.qr(a[,mode])Compute the qr factorization of a matrix.linalg.svd(a[,full_matrices,compute_uv])Singular Value Decomposition.

412

Matrix eigenvalues413

linalg.eig(a)Compute the eigenvalues and right eigenvectors of a square array.linalg.eigh(a[,UPLO])Return the eigenvalues and eigenvectors of a Hermitian or symmetric matrix.linalg.eigvals(a)Compute the eigenvalues of a general matrix.linalg.eigvalsh(a[,UPLO])Compute the eigenvalues of a Hermitian or real symmetric matrix.linalg.eig(a)Compute the eigenvalues and right eigenvectors of a square array.

414

Norms and other numbers415

linalg.norm(x[,ord,axis,keepdims])Matrix or vector norm.linalg.cond(x[,p])Compute the condition number of a matrix.linalg.det(a)Compute the determinant of an array.linalg.matrix_rank(M[,tol])Return matrix rank of array using SVD method Rank of the array is the number of SVD singular values of the array that are greater thantol.linalg.slogdet(a)Compute the sign and (natural) logarithm of the determinant of an array.trace(a[,offset,axis1,axis2,dtype,out])Return the sum along diagonals of the array.

416

Solving equations and inverting matrices417

linalg.solve(a,b)Solve a linear matrix equation, or system of linear scalar equations.linalg.tensorsolve(a,b[,axes])Solve the tensor equationax=bfor x.linalg.lstsq(a,b[,rcond])Return the least-squares solution to a linear matrix equation.linalg.inv(a)Compute the (multiplicative) inverse of a matrix.linalg.pinv(a[,rcond])Compute the (Moore-Penrose) pseudo-inverse of a matrix.linalg.tensorinv(a[,ind])Compute the inverse of an N-dimensional array.

418

4.

Moon Yong Joon419

Moon Yong Joon420

421

() "a b " , n- . , "a, b, c " .422

a b R A, B A B (binary relation) AxB aA bB (a, b)R , a Rb (a, b)R

(domain) R : dom(R)(range) : ran(R)423

424

: (arrow diagram) A, B A a B b 425

: (coordinate diagram) A, B A a x , B b y 426

: (relation matrix) A, B . 0 1 (boolean matrix) 427

: (directed graph) A A (vertex) , (a, b) a b (edge)

428

429

: 1 1) : R A a A (a,a) R R . ) A = {a, b, c} R (a,a), (b,b), (c,c) .2) : R X . x X (x,x) ! R R .

430

: 2 3) : R A a, b A (a,b) R (b,a) R R . ) A = {a, b, c} R (a,b) (b,a) .431

: 34) : R A a, b A (a,b) R, (b,a) R a b ) A = {a, b, c} R1 = { (a,b), (b,a) } . R2 = { (a,b), (c,a) } .

5) : R A a, b, c A (a,b) R (b,c) R (a,c) R R . ) A = {a, b, c} R (a,b), (b,c) (a,c) R .

432

: 46) : , , . , X ~ , a, b, c : a ~ a: a ~ b => b ~ a: a ~ b, b ~ c => a ~ c .433

Python relation/sequence(tuple) data type434

Sequence : Tuple Type435

Sequence - Tuple tuple immutable Slicing String

Python ExpressionResultsDescriptionT =(1,)(1,) (,) T = (1,2,3,4)(1, 2, 3, 4) len((1, 2, 3))3Length (1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6) Concatenation('Hi!) * 4'Hi!Hi!Hi!Hi!' string 3 in (1, 2, 3)True Membershipfor x in (1, 2, 3): print x,1 2 3 - Iteration

436

Sequence- Tuple tuple Function Descriptioncmp(tuple1, tuple2)Compares elements of both tuples.len(tuple)Gives the total length of the tuple.max(tuple)Returns item from the tuple with max value.min(tuple)Returns item from the tuple with min value.tuple(seq)Converts a list into a tuple.str(tuple)Produces a printable string representation of a tupletype(tuple)Returns the type of the passed variable. If passed variable is tuple, then it would return a tuple type.

437

Tuple Tuple (count), (index)

438

439

Tuple : Tuple tuple copy

list 440

Tuple 441

Tuple mutable tuple immutable mutable . ,

442

5.

Moon Yong Joon443

Moon Yong Joon444

445

(Graph) . G (vertex or node) V (edge or line) E .

V : (vertex) E : (edge) 446

(Graph) : V, E 447

(adjacent) (adjacent) a, b (a,b) , a b (adjacent) 448

449

(Degree) v deg(v) loop 2 450

G n , e 451

452

(Path) : (length):

(closed) : (cycle): 453

454

() (undirected graph)455

, , 456

() G . G .457

Moon Yong Joon458

459

() digraph/ directed graph () . 460

(arc) V V A (V,A) . V (vertex) A (arc) . (a,b) a (initial vertex), b (terminal vertex) .461

, , 462

() H . H .463

464

(complete) 465

3 3, 4 6.466

(subgraph) .467

G1, G2, G3 G .

468

G1, G2, G3 G .G1 26 . , G2, G3 .G3 G2, .

469

(adjacency matrix) G = (V,E), |V| = n(1) .adj_mat[i][j] =1 if (vi, vj) (adjacent)0 470

: 471

472

:

6.

Moon Yong Joon473

Moon Yong Joon474

475

, , ,

476

477

180 A

A sin() b/c cos() a/c tan() b/a478

479

: .

480

: *sin() , *cos()

481

482

A rcosB rsin

r 1 483

3, 4 5

484

()

Moon Yong Joon485

486

xy r P OP

1

487

sqrt(3**2 + 4**2)=5

488

489

x * , Y *

490

Radian & degree

Moon Yong Joon491

degrees/radians 492

degrees radians degrees radians

Degrees Radians

493

degree radian 494

degrees radians 2 360, 1radian 57.3

495

degrees radians: 90, pi/2

496

radians degrees : numpy np.deg2rad, np.rad2deg radians degree

180

497

radians -> degrees : numpy np.degrees radians degree

np.degrees(radian, )

498

degrees-> radians : numpy np.radians degree radian np.radians(radian, )

499

Moon Yong Joon500

cosine501

cosine .

502

cosine .

503

cosine : numpy .

504

cosine .

505

sine506

sine sine radians

507

sine .

508

sine : numpy .

509

sine .

510

tangent511

tan tangent radians

512

tangent tan .

513

tangent : numpy tan .

514

tangent tan .

515

Moon Yong Joon516

517

n*/2 + 90n + 1. n*/2 + 90n + , n 0< < /2 , 0 < cos, cos-> sin, tan-> cot 3. (+,-) 1234 sin++-- cos+--+ tan+-+-

518

519

: n*/2 + 90n + n n=0, , 4n=2, , 2 sin(-x) = -sin(x) sin( -x) = sin(x) cos(-x) = cos(x) cos(-x) = -cos(x) tan(-x) = - tan(x) tan(-x) = -tan(x)

520

: n*/2 + 90n + n

521

: n*/2 + 90n + n n=1, , 1n=3, , 3 sin(/2 -x) = cos(x) sin(3/2 -x) = -cos(x) cos(/2-x) = sin(x) cos(3/2-x) = -sin(x) tan(/2-x) = cot(x) tan(3/2-x) = cot (x)

1522

: n*/2 + 90n + n

523

/2 :524

: /2 A 120 /2(90)+30 sin cos, cos sin

rabA=120B=30

525

: 90 90 , B sin A cos

AB

cos(A) = sin(B), sin(B) = cos(A) , tan(B) = cot(A) A + B = 90 B 526

/2

527

: 180 180 90 +

rabA=120B=30 sin(A) = cos(B) = b/r cos(A) = - sin(B) = - a/r tan(A) = - cot(B) = -b/a528

/2 +

529

530

Sin sin pi 2pi

531

cos cos pi 2pi

532

: - 180 B

rabB=30 sin(-B) = sin(B) = b/r cos(-B) = - cos(B) = - a/r tan(-B) = - tan(B) = -b/a

533

: + A 120 (180)+30 sin sin, cos cos

rabA=210B=30

534

2 535

: 2 - A 360 2(360)-30 sin -sin, cos cos

rabA=360B=30

536

: 2 + 2 .

rabA=360B=30537

Moon Yong Joon538

Sin 539

Sin sin sin

rabtssin(+ ) = sin() cos() + cos() sin() = a/r * b/s + b/r * t/s

sin() cos() = a/r * b/s cos() sin() = b/r * t/s

sin() = a/r , sin() = t/s540

Sin sin

541

Sin 542

Sin sin

Cos 90 0 543

cos /544

cos cos

545

cos cos

546

Moon Yong Joon547

548

(, : reciprocal) (-, : multiplicative inverse) 1, . x 1/x x -1 . 1

549

550

Python

551

* = 1 * = 1

552

Moon Yong Joon553

554

555

y= arcsinxy= sin-1xx= siny1 +1/2 y /2y= arccosxy= cos-1xx= cosy1 +10 y y= arctanxy= tan-1xx= tany/2