-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamplenumpyarray11.py
More file actions
95 lines (86 loc) · 2.31 KB
/
examplenumpyarray11.py
File metadata and controls
95 lines (86 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
def exampleCalculate():
arrayvariable=np.array([[1,3,5],[2,4,6]])
print("input variable a = \n{}\n".format(arrayvariable))
print("plus element a + a\n{}".format(arrayvariable + arrayvariable))
'''
plus element a + a
[[ 2 6 10]
[ 4 8 12]]
'''
print("minus element a - a\n{}\n".format(arrayvariable-arrayvariable))
'''
minus element a - a
[[0 0 0]
[0 0 0]]
'''
print("mul element a * a\n{}\n".format(arrayvariable*arrayvariable))
'''
mul element a * a
[[ 1 9 25]
[ 4 16 36]]
'''
print("div element a / a\n{}\n".format(arrayvariable/arrayvariable))
'''
div element a / a
[[1. 1. 1.]
[1. 1. 1.]]
'''
def exampleMulMatrix():
print("each Matrix Calculate\n")
a = np.array([1,5,10])
b = np.array([2,7,9])
c = np.array([1,3])
print("input array a, b\na={}, b={}".format(a,b))
print("mul a*b = {}\n".format(a*b))
print("np.dot(a,b)= {}\n".format(np.dot(a,b)))
'''
input array a, b
a=[ 1 5 10], b=[2 7 9]
mul a*b = [ 2 35 90]
np.dot(a,b)= 127
'''
print("input array a, c\na={}, c={}".format(a,c))
#print("mul a*b = {}\n".format(a*c)) ValueError: operands could not be broadcast together with shapes (3,) (2,)
#print("np.dot(a,b)= {}\n".format(np.dot(a,c))) ValueError: shapes (3,) and (2,) not aligned: 3 (dim 0) != 2 (dim 0)
def exampleMultiDim():
a = np.array([[2,3],[4,5]])
b = np.array([[6,7],[8,9]])
print("input array a\na={}\nb={}\n".format(a,b))
print("matrix mul axb\n{}\n".format(np.dot(a,b)))
print("axb={}\n".format(a*b))
'''
input array a
a=[[2 3]
[4 5]]
b=[[6 7]
[8 9]]
matrix mul axb
[[36 41]
[64 73]]
axb=[[12 21]
[32 45]]
'''
def exampleMultiDim2():
a = np.array([[2,3],[4,5]])
b = np.array([6,7])
print("np.dot(a,b)\n{}\n".format(np.dot(a,b)))
print("np.dot(b,a)\n{}\n".format(np.dot(b,a)))
'''
np.dot(a,b)
[2,3]
* [6,7] = [33 59]
[4,5]
np.dot(b,a)
[2,3]
[6, 7] * = [40 53] = [2*6+7*4, 3*6+7*5]
[4,5]
'''
def main():
print("example11 linear algebra\nElement-Wise\n")
exampleCalculate()
exampleMulMatrix()
exampleMultiDim()
exampleMultiDim2()
if __name__=="__main__":
main()