import numpy as np
import pylab as pl
import sympy as sy

# empty class for variables
class vector:
    pass
#

# frequency, radians and Laplace representation
fqaxst = 1.0e2
fqaxsp = 100.0e6
fqnop  = 1001
fq     = np.logspace(np.log10(fqaxst),np.log10(fqaxsp),fqnop)
wq     = 2*np.pi*fq
s      = 1.0j*wq

ntf1   = np.exp(-s*1.0/10e6)

npnt1  = 1
npnt2  = 1
const1 = 1

### solve #1 with lambdify 
#   set equation, variables and constants to solve
alg_phiout, alg_npnt1, alg_npnt2 = sy.symbols('alg_phiout, alg_npnt1, alg_npnt2')
alg_ntf1, alg_const1             = sy.symbols('alg_ntf1, alg_const1', complex = True)

subst1       = (alg_npnt1, alg_npnt2, alg_ntf1, alg_const1)

sol          = vector()    
sol.algnpnt1 = (alg_npnt1-alg_phiout/alg_const1)*(alg_ntf1+alg_npnt2)-alg_phiout
sol.solnpnt1 = sy.simplify(sy.diff(sy.solve(sol.algnpnt1, alg_phiout)[0], alg_npnt1,1))
sol.refnum1  = sy.lambdify(subst1, sol.solnpnt1)
tf1          = sol.refnum1(npnt1, npnt2, ntf1, const1)

# plot
pl.figure(1)
pl.semilogx(fq,10*np.log10(np.abs(tf1)**2),label='tf1')
pl.legend(loc='lower right')
pl.title('TF')
pl.xlabel('Frequency, Hz')
pl.ylabel('Spectrum, dB')
pl.grid(b='on')
pl.show()

### solve #2 with lambdify using vector as dot-class variables
#   problem with code below, which wasn't a probelm before updating Canopy package manager 2014-July-01
#   I do not want all variables as signle variables but bundled in a vector dot.class

# set equation, variables and constants to solve
alg = vector()

alg.phiout, alg.npnt1, alg.npnt2 = sy.symbols('alg.phiout, alg.npnt1, alg.npnt2')
alg.ntf1, alg.const1             = sy.symbols('alg.ntf1, alg.const1', complex = True)

subst2       = (alg.npnt1, alg.npnt2, alg.ntf1, alg.const1)
    
sol.algnpnt2 = (alg.npnt1-alg.phiout/alg.const1)*(alg.ntf1+alg.npnt2)-alg.phiout
sol.solnpnt2 = sy.simplify(sy.diff(sy.solve(sol.algnpnt2, alg.phiout)[0], alg.npnt1,1))
sol.refnum2  = sy.lambdify(subst2, sol.solnpnt2)

# problem with new version of lambdify in new sympy, old version of lambdify/sympy worked
# Gives the following error code:
tf2          = sol.refnum2(npnt1, npnt2, ntf1, const1)
# tf2        = sol.refnum2(npnt1, npnt2, ntf1, const1)
#   In [10]: %run testofdotclasslambdify.py
#    File "<string>", line 1
#      lambda alg.npnt1,alg.npnt2,alg.ntf1,alg.const1: (alg.const1*(alg.npnt2 + alg.ntf1)/(alg.const1 + alg.npnt2 + alg.ntf1))
#               ^
#   SyntaxError: invalid syntax
# In [11]:

# plot
pl.figure(2)
pl.semilogx(fq,10*np.log10(np.abs(tf1)**2),label='tf1')
pl.legend(loc='lower right')
pl.title('TF')
pl.xlabel('Frequency, Hz')
pl.ylabel('Spectrum, dB')
pl.grid(b='on')
pl.show()
