Community RSS Feed
https://community.wolfram.com
RSS Feed for Wolfram Community showing any discussions in tag Mathematics sorted by activeExample of solving the Cahn-Hilliard equation with NDSolve
https://community.wolfram.com/groups/-/m/t/3291843
&[Wolfram Notebook][1]
[1]: https://www.wolframcloud.com/obj/d7b3e58f-bc39-4d23-8d1b-162b2e0127cdDavid Moore2024-10-06T14:18:41ZGalois groups and the symmetries of polynomials
https://community.wolfram.com/groups/-/m/t/3292345
&[Wolfram Notebook][1]
[1]: https://www.wolframcloud.com/obj/16d8ca1c-110f-4ae6-94be-d99caa87d903Paul Abbott2024-10-07T07:59:00ZFindMaximum::nrnum: The function value is not a real number
https://community.wolfram.com/groups/-/m/t/3291798
Hi everyone,
I'm working on an optimization problem with three decision variables: n (shipment frequency), x (replenishment cycle in days), and g (preservation technology investment cost). To find the optimal values for these variables, I fix n at various values and use FindMaximum to solve for x and g. Then, I compare the total profit for different n values to determine the optimal solution.
However, I’m encountering the following error when I try to run my code :
> FindMaximum::nrnum: The function value -7.431150025641089*10^1001-2.427197794764971*10^1017 I is not a real number at {x,g} = {2.15602,-2.3412}.
Here is the code :
Clear["Global`*"]
L = 7;
w = 0.064;
ww = 2;
R = 250;
Kf = 50;
Km = 0.5;
cf = 0.1;
cg = 0.01;
m = 0.2;
ml = 0.01;
Kp = 30;
Kj = 0.3;
hp = 0.05;
ht = 0.2;
Kr = 10;
Ks = 1;
hr = 0.1;
hs = 0.07;
j = 0.001;
k = 6.87;
q = 0.1;
a = 80;
b = 0.2;
Pv = 1;
pw = 0.5;
pf = 0.7;
pp = 2;
pr = 10;
c = 0.8;
S = 2;
Sv = 3;
Sr = 1;
T = 0.5;
v = 0.15;
vp = 1;
y = 400;
z = 100;
f = 0.9;
l = 0.3;
Ik = 0.1;
M = 0.5;
bk = 1 - f;
n = 1;
Tf = - (Log[1/z (k/ww - 1)]/q);
TP[x_, g_] =
pr/x ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(1 - b)) - (
hr + c hs + g + c j)/
x (2 x (-((a (-1 + b) (1 - E^(-y g)) x^2)/L))^(1/(1 - b))) - (Kr +
c Ks)/x - (Kp + Kf + c Km + c Kj)/(
n x) - (c pp + c pf)/x ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(
1/(1 - b)) - (S + c T)/
x^2 - (hp + c ht + g + c j)/(
2 R x) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(2/(
1 - b)) - ((hp + c ht + g + c j) (n - 1))/(
2 x) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(2/(
1 - b)) (x ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^1/(b - 1) - 1/
R) - (Sv + c Sr)/(n x)^2 - ((vp + c v)/(
n x)) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(
1 - b)) - ((Pv + c pw) w)/(
x f ww) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(
1 - b)) - (cf f + c cg f + m bk + c ml bk)/(
x f ww ) ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(
1 - b)) (k Tf + k/q (Log[1 + z E^(-q Tf)] - Log[1 + z])) +
pr/x l ((
a E^(-g y) (-1 + E^(g y)) x^3 ((a (1 - b) (1 - E^(-g y)) T^2)/
L)^(b/(1 - b)) (3 (-1 + 2^(1/(-1 + b))) (-1 + b) - 2 ))/(
3 L) + ((a (1 - b) (x^2))/(2 L) (1 - E^(-y g)))^(1/(
1 - b)) (M - x));
FindMaximum[{TP[x, g]}, {x, g}]
Any insights on why this might be happening and how I can solve this problem?Shafa Hananta2024-10-07T06:43:29ZNumerical instability in simulation
https://community.wolfram.com/groups/-/m/t/3292107
Hello. when I run the code below, i get the error NDSolveValue::reinitfail: Unable to reinitialize the system at t = 11.692137665979317` within specified tolerances. which i think is due to numerical instability in the calculations. any help on how to solve this issue would be greatly appreciated.
ClearAll["Global`*"]
m[n_] := Piecewise[{{0.1, n == 40}}, 0.0001];
l[n_] := 0.02;
\[Zeta][t_] := 0.1 Cos[7 t]
r[n_, t_] := \[Zeta][t] UnitVector[3, 3] +
Sum[UnitVector[3, 1]*l[i] Sin[\[Theta][i, t]] -
UnitVector[3, 3]*l[i] Cos[\[Theta][i, t]], {i, n}];
rdot[n_, t_] := D[r[n, t], t];
tt[nn_] := 1/2 Sum[m[n]*Total[rdot[n, t]^2], {n, nn}];
vv[nn_] := g*Sum[m[n]*r[n, t][[3]], {n, nn}];
ll[nn_] := tt[nn] - vv[nn];
rr[nn_] := 1/2 cd*Sum[Total[rdot[n, t]^2], {n, nn}];
eqLag[k_, nn_] :=
D[D[ll[nn], D[\[Theta][k, t], t]], t] -
D[ll[nn], \[Theta][k, t]] == -D[rr[nn], D[\[Theta][k, t], t]];
Block[{nn = 40,(*m=Function[1],l=Function[1],\[Zeta]=Function[1],*)
cd = 0.00005, g = 9.81},
eqs = Table[eqLag[n, nn], {n, nn}](*//Simplify*);
initialData =
Flatten[Table[{\[Theta][k, 0] == 0.05,
Derivative[0, 1][\[Theta]][k, 0] == 0}, {k, nn}]];
sols = NDSolveValue[
Flatten@{eqs, initialData,
WhenEvent[
0.02 (Sin[\[Theta][1, t]] + Sin[\[Theta][2, t]] +
Sin[\[Theta][3, t]] + Sin[\[Theta][4, t]] +
Sin[\[Theta][5, t]] + Sin[\[Theta][6, t]] +
Sin[\[Theta][7, t]] + Sin[\[Theta][8, t]] +
Sin[\[Theta][9, t]] + Sin[\[Theta][10, t]] +
Sin[\[Theta][11, t]] + Sin[\[Theta][12, t]] +
Sin[\[Theta][13, t]] + Sin[\[Theta][14, t]] +
Sin[\[Theta][15, t]] + Sin[\[Theta][16, t]] +
Sin[\[Theta][17, t]] + Sin[\[Theta][18, t]] +
Sin[\[Theta][19, t]] + Sin[\[Theta][20, t]] +
Sin[\[Theta][21, t]] + Sin[\[Theta][22, t]] +
Sin[\[Theta][23, t]] + Sin[\[Theta][24, t]] +
Sin[\[Theta][25, t]] + Sin[\[Theta][26, t]] +
Sin[\[Theta][27, t]] + Sin[\[Theta][28, t]] +
Sin[\[Theta][29, t]] + Sin[\[Theta][30, t]] +
Sin[\[Theta][31, t]] + Sin[\[Theta][32, t]] +
Sin[\[Theta][33, t]] + Sin[\[Theta][34, t]] +
Sin[\[Theta][35, t]] + Sin[\[Theta][36, t]] +
Sin[\[Theta][37, t]] + Sin[\[Theta][38, t]] +
Sin[\[Theta][39, t]] + Sin[\[Theta][40, t]])
<= 0,
Derivative[0, 1][\[Theta]][40, t] -> -0.8*
Derivative[0, 1][\[Theta]][40, t]]},
Table[\[Theta][k, t], {k, nn}], {t, 0, 20},
Method -> {"EquationSimplification" -> "Residual"}]]Ee Kin Chan2024-10-07T02:06:00ZSolving system of equations gives empty output, but gives result when reduced
https://community.wolfram.com/groups/-/m/t/522486
I want solve equation group below, but you can see the result return a {},
but when i reduce the equation group like this , it can give a solution(Seeing in the image),
why is this ? ![enter image description here][1]
[1]: /c/portal/getImageAttachment?filename=mathematica.png&userId=522471Panron Ryan2015-07-04T16:27:22ZWhat does TrapezoidalRule at NIntegrate really do?
https://community.wolfram.com/groups/-/m/t/3290658
Hello everybody,
I have a question as regards the TrapezoidalRule at the implemented NIntegrate function of Mathematica.
If I calculate the numeric(!) integral of f(x)=x^2 from 0 to 3 with 5 trapezoids by hand, I get a result of 9.18. If I use the implemented method "TrapezoidalRule" with the option "Points" -> 5 it gives me exactly 9.0.
So I doubt, that I do not fully understand what Mathematica does when using this Rule. I have tried a lot of (changing the WorkingPrecision, setting the MaxRecursions to 0 etc...), but it always comes up with the "real" (analytical) result.
Can anyone please explain what's going on?
Thanks a lot,
JJJ
f[x_] := x^2;
a = 0; b = 3;
\[CapitalDelta]x[n_] := (b - a)/n;
left[n_] := \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 0\), \(n - 1\)]\(f[
a + i*\[CapitalDelta]x[n]]*\[CapitalDelta]x[n]\)\);
right[n_] := \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n\)]\(f[
a + i*\[CapitalDelta]x[n]]*\[CapitalDelta]x[n]\)\);
trap[n_] := (left[n] + right[n])/2;
trap2[n_] := \[CapitalDelta]x[n]/2 (f[a] + 2 \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n - 1\)]\(f[
a + i*\[CapitalDelta]x[n]]\)\) + f[b]);
{left[5.], right[5.], trap[5.], trap2[5.]}
NIntegrate[f[x], {x, a, b},
Method -> {"TrapezoidalRule", "Points" -> 3}, WorkingPrecision -> 10,
MaxRecursion -> 0]Joachim Janezic2024-10-04T13:08:10Z3*5/3*5 versus 3*5:3*5 in Wolfram Alpha
https://community.wolfram.com/groups/-/m/t/3290739
Hello fellows :-)
Why WolframAlpha give the answer 25 (correct) for 3x5/3x5 but 1 for 3x5:3x5 ?
Thanks.Sergio da Silva2024-10-04T10:13:54ZIs the atlas 2 add-on no longer supported by anyone?
https://community.wolfram.com/groups/-/m/t/1303742
Is `atlas 2` add-on to *Mathematica* supported by anybody now? Links to it at wolfram.com have gone dead, and the website of the publisher, digi-area.com, also has vanished.Murray Eisenberg2018-03-17T20:12:04ZCommutative avatars of representations of semisimple Lie groups
https://community.wolfram.com/groups/-/m/t/3290514
![Commutative avatars of representations of semisimple Lie groups math printed with J850 Digital Anatomy 3D Printer ][1]
&[Wolfram Notebook][2]
[back-up]: https://community.wolfram.com//c/portal/getImageAttachment?filename=Main03102024.png&userId=20103
[1]: https://community.wolfram.com//c/portal/getImageAttachment?filename=2435qtegdsb.jpg&userId=11733
[2]: https://www.wolframcloud.com/obj/06872339-7181-493f-9079-759464171944Tamas Hausel2024-10-03T20:40:45ZFinding missing gems amidst chaos
https://community.wolfram.com/groups/-/m/t/3289552
![enter image description here][1]
&[Wolfram Notebook][2]
[1]: https://community.wolfram.com//c/portal/getImageAttachment?filename=4418hero.png&userId=20103
[2]: https://www.wolframcloud.com/obj/5a57cc41-0643-46b1-872a-2605ee2f03baBrad Klee2024-10-03T01:23:58ZLoop of solving integrals constants doesn't work properly?
https://community.wolfram.com/groups/-/m/t/3290199
There are 6 steps whereby I use DSolve, then Update and evaluate the Mathematica package. Each step the output yields a function which contains obviously integration constants. For example Step 2 output yields a function with integration constants C1 , C2, C3 . Step 3 outputs yields a function with integration constants C1, C2 (instead of C4 , C5). Step 4 outputs a function with integration constants, C1, C2 (instead of C6 , C7). See attachment
&[Wolfram Notebook][1]
May you please assist with a way of achieving obtaining continuous integration constants.
[1]: https://www.wolframcloud.com/obj/74a5f355-4170-4017-850e-be93ed4557eeNomsa Ledwaba2024-10-03T08:47:11ZWhat is the substitution or replacement method for multiple functions and their derivatives?
https://community.wolfram.com/groups/-/m/t/3290412
I want to substitute functions and their derivatives in this:
{LXi = x*D[Xi[2][t], t] + Sqrt[x] h[t],
Xi[2][t]}; LPhi = { (p[t] + f[x, t]) u + g[x, t]};
f[x, t] =
C[1][t] + ((-((4 k \[Theta] h[t])/Sqrt[x]) +
6 Sqrt[x] (k h[t] + 2 Derivative[1][h][t]) +
6 x (k Derivative[1][Xi[2]][t] + (Xi[2]^\[Prime]\[Prime])[t]))/(
8 k \[Theta]))
Is this correct?
{LXi, LPhi} =
ReplaceAll[{LXi = {x*D[Xi[2][t], t] + Sqrt[x] h[t], Xi[2][t]},
LPhi = { (p[t] +
1/(8 k \[Theta]) (-(1/Sqrt[x]) 4 k \[Theta] h[t] +
6 Sqrt[x] (k (*h[t]*)+ 2 D[h[t], t]) +
6 x (k D[Xi[2][t], t] + D[Xi[2][t], {t, 2}]))) u +
g[x, t]}}, {h[t] ->
E^((Sqrt[k] t Sqrt[3 k + 8 \[Theta]])/(2 Sqrt[3])) C[1] +
E^(-((Sqrt[k] t Sqrt[3 k + 8 \[Theta]])/(2 Sqrt[3]))) C[2],
p[t] -> C[3] - (
3 E^(-((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (-1 + E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (-3 k +
3 E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3]) k - 8 \[Theta] +
8 E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3]) \[Theta] +
Sqrt[3] Sqrt[k (3 k + 8 \[Theta])] +
Sqrt[3] E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3]) Sqrt[
k (3 k + 8 \[Theta])]) C[5])/(8 (3 k + 8 \[Theta])) - (
3 E^(-((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (-1 + E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (3 Sqrt[3] k +
3 Sqrt[3] E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3]) k +
8 Sqrt[3] \[Theta] +
8 Sqrt[3] E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3]) \[Theta] - 3 Sqrt[k (3 k + 8 \[Theta])] +
3 E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3]) Sqrt[
k (3 k + 8 \[Theta])]) C[6])/(
8 (3 k + 8 \[Theta]) Sqrt[k (3 k + 8 \[Theta])]),
Xi[2][t] ->
C[4] + (Sqrt[3]
E^(-((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (-1 + E^((2 t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3])) C[
5])/(2 Sqrt[k (3 k + 8 \[Theta])]) + (
3 E^(-((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (-1 + E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3]))^2 C[
6])/(2 k (3 k + 8 \[Theta])),
D[h[t], t] -> (
E^((Sqrt[k] t Sqrt[3 k + 8 \[Theta]])/(2 Sqrt[3])) Sqrt[k]
Sqrt[3 k + 8 \[Theta]] C[1])/(2 Sqrt[3]) - (
E^(-((Sqrt[k] t Sqrt[3 k + 8 \[Theta]])/(2 Sqrt[3]))) Sqrt[k]
Sqrt[3 k + 8 \[Theta]] C[2])/(2 Sqrt[3]) ,
D[Xi[2][t], t] ->
E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3]) C[5] -
1/2 E^(-((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (-1 + E^((2 t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3])) C[
5] + (
Sqrt[3] (-1 + E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3])) Sqrt[
k (3 k + 8 \[Theta])] C[6])/(k (3 k + 8 \[Theta])) - (
Sqrt[3] E^(-((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (-1 + E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3]))^2 Sqrt[k (3 k + 8 \[Theta])] C[6])/(
2 k (3 k + 8 \[Theta])),
D[Xi[2][t], {t, 2}] -> (
2 E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3])
k (3 k + 8 \[Theta]) C[5])/(
Sqrt[3] Sqrt[k (3 k + 8 \[Theta])]) + (
E^(-((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (-1 + E^((2 t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) k (3 k + 8 \[Theta]) C[5])/(
2 Sqrt[3] Sqrt[k (3 k + 8 \[Theta])]) - (
2 E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3]) Sqrt[
k (3 k + 8 \[Theta])] C[5])/Sqrt[
3] + ((-2 (-1 + E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) k (3 k + 8 \[Theta]) +
1/2 E^(-((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (-1 + E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3]))^2 k (3 k + 8 \[Theta]) +
3/2 E^(-((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) (2/3 E^((2 t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[3])
k (3 k + 8 \[Theta]) +
2/3 E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3]) (-1 + E^((t Sqrt[k (3 k + 8 \[Theta])])/Sqrt[
3])) k (3 k + 8 \[Theta]))) C[6])/(
k (3 k + 8 \[Theta]))
Please show a generic way of substitution if possible, without having to manually find their derivatives first before substitution.Nomsa Ledwaba2024-10-03T10:03:23ZGauss-Bonnet for form curvatures
https://community.wolfram.com/groups/-/m/t/3289909
![enter image description here][1]
&[Wolfram Notebook][2]
[1]: https://community.wolfram.com//c/portal/getImageAttachment?filename=8628Main02102024.png&userId=20103
[2]: https://www.wolframcloud.com/obj/743858fd-433c-4c2c-8b52-6779d10de985Oliver Knill2024-10-02T15:21:49ZMatrixSymbol simplifications
https://community.wolfram.com/groups/-/m/t/3288833
I'm exploring the new 14.1 MatrixSymbol functionality, hoping to eventually simplify complex and lengthy matrix expressions. But I'm running into limitations even with simple expressions. For example:
A = MatrixSymbol["A", {n, n}];
FullSimplify[A + Transpose[A], Assumptions -> A == Transpose[A]]
Out[18]= 2 Transpose[(MatrixSymbol["A", {n, n}])]
(** OK !! **)
FullSimplify[A - Transpose[A], Assumptions -> A == Transpose[A]]
Out[19]= 0
(** should we not see O_n,n ? **)
FullSimplify[Inverse[A] . Transpose[A], Assumptions -> A == Transpose[A]]
Out[20]= Inverse[(MatrixSymbol[
"A", {n, n}])] . Transpose[(MatrixSymbol["A", {n, n}])]
(**I was hoping to see an identity matrix **)
What am I missing?Eric Michielssen2024-09-30T23:57:43ZEmpowering mathematics education through programming with Wolfram AI and chat-enabled notebooks
https://community.wolfram.com/groups/-/m/t/3288988
&[Wolfram Notebook][1]
[1]: https://www.wolframcloud.com/obj/390b3f6c-6c28-477b-9f08-d74f7f24ea7dPaul Abbott2024-10-01T17:36:50ZTopological minibands and quantum anomalous hall state in Moiré insulators
https://community.wolfram.com/groups/-/m/t/3288963
![enter image description here][1]
&[Wolfram Notebook][2]
[1]: https://community.wolfram.com//c/portal/getImageAttachment?filename=Main01102024.png&userId=20103
[2]: https://www.wolframcloud.com/obj/7f959940-163f-40c4-a122-e86781741ed1Kaijie Yang2024-10-01T15:33:12ZCreative generative design with mathematical marbling
https://community.wolfram.com/groups/-/m/t/3258063
[![Generative design with mathematical marbling][1]][2]
&[Wolfram Notebook][3]
[1]: https://community.wolfram.com//c/portal/getImageAttachment?filename=MathematicalMarblingBanner.jpg&userId=20103
[2]: https://community.wolfram.com//c/portal/getImageAttachment?filename=MathematicalMarblingBanner.png&userId=20103
[3]: https://www.wolframcloud.com/obj/18517ffb-0f53-4ab0-a2c9-a989b51c6268Phileas Dazeley-Gaist2024-08-27T14:59:23ZThe limit space of self-similar groups and Schreier graphs
https://community.wolfram.com/groups/-/m/t/3288175
![enter image description here][1]
&[Wolfram Notebook][2]
[1]: https://community.wolfram.com//c/portal/getImageAttachment?filename=Main30092024.png&userId=20103
[2]: https://www.wolframcloud.com/obj/e4496ce7-2068-4b18-99e9-c9dcb1bb765dBozorgmehr vaziri2024-09-30T19:18:56ZSolving differential equation depending on radial and time parameter
https://community.wolfram.com/groups/-/m/t/3285392
Hi, I hope you are all doing well so I am trying to solve this differential equations which depends on radial and a time parameter to find the velocity, as an initial condition i took the value of the stationary condition which I solved separately. So when i tried to solve the final equations i got these errors which I don't know what causes it, is it the initial conditions or the method in which i want to solve it. So if any one of you have an idea it would help me a lot Thank you all in advance
&[Wolfram Notebook][1]
[1]: https://www.wolframcloud.com/obj/c772837f-00e0-42a3-9c1d-4a33cc33c6daLeo Murphy2024-09-27T09:53:55ZUnexpected behaviour from NMaximize optimising a stochastic function
https://community.wolfram.com/groups/-/m/t/3286311
I have a complicated function whose output contains noise. I'm interested in using Differential Evolution via NMaximize to solve it. However, NMaximize appears to compile or somehow 'freeze' the function outside its loop.
Below is a minimal example. Here, 'function 1' produces the expected behaviour in finding the argmax $\pi$ alongside a different random maximum each time.
That is, for three example runs:
```
{{0.368844, {\[Eta] -> 3.14159}}, {1.29786, {\[Eta] ->
3.14159}}, {0.128056, {\[Eta] -> 3.14159}}}
```
My actual problem is more like 'function 2'. But in this case it provides the same result three times. The result is also wrong and not $\pi$ - probably linked with the unexpected behaviour:
```
{{0.08951, {\[Eta] -> 3.89056}}, {0.08951, {\[Eta] ->
3.89056}}, {0.08951, {\[Eta] -> 3.89056}}}
```
Here is the example code:
```
ClearAll["Global`*"]
(* function 1 *)
f1 := NMaximize[{1.0 - (\[Pi] - \[Eta])^2 +
RandomVariate[NormalDistribution[]],
0. < \[Eta] < 4.}, \[Eta] \[Element] Reals,
Method -> "DifferentialEvolution" ];
Table[f1, {3}]
(* function 2 *)
w[\[Eta]_?NumericQ] :=
Block[{\[Epsilon]}, \[Epsilon] :=
RandomVariate[NormalDistribution[]];
1.0 - (\[Pi] - \[Eta])^2 + \[Epsilon]];
f2 := NMaximize[{w[\[Eta]], 0. < \[Eta] < 4.}, \[Eta] \[Element]
Reals, Method -> "DifferentialEvolution" ];
Table[f2, {3}]
```Cameron Turner2024-09-27T21:51:02ZHelp solving a cubic equation with a parameter
https://community.wolfram.com/groups/-/m/t/3284125
&[Wolfram Notebook][1]
[1]: https://www.wolframcloud.com/obj/42b03182-7ec1-4772-bc4a-37883605494bJackie cao2024-09-26T02:56:58ZSimplifying multiple inequalities
https://community.wolfram.com/groups/-/m/t/3283386
Can you please tell me which function in Wolfram can convert the formula
(y == 0 && x == 0) || (y == 1 && x == 1) || (0 < y < 1 && x == y)
to the formula:
0 <= y <= 1 && x == y
I can't even prove their equivalence:
FullSimplify[((y == 0 && x == 0) || (y == 1 && x == 1) || (0 < y < 1 && x == y)) \[Equivalent] (0 <= y <= 1 && x == y), (x | y) \[Element] Reals]
Wolfram works in one direction only:
FullSimplify[(0 <= y <= 1 && x == y) \[Implies] ((y == 0 && x == 0) || (y == 1 && x == 1) || (0 < y < 1 && x == y)), (x | y) \[Element] Reals]
Out: TrueSasha Mandra2024-09-25T21:20:13ZTensor networks, Einsum optimizers and the path towards autodiff
https://community.wolfram.com/groups/-/m/t/2437093
I've been thinking about what it would take to make a generically optimal autodiff engine, sorely [missing](https://mathematica.stackexchange.com/questions/256323/neural-network-automatic-differentiation-autograd) right now and it comes down to einsum optimization, a well studied problem.
To see why you need an einsum optimizer, consider differentiating $F(x)=f(g(h(x)))$. Using chain rule we can write the derivative as the product of intermediate Jacobian matrices.
$$\partial F = \partial f \cdot \partial g \cdot \partial h$$
The optimal order of computing this matrix product depends on shapes of constituent matrices. This is the well known [matrix-chain problem](https://en.wikipedia.org/wiki/Matrix_chain_multiplication). For neural network applications, $f$ is the "loss" with scalar output, while intermediate results are high-dimensional, so the matrix product starts with short Jacobian matrix followed by square'ish matrices, and the optimal order is "left-to-right", aka "reverse mode differentiation." The process of doing vector/matrix product is known as the "backward" step in autodiff systems.
![enter image description here][1]
For different set of matrix shapes, you may need a different order. For instance, if output of $F$ is high-dimensional but $x$ is scalar, it's faster to multiply your Jacobians in the opposite order:
![enter image description here][2]
Or, suppose both input and output of $F$ are high-dimensional, but an intermediate result is low-dimensional. Then an optimal order would go outwards in both directions, followed by an outer product in the last step:
![enter image description here][3]
Commonly occuring orders have names in autodiff literature -- forward, reverse, mixed, cross-country, but there's really an infinity of such orders. You can solve this problem more generally by treating it as a problem of finding an optimal contraction order. This is a well-known problem in graph theory literature, corresponding to the problem of finding optimal [triangulation](https://en.wikipedia.org/wiki/Matrix_chain_multiplication#More_efficient_algorithms) in the case of chain (see David Eppstein's [notes](https://www.ics.uci.edu/~eppstein/260/011023/)).
To see the connection of matrix chain problem to tensor networks, note that a product of matrices ABC can be written as a tensor network by using Einstein's summation notation:
$$ABC=A^{i}_j B^j_k C^k_l$$
This corresponds to a chain-structured tensor network below:
![enter image description here][4]
There are different orders in which you can perform the matrix multiplication. For instance, you could do $A(BC)$ or $(AB)C$. A parenthesization like $A(BC)$ is a hierarchical clusterings of tensors $A,B,C$ which is known as the "carving decomposition" of the tensor network graph, and it provides an order of computing the result, known as the "elimination order". For instance, computing $A(BC)$ corresponds to eliminating $k$, then $j$ and corresponding carving decomposition can be visualized below:
![enter image description here][5]
For a general tensor network, the problem of optimal elimination corresponds to the problem of finding the [optimal carving decomposition](https://arxiv.org/abs/1908.11034). If individual tensor dimensions are all very large, this is equivalent to finding minimum-width carving decomposition, which is also equivalent to finding a tree embedding that [minimizes edge congestion](https://arxiv.org/abs/1906.00013) for the tensor network graph.
For instance, for the contraction problem in the [paper](https://arxiv.org/pdf/1908.11034.pdf), to compute $K=A_{ij} B_{ml}^i C_o^l D_k^j E_n^{km}F^{no}$, elimination sequence $j,m,i,k,o,l,n$ corresponds to the contraction tree below, giving edge congestion of $4$, because of the four colored bands along the edge $H$, indicating that it is a rank-4 tensor.
![enter image description here][6]
The rank of largest intermediate tensor in the minimum-width contraction schedule is the "carving-width" of the graph, so if Mathematica's `GraphData` supported "Carvingwidth" property, we could see "4" as the result of `GraphData[{"Grid",{3,2}}, "Carvingwidth"]` . Carving width of planar graphs as well as their minimum width carving decomposition can be computed in $O(n^3)$ time using the [Ratcatcher algorithm](https://www.caam.rice.edu/~ivhicks/pb1.ijoc.pdf).
For the simpler case of chain structured tensor network, [we can solve the problem]((https://colab.research.google.com/drive/1LUbm7cS_slkC7BDKgOb58GrFqP-KQBI-)) by using [opt_einsum](https://github.com/dgasmith/opt_einsum) package which can handle general tensor networks. The package has been targeted for large tensor networks occurring in quantum mechanics, so solutions it gives for small tensor networks are sometimes [suboptimal](https://github.com/dgasmith/opt_einsum/issues/167).
Another way to approach this problem is to view it as a sum over weighted paths problem. Suppose our functions $f,g,h$ all have 2 dimensional inputs and outputs. Given that $F(x)=f(g(h(x)))$, $\partial F$ is the matrix product $\partial f \cdot \partial g \cdot \partial h$, visualized pictorially below
![enter image description here][7]
Derivative of this composition reduces to computing the sum over all paths for a fixed starting point and ending point, where weight of each path is the product of weights of individual edges, with edge weights corresponding to partial derivatives of intermediate functions $f,g,h$
<img src="https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at8.54.18AM.png&userId=1046145" width="400">
For instance, $\partial F_{1,1}$ is the sum over the following 4 paths, all with endpoints at $i=1$ and $l=1$:
<img src="https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at3.53.59PM.png&userId=1046145" width="300">
Hence, an einsum optimizer provides an efficient way to solve the "sum over weighted paths" problem by reusing intermediate results optimally.
Classical solution to this problem is a dynamic programming [algorithm](https://en.wikipedia.org/wiki/Matrix_chain_multiplication#A_dynamic_programming_algorithm) with $O(n^3)$ scaling, but there's an $O(n^2)$ algorithm described [here](https://arxiv.org/abs/2104.01777), with a Mathematica implementation [here](https://github.com/LeMinhThong/Speedup-Matrix-Chain-Multiplication).
For more general automatic differentiation, you need an einsum optimizer that can optimize computation of a **sum** of tensor networks.
To see why, consider differentiating the following function:
$$F(x)=f(g(h(x))+h(x))$$
This addition corresponds to a "skip-connection", which is popular in neural network design. You "skip" an application of $g$ in the second term. As the sum of weighted paths, the derivative of $F$ can be visualized below:
![enter image description here][8]
This doesn't neatly fit into tensor network notation, however, it does reduce to the sum of two tensor networks:
<img src="https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at9.00.08AM.png&userId=1046145" width="400">
These tensor networks have sub-chains in common, so an optimal computation schedule may reuse intermediate across two networks. The problem of optimal AD schedule for skip connections is described in more detail [here](https://cs.stackexchange.com/questions/147773/optimizing-a-sum-of-matrix-chains).
Another case where you need to optimize a sum of tensor networks is when computing Hessian-vector product of simple function composition. This comes up in Chemistry problems, see discussion [here](https://community.wolfram.com/groups/-/m/t/2380163).
For a composition $F(x)=h4(h3(h2(h1(x))))$ with scalar-valued $h4$, the Hessian vector product of $F$ with vector $v$ is the following sum of tensor networks:
![enter image description here][9]
We can visually inspect this sum to manually come up with a schedule for computing Hessian-vector product $\partial^2 F \cdot h$ that takes about the same time as computing the derivative $\partial F$
![enter image description here][10]
Here's a [notebook](https://www.wolframcloud.com/obj/yaroslavvb/newton/hvp.nb) (see below) implementing this schedule to compute Hessian-vector product of composition of functions $h_1, h_2, \ldots, h_n$ in Mathematica. It gives the same result as the built-in `D[]` functionality, but much faster.
The main parts of implementation are below. First define compute Jacobians and Hessians of intermediate functions stored in a list `hs`:
```
(* derivative of i'th layer *)
dh[i_] := (
vars = xs[[i]];
D[hs[[i]][vars], {vars, 1}] /. Thread[vars -> a[i - 1]]
);
(* Hessian of i'th layer *)
d2h[i_] := (
vars = xs[[i]];
D[hs[[i]][vars], {vars, 2}] /. Thread[vars -> a[i - 1]]
);
```
Now define the recursions to compute the schedule above. In autodiff literature, this would be called "foward-on-backward" schedule:
```
(* Forward AD, f[i]=dh[i]....dh[1].v *)
f[0] = v0;
f[i_?Positive] := dh[i] . f[i - 1];
(* Backward AD, *)
b[0] = {};
b[i_?Positive] := dot[b[i - 1], dh[n - i + 1]];
(* Activations *)
a[0] := x0;
a[i_?Positive] := h[i][a[i - 1]];
```
Now define the step that combines previous quantities into Hessian-vector products:
```
fb[i_] := dot[b[n - i], d2h[i]] . f[i - 1];
F[0] = fb[n];
F[i_?Positive] := F[i - 1] . dh[n - i] + fb[n - i];
```
After hours of manually debugging mismatched indices, it was especially satisfying to be able to write the following and have it work:
```
hess=D[H,{x0,2}];
Assert[Reduce[F[n-1]==hess.v0]];
```
Another example of derivative quantity that an autodiff engine should support is the Hessian trace. For the problem above, it can be written as the following sum of tensor networks:
![enter image description here][11]
Boxes corresponding to $\nabla h_i$ are intermediate Jacobian matrices, while entries like $\nabla^2 h_i$ are Hessian tensors. They are potentially large, but for common intermediate functions occurring in neural networks Hessians tend to be structured -- 0, rank-1, diagonal or a combination of the two. For instance, for ReLU neural network with linear/convolutional layers, the only component with a non-zero Hessian is the $h4$ loss function, hence Hessian trace reduces to a loop tensor network
![enter image description here][12]
Furthermore, if the loss function $h4$ is the cross entropy loss, its Hessian corresponds to a
[difference of diagonal and rank-1](https://yaroslavvb.medium.com/using-evolved-notation-to-derive-the-hessian-of-cross-entropy-loss-195f8c7b3a92) matrices, so the trace can be written as the following difference of two tensor networks
<img src="https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at11.36.13AM.png&userId=1046145" width="300">
The 3 way hyper-edge connected to `p` in the diagram above is called the "COPY-tensor" in Quantum Tensor Network literature (see Biamonte's excellent [notes](https://arxiv.org/abs/1912.10049)), but in einsum notation it's just an index that occurs in 3 factors, `np.einsum("i,ik,il->kl", p, h3, h3)`
In addition to einsum notation, the expression above can be written in matrix notation as follows
$$\text{tr} \nabla^2F=\|\text{diag}(\sqrt{p}) \nabla h_3 \nabla h_2 \nabla h_1\|_F^2 - p'\nabla h_3 \nabla h_2 \nabla h_1 \nabla h_1' \nabla h_2' \nabla h_3' p $$
Carl Woll has a [nice package](https://mathematica.stackexchange.com/questions/207336/generating-an-efficient-way-to-compute-einsum) for converting einsums to matrix notation, it could probably be extended to also work for this case.
The problem of optimally contracting a general tensor network is hard, and people who implemented packages like [opt_einsum](https://github.com/dgasmith/opt_einsum) and [Cotengra](https://github.com/jcmgray/cotengra) mainly tried to approximate optimal solutions for large problems, like [validating](https://quantum-journal.org/papers/q-2021-03-15-410/) Google's Sycamore quantum supremacy result.
However, tensor networks occuring up in automatic differentiation tasks are much easier than general tensor networks:
- all examples above are planar, for which a schedule within a factor of $n*d$ of optimal cost can be found in $O(n^3)$ time using Ratcatcher algorithm, $n$ is the number of tensors and $d$ is the largest [bond dimension](https://tensornetwork.org/mps/#toc_1), ([discussion](https://cstheory.stackexchange.com/questions/50794/complexity-of-optimal-elimination-for-a-planar-tensor-network))
- furthermore, all examples above are "series-parallel" graphs, treewidth=2
- furthermore, some examples are trees, for which a polynomial algorithm to obtain an optimal contraction algorithm has been described [here](https://journals.aps.org/pre/abstract/10.1103/PhysRevE.100.043309)
A practically interesting solution would be to create an einsum optimizer that can do one or more of the following:
- compute optimal contraction schedule for a tree-structured tensor network, allowing hyper-edges (detailed example [here](https://cs.stackexchange.com/questions/146680/generalizing-matrix-chain-problem-optimal-summation-in-a-tree))
- compute optimal contraction schedule for a series-parallel tensor network
- compute optimal computation schedule for a sum of chain tensor networks (especially [sums corresponding](https://cs.stackexchange.com/questions/147773/optimizing-a-sum-of-matrix-chains) to derivatives of compositions with skip connections)
- compute optimal computation schedule for sum of tree tensor networks
- compute optimal computation schedule for a sum of series-parallel tensor networks
A tool that can do all of these efficiently for networks with 2-20 tensors would cover most autodiff applications that make sense to attempt in Mathematica.
&[Wolfram Notebook][13]
[1]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at5.02.41PM.png&userId=1046145
[2]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at5.03.12PM.png&userId=1046145
[3]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at5.39.19PM.png&userId=1046145
[4]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at8.48.16AM.png&userId=1046145
[5]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-05at12.58.47PM.png&userId=1046145
[6]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-05at7.39.30AM.png&userId=1046145
[7]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at8.53.12AM.png&userId=1046145
[8]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at8.57.59AM.png&userId=1046145
[9]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at9.07.49AM.png&userId=1046145
[10]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at3.33.03PM.png&userId=1046145
[11]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at10.48.12AM.png&userId=1046145
[12]: https://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2022-01-04at10.05.30AM.png&userId=1046145
[13]: https://www.wolframcloud.com/obj/268f3a39-1e2e-4218-86da-1cca0dbb9f81Yaroslav Bulatov2022-01-04T18:30:14Z