Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can it be that `sympy` is much, much slower than Mathematica?

I'm reproducing Mathematica results using Sympy, and I'm new to the latter, so I might be doing things wrong. However, I noticed that some stuff that took a minute at max using Mathematica is just taking forever (read: did not finish after I started it an hour ago) in sympy. That applies both to Simplify(), and solve(). Am I doing something wrong, or is that really the case?

I'll attach my solve() case:

import sympy as sp
from sympy import init_printing
init_printing()

p, r, c, p, y, Lambda = sp.symbols('p r c p y 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*(p-c)
SOfR = etaOfR*y/r*(r-c)
sp.solve(S(p)-SOfR, F)

The corresponding 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) p r + 
    c (lambda p + r) + (c - 
       p) r ProductLog[-E^(((-c lambda p + (c (-1 + lambda) + 
           p) r)/((c - p) r)))])/(lambda (c - p) r)}}
like image 249
FooBar Avatar asked Dec 12 '15 17:12

FooBar


1 Answers

The correct way to do it is:

from sympy import *
init_printing()
p, r, c, p, y, lam, f = symbols('p r c p y 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 notebook here: http://nbviewer.ipython.org/gist/jankoslavic/0ad7d5c2731d425dabb3

The results is equal to the one from Mathematica (see last line) and Sympy performance is comparable.

like image 72
jankos Avatar answered Sep 20 '22 12:09

jankos