I have written code which creates matrices with name matrixi, where i is replaced with the current loop number:
for i in range(len(node2)):
if sOrP[i] == 'S':
#print('series connection')
matrixTemplate = numpy.array([[1.0, 0.0], [0.0, 1.0]]) #Got to put 1.0 else it doesnt work
matrixTemplate[0][1] = frequenciesList[0][i]
globals()['matrix%s' % i] = matrixTemplate
#print(matrixTemplate)
elif sOrP[i] == 'P':
#print('parallel connection')
matrixTemplate = numpy.array([[1.0, 0.0], [0.0, 1.0]])
matrixTemplate[1][0] = 1 / frequenciesList[0][i]
globals()['matrix%s' % i] = matrixTemplate
#print(matrixTemplate)
I then need to multiply the created matrices together:
Ty = matrix0 @ matrix1 @ matrix2 @ matrix3 @ matrix4 @ matrix5 @ matrix6 @ matrix7 @ matrix8 @ matrix9
This works but the code has to be able to take multiple inputs with potentially more or fewer matrices being created. In that case it wouldn't work.
Would it be possible to do the multiplication part using a loop or function?
You could use a list (or a dictionary) to hold your matrices:
matrices = []
for i in range(len(node2)):
if (sOrP[i] == 'S'):
#print('series connection')
matrixTemplate = numpy.array([[1.0, 0.0],[0.0, 1.0]]) #Got to put 1.0 else it doesnt work
matrixTemplate[0][1] = frequenciesList[0][i]
matrices.append(matrixTemplate)
#print(matrixTemplate)
elif (sOrP[i] == 'P'):
#print('parallel connection')
matrixTemplate = numpy.array([[1.0, 0.0],[0.0, 1.0]])
matrixTemplate[1][0] = 1/frequenciesList[0][i]
matrices.append(matrixTemplate)
#print(matrixTemplate)
And then use reduce and numpy.matmul to compute your total matrix product:
from functools import reduce
Ty = reduce(numpy.matmul, matrices)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With