Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A matrix of matrices
#1
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
Reply
#2
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
Reply
#3
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.
Reply
#4
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
Reply
#5
Thank you, I will check this out.
Meanwhile, if anyone has any input, it is more than welcome :)
Reply
#6
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
Reply
#7
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 !)
Reply
#8
But for N=2 there two choices.
Which is the right one ?
Reply
#9
It's the first one you wrote.
Thanks for your help :)
Reply
#10
So I  am waiting for your solution code.
How do you code it ? Pure Python or with Numpy.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Estimating transition matrices in python Shreya10o 1 2,106 May-14-2020, 10:41 PM
Last Post: Larz60+
  How to optimize between two matrices, one of the matrices is derived from Excel data niloufar 0 1,656 Nov-05-2019, 06:28 PM
Last Post: niloufar
  Pandas dataframe: sum of exponentially weighted correlation matrices per row vvvcvvcv 1 3,267 May-29-2018, 01:09 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020