Define functions used in weak form

Hi, I am trying to define a function that would be in the weak form in FEniCS.

However the function’s derivative is quite complicated, I am wondering if there is any good way of defining it FEniCS.
The first function I need to define is

,
where u is the variable to be solved. (I am solving a nonlinear PDE).
I implement it in the following way

The second function I need to define is the derivative of the above function (wrt u).
I can compute the derivative and directly define it, which would be a long expression. This is one way. However, I am wondering if there is a better way, especially in case when the derivative becomes too complicated.

E.g. In computing the right hand side f(x,y) of a manufacture PDE, we can use sympy’s functionalities to get an expression for f(x,y), convert the expression to C++ code and then define it using fenics Expression which recognizes C++ language. Is there a similar method for the problem I encountered? This situation is a bit different because I am defining a nonlinear function in the variable u.

Thank you very much!

You can automatically take derivatives with the diff function. The one point to keep in mind is that the variable with respect to which you differentiate must be labeled as a Variable before defining the function to be differentiated. So, in this case, it would look something like:

u = variable(u)
F_inv = Fermi_inv(u)
dF_inv_du = diff(F_inv,u)

where dF_inv_du is \frac{d\mathcal{F}^{-1}(u)}{du}.

Thank you! This is very helpful.