Frank-Rene Schäfer
Frank-Rene Schäfer

Reputation: 3352

sympy: Vector/Matrix repetition

What is the most elegant way in sympy to construct a matrix from a repeated vector. That is, given a row vector

     V = [ v00, v01, v02 ]

the goal is to find an operation op such that

     M = op(V, N)

delivers a matrix M consisting of N rows which are equal to V, i.e.

    /  v00  v01  v02  \
    |  v00  v01  v02  |
M = |      ...        |
    |                 | 
    \  v00  v01  v02  /

similar to what can be achieved by tile in numpy.

Upvotes: 1

Views: 468

Answers (1)

user6764549
user6764549

Reputation:

I cannot gurantee that this is the most elegant way to do it and probably you are already using something like this but the following works:

import sympy as s

def copyRow(N,V):
    M = V
    for i in range(N):
        M = M.row_insert(1,V)
    return M

v00,v01,v02 = s.symbols('v00,v01,v02')

V = s.Matrix([ [v00, v01, v02 ] ])

M = copyRow(5,V)

Upvotes: 2

Related Questions