-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamplenumpyarray12.py
More file actions
105 lines (97 loc) · 2.76 KB
/
examplenumpyarray12.py
File metadata and controls
105 lines (97 loc) · 2.76 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
96
97
98
99
100
101
102
103
104
105
import numpy as np
import sys
def examplescalar():
print("==example {}()===".format(sys._getframe().f_code.co_name))
a=np.array([1,2,3])
c=np.array([5,5,5])
b=5
print("input variable a = {}, b={},c ={}\n".format(a,b,c))
print("scalar mul a * b = {}\n".format(a*b))
print("scalar mul a * c = {}\n".format(a*c))
'''
input variable a = [1 2 3], b=5
scalar mul a * b = [ 5 10 15] ~ 1
scalar mul a * c = [ 5 10 15] ~ 2
fomula 1 = 2 ( same )
'''
def examplebroadcasting():
print("==example {}()===".format(sys._getframe().f_code.co_name))
a=np.arange(3)
b=5
print("input a={}, b={}\n".format(a,b))
print("calculate a*b = {}\n".format(a*b)) #left to right
# calculate a*b = [ 0 5 10]
print("calculate b*a = {}\n".format(b*a))
# calculate b*a = [ 0 5 10]
def examplebroadcastingmatrix():
print("==example {}()===".format(sys._getframe().f_code.co_name))
a=np.ones((3,3))
b=np.arange(3)
print("input variable\n{}\n{}\n".format(a,b))
print("calculate a*b\n{}\n".format(a*b))
print("np.dot(a,b)\n{}\n".format(np.dot(a,b)))
print("calculate b*a\n{}\n".format(b*a))
'''
* input variable
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[0 1 2]
* calculate a*b
[[0. 1. 2.]
[0. 1. 2.]
[0. 1. 2.]]
* np.dot(a,b)
[3. 3. 3.]
* calculate b*a
[[0. 1. 2.]
[0. 1. 2.]
[0. 1. 2.]]
'''
def examplebroadcastingmatrix2():
print("==example {}()===".format(sys._getframe().f_code.co_name))
a = np.array([[0],[1],[2]])
b = np.arange(3)
c = np.arange(2)
print("*input variable\na={},b={},c={}\n".format(a, b,c))
print("*calculate a*b {}{}\n{}\n".format(np.shape(a), np.shape(b), a*b))
print("*calculate b*a {}{}\n{}\n".format(np.shape(b), np.shape(a), b*a))
print("*calculate a*c {}{}\n{}\n".format(np.shape(a), np.shape(c), a*c))
print("*calculate c*a {}{}\n{}\n".format(np.shape(c),np.shape(a),c*a))
'''
*input variable
a=[[0]
[1]
[2]],b=[0 1 2]
*calculate a*b
[[0 0 0]
[0 1 2]
[0 2 4]]
*calculate b*a
[[0 0 0]
[0 1 2]
[0 2 4]]
*calculate a*c
[[0 0]
[0 1]
[0 2]]
*calculate c*a
[[0 0]
[0 1]
[0 2]]
'''
def examplebroadcastingerror():
print("==example {}()===".format(sys._getframe().f_code.co_name))
a = np.ones((3,3))
b = np.arange(2)
print("input variable\na={},b={}\n".format(a,b))
#print("calc a*b\n{}\n".format(a*b)) #ValueError: operands could not be broadcast together with shapes (3,3) (2,)
def main():
print("numpy broadcasting calculate\n")
examplescalar()
examplebroadcasting()
examplebroadcastingmatrix()
examplebroadcastingmatrix2()
examplebroadcastingerror()
if __name__=="__main__":
main()