-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumpy_multiply.py
134 lines (82 loc) · 2.84 KB
/
numpy_multiply.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
"""Numpy Multiply.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1lsLoIXWSvIwmPMO0BfZJVRxtPUrQIGXl
# **Numpy Multiply**
"""
import numpy as np
"""**1-D Array**"""
a_1 = np.array([1,2])
b_1 = np.array([3,4])
"""**2-D Array**"""
a=np.array([[1,2],
[3,4]])
b=np.array([[11,12],
[13,14]])
"""**3D Array**"""
a_3 = np.random.rand(8,13,13)
b_3 = np.random.rand(8,13,13)
"""**np.dot**
sum(a[i, :]* b[:, j])]
* If both a and b are 1-D arrays, it is inner product of vectors
* If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.
* If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
"""
np.dot(a_1,b_1)
np.dot(a,b)
"""**np.inner**
sum(a[i, :]* b[j, :])]
Ordinary inner product of vectors for 1-D arrays (without complex conjugation), in higher dimensions a sum product over the last axes.
"""
# 1-D arrays dot is inner product of vectors
np.inner(a_1,b_1), np.dot(a_1,b_1)
np.einsum('i,i->', a_1, b_1)
# 2-D array
np.inner(a,b)
# np.dot(a,b) = np.inner(a,b.T)
np.allclose(np.dot(a,b),np.inner(a,b.T))
# np.dot(a,b.T) = np.inner(a,b)
np.allclose(np.dot(a,b.T),np.inner(a,b))
"""**np.malmul or a @ b**
matrix Multiplication
matmul differs from dot in two important ways:
1. Multiplication by scalars is not allowed, use * instead.
2. Stacks of matrices are broadcast together as if the matrices were elements, respecting the signature (n,k),(k,m)->(n,m)
"""
# If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.
np.matmul(a,b)
np.einsum('ij,jk->ik', a, b)
# np.malmul = a @ b
np.allclose(np.matmul(a,b), a@b)
"""**np.multiply or a * b**
element-wise multiplication
"""
# If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
print("multiply:")
print(np.multiply(a,1))
print("-------")
print("dot:")
print(np.dot(a,1))
# 1-D Array element-wise multiplication
np.multiply(a_1,b_1)
np.einsum('i,i->i', a_1, b_1)
# 2-D Array element-wise multiplication
np.multiply(a,b)
np.einsum('ij,ij->ij', a, b)
# np.multiply(a,b) = a*b
np.allclose(np.multiply(a,b), a*b)
"""**np.outer**
Compute the outer product of two vectors.
Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product [1] is:
[[a_0*b_0 a_0*b_1 ... a_0*b_N ]
[a_1*b_0 .
[ ... .
[a_M*b_0 ... a_M*b_N ]]
"""
np.outer(a_1,b_1)
np.einsum('i,j->ij', a_1.ravel(), b_1.ravel())
"""**3-D Array**"""
np.allclose(np.einsum('ijk,ijk->ijk', a_3,b_3), a_3*b_3) # True
np.allclose(np.einsum('ijk,ikl->ijl', a_3,b_3), a_3@b_3) # True
np.allclose(np.einsum('ijk,lkm->ijlm',a_3,b_3), a_3.dot(b_3)) # True