0

say I want to print the equation g(x) in the form g(x) = x^2 *........

how do I do it? This is my first time using python

import numpy as np
import matplotlib.pyplot as plt
import math
from sympy.mpmath import *



f = lambda x: ((x^2)*math.exp(-x))
dfdx = lambda x: diff(f,x)
d2fdx2 = lambda x: diff(dfdx,x)
g = lambda x: ((math.exp(x)/math.factorial(2))*d2fdx2)
print(g)

(edit) the output im getting is

function lambda at 0x0671C198

Joe_Rose
  • 48
  • 1
  • 1
  • 7
  • 2
    Wow, this doesn't look like a first time python user. Normally new users start with something more simple. Are you already familiar with another language?? –  Oct 31 '15 at 19:16
  • 1
    Familiar with c++ and Java only. My math classes now require me to switch to learn python at a very fast rate and Im trying to cope – Joe_Rose Oct 31 '15 at 19:18
  • Alright kinda figured that I'm gonna read up on `lambda` real quick, so I can see what it does so I can help. I have no experience with `lambda`. –  Oct 31 '15 at 19:20
  • 1
    Thanks for taking the effort! – Joe_Rose Oct 31 '15 at 19:21
  • Oh alright it's just like saying `def f(x):` time to start testing your code. –  Oct 31 '15 at 19:22
  • See if [this](http://stackoverflow.com/questions/11112046/create-a-lambda-function-from-a-string-properly) helps. I'm still testing with it. –  Oct 31 '15 at 19:33
  • 1
    I'll try using the eval function from your link, hold on – Joe_Rose Oct 31 '15 at 19:36
  • `eval` didn't work for me :/ –  Oct 31 '15 at 19:39
  • yeah using eval did not work for me as well.. I know this is a simple thing but its really bothering me that I can't do it. All i want to do is output the equation in the form "x^2 + 2x + 2........... ". – Joe_Rose Oct 31 '15 at 19:42
  • I've found 2 answers: consists of putting the function in an entire separate module and the other is something I have no idea how to do [2](https://github.com/uqfoundation/dill/blob/master/README.md) –  Oct 31 '15 at 19:51
  • Could you show me the one that you know how to do? – Joe_Rose Oct 31 '15 at 19:53
  • 2
    Duplicate of http://stackoverflow.com/questions/334851/print-the-code-which-defined-a-lambda-function. Or do you want to have it formated more nicely? – MaxNoe Oct 31 '15 at 20:00
  • Related: [How can I get the source code of a Python function?](http://stackoverflow.com/q/427453/3821804) – GingerPlusPlus Oct 31 '15 at 20:09
  • @DeliriousMistakes that just printed out what I set g to instead of solving the equation and outputting it. jepio already gave an answer that works but I just wanted you to know I appreciate your effort! – Joe_Rose Oct 31 '15 at 20:22
  • @Joe_Rose Thanks nice to know it was appreciated. –  Oct 31 '15 at 21:09

1 Answers1

4

First of drop all the the unnecessary imports and stick to what you really need. Symbolic math is what sympy was made for so check out the documentation for that.

In sympy you have to define symbols first

import sympy
x = symbols('x')

Now you would use the symbol x to construct an expression using builtin operators and functions in the sympy module. Be aware that ** is exponentiation and ^ is logical xor.

f = x ** 2 * sympy.exp(-x)
dfdx = sympy.diff(f, x)
d2fdx2 = sympy.diff(f, x, x)
g = sympy.exp(x) / sympy.factorial(2) * d2fdx2

When you write g in the interactive interpreter it will write the expression the way you want it. Can't show that here but atleast I can do this:

>>> print(g)
x**2/2 - 2*x + 1

You cannot do what you want with the math, sympy.mpmath and numpy modules as they exist for numerical evalutions - they want numbers and give you number.

If you later want to evaluate your expression for a given value of x you could do

val_at_point = g.evalf(subs={x: 1.5})

where subs is a dictionary.

Or you could turn g into a python lambda function:

fun_g = sympy.lambdify(x, g)
val_at_point = fun_g(1.5)

If you're doing this for a math class you probably want to be working in the interpreter anyway in which case you can start by writing

>>> from sympy import *

so that you can skip all the sympy. stuff in the above code samples. I left them there just to show where symbols come from.

jepio
  • 2,276
  • 1
  • 11
  • 16
  • Just when I was about to finish my own answer. Maybe you should show the `evalf` method, to see how one gets numbers. – MaxNoe Oct 31 '15 at 20:07
  • And do not do the `from bla import *` import. – MaxNoe Oct 31 '15 at 20:10
  • 2
    I'd disagree with the second comment: `from bla import *` is made for the interpreter and makes work much easier. In a module with many imports - never. But interactively its the only thing that makes sense. – jepio Oct 31 '15 at 20:12
  • Thanks jepio! Pointing out wrong library usage teaches me to dig into libraries next time to see what input arguements they take – Joe_Rose Oct 31 '15 at 20:18
  • 1
    and also your solution helped me get what I wanted! – Joe_Rose Oct 31 '15 at 20:19
  • @jepio: *interactively `from bla import *` is the only thing that makes sense*, except `from bla import a, b, c`. – GingerPlusPlus Nov 01 '15 at 21:27