Could it be that sympy is much, much slower than Mathematica?

I am reproducing Mathematica results using Sympy and I am new to the latter, so I might be wrong. However, I noticed that some of the things that took a minute to max using Mathematica were just forever (read: didn't finish after I started it an hour ago) in sympy. This applies to both Simplify() and solve() . Am I doing something wrong, or is it really so?

I will attach my solve() case:

 import sympy as sp from sympy import init_printing init_printing() p, r, c, p, y, Lambda = sp.symbols('prcpy Lambda') F = sp.Symbol('F') eta1 = lambda p: 1/(1-sp.exp(-Lambda) * sp.exp(-Lambda)*(sp.exp(Lambda) - 1 - Lambda)) eta2 = lambda p: 1/(1-sp.exp(-Lambda)) * sp.exp(-Lambda)/(1-F) * (sp.exp(Lambda*(1- F)) - 1 - Lambda*(1-F)) eta = lambda p: 1 - eta1(p) + eta2(p) etaOfR = sp.limit(eta(p), F, 1) S = lambda p: eta(p)*y/p*(pc) SOfR = etaOfR*y/r*(rc) sp.solve(S(p)-SOfR, F) 

Relevant Mathematica Code:

 ClearAll[r, p, lambda, a, A, c, eta, f, y, constant1, constant2, eta, \ etaOfR] constant1[lambda_] := Exp[-lambda]/(1 - Exp[-lambda]); constant2[lambda_] := constant1[lambda]*(Exp[lambda] - 1 - lambda); eta[lambda_, f_] := 1 - constant2[lambda] + constant1[lambda]*(Exp[lambda*(1 - f)] - 1 - lambda*(1 - f)) ; etaOfR[lambda_] := Limit[eta[lambda, f], f -> 1]; expression1[lambda_, f_] := y/p (p - c) eta[lambda, f] == y/r (r - c) etaOfR[lambda]; Solve[expression1[lambda, f], f] // FullSimplify 

Output:

 {{f -> (-(1 + lambda) pr + c (lambda p + r) + (c - p) r ProductLog[-E^(((-c lambda p + (c (-1 + lambda) + p) r)/((c - p) r)))])/(lambda (c - p) r)}} 
+5
source share
1 answer

The correct way to do this is:

 from sympy import * init_printing() p, r, c, p, y, lam, f = symbols('prcpy lambda f') constant1 = exp(-lam) / (1 - exp(-lam)) constant2 = constant1 * (exp(lam) - 1 - lam) eta = 1 - constant2 + constant1 * (exp(lam * (1-f)) - 1 - lam * (1 - f)) etaOfR = limit(eta, f, 1) expression1 = Eq(y / p * (p - c) * eta, y / r * (r - c) * etaOfR) solve(expression1, f) 

You can also check the laptop here: http://nbviewer.ipython.org/gist/jankoslavic/0ad7d5c2731d425dabb3

The results are the same as those in Mathematica (see the last line), and Sympy's performance is comparable.

+5
source

Source: https://habr.com/ru/post/1237988/


All Articles