Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Matrix Multiplication | Section
Mastering Linear Algebra Fundamentals
Seksjon 1. Kapittel 6
single

single

bookMatrix Multiplication

Sveip for å vise menyen

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=Ai0B0j+Ai1B1j+...+Ai n1Bn1 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, ABA * B is not equal to BAB * 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.

Oppgave

Sveip for å begynne å kode

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 6
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt