Solving set of linear equations in plaintext using sympy

84 views
Skip to first unread message

Bjorn

unread,
Nov 9, 2016, 5:18:56 AM11/9/16
to sympy
Hi, I am a sympy newbie, and I would like to use sympy to solve equation systems. I don't expect these systems to be huge or complicated. 

I would like to be able to process equations in plaintext such as this:

sample_volume=20
final_concentration
=0.55
ethanol_concentration
=0.96

final_volume
= ethanol_vol+sample_volume
final_concentration
*final_volume=ethanol_vol*ethanol_concentration

I have hacked together a simple sympy script that solves the above system. 
I have a couple of questions regarding this:

- has this been done before? I would like to use tested code if possible.
- is it possible to incorporate units in the calculations? This issue suggests that it is not.
- are there any immediately apparent gotchas the code above? I would like to have a system for solving equations not much more complex than the example.

Grateful for input. I asked the same question on stackoverflow but I got no response.

Thanks,
Björn

Björn Dahlgren

unread,
Nov 13, 2016, 3:25:17 AM11/13/16
to sympy

On Wednesday, 9 November 2016 11:18:56 UTC+1, Bjorn wrote:
Hi, I am a sympy newbie, and I would like to use sympy to solve equation systems. I don't expect these systems to be huge or complicated. 
/.../

- has this been done before? I would like to use tested code if possible.

Hi, I haven't seen exactly this approach, it looks fine, however I would recommend using the parse function if you will be parsing user-provided strings.
 
- is it possible to incorporate units in the calculations? This issue suggests that it is not.

I typically do dedimensionalisation at the interface (removing units before manipulating the expressions, and reattaching them before return).
Ideally SymPy would do this for the user, but if you need to use e.g. numerical non-linear solvers in e.g. SciPy you would still need to do this.
 
- are there any immediately apparent gotchas the code above? I would like to have a system for solving equations not much more complex than the example.

It really depends on what systems you will be solving. As long as they are linear SymPy will provide you with solutions (but expressions may grow
unmanageably large quite quickly as the system grows if done symbolically). SymPy is also able to solve many non-linear equations.
What are your overall goal here? (ethanol density is not linear with respect to mass fraction water to start with..)
 
Best regards,
Björn Dahlgren

Shekhar Prasad Rajak

unread,
Dec 15, 2016, 1:07:04 AM12/15/16
to sympy
I think you want to solve for final_volume, ethanol_vol

 In [2]: sample_volume=20
       ...: final_concentration=0.55
       ...: ethanol_concentration=0.96
       ...: 
    
    In [4]: fv,ev = symbols('fv,ev')
    
    In [5]: expr1 = fv - ev+sample_volume
    
    In [6]: expr2 = final_concentration*fv - ev*ethanol_concentration 
    
    In [7]: solve([expr1,expr2], [fv,ev])
    Out[7]: {ev: -26.8292682926829, fv: -46.8292682926829}
    
    # I will recommend you to use solveset's linear system solver (linsolve)
    In [8]: linsolve([expr1,expr2], [fv,ev])
    Out[8]: {(-46.8292682926829, -26.8292682926829)}


  --
Shekhar

Bjorn

unread,
Dec 16, 2016, 9:39:59 AM12/16/16
to sympy
Hi and thanks for the reply,

Your solution seems correct except for the values being negatives.
how is linsolve different from solve? I would also like to have the equation simplified so that
the unknown is expressed in terms of the knowns like:

ethanol_vol = final_concentration*sample_volume/(ethanol_concentration-final_concentration)
see first cell in this sympy notebook.

/bjorn  

Shekhar Prasad Rajak

unread,
Dec 18, 2016, 4:56:06 AM12/18/16
to sympy
Oh ! I put the 1st eq wrong. It is this :

In [15]: fv,ev = symbols('fv,ev')

In [17]: expr1 = -fv + ev+sample_volume


In [18]: expr2 = final_concentration*fv - ev*ethanol_concentration


In [19]: solve([expr1,expr2], [fv,ev])

Out[19]: {ev: 26.8292682926829, fv: 46.8292682926829}


In [20]: linsolve([expr1,expr2], [fv,ev])

Out[20]: {(46.8292682926829, 26.8292682926829)}


linsolve is newly implemented linear system solver for solveset.  You can use invert_real to get particular symbol at lhs side.

--
Shekhar
Reply all
Reply to author
Forward
0 new messages