- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Compute the tensor dot product for arrays with different dimensions with double contraction in Python

Given two tensors, a and b, and an array_like object containing two array_like objects, (a_axes, b_axes), sum the products of a’s and b’s elements (components) over the axes specified by a_axes and b_axes. The third argument can be a single non-negative integer_like scalar, N; if it is such, then the last N dimensions of a and the first N dimensions of b are summed over.

To compute the tensor dot product for arrays with different dimensions, use the numpy.tensordot() method in Python. The a, b parameters are Tensors to “dot”. The axes parameter, integer_like If an int N, sum over the last N axes of a and the first N axes of b in order. The sizes of the corresponding axes must match. The axes = 2 is for tensor double contraction.

## Steps

At first, import the required libraries −

import numpy as np

Creating two numpy arrays with different dimensions using the array() method −

arr1 = np.array(range(1, 9)) arr1.shape = (2, 2, 2) arr2 = np.array(('p', 'q', 'r', 's'), dtype=object) arr2.shape = (2, 2)

Display the arrays −

print("Array1...\n",arr1) print("\nArray2...\n",arr2)

Check the Dimensions of both the arrays −

print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim)

Check the Shape of both the arrays −

print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape)

To compute the tensor dot product for arrays with different dimensions, use the numpy.tensordot() method −

print("\nTensor dot product...\n", np.tensordot(arr1, arr2, axes = 2))

## Example

import numpy as np # Creating two numpy arrays with different dimensions using the array() method arr1 = np.array(range(1, 9)) arr1.shape = (2, 2, 2) arr2 = np.array(('p', 'q', 'r', 's'), dtype=object) arr2.shape = (2, 2) # Display the arrays print("Array1...\n",arr1) print("\nArray2...\n",arr2) # Check the Dimensions of both the arrays print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim) # Check the Shape of both the arrays print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape) # To compute the tensor dot product for arrays with different dimensions, use the numpy.tensordot() method in Python print("\nTensor dot product...\n", np.tensordot(arr1, arr2, axes = 2))

## Output

Array1... [[[1 2] [3 4]] [[5 6] [7 8]]] Array2... [['p' 'q'] ['r' 's']] Dimensions of Array1... 3 Dimensions of Array2... 2 Shape of Array1... (2, 2, 2) Shape of Array2... (2, 2) Tensor dot product... ['pqqrrrssss' 'pppppqqqqqqrrrrrrrssssssss']

- Related Articles
- Compute the tensor dot product for arrays with different dimensions in Python
- Compute the tensor dot product for arrays with different dimensions with array-like axes in Python
- Compute the tensor dot product for arrays with different dimensions over specific axes in Python
- Compute the tensor dot product in Python
- Get the Kronecker product of two arrays with different dimensions in Python
- Return the cross product of two (arrays of) vectors with different dimensions in Python
- Tensor contraction with Einstein summation convention in Python
- Get the Kronecker product of arrays with 4D and 3D dimensions in Python
- Find Maximum dot product of two arrays with insertion of 0's in C++
- Return the inner product of two masked arrays with different shapes in Numpy
- Return the outer product of two masked arrays with different shapes in Numpy
- Return the dot product of two masked arrays in Numpy
- Multiplying two matrices in JavaScript with different dimensions
- Convert inputs to arrays with at least two dimensions in Numpy
- Convert inputs to arrays with at least three dimensions in Numpy