# Derivative of long expression renders Piecewise[…<<…>>…] in Jupyter notebook

My mathematical problem is to solve the optimization problem: $$Max_{u} \quad \mathbb{E}[\gamma S-S^2]$$ where $$S= A_0 u X+a-\max(\alpha u X+b,0)$$ with $$\gamma,A_0,a,\alpha,b$$ some constants and $$X\sim N(\mu,\sigma)$$.

As a starting point, I was trying to compute the tedious expectation via

Expectation[gamma*(a0*u*x+a-Max[alp*u*x+b,0])-((a0*u*x+a-Max[alp*u*x+b,0]))^2, x \[Distributed] NormalDistribution[mu, sig]]

and here is the output:

using the Wolfram Engine (via Jupyter Interface). After that, I was trying to take the partial derivative with respect to $$u$$ using

D[%,u]

and it renders a very strange output:

My feeling is that the expression is so long that it is unable to display it so the problem is not from the mathematical difficulty but more from the display. I searched on stackexchange but couldn't find a similar issue.

Do you know what the exact issue is and I can get the proper derivative ?

Thanks a lot !

Edit: The problem is now solved thanks to the solution below. Since some people comment that they can't recover my output, here is the full Jupyter output:

• The community expects the following from you: ✅: A clear description of an on-topic problem or goal. ❌: A minimal working Wolfram Language code example, formatted, easy to copy&paste, in Raw InputForm Not images!. ❌. An example of what you expect as output. ❌. Some proof of minimal Mathematica knowledge. ❌. Minimum due diligence: Share how you have searched the site and documentation, your attempts and reasons to believe an answer exists. Aug 8, 2022 at 16:53
• I guess that's not a very likely explanation, though Aug 9, 2022 at 14:36
• Can't reproduce the mentioned output in v12.3 and v13.1. I don't think this is something related to the powfulness of computer. The real Out[16] can't be the one you show. Aug 9, 2022 at 14:59
• OK, so you only cut part of the output in the first screenshot. Then please notice the Out[1] in Edit is exactly a Piecewise[…] function. It's the default 2D display (formally we call it StandardForm) of it. If you still don't understand what I mean, just execute e.g. Piecewise[{{a, a > 0}}, -a] and observe. For more info, check the document of Piecewise. Aug 9, 2022 at 17:08
• Somewhat related: mathematica.stackexchange.com/q/128406/1871 Aug 9, 2022 at 17:46

Clear["Global`*"]

To get the simplest form you need to tell Mathematica any constraints on the variables. For example,

$Assumptions = {mu ∈ Reals, sig > 0, alp > 0, u > 0, b > 0};$Assumptions will automatically be used by any function that uses the option Assumptions (e.g., Expectation, FullSimplify)

expr = Expectation[
gamma*(a0*u*x + a -
Max[alp*u*x + b, 0]) - ((a0*u*x + a - Max[alp*u*x + b, 0]))^2,
x \[Distributed] NormalDistribution[mu, sig]] // FullSimplify

(* 1/2 (-2 a^2 -
b (b + gamma) - (-2 a0 (b + gamma) +
alp (2 b + gamma)) mu u - (2 a0^2 - 2 a0 alp + alp^2) (mu^2 +
sig^2) u^2 + 2 a (b + gamma + (-2 a0 + alp) mu u) -
alp E^(-((b + alp mu u)^2/(2 alp^2 sig^2 u^2))) Sqrt[2/π]
sig u (-2 a + b +
gamma + (-2 a0 + alp) mu u) + (-b (-2 a + b +
gamma) + (2 a alp + 2 a0 b - alp (2 b + gamma)) mu u -
alp (-2 a0 + alp) (mu^2 + sig^2) u^2) Erf[(b + alp mu u)/(
Sqrt[2] alp sig u)]) *)

Taking the derivative

D[expr, u] // FullSimplify

(* a (-2 a0 + alp) mu + a0 (b + gamma) mu -
1/2 alp (2 b + gamma) mu - (2 a0^2 - 2 a0 alp + alp^2) (mu^2 +
sig^2) u + (
E^(-((b + alp mu u)^2/(2 alp^2 sig^2 u^2)))
sig (-2 a0 b - alp (-2 a + gamma + 2 (-2 a0 + alp) mu u)))/Sqrt[
2 π] +
1/2 ((2 a alp + 2 a0 b - alp (2 b + gamma)) mu -
2 alp (-2 a0 + alp) (mu^2 + sig^2) u) Erf[(b + alp mu u)/(
Sqrt[2] alp sig u)] *)