Programming

  minte9
LearnRemember



Numpy / Multiplication


Addition

For addition simply use + operator.
 
""" Operations / Addition and Substraction (+ -)
"""

import numpy as np

A = np.array([
    [1, 1],
    [2, 2],
])

B = np.array([
    [1, 1],
    [3, 3],
])

C = np.add(A, B) # first method
D = A + B        # second method

assert (C == D).all()

print("A =\n", A)
print("B =\n", B)
print("A + B =\n", C)

"""
    A =
     [[1 1]
      [2 2]]
    B =
     [[1 1]
      [3 3]]
    A + B = 
     [[2 2]
      [5 5]]
"""

Multiplication

For element-wise multiplication we use *
 
""" Operation / Matrix multiplication
For element-wise multiplication we use *
"""

import numpy as np

A = np.array([
    [1, 1],
    [2, 2],
])

B = np.array([
    [1, 1],
    [3, 3],
])

C = np.dot(A, B)    # first method
D = A @ B           # second method
E = A * B           # element-wise multiplication

assert (C == D).all()
assert (E[1, 1] == 6)     

print("A =\n", A)
print("B =\n", B)
print("A @ A =\n", A @ A)
print("A * A =\n", A * A)

"""
    A =
     [[1 1]
      [2 2]]
    B =
     [[1 1]
      [3 3]]
    A @ A =
     [[3 3]
      [6 6]]
    A * A =
     [[1 1]
      [4 4]]
"""





References