hanspi42 / signalflowgrapher

This Python tool allows you to draw signal-flow graphs, calculate transfer functions (SymPy code is generated for further use in Jupyter notebooks), do graph manipulations (e.g., node elimination and graph transposition), and save a graph as TikZ for use in LaTeX documentation.
Artistic License 2.0
32 stars 6 forks source link

Re-order output of "generate mason" #28

Closed hanspi42 closed 4 years ago

hanspi42 commented 4 years ago

Example now:

import sympy as sp

Delta = sp.symbols('Delta')
P1,D1 = sp.symbols('P1,D1')
L1,L2 = sp.symbols('L1,L2')
T_num = sp.symbols('T_num')
T_den = sp.symbols('T_den')
T = sp.symbols('T')
C_2,gm1,C_1,gm4,s,gm2,gm3 = sp.symbols('C_2,gm1,C_1,gm4,s,gm2,gm3')

determinant = [(Delta, -L1 - L2 + 1)]
paths = [(P1, gm2*gm4/(C_1*C_2*s**2)), (D1, 1)]
loops = [(L1, -gm1*gm2/(C_1*C_2*s**2)), (L2, -gm3/(C_2*s))]
numerator = [(T_num, D1*P1)]
denominator = [(T_den, Delta)]
transfer_function = [(T, T_num/T_den)]

T.subs(transfer_function).subs(numerator).subs(denominator).subs(determinant).subs(paths).subs(loops)

This is disadvantageous when users compute several transfer function from the same graph. Better output:

import sympy as sp

Delta = sp.symbols('Delta')
L1,L2 = sp.symbols('L1,L2')
T_num = sp.symbols('T_num')
T_den = sp.symbols('T_den')
C_2,gm1,C_1,gm4,s,gm2,gm3 = sp.symbols('C_2,gm1,C_1,gm4,s,gm2,gm3')

loops = [(L1, -gm1*gm2/(C_1*C_2*s**2)), (L2, -gm3/(C_2*s))]
determinant = [(Delta, -L1 - L2 + 1)]
denominator = [(T_den, Delta)]

P1,D1 = sp.symbols('P1,D1')
paths = [(P1, gm2*gm4/(C_1*C_2*s**2)), (D1, 1)]
numerator = [(T_num, D1*P1)]
T = (T_num/T_den).subs(numerator).subs(denominator).subs(determinant).subs(paths).subs(loops).simplify()
display(T)

This makes it possible to just copy the last fivlines for the subsequent transfer functions, because the lines before will never change.