Den onsdagen den 21:e maj 2014 kl. 13:28:54 UTC+2 skrev Dima Pasechnik:
>
> On 2014-05-21, st...@joa.me.uk <javascript:> <st...@joa.me.uk<javascript:>> 
> wrote: 
> > Hi all, 
> > 
> > I use sage to solve a system of equations describing and electronical 
> > filter. I then use sympy and codegen to generate c code that I use in my 
> > main code written in c. This works almost like a charm, expept that it 
> > produces files that are 100s of kb long. Looking into the expressions 
> there 
> > seems to be a lot of possiblities to reduce this but I've found no way 
> of 
> > doing this automatically and by hand, well... any ideas anyone? I 
> include a 
> > small sage script that do what I want it to do, almost... 
>
> your script did not make it to this post, for some reason... 
> I imagine you can produce these expressions in Sage readable format, 
> right? (extracting them from C code does not look like fun...) 
>
> Are you asking how to simplify them? 
>
>
Hi,

so the SAGE core of the problem is small

reset
from sympy.utilities.codegen import codegen
import os
#Define currents to be solved for
itot=var('itot')
icable1=var('icable1')
iplu=var('iplu')
icable2=var('icable2')
i1=var('i1')
i2=var('i2')
i3=var('i3')
i4=var('i4')
i5=var('i5')
i6=var('i6')
i7=var('i7')
i8=var('i8')
i9=var('i9')
#Define input "constants"
Uut=var('Uut')
Rut=var('Rut',domain='real')
Cplunger=var('Cplunger',domain='real')
R1=var('R1',domain='real')
R2=var('R2',domain='real')
C=var('C',domain='real')
Ccable1=var('Ccable1',domain='real')
Ccable2=var('Ccable2',domain='real')
Ccable3=var('Ccable3',domain='real')
Rin=var('Rin',domain='real')
Cin=var('Cin',domain='real')
w=var('w',domain='real')
#equations to solve
eq1 = itot==icable1+iplu
eq2 = iplu==icable2+i1
eq3 = i1==i2+i3
eq4 = i3==i4+i5
eq5 = i5==i6+i7
eq6 = i7==i8+i9
#+5e3/(1+1j*w*5e-4)
#+5e3/(1+1j*w*5e-4)
#+5e3/(1+1j*w*5e-4)
eq7 = Uut==itot*(Rut)+icable1/(1j*Ccable1*w)
eq8 = Uut==itot*(Rut)+iplu/(1j*w*Cplunger)+i2*R1
eq9 = Uut==itot*(Rut)+iplu/(1j*w*Cplunger)+icable2/(1j*Ccable2*w)
eq10 = i2*R1==i3*R2+i4/(1j*C*w)
eq11 = i4/(1j*C*w)==i8*Rin
eq12 = i8*Rin==i9/(1j*Cin*w)
eq13 = i6/(1j*Ccable3*w)==i4/(1j*C*w)
#Solve equations
S=solve([eq1,eq2,eq3,eq4,eq5,eq6,eq7,eq8,eq9,eq10,eq11,eq12,eq13],itot,icable1,iplu,icable2,i1,i2,i3,i4,i5,i6,i7,i8,i9,solution_dict=True)


and from there I get

i8real=(S[0][i8].subs({Uut:1})).real()

and

i8imag=(S[0][i8].subs({Uut:1})).imag()


These are horrible expression that I have not been able to simplify nor do 
some intelligent substituions... I'm hoping that there is some good way to 
break out subexpressions and replace them with a variable, or simplify (if 
possible..)

cheers

Joa

 

> > 
> > 
> > cheers 
> > 
> > 
> > Joa 
> > 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to