Python Forum

Full Version: A matrix of matrices
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,
I am struggling with great frustration on constructing a matrix.
Here is the whole story: as part of my studies I need to solve some differential partial equations using python. The whole theory is no problem and setting up loops and everything is rather easy. However, I am having a real hard time building this matrix: here is an example of a 3²x3² matrix

4 -1 0 1 0 0 0 0 0
-1 4 -1 0 1 0 0 0 0
0 -1 4 0 0 1 0 0 0
1 0 0 4 -1 0 1 0 0
0 1 0 -1 4 -1 0 1 0
0 0 1 0 -1 4 0 0 1
0 0 0 1 0 0 4 -1 0
0 0 0 0 1 0 -1 4 -1
0 0 0 0 0 1 0 -1 4

So we have a matrix made of blocks:
A1= 4 -1 0
-1 4 -1
0 -1 4
A2 would be the identity and A3 a block of zeros.
(one thing that makes it harder is that the diagonals of -1 are interrupted by zeros).
Finally the biggest problem is that the dimension of each block is N by N (not just 3 by 3) and thus the dimension of the whole matrix will be N² by N².
Maybe I'm being stupid but I can't get my head around it.
Some help would definitely be appreciated...!
I have two main questions:
1:I think it is possible to do that by making an array of arrays, but then, how to initiate the array? and how to implement each array inside?
2: Could a double loop work? Maybe modulo some rectifications afterwards to put the zeros instead?

I would really to make the first option work (it would help me understand more of python); the second option would just be... normal i guess.

Anyways,
Thanks guys!
Max
you should install the numpy package, it will make your life much easier.
Here's the manual on matrices: https://docs.scipy.org/doc/numpy-1.13.0/...atrix.html
I do have numpy and I did check this documentation before. My main problem is to initialize the matrix and fill it up with a loop, which i can't seem to get right.
When I was working, I used a lot of math. Now that I am retired,
my need for it has also retired, so I am not up to date on the use of numpy.

However, I can point you to some example code, see: http://nullege.com/codes/search?cq=numpy.matrix
Thank you, I will check this out.
Meanwhile, if anyone has any input, it is more than welcome :)
Is this right for n=2,3,4... ?

A3 = Matrix 3x3
E3 = Identity 3x3
Z3 = Zero 3x3

Output:
Case N=2: M = A2 E2     E2 A2 or M = A2 Z2     Z2 A2 Case N=3: M = A3 E3 Z3     E3 A3 E3     Z3 E3 A3 Case N=4: M = A4 E4 Z4 Z4     E4 A4 E4 Z4     Z4 E4 A4 E4     Z4 Z4 E4 A4
yes it's exactly like that ! (I should have written it this way in my thread haha, much clearer and each block matrix is easy to set !)
But for N=2 there two choices.
Which is the right one ?
It's the first one you wrote.
Thanks for your help :)
So I  am waiting for your solution code.
How do you code it ? Pure Python or with Numpy.
Pages: 1 2