Requesting help to build a symbolic function

26 views
Skip to first unread message

Gayanath Chandrasena

unread,
Aug 28, 2021, 12:05:25 PM8/28/21
to sympy
Hi all,

I am just a new user for SymPy. I am self learning this library for my undergrauate research. But in the middle of the process I am stucked with one code.

So I have  defined a function with a subscript.
    
                         U_n= x^n + 1/x^n

When I consider   (U_1)^3 I get (substitute n=1)

                        (U_1)^3 = (x+1/x)^3

Then after simplifying this I get 
       
                        (U_1)^3 = (x^3 + 1/x^3) + 3(x+ 1/x)      

But one can see this answer as 

                                       (U_1)^3 =   U_3 + 3U_1

How to get the output in terms of U_n 's ?

Can someone please give an idea how to build this code using SymPy. It would be a very big help for my research.

Thank you very much.

Gayanath Chandrasena.



            


Chris Smith

unread,
Aug 28, 2021, 1:02:06 PM8/28/21
to sympy
Your question is somewhat vague and one can imagine lots of variations of expression in which you are looking for this pattern, but since the x + 1/x is hyperbolic-like if you think of the x as exp(x), the following is a possibility:

def u(n):
    return x**n+1/x**n

def unu(eq):
    c = (eq.subs(x, exp(x))).simplify()/2
    return c.subs(cosh, Function('u')).subs(x,1)


>>> u(3) + 3*u(1)
x**3 + 3*x + 3/x + x**(-3)
>>> unu(eq)
3*u(1) + u(3)

/c

Reply all
Reply to author
Forward
0 new messages