numpy.matmul() for Matrix Multiplication | Linear Algebra using Python

Linear Algebra using Python | numpy.matmul() for Matrix Multiplication: Here, we are going to learn about the numpy.matmul() for matrix multiplication in Python?
Submitted by Anuj Singh, on May 22, 2020

Prerequisite: Linear Algebra | Defining a Matrix

Numpy is the library of function that helps to construct or manipulate matrices and vectors. The function numpy.matmul() is a function used for matrix multiplication. The example of matrix multiplication is shown in the figure.

There is a fundamental rule followed by every matrix multiplication, If the matrix A (with dimension MxN) is multiplied by matrix B (with dimensions NxP) then the resultant matrix (AxB or AB) has dimension MxP. In other words, the number of columns in matrix A and the number of rows in matrix B must be equal.

NumPy

Syntax:

    matrix_Multiplication = numpy.matmul(Matrix_1, Matrix_2)

Input parameters: Matrix_1, Matrix_2 the two matrices (following the above-mentioned rule).

Python code to demonstrate example of numpy.matmul() for matrix multiplication

# Linear Algebra Learning Sequence 
# Matrix Multiplication using 
# function in numpy library

import numpy as np

# Defining two matrices
V1 = np.array([[1,2,3],[2,3,5],[3,6,8],[323,623,823]])
V2 = np.array([[965,2413,78],[223,356,500],[312,66,78]])

print("--Matrix A--\n", V1)
print("\n\n--Matrix B--\n", V2)

# using function np.matmul( )
AB = np.matmul(V1, V2)

print("\n\n--Matrix Multiplication AB-- \n", AB)

Output:

--Matrix A--
 [[  1   2   3]
 [  2   3   5]
 [  3   6   8]
 [323 623 823]]


--Matrix B--
 [[ 965 2413   78]
 [ 223  356  500]
 [ 312   66   78]]


--Matrix Multiplication AB-- 
 [[   2347    3323    1312]
 [   4159    6224    2046]
 [   6729    9903    3858]
 [ 707400 1055505  400888]]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.