Skip to content

Latest commit

 

History

History
74 lines (70 loc) · 1.81 KB

File metadata and controls

74 lines (70 loc) · 1.81 KB

[*] Mathematical Operations

[*] Artihmetic

>>> a = np.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> a + 2
array([[3, 4, 5],
       [6, 7, 8]])
>>> a - 2
array([[-1,  0,  1],
       [ 2,  3,  4]])
>>> a * 2
array([[ 2,  4,  6],
       [ 8, 10, 12]])
>>> a/2
array([[0.5, 1. , 1.5],
       [2. , 2.5, 3. ]])
>>> np.sin(a)
array([[ 0.84147098,  0.90929743,  0.14112001],
       [-0.7568025 , -0.95892427, -0.2794155 ]])
>>> a ** 2
array([[ 1,  4,  9],
       [16, 25, 36]])

[*] Non-linear functions

>>> arr = np.array([[1, 2], [3, 4]])
>>> # Raised to power of e
... print(repr(np.exp(arr)))
array([[ 2.71828183,  7.3890561 ],
       [20.08553692, 54.59815003]])
>>> # Raised to power of 2
... print(repr(np.exp2(arr)))
array([[ 2.,  4.],
       [ 8., 16.]])
>>> 
>>> arr2 = np.array([[1, 10], [np.e, np.pi]])
>>> # Natural logarithm
... print(repr(np.log(arr2)))
array([[0.        , 2.30258509],
       [1.        , 1.14472989]])
>>> # Base 10 logarithm
... print(repr(np.log10(arr2)))
array([[0.        , 1.        ],
       [0.43429448, 0.49714987]])

[*] Matrix Multiplication

>>> arr1 = np.array([1, 2, 3])
>>> arr2 = np.array([-3, 0, 10])
>>> print(np.matmul(arr1, arr2))
27
>>> 
>>> arr3 = np.array([[1, 2], [3, 4], [5, 6]])
>>> arr4 = np.array([[-1, 0, 1], [3, 2, -4]])
>>> print(repr(np.matmul(arr3, arr4)))
array([[  5,   4,  -7],
       [  9,   8, -13],
       [ 13,  12, -19]])
>>> print(repr(np.matmul(arr4, arr3)))
array([[  4,   4],
       [-11, -10]])
>>> # This will result in ValueError
... print(repr(np.matmul(arr3, arr3)))
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 3 is different from 2)