[Tutor] matrix insertion routine for MNA

Kent Johnson kent_johnson at skillsoft.com
Sun Oct 24 23:55:15 CEST 2004


You could make the elements of your matrix themselves lists. This would not 
be a numarray matrix, just a Python list of lists - actually a list of 
lists of lists. Like this:
 >>> a=[]
 >>> for i in range(5):
...     col = []
...     for j in range(5):
...             col.append([])
...     a.append(col)
 >>> a
[[[], [], [], [], []], [[], [], [], [], []], [[], [], [], [], []], [[], [], 
[], [], []], [[], [], [], [], []]]

You can think of a as a 5x5 array each of whose elements is a list. You can 
append elements to the list:
 >>> a[1][2]
 >>> a[1][2].append(3)
 >>> a[1][2].append(4)
 >>> a[1][2]
[3, 4]

When you are done appending you can loop through a and replace the lists 
with the sums of their elements
 >>> a[1][2] = sum(a[1][2])
 >>> a[1][2]

If you do that for each list in a, you will end up with a 5x5 array of numbers.


At 12:44 AM 10/20/2004 -0400, CryptoLevel9 at aol.com wrote:
>I originally posted under the subject Values in matrices.  The two 
>responses that I received from that topic were excellent and helped me a 
>lot, but I have run into a problem that I did not anticipate having when I 
>first asked the question.  What I need help with now is how to assign 
>multiple values to the same locations in a pre-constructed matrix and then 
>sum the values at those points.  Unfortunately, there is no way to 
>determine which values will need to be summed before placing them into the 
>matrix without defeating the entire purpose of the program, that is why 
>the values have to be summed after they are placed in the matrix.  My 
>rational behind writing a program that does this is that I am trying to 
>implement the MNA (Modified Nodal Analysis) Algorithm (a technique for 
>"solving" electrical circuits) in a program making the assumptions that 
>the circuit to be solved is planar, that the user already knows the values 
>for every element in the circuit and which nodes the element is connected 
>to.  I have looked through the owners manual for numarray and saw that 
>"The numarray constructor takes a data argument, an optional type, and an 
>optional shape argument. If the data argument is a sequence, then array 
>creates a new object of type numarray, and fills the array with the 
>elements of the data object. The shape of the array is determined by the 
>size and nesting arrangement of the elements of data." This gives me the 
>impression that what I am asking cannot be done by making every point in 
>the matrix a list and then summing the individual lists after the values 
>have been placed in them, which is what I thought of doing first.  I would 
>prefer not making any more variable names than I had listed in my original 
>post in order to keep from confusing myself as too what each internal 
>variable means.  Any help either on how to do what I am asking or how to 
>circumvent the need to sum values after they have placed in placed in the 
>matrix would be appreciated.
>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list