I have recently been given quite a complex piece of work to do by my computer science supervisor. The idea is that the function:- [def shape_resistivity(shape,R):] allows the user to input a shape and resistance value like so:
[shape=[\ ' ', '++++', 'xxxx', 'xxxx', 'xxxx', '----', ' ']
'R' is an arbitrary value in ohms.
Here, the given shape is a pictorial representation of an 'I' shaped circuit where the positive terminals are at the top, each 'x' represents a resistor cell and are given a resistance value 'R', and the negative cells are at the bottom.
What i am trying to do is take the list of strings, which must be of equal length, and use a nested for loop to go over each character and its neighbours for creation of the relevant equations.
So far, I have constructed the following code which initially indexes the list of strings so i can then recall them to place them into the relevant resistor and node equations.
def index_chars(shape): coords= neg_list= node_eqn= rownum=0 for row in shape: rownum+=1 charnum=0 for char in row: if char==' ': rownum=0 charnum=0 continue else: charnum+=1 if char=='+': char='pos term' rownum=0 charnum=0 coords.append((char,rownum,charnum)) if char=='x': char='RC' coords.append((char,rownum,charnum)) if char=='-': char='RC' neg_list.append((char,rownum-1,charnum)) return coords,neg_list
I have also tried to use numpy to manipulate arrays in a similar fashion:
from numpy import * def create_array(shape,R): import numpy coords= row_length=0 for row in shape: row_length+=1 char_length=0 for char in row: char_length+=1 if char==' ': char=0 else: if char=='+': char=1 if char=='x': char=R if char=='-': char=1 coords.append(char) coordinates=numpy.array(coords) coordinates.resize([row_length,char_length]) return coordinates def index_array(shape,R): import numpy coordinates=create_array(shape,R) index_coord=ndenumerate(coordinates) for position,value in index_coord: print position,value
I believe, by using a nested for loop, that i can somehow go through the shape/list of strings and somehow look at each 'x' and look at its neighbours. In pseudo code "Look at x, append x to a list and append the x above it, below it and left and right of it if there should be x's in these positions. This equals one node equation." etc.
Despite being able to index the shape, I cannot think how to write the condition statements necessary to create the above equations.
I hope this makes sense.