tensor multiplication
different ranks
tensor algebra
mathematical operations
linear algebra

Multiply Tensors with different ranks

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Multiplying tensors with different ranks is not one single operation. It can mean element-wise multiplication with broadcasting, an outer product, or a contracted product such as matrix multiplication or tensordot. The right answer depends on which axes should align and what shape you want as the result.

Start by Deciding What “Multiply” Means

Two tensors can interact in several valid ways. The most common interpretations are:

  • element-wise multiplication with broadcasting
  • outer product, which increases rank
  • contraction over one or more axes, which reduces rank

If you skip this definition and ask only whether ranks are different, you still do not know which operation should happen.

For example, a vector of shape (3,) and a matrix of shape (2, 3) can be combined in at least three meaningful ways depending on the intended math.

Element-Wise Multiplication Uses Broadcasting

If the shapes are compatible under broadcasting rules, frameworks such as NumPy and TensorFlow can multiply element by element even when the ranks differ.

python
1import numpy as np
2
3matrix = np.array([[1, 2, 3],
4                   [4, 5, 6]])
5vector = np.array([10, 20, 30])
6
7result = matrix * vector
8print(result)

Output:

python
[[ 10  40  90]
 [ 40 100 180]]

Here the vector of shape (3,) is broadcast across the rows of the matrix of shape (2, 3). The ranks differ, but the trailing dimensions are compatible.

Outer Product Creates a Higher-Rank Tensor

If you want every element of one tensor multiplied by every element of another, you want an outer product. This increases rank rather than trying to match existing axes.

python
1import numpy as np
2
3a = np.array([1, 2, 3])
4b = np.array([4, 5])
5
6result = np.outer(a, b)
7print(result)

Output:

python
[[ 4  5]
 [ 8 10]
 [12 15]]

Conceptually, this turns a rank-1 tensor and another rank-1 tensor into a rank-2 result. Generalized outer products can be expressed with einsum or by reshaping and broadcasting manually.

Contraction Multiplies and Sums Over Chosen Axes

Sometimes the intended operation is not element-wise at all. Matrix multiplication is really a contraction over one axis. The same idea extends to higher-rank tensors.

With NumPy, tensordot is often the clearest API:

python
1import numpy as np
2
3x = np.arange(12).reshape(2, 2, 3)
4y = np.arange(6).reshape(3, 2)
5
6result = np.tensordot(x, y, axes=([2], [0]))
7print(result.shape)
8print(result)

This contracts the last axis of x with the first axis of y. The result shape is formed from the remaining axes.

That is why “different ranks” by itself is not a problem. What matters is whether the chosen contraction axes are compatible.

einsum Makes the Axis Logic Explicit

When broadcasting and contraction rules start to feel opaque, einsum can clarify the intent by naming axes directly.

python
1import numpy as np
2
3matrix = np.array([[1, 2, 3],
4                   [4, 5, 6]])
5vector = np.array([10, 20, 30])
6
7result = np.einsum('ij,j->ij', matrix, vector)
8print(result)

This example performs the same broadcasted scaling as earlier, but with an explicit axis description. einsum is especially useful for higher-rank tensor code where silent broadcasting would be hard to read.

TensorFlow Uses the Same Core Ideas

TensorFlow follows similar patterns. Element-wise multiplication relies on broadcasting, while contraction can use tf.tensordot or tf.einsum.

python
1import tensorflow as tf
2
3x = tf.constant([[1.0, 2.0, 3.0],
4                 [4.0, 5.0, 6.0]])
5y = tf.constant([10.0, 20.0, 30.0])
6
7print(tf.multiply(x, y))

If the shapes are not broadcast-compatible, TensorFlow raises a shape error instead of guessing what you meant.

Reshaping Is Often Part of the Solution

When the intended multiplication is clear but the raw shapes do not line up, reshape one tensor so the operation matches the intended axis structure.

For example, if you want a column vector instead of a row-wise broadcast, reshape explicitly:

python
1import numpy as np
2
3matrix = np.array([[1, 2, 3],
4                   [4, 5, 6]])
5vector = np.array([10, 20])
6
7result = matrix * vector[:, None]
8print(result)

That [:, None] turns the vector from shape (2,) into (2, 1), which changes how broadcasting works.

Common Pitfalls

The most common mistake is asking for “multiplication” without specifying whether the goal is element-wise multiplication, an outer product, or a contraction.

Another mistake is relying on broadcasting without checking which axes are being expanded. The code may run and still compute the wrong math.

Developers also overlook reshaping. A small explicit reshape often makes the intended operation both correct and readable.

Summary

  • Tensors with different ranks can still be multiplied, but the operation must be defined clearly.
  • Broadcasting handles element-wise multiplication when shapes are compatible.
  • Outer products increase rank, while contractions such as tensordot reduce selected axes.
  • 'einsum is often the clearest way to express complex tensor multiplication.'
  • When shapes do not line up, reshape deliberately instead of hoping broadcasting guesses your intent.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.