12

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 )

?

Bill Bell
  • 21,021
  • 5
  • 43
  • 58
hpaantee
  • 147
  • 2
  • 7

2 Answers2

19

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]])
Bill Bell
  • 21,021
  • 5
  • 43
  • 58
-2

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)

blue_note
  • 27,712
  • 9
  • 72
  • 90