Python Forum
A matrix of matrices - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: A matrix of matrices (/thread-6452.html)

Pages: 1 2


A matrix of matrices - MaximeDt - Nov-23-2017

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


RE: A matrix of matrices - Larz60+ - Nov-23-2017

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/reference/generated/numpy.matrix.html


RE: A matrix of matrices - MaximeDt - Nov-23-2017

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.


RE: A matrix of matrices - Larz60+ - Nov-23-2017

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


RE: A matrix of matrices - MaximeDt - Nov-23-2017

Thank you, I will check this out.
Meanwhile, if anyone has any input, it is more than welcome :)


RE: A matrix of matrices - heiner55 - Nov-23-2017

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



RE: A matrix of matrices - MaximeDt - Nov-24-2017

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 !)


RE: A matrix of matrices - heiner55 - Nov-24-2017

But for N=2 there two choices.
Which is the right one ?


RE: A matrix of matrices - MaximeDt - Nov-24-2017

It's the first one you wrote.
Thanks for your help :)


RE: A matrix of matrices - heiner55 - Nov-25-2017

So I  am waiting for your solution code.
How do you code it ? Pure Python or with Numpy.