Problem
Within SymPy, I am trying to create a set of X_i
matrices each of size n_i x p
where i = 1, ... M
(Pinheiro and Bates, 2000, pg. 58). Is this something that can be done? I’d also like to be able to print the set of matrices to see each one… Eventually, every X_i
matrix is going to become part of further operations…
I tried looking at the documentation for Indexed
, but my understanding is that this just gives me a Symbol that I can index over.
Attempted Solutions
For example,
import sympy as sm
sm.init_printing()
M = sm.symbols('M', integer = True) # M groups
i = sm.Idx('i', (1, M))
X = sm.IndexedBase('X')
X[i].ranges
Gives
[(1, M)]
However, this is not a set of matrices and each matrix does not have a variable number of rows (depending on $n_i$).
So then I tried to search further online and asked ChatGPT, then I got the following responses, which seems to say that I can’t set an arbitrary upper limit to the index.
ChatGPT gave this template to work from, although this is by assigning p = 2
and M = 5
; and with a constant number of rows for each Matrix (5).
n = 5
p = 2
M = 5
X=[sm.Matrix(n, p, lambda r, c: f'a_{r+1}{c+1}_{i}') for i in range(1,M+1)]
X
Giving
⎡⎡a₁₁ ₁ a₁₂ ₁⎤ ⎡a₁₁ ₂ a₁₂ ₂⎤ ⎡a₁₁ ₃ a₁₂ ₃⎤ ⎡a₁₁ ₄ a₁₂ ₄⎤ ⎡a₁₁ ₅ a₁₂ ₅
⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢
⎢⎢a₂₁ ₁ a₂₂ ₁⎥ ⎢a₂₁ ₂ a₂₂ ₂⎥ ⎢a₂₁ ₃ a₂₂ ₃⎥ ⎢a₂₁ ₄ a₂₂ ₄⎥ ⎢a₂₁ ₅ a₂₂ ₅
⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢
⎢⎢a₃₁ ₁ a₃₂ ₁⎥, ⎢a₃₁ ₂ a₃₂ ₂⎥, ⎢a₃₁ ₃ a₃₂ ₃⎥, ⎢a₃₁ ₄ a₃₂ ₄⎥, ⎢a₃₁ ₅ a₃₂ ₅
⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢
⎢⎢a₄₁ ₁ a₄₂ ₁⎥ ⎢a₄₁ ₂ a₄₂ ₂⎥ ⎢a₄₁ ₃ a₄₂ ₃⎥ ⎢a₄₁ ₄ a₄₂ ₄⎥ ⎢a₄₁ ₅ a₄₂ ₅
⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢
⎣⎣a₅₁ ₁ a₅₂ ₁⎦ ⎣a₅₁ ₂ a₅₂ ₂⎦ ⎣a₅₁ ₃ a₅₂ ₃⎦ ⎣a₅₁ ₄ a₅₂ ₄⎦ ⎣a₅₁ ₅ a₅₂ ₅
⎤⎤
⎥⎥
⎥⎥
⎥⎥
⎥⎥
⎥⎥
⎥⎥
⎥⎥
⎦⎦
Although further tweaking could probably give a set of M
matrices each with a different number of n
rows, my suspicion is that (1) this will not work with the rest of the SymPy operations (matrix algebra, vector calculus and more); and (2) that this is not idiomatic SymPy given the absence of any invocation of Idx
or IndexedBase
. Any help would be greatly appreciated.
Acceptable, but Less Preferred, Solutions
M
is a defined integer to create a concrete upper limit, likeM = 5
- Each
i
ofn_i
andp
are defined integers, like (n = [10, 10, 12, 12, 24]; p = 5) so that eachX_i
matrix is concretely defined
user26548996 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.