Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A matrix of matrices
#15
This matrix represents a square surface of side N². The utltimate goal of my exercise is to approximate the solution of the heat equation on this surface, and for this I am using the finite difference method. This whole part is actually rather easy. A couple of loops in and the job is done. Only thing is that, as I said, I am quite new at Python and there stil are stuff that confuse me (like first element of a matrix starts at 0 and not 1). So I'm getting there but it takes time. Python on top of it seems more rigid than the other languages I used to use.
Maybe in the end I only have syntax issues. I tried to concatenate the matrices. That sort of works but it only gives me a "line of matrices", and not a proper array. I also tried to think of it as a list of lists, which totally make sense, but as I said in my first post, I have no idea how to initialize such array; filling it would be no problem.
So maybe there are some more dedicated functions and tools in numpy that I'm not aware of. Thus my thread.
To answer your question, there is "theoretically" no limits on N. And given my exercise, it's going to be rather large.
Cheers!

And... Actually thanks to your help and to the links you posted, I finally found the solution!

u=-1*ones(Ns-1)
D=4*eye(Ns)+diag(u,1)+ diag(u,-1)
E=-eye(Ns)
N=Ns*Ns
A=array([0]*N*N).reshape(N,N)
for i in range (0,N,Ns):
      A[i:i+Ns,i:i+Ns]=D
      A[i:i+Ns,i+Ns:i+2*Ns]=E
      A[i+Ns:i+2*Ns,i:i+Ns]=E
A[N-Ns:N,N-Ns:N]=D
print('A = ', A)
For Ns=3:
Output:
A = [ [ 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]]
for Ns=4
Output:
A = [ [ 4 -1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0] [-1 4 -1 0 0 -1 0 0 0 0 0 0 0 0 0 0] [ 0 -1 4 -1 0 0 -1 0 0 0 0 0 0 0 0 0] [ 0 0 -1 4 0 0 0 -1 0 0 0 0 0 0 0 0] [-1 0 0 0 4 -1 0 0 -1 0 0 0 0 0 0 0] [ 0 -1 0 0 -1 4 -1 0 0 -1 0 0 0 0 0 0] [ 0 0 -1 0 0 -1 4 -1 0 0 -1 0 0 0 0 0] [ 0 0 0 -1 0 0 -1 4 0 0 0 -1 0 0 0 0] [ 0 0 0 0 -1 0 0 0 4 -1 0 0 -1 0 0 0] [ 0 0 0 0 0 -1 0 0 -1 4 -1 0 0 -1 0 0] [ 0 0 0 0 0 0 -1 0 0 -1 4 -1 0 0 -1 0] [ 0 0 0 0 0 0 0 -1 0 0 -1 4 0 0 0 -1] [ 0 0 0 0 0 0 0 0 -1 0 0 0 4 -1 0 0] [ 0 0 0 0 0 0 0 0 0 -1 0 0 -1 4 -1 0] [ 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1 4 -1] [ 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1 4]]
Thanks a lot!
Reply


Messages In This Thread
A matrix of matrices - by MaximeDt - Nov-23-2017, 03:04 AM
RE: A matrix of matrices - by Larz60+ - Nov-23-2017, 03:12 AM
RE: A matrix of matrices - by MaximeDt - Nov-23-2017, 03:15 AM
RE: A matrix of matrices - by Larz60+ - Nov-23-2017, 03:20 AM
RE: A matrix of matrices - by MaximeDt - Nov-23-2017, 03:31 AM
RE: A matrix of matrices - by heiner55 - Nov-23-2017, 04:44 PM
RE: A matrix of matrices - by MaximeDt - Nov-24-2017, 01:29 AM
RE: A matrix of matrices - by heiner55 - Nov-24-2017, 09:45 AM
RE: A matrix of matrices - by MaximeDt - Nov-24-2017, 04:00 PM
RE: A matrix of matrices - by heiner55 - Nov-25-2017, 06:18 AM
RE: A matrix of matrices - by MaximeDt - Nov-26-2017, 02:02 AM
RE: A matrix of matrices - by heiner55 - Nov-26-2017, 04:31 AM
RE: A matrix of matrices - by MaximeDt - Nov-26-2017, 04:39 AM
RE: A matrix of matrices - by heiner55 - Nov-26-2017, 05:24 AM
RE: A matrix of matrices - by MaximeDt - Nov-26-2017, 08:13 AM
RE: A matrix of matrices - by heiner55 - Nov-26-2017, 09:03 AM
RE: A matrix of matrices - by heiner55 - Nov-26-2017, 12:50 PM
RE: A matrix of matrices - by MaximeDt - Nov-26-2017, 01:12 PM
RE: A matrix of matrices - by heiner55 - Nov-26-2017, 01:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Estimating transition matrices in python Shreya10o 1 2,128 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,681 Nov-05-2019, 06:28 PM
Last Post: niloufar
  Pandas dataframe: sum of exponentially weighted correlation matrices per row vvvcvvcv 1 3,297 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