From that I run the following code :
import sympy as sp
H=-(-CB*Ve*s*(-CA*s - 1/RA)**2/(RB*(CB*s + 1/RB)*(CA*s - (-CA*s - 1/RA)**2/(CA*s - CB**2*s**2/(CB*s + 1/RB) + CB*s + 1/RA) + 2/RA)*(CA*s - CB**2*s**2/(CB*s + 1/RB) + CB*s + 1/RA)) - CB*Ve*s/(RB*(CB*s + 1/RB)))/(Ve*(CA*s - CB**2*s**2/(CB*s + 1/RB) + CB*s + 1/RA))
display(H1)H1.simplify()display(H1)
H2 = H.simplify()display(H2)H3=H2.subs({RB:1e3,CA:1e-6})H3.simplify()display(H3)
And I obtain what follows. I am surprized that inserting values instead of some symbols disables sympy simplification. In some more complex cases, the computation times required for simplification are huge and lead to non simplified expressions.
Any idea why ?
Best regards,
Mike
--
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 sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/5622efd3-9718-4d8b-b5cb-373235ab7d42o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.
Perhaps my example was not clear enough. I start with a fully symbolic expression. Then, I try two things :- ask a simplify on the fully symbolic expression, that works well,
- replace some symbols in the initial expression and then simplify.
The first step shows that a simplified expression exists, and replacing by values may not change this fact (it can lead to an even more simplified expresssion isn'nt it ?).
I do not understand why Sympy cannot simplify at all the initial expression when symbols are replaced by values, whereas we know that a simplified expresssion exists as Sympy itself found it from the fully symbolic version.
My guess would be that due to rounding errors, when the floating
point numbers get combined - added or subtracted etc. those
rounding errors make what would be exact matches become inexact.
For example A-B+B might be slightly different from A. Put another
way, floating point numbers don't (quite) obey the laws of
arithmetic!
I would definitely advise that you work with symbols and integers until you are ready to substitute floating point numbers.
I can't quite see why you would want to substitute and then simplify.
David
On Fri, 31 Jul 2020 at 17:26, Mikhael Myara
<mikhae...@umontpellier.fr> wrote:
>
> Thanks for your answer.
>
> I don’t do it « for sport » ;-) My example is a reduced example coming from a practical situation I encountered.
I understand that you have reduced this but it is a lot better if the
reduction is self-contained so that others can literally copy-paste
the code to test what is happening with an expression that
demonstrates the issue. I could probably tell you exactly what the
problem is in a given example if you provided minimal code for that
example.
import sympy as sp
H=-(-CB*Ve*s*(-CA*s - 1/RA)**2/(RB*(CB*s + 1/RB)*(CA*s - (-CA*s - 1/RA)**2/(CA*s - CB**2*s**2/(CB*s + 1/RB) + CB*s + 1/RA) + 2/RA)*(CA*s - CB**2*s**2/(CB*s + 1/RB) + CB*s + 1/RA)) - CB*Ve*s/(RB*(CB*s + 1/RB)))/(Ve*(CA*s - CB**2*s**2/(CB*s + 1/RB) + CB*s + 1/RA))H1=H.subs({RB:1e3,CA:1e-6})
display(H1)H1.simplify()display(H1)H2 = H.simplify()display(H2)H3=H2.subs({RB:1e3,CA:1e-6})
H3=H3.simplify()display(H3)
> Here it is :
> I developed a small software that solves the voltages and currents of an electronic circuit described by means of a standard format (« netlist »). This file is parsed, equations are solved. In this netlist file, the values of the components are given.
How exactly are they given in the file (e.g. to how many digits)?
If the file has something like 0.12 then you can read that in directly
as Rational('0.12') rather than converting the string to a float. Then
you will have an object that represents the value from the file
exactly with no rounding error. Alternatively you can use nsimplify to
convert the floats to an approximate rational representation (direct
string to Rational is better though).
> I imagined that I could mix symbolic and numerical values in the description of the circuit (for example we want to study de variations of a single component, not of all), and it seems to be a bad idea because mixed expressions seem to be really difficult to handle for Sympy.
Generally speaking simplification will work better with numbers rather
than symbols. However this is only true if you give numbers in exact
form. When you pass a float to sympy it will be treated as an
inherently imprecise object. As a result many simplifications will be
refused and also any arithmetic will be computed in floating point.
The kind of simplification needed here is most likely factorisation
and cancellation of polynomials which is poorly conditioned in
floating point.
> So I will have to make 100% symbolic treatment and then only replace the values of the components. It was to explore the possibilities for my ? students : this kind of question will help me giving to them good orientations during classroom work.
If the class work involves using sympy then good orientation would be
to avoid the use of floats in symbolic computation.
--
Oscar
> To unsubscribe from this group and stop receiving emails from it, send an email to sy...@googlegroups.com.