Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: sympy Date: Thu, 31 Mar 2016 23:26:29 +0100 Lines: 76 Message-ID: References: <733f5f0d-9b4e-4023-897b-e1f2730c39cb@googlegroups.com> <9c8497d9-f158-4f94-9a9f-39d6cb63850c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de qZv0rGpdkfkWoNmioU94Zw+jl7QzkGM2sHcBChCM9HuA== Return-Path: 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; '(even': 0.05; '(python': 0.05; 'float': 0.05; 'lines,': 0.05; 'responding': 0.07; 'cc:addr :python-list': 0.09; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; '__future__': 0.09; 'below).': 0.09; 'types:': 0.09; 'assume': 0.11; 'python.': 0.11; 'def': 0.13; '2016': 0.16; '[4]:': 0.16; 'cc:name:python list': 0.16; 'coerced': 0.16; 'exactly?': 0.16; 'expression.': 0.16; 'intended.': 0.16; 'kern': 0.16; 'lambda': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'substituted': 0.16; 'substituting': 0.16; 'wrote:': 0.16; 'numerical': 0.18; 'pfxlen:0': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '64-bit': 0.22; 'arguments': 0.22; 'pass': 0.22; 'trying': 0.22; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'example': 0.26; '(which': 0.26; 'earlier': 0.27; 'followed': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'sequence': 0.27; 'function': 0.28; 'values': 0.28; 'division': 0.29; 'forces': 0.29; 'symbols': 0.29; 'asked': 0.29; 'post': 0.31; 'implement': 0.32; 'returned': 0.32; 'int': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'clear': 0.35; 'robert': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'possible': 0.36; '(i.e.': 0.36; 'faster': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:209': 0.38; 'does': 0.39; 'enough': 0.39; 'takes': 0.39; 'skip:x 10': 0.40; 'some': 0.40; 'your': 0.60; 'here:': 0.63; 'march': 0.64; 'results': 0.66; 'reply': 0.68; 'completed': 0.69; 'apart': 0.70; 'transfer': 0.73; 'one).': 0.84; 'oscar': 0.84 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=gTXwdsUTt9O9a0IMmOkT25bYWqN1aYKwWqDkl52ZVdU=; b=ISpzexiLvSVo/AEpPIG+n7W6OAm3Lsir+B/RVjB4PD2L+PmEI/szhwzIcKQjA49STu x3jNnsw81FJcXCA2KrS0Xu246sM/AO+cq1aB1Jyxb1fqJ8cqT/X8+WCkZDxqkIRz7F+W uzjgVUVex4mnjuR2d3wgwhmWljqznK5LmOLluqh0lqzQ2Yh4SBbBQAl++pUZ8hKsFJe5 xg4Q+vVPcozYU21BsOaehSsyR6sOzlKoDH9DF2Y6JjbZM8iAnOzfkz8eZJzI6UIk8J+K 1cvgDvcrFehC1m46PDvEs3bsrw4j2d6kK5mNghQVn/+Xvkqu3GSmMd0hjgLjSBolS2AJ eUrg== 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=gTXwdsUTt9O9a0IMmOkT25bYWqN1aYKwWqDkl52ZVdU=; b=IS6JUIMIQa/qrclTvONvVDtI8PxjsBa+gWIQFjCU24eY9yQOyZH0rMicr2kEDHFP5D cDeHAK57ci6DzK2GMQ+b5mYEO56TKKodPTnmGRpGtAotEObdZM9sg9QTBuog/54CrTio sZOMtIfuOUKSTIZK/oG3pRNiUGe02RiFaweYHDODFqtmRVfJGI6vKjjs0AUDE0jr9jTU 2q8McGpFoItZOOelQr33oaXGAxX9CAcP6UOIWv7RZ07XidG5K6d5HwKQV3fL03oVci6u YjHbUM9kxgKs9SJkblrxCEQ1/L6ufs1mrZE2iERm0BpOdB7N68DrGSeP8ta5j11UPi7w Y1xw== X-Gm-Message-State: AD7BkJKnpA5SAS6obhMwfDKgdSLTH5O1Rh74TGvE6W2HsLWnAkjUljugbgvKxlQ+lTAkl9k5+5teB6LkEMOaLA== X-Received: by 10.25.218.1 with SMTP id r1mr1523402lfg.130.1459463208963; Thu, 31 Mar 2016 15:26:48 -0700 (PDT) In-Reply-To: <9c8497d9-f158-4f94-9a9f-39d6cb63850c@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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:106185 On 31 March 2016 at 22:33, Poul Riis wrote: > Den onsdag den 30. marts 2016 kl. 13.17.33 UTC+2 skrev Poul Riis: >> Is it possible to transfer results from sympy to 'normal' python. >> >> In the case below I think my intention is clear enough but it does not work as intended. How can it be done? >> >> Poul Riis >> >> from sympy import * >> x=Symbol('x') >> ftext=diff(1/(x**2+1),x) >> >> def f(t): >> return ftext.subs(x,'t') >> >> print(f(3)) > > Well, cos(1) should have been cos(1.0) (which forces numerical evaluation, try example below). > I am just trying to implement one little thing that all CAS tools can do in a few lines, namely finding the derivative of a given function followed by evalution of numerical values, something like: > define(fm(x),diff(f(x),x)) > fm(1.0) > > Sympy can find the derivative, and once that has been completed I would expect that there is some way to find numerical values just as fast as if the derivative had been given 'by hand'. But how exactly? I assume that you're responding to me (even though your post is a reply to yourself). Robert Kern already answered this question earlier in the thread: $ 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]: f = lambdify(x, sin(x).diff(x)) In [2]: f Out[2]: > In [3]: f(1) Out[3]: 0.540302305868 We can pull apart this function f returned here: In [4]: import dis In [5]: dis.dis(f) 1 0 LOAD_GLOBAL 0 (cos) 3 LOAD_FAST 0 (x) 6 CALL_FUNCTION 1 9 RETURN_VALUE In [6]: f.func_globals['cos'] Out[6]: So the function f returned by lambdify uses math.cos which is the 64-bit float function I mentioned earlier (i.e. the faster one). You can pass an int in and it will be coerced to float. You should find that the performance of f is as good as lambda x: math.cos(x). So lambdify takes an expression and a sequence of symbols and generates a function whose arguments are substituted for the sequence of symbols. The return value of the function is the result of substituting the arguments for the symbols in the expression. I think this is what you asked for. -- Oscar