0

I want to define a symbolised function expFun to use it later for an integration. I am referring to this link.

My current code looks like

import numpy as np
import sympy as sym

a1= sym.Symbol('a1')
a2= sym.Symbol('a2')
X1= sym.Symbol('X1')
X2= sym.Symbol('X2')
T= sym.Symbol('T')
u= sym.Symbol('u')

def expFun(a1,a2,X1,X2,T,u):
    return X1*sym.exp(-a1*(T-u))+X2*sym.exp(-a2*(T-u))


expFun(sym.Symbol('a1 a2 X1 X2 T u'))

The last line gives an error, saying

TypeError: expFun() missing 5 required positional arguments: 'a2', 'X1', 'X2', 'T', and 'u'

I have also tried this that is not working neither:

expFun(sym.Symbol('a1','a2', 'X1','X2','T','u'))

Putting those arguments into a list or set didn't hep too.

Would anyone please tell me how I can fix this?

#Updates#

Following hpaulj's comments below, I updated my code as follows. But I still get errors :(

from __future__ import division
import sympy as sym
X1, X2, a1, a2, T, u = sym.symbols('a1 a2 X1 X2  T u')

def expFun1(a1,a2,X1,X2,T,u):
    return X1*np.exp(-a1*(T-u))+X2*np.exp(-a2*(T-u))

expFun1(*sym.symbols('a1 a2 X1 X2 T u'))

Then the error message

TypeError: loop of ufunc does not support argument 0 of type Mul which has no callable exp method

Then I tried this

def expFun2(a1,a2,X1,X2,T,u):
    return X1*sym.exp(-a1*(T-u))+X2*sym.exp(-a2*(T-u))

expFun2(*sym.symbols('a1 a2 X1 X2 T u'))

and getting funny output on the Skpyder 4 console window. enter image description here

I am using Python 3.7.6 with Spyder version 4.2.1

yufiP
  • 103
  • 6
  • You probably want `symbols` rather than `Symbol`. – Oscar Benjamin Feb 10 '21 at 23:27
  • `expFun(a1, a2, X1, X2, X3, T, u)` - pass the symbol variables you've already created.. – hpaulj Feb 11 '21 at 04:32
  • `expFun1` contains numpy functions. Numpy doesn't understand about sympy symbols, you need to treat them as separate worlds. Here you can use sympy's `sp.exp` instead of `np.exp`. In general, it is a good idea not to import numpy it the parts where you are working with sympy. – JohanC Feb 13 '21 at 11:18

2 Answers2

1

Thank you for all your helpful comments. Combining the advices, I managed to achieve what I initially intended. Here are the summaries.

import sympy as sym
sym.init_printing(use_unicode=False, wrap_line=True)

X1, X2, a1, a2, T, u = sym.symbols('X1 X2 a1 a2 T u')

def expFun2(X1,X2,a1,a2,T,u):
    return X1*sym.exp(-a1*(T-u))+X2*sym.exp(-a2*(T-u))

expFun2(*sym.symbols('X1 X2 a1 a2 T u'))

sym.init_printing(use_unicode=False, wrap_line=True) solved the display issue I reported in my question.

enter image description here

yufiP
  • 103
  • 6
0

In an isympy session:

>>> 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)
...

In [1]: def foo(a,b,c):
   ...:     return a+b*c
   ...: 

In [2]: foo(x,y,z)
Out[2]: x + y⋅z

In [3]: foo(symbols('a b c'))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-2bd97132ca00> in <module>
----> 1 foo(symbols('a b c'))

TypeError: foo() missing 2 required positional arguments: 'b' and 'c'

In [4]: foo(*symbols('a b c'))
Out[4]: a + b⋅c

foo, and your expFun, is a python function, not specifically sympy. Regular Python syntax applies.

symbols(...) "returns a sequence of symbols", i.e. a python tuple (or depending on input just one symbol object).

In [13]: symbols('x y')               # a tuple
Out[13]: (x, y)

In [14]: a1, a2 = symbols('x y')   # unpack that tuple 

In [15]: a1
Out[15]: x

foo(*symbols('a b c')) uses the * to unpack that tuple into the 3 arguments that foo takes.

Symbol returns just one symbol object, not multiple like symbols:

In [18]: Symbol('x y')
Out[18]: x y
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Thank you hpaulj for the helpful. I got the ideas, but I still can't figure out how to achieve desired outcome, which is getting symbolised function for my `expFun`. I updated my post above, would you mind giving me further guidance? – yufiP Feb 12 '21 at 19:16