Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106179

Re: sympy

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Newsgroups comp.lang.python
Subject Re: sympy
Date Thu, 31 Mar 2016 19:59:31 +0100
Lines 93
Message-ID <mailman.276.1459450793.28225.python-list@python.org> (permalink)
References <733f5f0d-9b4e-4023-897b-e1f2730c39cb@googlegroups.com> <56fbcd01$0$1599$c3e8da3$5496439d@news.astraweb.com> <99b7cf43-50ff-4de7-8de0-e324658682bf@googlegroups.com> <56fbf7e7$0$1591$c3e8da3$5496439d@news.astraweb.com> <244e1277-e105-4419-a449-7f2012c6d78e@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de x3qU9SsGcsfHmJpKVzMo1QWoBR9ms76QgH2iKZNT0JMw==
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(python': 0.05; 'binary': 0.05; 'cache': 0.05; 'float': 0.05; 'automate': 0.07; 'expressions': 0.07; 'repeated': 0.07; 'rewrite': 0.07; 'seemed': 0.07; 'cc:addr:python-list': 0.09; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; '__future__': 0.09; 'below).': 0.09; 'occurrences': 0.09; 'type)': 0.09; 'types:': 0.09; 'python': 0.10; 'def': 0.13; 'argument': 0.15; '2016': 0.16; '[4]:': 0.16; 'calculates': 0.16; 'cc:name:python list': 0.16; 'evaluates': 0.16; 'evaluating': 0.16; 'fpu': 0.16; 'inputs': 0.16; 'measuring': 0.16; 'module:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'substitutes': 0.16; 'substituting': 0.16; 'x).': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'pfxlen:0': 0.18; 'tree': 0.18; 'creates': 0.18; 'input': 0.18; '>>>': 0.20; 'math': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'do.': 0.22; '64-bit': 0.22; 'amounts': 0.22; 'symbolic': 0.22; 'trying': 0.22; 'seems': 0.23; 'second': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'example': 0.26; '(which': 0.26; 'message- id:@mail.gmail.com': 0.27; 'mathematical': 0.27; 'function': 0.28; 'actual': 0.28; 'calculated': 0.29; 'decimal': 0.29; 'division': 0.29; 'way?': 0.29; "i'm": 0.30; 'that.': 0.30; 'code': 0.30; 'operations': 0.31; 'generally': 0.32; 'point': 0.33; 'surprised': 0.33; 'received:google.com': 0.35; 'done': 0.35; 'library.': 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'basic': 0.36; 'evaluation': 0.36; 'faster': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'doing': 0.38; 'version': 0.38; 'received:209': 0.38; 'does': 0.39; 'takes': 0.39; 'rather': 0.39; 'skip:x 10': 0.40; 'your': 0.60; 'here.': 0.62; 'more': 0.63; 'different': 0.63; 'times': 0.63; 'march': 0.64; 'answer.': 0.72; 'capabilities': 0.72; 'evaluate': 0.72; 'increase': 0.73; '(your': 0.84; 'calculations': 0.84; 'oscar': 0.84; 'walking': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=fiqc6VJE/4ljysQKxGAYIDyROvPQx6oNWMYu1aCixEk=; b=Tioi6iergjS3MjMj1E7QDMAnw1hglgRrVRmvul0hBDJ/v/swBWhaR+DCQefy2e7ebm nXv7BvprpV4ur76F3hVjSOhw20T/cHXkpomwnvnMTtIOZNck2bDMGvSxKO9irrvDFxZq Z7goVHGL/uPwpPikgD2OuLD3NbxfAKaCHTImQSyXoARc7zgunpX6L3hx5wgqUQhfjCjV dakiXFucolzIIRKfFq1jViOmD+gXhTMHRRi1G7dYpx9L8pT/CEpSJnarRVDDyMeBclLb AdxMhQv33WmLPAUjXjCZiYN3R/VtPVkVk6ZcGfR9BbeQtL6k88mGLZhRDhwalmK92cz8 cZVg==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=fiqc6VJE/4ljysQKxGAYIDyROvPQx6oNWMYu1aCixEk=; b=Wp9KnXre0h/i0Sv6r+TYQ29nDsa75m3sqSkNV8HJDovXIrfTVfjA1B1UNgR1ZRIz0v Usk5q1Wj4Lw3p6e65SS0mYIhmT9gMJDWy18drdWIL2XXMwET6oB+bwbRWK1iR2tSh6Ep ybHm9m1/aEv3Gs91dkv11RI6wxkilY2rbzbACc1qJ0Pac7XtgOjeJ/+tfa6G3LB7OBmo jQ3nAQpwDJhGPe7ws2ZbxNe/bjWjNFVBylhY9Abxy5PpubPyyomOCZiuJika34jH4sEH 0XHVgHXTdJGmdcIwHGNoX+Y9er0slyjfmMXK0Mjgc9WHo1T9kMKtmsiBBh4J204nQBR9 kslQ==
X-Gm-Message-State AD7BkJLCYryRKc8gi0TI2Ki83lVk1oyEUu/lxvwQsP1aqP7K6XMdbS82J8fM3LjsmfB0TytG+45lo0OcVzVdNg==
X-Received by 10.112.135.39 with SMTP id pp7mr167759lbb.43.1459450791369; Thu, 31 Mar 2016 11:59:51 -0700 (PDT)
In-Reply-To <244e1277-e105-4419-a449-7f2012c6d78e@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:106179

Show key headers only | View raw


On 31 March 2016 at 11:57, Poul Riis <priisdk@gmail.com> wrote:
>
> ... However, the sympy way seems to be about 70 times slower than using the derivative calculated 'by hand' (try the example below).
> Can it be done in a more efficient way?
>
> Poul Riis
>
>
>
> from sympy import *
> from time import *
> x=Symbol('x')
> ftext=diff(sin(x),x)
>
> def fmsympy(t):
>    return ftext.evalf(subs={x:t})
>
> def fm(t):
>     return cos(t)

I think you have misunderstood what is going on here. Let's actually
try those two methods out:

$ isympy
IPython console for SymPy 0.7.5 (Python 2.7.9-64-bit) (ground types: gmpy)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)

Documentation can be found at http://www.sympy.org

In [1]: ftext = diff(sin(x), x)

In [2]: ftext
Out[2]: cos(x)

In [3]: ftext.evalf(subs={x:1})
Out[3]: 0.540302305868140

In [4]: cos(1)
Out[4]: cos(1)

So the first version (your fmsympy) takes the symbolic expression
cos(x) represented as a sympy expression tree and substitutes 1 for x
(which requires walking the tree to look fo all occurrences of x).
Then it evaluates the result to 15 decimal digits of precision using
the pure Python mpmath multiprecision math library. To prove that it's
not really a float calculation let's increase the precision:

In [5]: ftext.evalf(subs={x:1}, n=100)
Out[5]: 0.5403023058681397174009366074429766037323104206179222276700972553811003947744717645179518560871830893

Your second version (fm) makes a sympy expression using the cos
function from sympy and the argument 1. This creates a symbolic sympy
expression cos(1) and returns that. It doesn't do any evaluation of
the expression to get the digits of the actual numeric answer.

So I'm not surprised that the two operations take different amounts of
time. One doesn't evaluate the expression and the other does using a
slow multiprecision library. Also note though that sympy uses a cache
to accelerate repeated calculations so if you time it doing the exact
same thing repeatedly in a loop then you may just be measuring
cache-hit performance for two different inputs rather than actual
evaluation time.

If you want to see one that's a lot faster use the cos function from
the math module:

In [9]: from math import cos

In [10]: cos(1)
Out[10]: 0.540302305868

This calculates cos(1) using IEEE 64-bit binary floating point
(Python's float type) with hardware acceleration from your processor's
FPU instructions. This should be a lot faster then either substituting
into symbolic expressions in sympy or evaluating trignometric
functions with mpmath.

Generally I would use sympy in order to derive the mathematical
expressions that I want to compute. However if I then want to evaluate
the expressions many times in a loop with different input numbers for
example then I would rewrite the expression without using sympy. Sympy
has code generation capabilities to automate this but you seemed to be
confused about basic sympy usage right now so I wouldn't recommend
them without knowing more about what you're trying to do.

--
Oscar

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

sympy Poul Riis <priisdk@gmail.com> - 2016-03-30 04:17 -0700
  Re: sympy Ben Finney <ben+python@benfinney.id.au> - 2016-03-30 22:29 +1100
  Re: sympy Ned Batchelder <ned@nedbatchelder.com> - 2016-03-30 04:39 -0700
  Re: sympy Steven D'Aprano <steve@pearwood.info> - 2016-03-30 23:56 +1100
    Re: sympy Poul Riis <priisdk@gmail.com> - 2016-03-30 08:23 -0700
      Re: sympy Robert Kern <robert.kern@gmail.com> - 2016-03-30 16:29 +0100
      Re: sympy Steven D'Aprano <steve@pearwood.info> - 2016-03-31 02:59 +1100
        Re: sympy Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-03-31 17:49 +1300
          Re: sympy Poul Riis <priisdk@gmail.com> - 2016-03-30 23:51 -0700
        Re: sympy Poul Riis <priisdk@gmail.com> - 2016-03-31 03:57 -0700
          Re: sympy Peter Otten <__peter__@web.de> - 2016-03-31 16:55 +0200
          Re: sympy Chris Angelico <rosuav@gmail.com> - 2016-04-01 04:05 +1100
          Re: sympy Peter Otten <__peter__@web.de> - 2016-03-31 19:51 +0200
          Re: sympy Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-31 19:59 +0100
  Re: sympy Poul Riis <priisdk@gmail.com> - 2016-03-31 14:33 -0700
    Re: sympy Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-03-31 23:26 +0100

csiph-web