Python matrix multiplication with variables

I know about the ability of python to do matrix multiplications. Unfortunately I don't know how to do this abstractly? So not with definite numbers but with variables.

Example:

M = ( 1   0 ) * ( 1   d )
    ( a   c )   ( 0   1 )

Is there some way to define a,c and d, so that the matrix multiplication gives me

( 1   d       )
( a   a*d + c )

?

Python matrix multiplication with variables

Bill Bell

20.4k5 gold badges42 silver badges57 bronze badges

asked Oct 20, 2017 at 12:33

1

Using sympy you can do this:

>>> from sympy import *
>>> var('a c d A B')
(a, c, d, A, B)
>>> A = Matrix([[1, 0], [a, c]])
>>> A
Matrix([
[1, 0],
[a, c]])
>>> B = Matrix([[1, d], [0, 1]])
>>> B
Matrix([
[1, d],
[0, 1]])
>>> M = A.multiply(B)
>>> M
Matrix([
[1,       d],
[a, a*d + c]])

answered Oct 20, 2017 at 15:05

Python matrix multiplication with variables

Bill BellBill Bell

20.4k5 gold badges42 silver badges57 bronze badges

Just like any variable, an array/matrix can only be initialized with specific values. The only thing you can do is make functions to make initialization easier

import numpy as np

def helper(a, c, d):
    A = np.array([[1, 0], [a, c]])
    B = np.array([[1, d], [0, 1]])
    return A @ B

(where the @ operator is explicit matrix multiplication operator)

answered Oct 20, 2017 at 12:44

Python matrix multiplication with variables

blue_noteblue_note

26k6 gold badges60 silver badges82 bronze badges

2

In Python, we can implement a matrix as nested list (list inside a list).

We can treat each element as a row of the matrix.

For example X = [[1, 2], [4, 5], [3, 6]] would represent a 3x2 matrix.

The first row can be selected as X[0]. And, the element in first row, first column can be selected as X[0][0].

Multiplication of two matrices X and Y is defined only if the number of columns in X is equal to the number of rows Y.

If X is a n x m matrix and Y is a m x l matrix then, XY is defined and has the dimension n x l (but YX is not defined). Here are a couple of ways to implement matrix multiplication in Python.

Source Code: Matrix Multiplication using Nested Loop

# Program to multiply two matrices using nested loops

# 3x3 matrix
X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
    [6,7,3,0],
    [4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
         [0,0,0,0],
         [0,0,0,0]]

# iterate through rows of X
for i in range(len(X)):
   # iterate through columns of Y
   for j in range(len(Y[0])):
       # iterate through rows of Y
       for k in range(len(Y)):
           result[i][j] += X[i][k] * Y[k][j]

for r in result:
   print(r)

Output

[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]

In this program, we have used nested for loops to iterate through each row and each column. We accumulate the sum of products in the result.

This technique is simple but computationally expensive as we increase the order of the matrix.

For larger matrix operations we recommend optimized software packages like NumPy which is several (in the order of 1000) times faster than the above code.

Source Code: Matrix Multiplication Using Nested List Comprehension

# Program to multiply two matrices using list comprehension

# 3x3 matrix
X = [[12,7,3],
    [4 ,5,6],
    [7 ,8,9]]

# 3x4 matrix
Y = [[5,8,1,2],
    [6,7,3,0],
    [4,5,9,1]]

# result is 3x4
result = [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*Y)] for X_row in X]

for r in result:
   print(r)

The output of this program is the same as above. To understand the above code we must first know about built-in function zip() and unpacking argument list using * operator.

We have used nested list comprehension to iterate through each element in the matrix. The code looks complicated and unreadable at first. But once you get the hang of list comprehensions, you will probably not go back to nested loops.

How do you do matrix multiplication in Python?

@ Python's Matrix Multiplication Operator.
A = np. matrix('3 1; 8 2') A. ... .
B = np. matrix('6 1; 7 9') B. ... .
A @ B. matrix([[25, 12], [62, 26]]).
# element at the top left. ... .
# element at the top right. ... .
# element at the bottom left. ... .
# element at the top right. ... .
A @ B == result..

How do you multiply a 3X3 matrix in Python?

Multiplication can be done using nested loops. Following program has two matrices x and y each with 3 rows and 3 columns. The resultant z matrix will also have 3X3 structure. Element of each row of first matrix is multiplied by corresponding element in column of second matrix.

How do you multiply a variable in Python?

In python, to multiply number, we will use the asterisk character ” * ” to multiply number. After writing the above code (how to multiply numbers in Python), Ones you will print “ number ” then the output will appear as a “ The product is: 60 ”.

How do you multiply a 2d matrix in Python?

Step1: input two matrix. Step 2: nested for loops to iterate through each row and each column. Step 3: take one resultant matrix which is initially contains all 0. Then we multiply each row elements of first matrix with each elements of second matrix, then add all multiplied value.