Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Matrix Multiplication | Section
Mastering Linear Algebra Fundamentals
SectionΒ 1. ChapterΒ 6
single

single

bookMatrix Multiplication

Swipe to show menu

Matrix multiplication is a core operation in linear algebra and is widely used in fields like data science, physics, and engineering. To multiply two matrices, you must first ensure that their dimensions are compatible. Specifically, if you have a matrix AA with dimensions mΓ—nm \times n (m rows and n columns) and a matrix BB with dimensions nΓ—pn \times p (n rows and p columns), you can multiply them because the number of columns in AA matches the number of rows in BB. The resulting matrix CC will have dimensions mΓ—pm \times p.

The element in the i-th row and j-th column of the product matrix is calculated by taking the dot product of the i-th row of matrix AA and the j-th column of matrix BB. This means you multiply corresponding elements and sum the products. Mathematically, for each element CijC_{ij}, you compute:

Cij=Ai0βˆ—B0j+Ai1βˆ—B1j+...+AiΒ nβˆ’1βˆ—Bnβˆ’1Β jC_{ij} = A_{i0} * B_{0j} + A_{i1} * B_{1j} + ... + A_{i\ n-1} * B_{n-1\ j}

This operation is not commutative: in general, Aβˆ—BA * B is not equal to Bβˆ—AB * A. Matrix multiplication is used to represent linear transformations, combine data, and solve systems of equations, among many other applications.

123456789101112131415
import numpy as np A = np.array([ [1, 2, 3], [4, 5, 6] ]) B = np.array([ [7, 8], [9, 10], [11, 12] ]) product = A @ B print(product)
copy

You can simplify matrix multiplication in Python using the numpy library and the @ operator. The @ operator performs matrix multiplication directly between two numpy arrays. When you use this operator, numpy automatically checks that the dimensions are compatible and performs the computation efficiently behind the scenes. This approach eliminates the need for explicit nested loops, making your code much simpler and easier to read. For example, if A and B are numpy arrays of compatible shapes, A @ B gives you the matrix product. This is the recommended way to perform matrix multiplication in modern Python code when working with numerical data.

Task

Swipe to start coding

Multiply two matrices using numpy arrays and the @ operator, without defining any functions.

  • Create numpy array A: [2434]\begin{bmatrix}2&4\\3&4\end{bmatrix}
  • Create numpy array B: [1213]\begin{bmatrix}1&2\\1&3\end{bmatrix}
  • Compute the matrix product using the @ operator and store in variable C.
  • Print the resulting matrix.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt