These calculations can be made much faster in SymPy. These are only
2x2 matrices so I would just take the basic formula for the inverse of
a 2x2 matrix and sub into that:

In [20]: Matrix([[x, y], [z, t]]).inv()
Out[20]:
⎡    t         -y    ⎤
⎢─────────  ─────────⎥
⎢t⋅x - y⋅z  t⋅x - y⋅z⎥
⎢                    ⎥
⎢   -z          x    ⎥
⎢─────────  ─────────⎥
⎣t⋅x - y⋅z  t⋅x - y⋅z⎦


The denominator can be factored out of the calculation. I expect that
you can make something work in SymPy here in a way that is much easier
than trying to use C/autowrap.

Yes, SymPy should be faster at handling this. I tried your code and I
found that it hung in equals. I think that's a bug that is already
reported:
https://github.com/sympy/sympy/issues/14675

Here's your code (including the necessary symbol definitions - please
include those!):

from sympy import *

rb, rc, rg, rs, rd, w, cb, cc, lg, ld, ls = symbols('rb, rc, rg, rs,
rd, w, cb, cc, lg, ld, ls')

Yint = Matrix([[2/rb + I*w*2*cb, -1/rb - I*w*cb], [-1/rb - I*w*cb,
1/rb + 1/rc +I*w*(cb + cc)]])
Zint = Yint.inv()
Zext = Matrix([[rg + rs + I*w*(lg + ls), rs + I*w*ls],[rs + I*w*ls, rd
+ rs + I*w*(ld + ls)]])
Z = Zext + Zint
Y = Z.inv()
Yre = sympy.re(Y)
Yim = sympy.im(Y)
err = (Yre_me - Yre)**2 + (Yim_me - Yim)**2

--
Oscar

On Sat, 7 Dec 2019 at 20:12, Matt Braunstein <[email protected]> wrote:
>
> The problem is related to multiple inverse and add operations. This is 
> related to representing a simple linear FET model. Below are the operations 
> that are being performed. I'm having to decompose these to generate equations 
> for use in optimization. There are additional matrix operations beyond these 
> I would like to do as well, but even just the below is too much.
>
> Yint = sympy.matrix([[2/rb + I*w*2*cb, -1/rb - I*w*cb], [-1/rb - I*w*cb, 1/rb 
> + 1/rc +I*w*(cb + cc)]])
> Zint = Yint.inv()
> Zext = sympy.matrix([[rg + rs + I*w*(lg + ls), rs + I*w*ls],[rs + I*w*ls, rd 
> + rs + I*w*(ld + ls)]])
> Z = Zext + Zint
> Y = Z.inv()
> Yre = sympy.re(Y)
> Yim = sympy.im(Y)
> err = (Yre_me - Yre)**2 + (Yim_me - Yim)**2
>
> Actually, when manually running these equations in the command line interface 
> for symengine, it completed even quicker than I expected. It took on the 
> order of a couple of seconds to complete. When doing the same completely in 
> python with Sympy, it took on the order of minutes to complete. This was the 
> reason I thought about using autowrap to generate the code in C++. Maybe I'm 
> just trying to fit a square peg in a round hole and need to keep looking for 
> the right tool for the job.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/b081b222-e4f7-40d4-ae7e-ed0e6bacd65b%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAHVvXxT%2BNZR4gs0jDO_gM95exQnO1Z55Qz_g37QuLEWhbubsuw%40mail.gmail.com.

Reply via email to