simplifying expressions with hyperbolic functions - sympy

Is there a command in sympy to simplify sinh(x)+cosh(x) to exp(x)? If I issue
from sympy import *
x = Symbol('x')
(sinh(x)+cosh(x)).simplify()
I just get sinh(x)+cosh(x) back, but I want to see exp(x) instead.

Even assuming that the simplify function in sympy was very good, what you suggest may not have worked, because what is "simple" is not rigorously defined.
I think what you want is the functionality present in .rewrite:
In [1]: (sinh(x)+cosh(x)).rewrite(exp)
Out[1]:
x
e
You can use .rewrite for many other transformations including gamma <-> combinatorics and inverse trig <-> logarithms.

Related

Computing the gradient of an objective function evaluated at the optimum in a dynamic optimization problem, pyomo

I am computing the solution to a dynamic non-linear optimization problem, that I set up usign the pyomo library. I use a ConcreteModel, with an objective function and several constraints, all time-indexed.
My objective function takes the form of a ScalarObjective (I am solving a dynamic general equilibrium problem in which I seek to maximize total welfare). I would like to compute the gradient of the objective, evaluated at the optimum, with respect to one of the model's variables at a given period t. My problem is a discrete-time problem.
I have tried many different options, asking AI chatbots for help (both You Chat and ChatGPT), but every solution I'm given is incorrect -- on this topic the AI chatbots seem to know very little.
I feel that some method in the library pyomo.dae could be of help, but I haven't found a solution yet. Could anyone help me, please?
You can do this using Pyomo's differentiate function. Here is a toy example:
import pyomo.environ as pyo
from pyomo.core.expr.calculus.derivatives import differentiate
m = pyo.ConcreteModel()
m.x = pyo.Var()
m.con = pyo.Constraint(expr=m.x<=10)
m.obj = pyo.Objective(expr=m.x**2)
pyo.SolverFactory('ipopt').solve(m)
print(pyo.value(m.x))
# -1.2528349584581178e-10
# Evaluate the derivative at current value of m.x
ddx = differentiate(m.obj, wrt=m.x)
print(ddx)
# -2.5056699169162357e-10
# Return derivative expression
ddx2 = differentiate(m.obj, wrt=m.x, mode='sympy')
print(ddx2)
# 2.0*x
You can read more about this function here: https://github.com/Pyomo/pyomo/blob/main/pyomo/core/expr/calculus/derivatives.py#L31

A prohibition on the disable of brackets in sympy

There is the following simple problem:
integrate((x-2)^3,x)
Instead of just giving a simple answer:
(x-2)^4/4
Sympy spreads out on an endless tape
x^4/4-2*x^3+6*x^2-8*x
It can be trained to give an easy answer, as (x-2)^4/4?
You can use the "manual" integration method:
In [24]: integrate((x-2)**3,x, manual=True)
Out[24]:
4
(x - 2)
────────
4

Sympy unit see value of constant?

If I use the units module in Sympy, I can't find how to see the values of units.
Say I wanted to see what is the gravitional constant.
import sympy.physics.units as u
G = u.gravitational_constant
What should I do to get some value out? I know it's possible to call Sympy's module "convert to" But it assumes I already know what the constant is.
For example to see the speed of light I can write:
u.convert_to(u.speed_of_light,u.meter/u.second)
>>> 299792458 m/s
But that assumes I know speed of light is a velocity.
Every Quantity has a dimension:
>>> from sympy.physics.units import *
>>> G.dimension
Dimension(length**3/(mass*time**2))
Knowing these, you can see the value for your dimensions of interest:
>>> G.convert_to(m**3/kg/s**2)
6.6743e-11*meter**3/(kilogram*second**2)

Find one solution of equation via sympy solve()

Is there a way to find one solution of a complicated equation via sympy. It seems finding all the solutions is too sophisticated.
I have tried to put quik = True but it does not improve the resolution.
If the equation is a polynomial you can request a particular RootOf solution; if it is a univariate expression you can get a numerical solution "near some intial guess" with nsolve.

Calculation where output is square polynomial plus remainder

My son is learning how to calculate the formula for a parabola using a directrix and focus point on his Khan Academy course. (a,b) is the focus point, k is the parameter for the directrix as y=k. I wanted to show him a simple way to check his results using Sympy; programming helps hugely in solidifying internal algorithms. Step 1 is clearly to set the equation out.
Parabola = Eq(sqrt((y-k)**2),sqrt((x-a)**2+(y-b)**2))
I first solved this for y, intending then to show how to substitute values and derive the equation, thus:
Y = solve(Parabola,y)
This was in a reasonable form, having collected the 1/(2b-2k) to the outside.
Next, I substituted the value of the focus and directrix into the equation, obtaining the equation y= 1/6*(x**2+16*x+49), which is correct.
He needed next to resolve this in a form (x+c1)(x+c2)+remainder. There does not seem to be a direct way to factor from the equation above into this form, at least not from an hour searching the docs.
Answer = Y[0].subs({a:-8,b:-1,k:-4})
factor(Answer,deep=True)
Of course I understand how to reduce to a square factorization plus remainder; my question is solely whether this is possible in sympy and, if so, how?
A second, perhaps trivial, question is why Sympy returns some factorizations as (constant - x) where (x -constant) is preferred: is there a way of specifying the form?
Thanks for any help, on behalf of my son, to whom I am showing the wonders of Sympy.
The process is usually called "completing the square". It is not implemented as a single SymPy method, but one can use the SymPy equation solver to find the coefficients of such a form of the polynomial:
>>> var('A B C')
>>> solve(Eq(Answer, A*(x-B)**2 + C), [A, B, C])
[(1/6, -8, -5/2)]
So the parabola vertex is at (8, -5/2), and the polynomial can be written as 1/6*(x+8)**2 - 5/2