Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the derivative of the max function #1960

Open
TongTianDaXia opened this issue Mar 24, 2023 · 4 comments
Open

Add support for the derivative of the max function #1960

TongTianDaXia opened this issue Mar 24, 2023 · 4 comments

Comments

@TongTianDaXia
Copy link

TongTianDaXia commented Mar 24, 2023

i want to simplify a long expression by vairable substitution, and the expression has a Derivative operator, like

Derivative(max(f1, f2), x)

when i set a map

map[Expression(max(f1, f2))] = s

and execute the subs.

expression.subs(map)

Then the term Derivative(max(f1, f2), x) = 0, and the whole expression collapse
So i find that the vairable substitution cut off the relation between the experssion and the variables

my question is how to deal with the operator Derivative when taking variable substitution,
can i remove it when there is an operator max?
or can i keep it when execute the variable substitution?

@rikardn
Copy link
Contributor

rikardn commented Mar 24, 2023

I am not sure I understand what the max is supposed to mean.

@TongTianDaXia
Copy link
Author

I am not sure I understand what the max is supposed to mean.

the symengine offer the operator max

class Max : public MultiArgFunction
{
public:
    IMPLEMENT_TYPEID(SYMENGINE_MAX)
    //! Max Constructor
    Max(const vec_basic &&arg);
    //! \return `true` if canonical
    bool is_canonical(const vec_basic &arg) const;
    //! \return canonicalized Max
    virtual RCP<const Basic> create(const vec_basic &arg) const;
};

//! Canonicalize Max:
RCP<const Basic> max(const vec_basic &arg);

the function is like
f(x, max(f1(x), f2(x)), ...)
and i'm computing the jacobian of $\partial f/\partial x$

@TongTianDaXia
Copy link
Author

i find there is a piecewise function
is it possible to express the derivative operator in piecewise expression for discountinous function
like abs(), max() and the others.

@rikardn rikardn changed the title a question about the vairable substitution, Add support for the derivative of the max function Apr 3, 2023
@rikardn
Copy link
Contributor

rikardn commented Apr 3, 2023

It seems as if derivatives of the max function is not yet handled by symengine.

Using sympy we get:

import sympy

x = sympy.Symbol('x')
f1 = x
f2 = x**2
expr = sympy.Max(f1, f2)
sympy.diff(expr, x)
Out[10]: 
2*x*Heaviside(x**2 - x) + Heaviside(-x**2 + x)

where if using symengine we get the unevaluated expression back Derivative(max(x, x**2), x)

The alternative to using the step function would be to use Piecewise, but I guess using Heaviside simplifies the calculations. Wolfram alpha uses the the piecewise form.

So to implement this in symengine first an implementation of the heaviside function is needed and then an entry in the derivative visitor for the max function.

@rikardn rikardn removed the question label Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants