numpy - dot product of a vector of matrices with a vector of scalars

  • Last Update :
  • Techknowledgy :

Returns the dot product of a and b. If a and b are both scalars or both 1-D arrays then a scalar is returned; otherwise an array is returned. If out is given, then it is returned.,If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.,If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:,If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b: dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

dot(a, b)[i, j, k, m] = sum(a[i, j,: ] * b[k,: , m])
>>> np.dot(3, 4)
12
>>> np.dot([2 j, 3 j], [2 j, 3 j])
   (-13 + 0 j)
>>> a = [
      [1, 0],
      [0, 1]
   ] >>>
   b = [
      [4, 1],
      [2, 2]
   ] >>>
   np.dot(a, b)
array([
   [4, 1],
   [2, 2]
])
>>> a = np.arange(3 * 4 * 5 * 6).reshape((3, 4, 5, 6)) >>>
   b = np.arange(3 * 4 * 5 * 6)[::-1].reshape((5, 4, 6, 3)) >>>
   np.dot(a, b)[2, 3, 2, 1, 2, 2]
499128
   >>>
   sum(a[2, 3, 2,: ] * b[1, 2,: , 2])
499128

Suggestion : 2

If your NumPy is new enough (1.6 or better), you could use numpy.einsum:

result = np.einsum('ijk,i -> jk', data, vector)

In[36]: data = np.array([
   [
      [1, 1, 1, 1],
      [2, 2, 2, 2],
      [3, 3, 3, 3]
   ],
   [
      [3, 3, 3, 3],
      [4, 4, 4, 4],
      [5, 5, 5, 5]
   ]
])

In[37]: vector = np.array([10, 20])

In[38]: np.einsum('ijk,i -> jk', data, vector)
Out[38]:
   array([
      [70, 70, 70, 70],
      [100, 100, 100, 100],
      [130, 130, 130, 130]
   ])

Or, without np.einsum, you could add extra axes to vector and take advantage of broadcasting to perform the multiplication:

In[64]: (data * vector[: , None, None]).sum(axis = 0)
Out[64]:
   array([
      [70, 70, 70, 70],
      [100, 100, 100, 100],
      [130, 130, 130, 130]
   ])

It should be:

np.einsum('i, ijk -> ijk', vector, data)

Suggestion : 3

How to Use Numpy Dot to Calculate the Python Dot Product,Use @ To Calculate the Python Dot Product,Now that we understand what the dot product between a 1 dimensional vector an a scalar looks like, let’s see how we can use Python and numpy to calculate the dot product:,In the next section, you’ll learn how to use the Python @ operator to calculate the dot product of numpy arrays.

Let’s take a look at what the function looks like:

import numpy as np
dot = np.dot(x, y)

Now that we understand what the dot product between a 1 dimensional vector an a scalar looks like, let’s see how we can use Python and numpy to calculate the dot product:

# Calculate the Dot Product in Python Between a 1 D Vector and a Scalar
import numpy as np
x = 2
y = np.array([1, 2, 3])

dot = np.dot(x, y)
print(dot)

# Returns: [2 4 6]

Let’s see how we can calculate the dot product of two one-dimensional vectors using numpy in Python:

# Calculate the Dot Product in Python Between two 1 - dimensional vectors
import numpy as np
x = np.array([2, 4, 6])
y = np.array([3, 5, 7])

dot = np.dot(x, y)
print(dot)

# Returns: 68

Let’s see how we can replicate our example of calculating the dot product between a scalar and a 1-dimensional array using the @ operator:

# Calculate the Dot Product in Python Between two 1 - dimensional vectors
import numpy as np
x = np.array([2, 4, 6])
y = np.array([3, 5, 7])

dot = x @ y
print(dot)

# Returns: 68

Now let’s take a look at how we can use the @ operator to calculate the dot product between two 2-dimenionsla arrays.

# Calculate the Dot Product in Python Between two 2 - dimensional vectors using @
import numpy as np
x = np.array([
   [1, 2, 3],
   [4, 5, 6]
])
y = np.array([
   [4, 5, 6],
   [7, 8, 9]
])

dot = x @ y.T
print(dot)

# Returns:
   #[[32 50] #[77 122]]

Suggestion : 4

This function returns the dot product of two arrays. For 2-D vectors, it is the equivalent to matrix multiplication. For 1-D arrays, it is the inner product of the vectors. For N-dimensional arrays, it is a sum product over the last axis of a and the second-last axis of b.,Note that the dot product is calculated as −,We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy. Agree Learn more ,NumPy - Mathematical Functions

This function returns the dot product of two arrays. For 2-D vectors, it is the equivalent to matrix multiplication. For 1-D arrays, it is the inner product of the vectors. For N-dimensional arrays, it is a sum product over the last axis of a and the second-last axis of b.

import numpy.matlib
import numpy as np

a = np.array([
   [1, 2],
   [3, 4]
])
b = np.array([
   [11, 12],
   [13, 14]
])
np.dot(a, b)

It will produce the following output −

[
   [37 40]
   [85 92]
]

Note that the dot product is calculated as −

[
   [1 * 11 + 2 * 13, 1 * 12 + 2 * 14],
   [3 * 11 + 4 * 13, 3 * 12 + 4 * 14]
]

Suggestion : 5

In this article, we’ll learn about the numpy dot() method to find the dot products. It covers scalars. vectors, arrays, and matrices. It also involves real analysis and complex number applications, graph visualizations, and more. The real contribution of this subject is in the Data Science and Artificial Intelligence fields. ,But, in the dot() function of the Numpy array, there is no place for the angle Θ. So, we just need to give two matrices or arrays as parameters. Thus, we shall implement this in a code:,For multidimensional arrays create arrays using the array() method of numpy. Then following the same above procedure call the dot() product. Then print it on the screen.,Then call our dot_product() function for taking the dot product and give those two arrays as parameters inside it.

[
   [1, 2, -3],
   [2, 3, 4],
   [4, -1, 1]
]
import numpy as np

list_1 = [23, 12, 3, 11]
print('Original list: ', list_1)

arr = nparray(list_1)
print('Numpy array: ', arr)

print('Data type of list_1', type(list_1))
print('Data type of arr', type(arr))

# Output
# Original list: [23, 12, 3, 11]
# Numpy array: array([23, 12, 3, 11])
# <class: list>
   # numpy.ndarray
import numpy as np

matrix = np.array([
   [2, 4, 5],
   [-1, -4, 8],
   [3, -1, 9]
])
print('Our matrix is: ', matrix)

# output:
   # Our matrix is:
   # array([
      [2, 4, 5],
      #[-1, -4, 8],
      #[3, -1, 9]
   ])
#
import numpy as np
a = np.array([
   [2, 3, 4],
   [-1, 3, 2],
   [9, 4, 8]
])
b = np.array([
   [4, -1, 2],
   [34, 9, 1],
   [2, 0, 9]
])

addition = a + b
subtraction = a - b
multiplication = a * b
division = a / b

print('Addition of arrays  a and b is: ', addition)
print('Subtraction of arrays  a and b is: ', subtraction)
print('Multiplication of arrays  a and b is: ', multiplication)
print('Division of arrays  a and b is: ', division)
a.b = | a | . | b | .cosΘ # general equation of the dot product
for two vectors
import numpy as np

var_1, var_2 = 34, 45 #
for scalar values
dot_product_1 = np.dot(var_1, var_2)
dot_product_1

#
for matrices
a = np.array([
   [2, 3, 4],
   [-1, 3, 2],
   [9, 4, 8]
])
b = np.array([
   [4, -1, 2],
   [34, 9, 1],
   [2, 0, 9]
])

dot_product_2 = np.dot(a, b)
dot_product_2

Suggestion : 6

In this example, we take two scalars and calculate their dot product using numpy.dot() function. Dot product using numpy.dot() with two scalars as arguments return multiplication of the two scalars.,In this example, we take two two-dimensional numpy arrays and calculate their dot product. Dot product of two 2-D arrays returns matrix multiplication of the two input arrays.,In this example, we take two numpy one-dimensional arrays and calculate their dot product using numpy.dot() function. We already know that, if input arguments to dot() method are one-dimensional, then the output would be inner product of these two vectors (since these are 1D arrays).,In this tutorial of Python Examples, we learned how to calculate the dot product of numpy arrays, with the help of well detailed example programs.

The syntax of numpy.dot() function is

numpy.dot(a, b, out = None)

Python Program

import numpy as np

a = 3
b = 4
output = np.dot(a, b)
print(output)

Output

12

Dot Product

output = [2, 1, 5, 4].[3, 4, 7, 8] = 2 * 3 + 1 * 4 + 5 * 7 + 4 * 8 = 77

Suggestion : 7

Last Updated : 18 Nov, 2021,GATE CS 2021 Syllabus

1._
dot(a, b)[i, j, k, m] = sum(a[i, j,: ] * b[k,: , m])

Output:

Dot Product of scalar values: 20
Dot Product: (-7 + 22 j)