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
Sektion 1. Kapitel 6
single

single

bookMatrix Multiplication

Stryg for at vise menuen

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.

Opgave

Stryg for at begynde at 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 desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 6
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt