SymPy - substitute sybolic entries in a matrix

后端 未结 3 1251
暗喜
暗喜 2021-01-24 00:54

I have a python function which generates a sympy.Matrix with symbolic entries. It works effectively like:

import sympy as sp
M = sp.Matrix([[1,0,2],[0,1,2],[1,2,         


        
3条回答
  •  隐瞒了意图╮
    2021-01-24 01:36

    In the general case, I assume you're looking for an n-by-m matrix of symbolic elements.

    import sympy
    
    def make_symbolic(n, m):
        rows = []
        for i in xrange(n):
            col = []
            for j in xrange(m):
                col.append(sympy.Symbol('a%d%d' % (i,j)))
            rows.append(col)
        return sympy.Matrix(rows)
    

    which could be used in the following way:

    make_symbolic(3, 4)
    

    to give:

    Matrix([
    [a00, a01, a02, a03],
    [a10, a11, a12, a13],
    [a20, a21, a22, a23]])
    

    once you've got that matrix you can substitute in any values required.

提交回复
热议问题