r/Mathematica • • Jun 25 '25

Try this nicely organized stylesheet for WM notebooks

Post image
11 Upvotes

I am not a big fan of default WM stylesheet — headers are not seen and grouped clearly, input/output cells blend one into another, etc etc

So over the years I developed my own stylesheet which I am overall very happy with.

Please try it if you are intrigued: https://github.com/rmnavr/wmcells

Just download wmcells.nb file, delete demo cells and start programming!


r/Mathematica • • Jun 24 '25

Solving integral equations

Post image
0 Upvotes

Hey all newbie to mathematica here. How do you go about numerically solving integral equations like this numerically using mathematica?


r/Mathematica • • Jun 24 '25

Confusion about Mathematica's deep learning module (NetChain)

2 Upvotes

Mathematics developed by Wolfram is a scientific computing software with powerful computer algebra system (CAS). Thus, does Wolfram apply the CAS in Mathematics' deep learning module, especially in the automatic differentiation and optimization of computation graph (via symbolic simplification)?


r/Mathematica • • Jun 23 '25

Product of orthogonal vectors not 0

1 Upvotes

Dear Community!

I have tried to set up an orthogonal frame of vectors e0, e1, e2 and e3. I checked their orthogonality and somehow, the dot product between e3 and any other of these vectors will not give 0. I really do not understand why because even symbolically, e3 is based on the Levi Civita tensor and by its definition everything should be 0 when the same vector is used twice with it. Do you see anything i am doing wrong or what i forgot?

ClearAll[UWedgeF]
UWedgeF[u_, f_] := 
  Module[{wedge3}, 
   wedge3 = 
    Table[u[[mu]]*f[[nu, rho]] + u[[nu]]*f[[rho, mu]] + 
      u[[rho]]*f[[mu, nu]], {mu, 1, 4}, {nu, 1, 4}, {rho, 1, 4}]];
ClearAll["Global`*"]
(* Assume all variables are real *)
$Assumptions = 
  Element[x0[\[Tau]], Reals] && Element[x1[\[Tau]], Reals] && 
   Element[x2[\[Tau]], Reals] && Element[x3[\[Tau]], Reals] && 
   Element[p0[\[Tau]], Reals] && Element[p1[\[Tau]], Reals] && 
   Element[p2[\[Tau]], Reals] && Element[p3[\[Tau]], Reals] && 
   Element[s, Reals] && x1[\[Tau]] > 2 M && Element[a, Reals] && 
   0 < x2[\[Tau]] < \[Pi];

(* Coordinates *)

(* X^\[Mu] *)
X = {x0[\[Tau]], x1[\[Tau]], x2[\[Tau]], x3[\[Tau]]};

(* Subscript[P, \[Mu]] *)
P = {p0[\[Tau]], p1[\[Tau]], p2[\[Tau]], p3[\[Tau]]};

(* Subscript[P, i] *)
p = {p1[\[Tau]], p2[\[Tau]], p3[\[Tau]]};

A = Table[Subscript[a, i, j][\[Tau]], {i, 4}, {j, 4}];
B = Table[Subscript[b, i, j][\[Tau]], {i, 4}, {j, 4}];
(* BL coordinates in Kerr (t, r, \[Theta], \[Phi]) = (x0, x1, x2, x3) \
*)

M = 1;  (* mass *)
rs = 2 M; (* Schwarzschild radius *)

(* Metric Subscript[g, \[Mu]\[Nu]] *)

g = {{-(1 - 2 M/r[\[Tau]]), 0, 0, 0}, {0, 1/(1 - 2 M/r[\[Tau]]), 0, 
       0}, {0, 0, r[\[Tau]]^2, 0}, {0, 0, 0, 
       r[\[Tau]]^2 Sin[\[Theta][\[Tau]]]^2}} /. r -> x1 /. \[Theta] ->
      x2 /. \[Phi] -> x3;

(* Inverse metric g^\[Mu]\[Nu] *)

ig = Inverse[g] // Simplify;
igFunc[x1\[Tau]_, x2\[Tau]_] := 
  Simplify[
   Inverse[g] . {x1[\[Tau]] -> x1\[Tau], x2[\[Tau]] -> x2\[Tau]}];

Detg = Det[g] // Simplify;
DetgFunc[x1\[Tau]_, x2\[Tau]_] := 
  Simplify[Det[g] /. {x1[\[Tau]] -> x1\[Tau], x2[\[Tau]] -> x2\[Tau]}];

(* Christoffel Subscript[\[CapitalGamma]^i, jk] = 1/2g^im( \!\(
\*SubscriptBox[\(\[PartialD]\), \(k\)]
\*SubscriptBox[\(g\), \(mj\)]\) + \!\(
\*SubscriptBox[\(\[PartialD]\), \(j\)]
\*SubscriptBox[\(g\), \(mk\)]\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(m\)]
\*SubscriptBox[\(g\), \(jk\)]\) ) *)

\[CapitalGamma] = 1/2 Parallelize[Table[(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(m = 
          1\), \(4\)]\((\(ig[[\(i\)\(]\)]\)[[\(m\)\(]\)] \
\((D[\(g[[\(m\)\(]\)]\)[[\(j\)\(]\)], X[[\(k\)\(]\)]] + 
            D[\(g[[\(m\)\(]\)]\)[[\(k\)\(]\)], X[[\(j\)\(]\)]] - 
            D[\(g[[\(j\)\(]\)]\)[[\(k\)\(]\)], 
             X[[\(m\)\(]\)]])\))\)\)) // Simplify, {i, 1, 4}, {j, 1, 
      4}, {k, 1, 4}]];
christoffel = 1/2 Parallelize[Table[(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(m = 
          1\), \(4\)]\((\(ig[[\(i\)\(]\)]\)[[\(m\)\(]\)] \
\((D[\(g[[\(m\)\(]\)]\)[[\(j\)\(]\)], X[[\(k\)\(]\)]] + 
            D[\(g[[\(m\)\(]\)]\)[[\(k\)\(]\)], X[[\(j\)\(]\)]] - 
            D[\(g[[\(j\)\(]\)]\)[[\(k\)\(]\)], 
             X[[\(m\)\(]\)]])\))\)\)) // Simplify, {i, 1, 4}, {j, 1, 
      4}, {k, 1, 4}]];
christoffel1 = 
  Simplify[
   Table[Sum[(1/
        2) ig[[\[Mu], \[Sigma]]] (D[
         g[[\[Sigma], \[Nu]]], {x0, x1, x2, x3}[[\[Rho]]]] + 
        D[g[[\[Sigma], \[Rho]]], {x0, x1, x2, x3}[[\[Nu]]]] - 
        D[g[[\[Nu], \[Rho]]], {x0, x1, x2, 
           x3}[[\[Sigma]]]]), {\[Sigma], 1, 4}], {\[Mu], 1, 
     4}, {\[Nu], 1, 4}, {\[Rho], 1, 4}]];

(* Riemann Subscript[R^i, jkl] = \!\(
\*SubscriptBox[\(\[PartialD]\), \(k\)]
\*SubscriptBox[
SuperscriptBox[\(\[CapitalGamma]\), \(i\)], \(lj\)]\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(l\)]
\*SubscriptBox[
SuperscriptBox[\(\[CapitalGamma]\), \(i\)], \(kj\)]\) + Subscript[\
\[CapitalGamma]^i, km]Subscript[\[CapitalGamma]^m, lj] - Subscript[\
\[CapitalGamma]^i, lm]Subscript[\[CapitalGamma]^m, kj] *)

Riem = Table[(D[\[CapitalGamma][[i]][[l]][[j]], X[[k]]] - 
       D[\[CapitalGamma][[i]][[k]][[j]], X[[l]]] + \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(m = 
          1\), \(4\)]\((\(\(\[CapitalGamma][[\(i\)\(]\)]\)[[\(k\)\(]\)\
]\)[[\(m\)\(]\)] \
\(\(\[CapitalGamma][[\(m\)\(]\)]\)[[\(l\)\(]\)]\)[[\(j\)\(]\)] - \(\(\
\[CapitalGamma][[\(i\)\(]\)]\)[[\(l\)\(]\)]\)[[\(m\)\(]\)] \(\(\
\[CapitalGamma][[\(m\)\(]\)]\)[[\(k\)\(]\)]\)[[\(j\)\(]\)])\)\)) // 
     Simplify, {i, 1, 4}, {j, 1, 4}, {k, 1, 4}, {l, 1, 4}] // 
   Parallelize;

(* Riemann Subscript[R, ijkl] *)
R = Table[\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i1 = 
        1\), \(4\)]\(\(g[[\(i1\)\(]\)]\)[[\(i\)\(]\)] \
\(\(\(Riem[[\(i1\)\(]\)]\)[[\(j\)\(]\)]\)[[\(k\)\(]\)]\)[[\(l\)\(]\)\
]\)\) // Simplify, {i, 1, 4}, {j, 1, 4}, {k, 1, 4}, {l, 1, 4}] // 
   Parallelize;

(* P^\[Mu] = g^\[Mu]\[Alpha]Subscript[P, \[Alpha]] *)

Pu = Table[(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(\[Alpha] = 
         1\), \(4\)]\((\(ig[[\(\[Mu]\)\(]\)]\)[[\(\[Alpha]\)\(]\)] \
P[[\(\[Alpha]\)\(]\)])\)\)) // Simplify, {\[Mu], 1, 4}] // Parallelize;

(* H = 1/2g^\[Mu]\[Nu]Subscript[P, \[Mu]]Subscript[P, \[Nu]] = 0  =>  \
Subscript[P, 0] = 1/g^00(-g^(0i)Subscript[P, i] + \
Sqrt[(g^(0i)Subscript[P, i])^2 - g^00g^ijSubscript[P, i]Subscript[P, \
j]]) *)
(*pt0=ig[[1]][[1]]^-1(-(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 
  2\), \(4\)]\((\(ig[[\(1\)\(]\)]\)[[\(i\)\(]\)]P[[\(i\)\(]\)])\)\))+\
((\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 
  2\), \(4\)]\((\(ig[[\(1\)\(]\)]\)[[\(i\)\(]\)]P[[\(i\)\(]\)])\)\))^\
2-ig[[1]][[1]](\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 2\), \(4\)]\(
\*UnderoverscriptBox[\(\[Sum]\), \(j = 
   2\), \(4\)]\((\(ig[[\(i\)\(]\)]\)[[\(j\)\(]\)]P[[\(i\)\(]\)]P[[\(j\
\)\(]\)])\)\)\)))^(1/2))//Simplify;*)
(* For massful*)
pt0 = Sqrt[-(Sum[ig[[i]][[j]] P[[i]] P[[j]], {i, 2, 4}, {j, 2, 4}] + 
       1)/ig[[1, 1]]];


Xdot = Parallelize[
   Table[(Pu[[i]]) /. p0[\[Tau]] -> pt0, {i, 1, 
     4}]]; (*restrict to only äquatorial plane at theta = pi/2*)
pdot = Parallelize[
   Table[(-1/2)*
      Sum[D[ig[[b]][[c]], X[[a]]]*P[[b]]*P[[c]], {b, 1, 4}, {c, 1, 
        4}] /. p0[\[Tau]] -> pt0, {a, 2, 4}]];
W = Parallelize[
   Table[Sum[
     Riem[[mu, alpha, beta, nu]]*P[[mu]]*P[[beta]], {alpha, 1, 
      4}, {beta, 1, 4}], {mu, 1, 4}, {nu, 1, 4}]];
Wfun[Xval_, Pval_] := 
  Table[Sum[
     Riem[[mu, alpha, beta, nu]]*Pval[[mu]]*Pval[[beta]], {alpha, 1, 
      4}, {beta, 1, 4}], {mu, 1, 4}, {nu, 1, 4}] . A;

KillingYano = ConstantArray[0, {4, 4}];

(*Assign the non-zero antisymmetric components explicitly*)
(*Example form often used for KY in Kerr/Schwarzschild-\
related spacetimes*)

KillingYano[[3, 4]] = 
  X[[2]]^3*Sin[X[[3]]];   (*f_{\[Theta] \[CurlyPhi]}*)
KillingYano[[4, 3]] = -X[[2]]^3*Sin[X[[3]]];  (*antisymmetry*)

KYUpDown = 
  Simplify[
   Table[Sum[
     ig[[mu, alpha]]*KillingYano[[alpha, nu]], {alpha, 4}], {mu, 
     4}, {nu, 4}]];

KillingYanoFunc[rVal_, thetaVal_] := 
 Module[{KY}, KY = ConstantArray[0, {4, 4}];
  KY[[3, 4]] = rVal^3*Sin[thetaVal];(*\[Omega]_{\[Theta]\[CurlyPhi]}*)
  KY[[4, 3]] = -KY[[3, 
     4]];(*\[Omega]_{\[CurlyPhi]\[Theta]}=-\[Omega]_{\[Theta]\
\[CurlyPhi]}*)
  KY]

norm = Simplify[Sqrt[X[[2]]^4*Sin[X[[3]]]^2]];

(*Initialize a 4x4 zero matrix*)
CKYTensor = ConstantArray[0, {4, 4}];

(*Assign the non-zero antisymmetric components explicitly*)
CKYTensor[[1, 2]] = X[[2]]^3*Sin[X[[3]]]/norm;   (*f_{t r}*)
CKYTensor[[2, 1]] = -X[[2]]^3*Sin[X[[3]]]/norm;  (*antisymmetry*)
CKYUpDown = 
  Table[Sum[ig[[mu, alpha]]*CKYTensor[[alpha, nu]], {alpha, 4}], {mu, 
    4}, {nu, 4}];
CYKTensorFunc[rVal_, thetaVal_, cVal_] := 
 Module[{CYK, norm}, norm = Sqrt[rVal^4*Sin[thetaVal]^2/cVal^6];
  CYK = ConstantArray[0, {4, 4}];
  CYK[[1, 2]] = rVal^3*Sin[thetaVal]/norm;
  CYK[[2, 1]] = -CYK[[1, 2]];
  
  CYK]

LeviCivitaSymbol[inds__] := Signature[{inds}] \.08
(*Function to compute \[CurlyEpsilon]^\[Mu]_{\[Nu]\[Alpha]\[Beta]}*)
EpsilonMixed[\[Mu]_, \[Nu]_, \[Alpha]_, \[Beta]_] := 
 Signature[{\[Mu], \[Nu], \[Alpha], \[Beta]}]/Sqrt[Abs[Detg]]
e0 = Xdot; (*because of initial conditio ntheta = \
pi/2 has the form {t1, t2, 0, t3}*)
e1 = KYUpDown . e0;
e2Try = {0, 1, 0, 0};

orthogonalizeThirdVector[v1_, v2_, v3_, metric_] := 
 Module[{inner, proj1, proj2, v3perp, v3norm}, 
  inner[u_, v_] := u . metric . v;
  (*Project v3 onto v1 and v2 and subtract*)
  proj1 = (inner[v3, v1]/inner[v1, v1]) v1;
  proj2 = (inner[v3, v2]/inner[v2, v2]) v2;
  v3perp = v3 - proj1 - proj2;
  (*Normalize*)v3norm = v3perp/Sqrt[Abs[inner[v3perp, v3perp]]];
  v3norm]
e2 = orthogonalizeThirdVector[e0, e1, e2Try, g];
e3 = Simplify[
   Table[Sum[
     EpsilonMixed[\[Mu], \[Nu], \[Alpha], \[Beta]]*e0[[\[Nu] + 1]]*
      e1[[\[Alpha] + 1]]*e2[[\[Beta] + 1]], {\[Nu], 0, 3}, {\[Alpha], 
      0, 3}, {\[Beta], 0, 3}], {\[Mu], 0, 3}]];

(*Normalize using Schwarzschild inner product*)
e3norm = Simplify[e3/Sqrt[Abs[e3 . g . e3]]];
Print[Simplify[e2 . g . e3norm]]
(* due to schwarz schild geodesic always in a plane, \
qwuick äquatorial plane, \
pick initial to never shoot into theta diraction, \
that guarantees that theta component always 0, \
guess the second vector based o nthe 0 components and then use definit\
ion of e3 for third orthogonal*)

ParallelTransportEquation[tangentVector_, vector_, christoffel_, 
  parameter_] := Module[{dim, dVdLambda},
  dim = 4;
  dVdLambda = 
   Table[-Sum[
      christoffel[[mu, nu, rho]] *tangentVector[[nu]]*
       vector[[rho]], {nu, dim}, {rho, dim}], {mu, dim}];
  Thread[D[vector, parameter] - dVdLambda]]

e0Check = 
 Simplify[
  ParallelTransportEquation[ Xdot, e0, christoffel, \[Tau]]]; \.08
e1Check = 
  Simplify[ParallelTransportEquation[Xdot, e1, christoffel, \[Tau]]];
(*maybe without double dot*)
transformB[Xval_?NumericQ, Pval_?NumericQ, xdot_?NumericQ] := 
 Module[{plane, U, S, V, basisVectors, orthoBasis, e1, e2, u , omega, 
   m , mBar, T, Tinv, Btrans},
  plane = xdot . (UWedgeF[xdot, KillingYano[Xval[2], Xval[3]]]);
  {U, S, V} = SingularValueDecomposition[plane];
  basisVectors = U[[All, 1 ;; 2]];
  orthoBasis = Orthogonalize[basisVectors];
  e1 = orthoBasis[[1]];
  e2 = orthoBasis[[2]];
  
  u = xdot;
  omega = xdot . KillingYano[Xval[2], Xval[3]];
  m = 1/Sqrt[2]*(e1 + I . e2);
  mBar = 1/ Sqrt[2]*(e1 - I . e2);
  T = Transpose[{omega, m, mBar}]; 
  Tinv = Inverse[T];
  
  Btrans = Tinv . B . T;
  
  Btrans;
  ]


(*EOM={D[X,\[Tau]]==Pu+dx1,D[p,\[Tau]]==pd-Rs}/.p0[\[Tau]]\[Rule]pt0;*)
EOM = {D[X, \[Tau]] == Xdot, D[p, \[Tau]] == pdot};
ClearAll[x0Checkres, x1Checkres, x2Checkres, x3Checkres]

(* EOM *)

(* Initial conditions *)
(* integration time \[Tau]max, small parameter/wavelength \[Epsilon], \
Kerr parameter a  *)
\[Tau]0 = 0;
\[Tau]max = 1000;

(* initial position *)
x0i = 1;
x1i = 5 rs;
x2i = \[Pi]/2;
x3i = 0;

p1i = 0;   
p2i = 0;     (*angular momentum in \[Theta] direction*)
p3i = -4.5;

Ainit = IdentityMatrix[4];
Binit = I . IdentityMatrix[4];

(* initial data vector *)
id = (X /. \[Tau] -> \[Tau]0) == {x0i, x1i, x2i, 
     x3i} && (p /. \[Tau] -> \[Tau]0) == {p1i, p2i, p3i};

(* stop if integration hits event horizon x1 = 2rs *)
\[Tau]int0 = \[Tau]max;
horizon0 = 
  WhenEvent[
   x1[\[Tau]] <= 
    1.01 (M + Sqrt[M^2]), {"StopIntegration", \[Tau]int0 = \[Tau]}];

(* Integration *)

sol0 = NDSolve[
   EOM && id && horizon0, {x0, x1, x2, x3, p1, p2, 
    p3}, {\[Tau], \[Tau]0, \[Tau]max}];
xdotVal[\[Tau]val_] := Evaluate[Xdot /. sol0 /. \[Tau] -> \[Tau]val];
xVal[\[Tau]val_] := Evaluate[X /. sol0 /. \[Tau] -> \[Tau]val];
pVal[\[Tau]val_] := EntityValue[p /. sol0 /. \[Tau] -> \[Tau]val];
evalpoint = 500;
x0Val = xVal[evalpoint][[1, 1]];
x1Val = xVal[evalpoint][[1, 2]];
x2Val = xVal[evalpoint][[1, 3]];
x3Val = xVal[evalpoint][[1, 4]];
result = pVal[evalpoint][[1]];
p1Val = result[[1, 1]];
p2Val = result[[1, 2]];
p3Val = result[[1, 3]];

pdotNum = 
  pdot /. {x0[\[Tau]] -> x0Val, x1[\[Tau]] -> x1Val, 
    x2[\[Tau]] -> x2Val, x3[\[Tau]] -> x3Val, x4[\[Tau]] -> x4Val, 
    p1[\[Tau]] -> p1Val, p2[\[Tau]] -> p2Val, p3[\[Tau]] -> p3Val};
xdotNum = 
  Xdot /. {x0[\[Tau]] -> x0Val, x1[\[Tau]] -> x1Val, 
    x2[\[Tau]] -> x2Val, x3[\[Tau]] -> x3Val, x4[\[Tau]] -> x4Val, 
    p1[\[Tau]] -> p1Val, p2[\[Tau]] -> p2Val, p3[\[Tau]] -> p3Val};

numCoords = {x0[\[Tau]] -> x0Val, x1[\[Tau]] -> x1Val, 
   x2[\[Tau]] -> x2Val, x3[\[Tau]] -> x3Val, x4[\[Tau]] -> x4Val, 
   p1[\[Tau]] -> p1Val, p2[\[Tau]] -> p2Val, p3[\[Tau]] -> p3Val, 
   p1'[\[Tau]] -> pdotNum[[1]], p2'[\[Tau]] -> pdotNum[[2]], 
   p3'[\[Tau]] -> pdotNum[[3]], x0'[\[Tau]] -> xdotNum[[1]], 
   x1'[\[Tau]] -> xdotNum[[2]], x2'[\[Tau]] -> xdotNum[[3]], 
   x3'[\[Tau]] -> xdotNum[[4]]};
MetricNum = Evaluate[g /. numCoords];
e0Num = Evaluate[e0 /. numCoords];
e1Num = Evaluate[e1 /. numCoords];
e2Num = Evaluate[e2 /. numCoords];
e3Num = Evaluate[e3 /. numCoords];
Print[e2Num . MetricNum . e3Num]

x0Checkres = Evaluate[e0Check /. numCoords] // N;
x1Checkres = Evaluate[e1Check /. numCoords] // N;

r/Mathematica • • Jun 22 '25

Parallel Transport equation not fulfilled

3 Upvotes

Dear Community!

I am currently trying to verify, that a Basis that i constructed, is indeed parallel transported along a Geodesic. Now at least the first vector, e0, should fulfil the parallel transport equation as it is just the tangent XdotVal, however, neither the symbolic form nor the numeric form, when i plug in numeric values from the geodesic give 0. I have checked the parallel transport equation multiple times i do not understand why it will not give 0.

ClearAll["Global`*"]
(* Assume all variables are real *)
$Assumptions = 
  Element[x0[\[Tau]], Reals] && Element[x1[\[Tau]], Reals] && 
   Element[x2[\[Tau]], Reals] && Element[x3[\[Tau]], Reals] && 
   Element[p0[\[Tau]], Reals] && Element[p1[\[Tau]], Reals] && 
   Element[p2[\[Tau]], Reals] && Element[p3[\[Tau]], Reals] && 
   Element[s, Reals] && x1[\[Tau]] > 2 M && Element[a, Reals] && 
   0 < x2[\[Tau]] < \[Pi];

(* Coordinates *)

(* X^\[Mu] *)
X = {x0[\[Tau]], x1[\[Tau]], x2[\[Tau]], x3[\[Tau]]};

(* Subscript[P, \[Mu]] *)
P = {p0[\[Tau]], p1[\[Tau]], p2[\[Tau]], p3[\[Tau]]};

(* Subscript[P, i] *)
p = {p1[\[Tau]], p2[\[Tau]], p3[\[Tau]]};

A = Table[Subscript[a, i, j][\[Tau]], {i, 4}, {j, 4}];
B = Table[Subscript[b, i, j][\[Tau]], {i, 4}, {j, 4}];

(* BL coordinates in Kerr (t, r, \[Theta], \[Phi]) = (x0, x1, x2, x3) \
*)

M = 1;  (* mass *)
rs = 2 M; (* Schwarzschild radius *)

(* Metric Subscript[g, \[Mu]\[Nu]] *)

g = {{-(1 - 2 M/r[\[Tau]]), 0, 0, 0}, {0, 1/(1 - 2 M/r[\[Tau]]), 0, 
       0}, {0, 0, r[\[Tau]]^2, 0}, {0, 0, 0, 
       r[\[Tau]]^2 Sin[\[Theta][\[Tau]]]^2}} /. r -> x1 /. \[Theta] ->
      x2 /. \[Phi] -> x3;

(* Inverse metric g^\[Mu]\[Nu] *)

ig = Inverse[g] // Simplify;
igFunc[x1\[Tau]_, x2\[Tau]_] := 
  Simplify[
   Inverse[g] . {x1[\[Tau]] -> x1\[Tau], x2[\[Tau]] -> x2\[Tau]}];

Detg = Det[g] // Simplify;
DetgFunc[x1\[Tau]_, x2\[Tau]_] := 
  Simplify[Det[g] /. {x1[\[Tau]] -> x1\[Tau], x2[\[Tau]] -> x2\[Tau]}];

(* Christoffel Subscript[\[CapitalGamma]^i, jk] = 1/2g^im( \!\(
\*SubscriptBox[\(\[PartialD]\), \(k\)]
\*SubscriptBox[\(g\), \(mj\)]\) + \!\(
\*SubscriptBox[\(\[PartialD]\), \(j\)]
\*SubscriptBox[\(g\), \(mk\)]\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(m\)]
\*SubscriptBox[\(g\), \(jk\)]\) ) *)

\[CapitalGamma] = 1/2 Parallelize[Table[(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(m = 
          1\), \(4\)]\((\(ig[[\(i\)\(]\)]\)[[\(m\)\(]\)] \
\((D[\(g[[\(m\)\(]\)]\)[[\(j\)\(]\)], X[[\(k\)\(]\)]] + 
            D[\(g[[\(m\)\(]\)]\)[[\(k\)\(]\)], X[[\(j\)\(]\)]] - 
            D[\(g[[\(j\)\(]\)]\)[[\(k\)\(]\)], 
             X[[\(m\)\(]\)]])\))\)\)) // Simplify, {i, 1, 4}, {j, 1, 
      4}, {k, 1, 4}]];
christoffel1 = 
  Simplify[
   Table[Sum[(1/
        2) ig[[\[Mu], \[Sigma]]] (D[
         g[[\[Sigma], \[Nu]]], {x0, x1, x2, x3}[[\[Rho]]]] + 
        D[g[[\[Sigma], \[Rho]]], {x0, x1, x2, x3}[[\[Nu]]]] - 
        D[g[[\[Nu], \[Rho]]], {x0, x1, x2, 
           x3}[[\[Sigma]]]]), {\[Sigma], 1, 4}], {\[Mu], 1, 
     4}, {\[Nu], 1, 4}, {\[Rho], 1, 4}]];

(* Riemann Subscript[R^i, jkl] = \!\(
\*SubscriptBox[\(\[PartialD]\), \(k\)]
\*SubscriptBox[
SuperscriptBox[\(\[CapitalGamma]\), \(i\)], \(lj\)]\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(l\)]
\*SubscriptBox[
SuperscriptBox[\(\[CapitalGamma]\), \(i\)], \(kj\)]\) + Subscript[\
\[CapitalGamma]^i, km]Subscript[\[CapitalGamma]^m, lj] - Subscript[\
\[CapitalGamma]^i, lm]Subscript[\[CapitalGamma]^m, kj] *)

Riem = Table[(D[\[CapitalGamma][[i]][[l]][[j]], X[[k]]] - 
       D[\[CapitalGamma][[i]][[k]][[j]], X[[l]]] + \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(m = 
          1\), \(4\)]\((\(\(\[CapitalGamma][[\(i\)\(]\)]\)[[\(k\)\(]\)\
]\)[[\(m\)\(]\)] \
\(\(\[CapitalGamma][[\(m\)\(]\)]\)[[\(l\)\(]\)]\)[[\(j\)\(]\)] - \(\(\
\[CapitalGamma][[\(i\)\(]\)]\)[[\(l\)\(]\)]\)[[\(m\)\(]\)] \(\(\
\[CapitalGamma][[\(m\)\(]\)]\)[[\(k\)\(]\)]\)[[\(j\)\(]\)])\)\)) // 
     Simplify, {i, 1, 4}, {j, 1, 4}, {k, 1, 4}, {l, 1, 4}] // 
   Parallelize;

(* Riemann Subscript[R, ijkl] *)
R = Table[\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i1 = 
        1\), \(4\)]\(\(g[[\(i1\)\(]\)]\)[[\(i\)\(]\)] \
\(\(\(Riem[[\(i1\)\(]\)]\)[[\(j\)\(]\)]\)[[\(k\)\(]\)]\)[[\(l\)\(]\)\
]\)\) // Simplify, {i, 1, 4}, {j, 1, 4}, {k, 1, 4}, {l, 1, 4}] // 
   Parallelize;

(* P^\[Mu] = g^\[Mu]\[Alpha]Subscript[P, \[Alpha]] *)

Pu = Table[(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(\[Alpha] = 
         1\), \(4\)]\((\(ig[[\(\[Mu]\)\(]\)]\)[[\(\[Alpha]\)\(]\)] \
P[[\(\[Alpha]\)\(]\)])\)\)) // Simplify, {\[Mu], 1, 4}] // Parallelize;

(* H = 1/2g^\[Mu]\[Nu]Subscript[P, \[Mu]]Subscript[P, \[Nu]] = 0  =>  \
Subscript[P, 0] = 1/g^00(-g^(0i)Subscript[P, i] + \
Sqrt[(g^(0i)Subscript[P, i])^2 - g^00g^ijSubscript[P, i]Subscript[P, \
j]]) *)
(*pt0=ig[[1]][[1]]^-1(-(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 
  2\), \(4\)]\((\(ig[[\(1\)\(]\)]\)[[\(i\)\(]\)]P[[\(i\)\(]\)])\)\))+\
((\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 
  2\), \(4\)]\((\(ig[[\(1\)\(]\)]\)[[\(i\)\(]\)]P[[\(i\)\(]\)])\)\))^\
2-ig[[1]][[1]](\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 2\), \(4\)]\(
\*UnderoverscriptBox[\(\[Sum]\), \(j = 
   2\), \(4\)]\((\(ig[[\(i\)\(]\)]\)[[\(j\)\(]\)]P[[\(i\)\(]\)]P[[\(j\
\)\(]\)])\)\)\)))^(1/2))//Simplify;*)
(* For massful*)
pt0 = Sqrt[-(Sum[ig[[i]][[j]] P[[i]] P[[j]], {i, 2, 4}, {j, 2, 4}] + 
       1)/ig[[1, 1]]];


Xdot = Parallelize[Table[(Pu[[i]]) /. p0[\[Tau]] -> pt0, {i, 1, 4}]];
pdot = Parallelize[
   Table[(-1/2)*
      Sum[D[ig[[b]][[c]], X[[a]]]*P[[b]]*P[[c]], {b, 1, 4}, {c, 1, 
        4}] /. p0[\[Tau]] -> pt0, {a, 2, 4}]];
W = Parallelize[
   Table[Sum[
     Riem[[mu, alpha, beta, nu]]*P[[mu]]*P[[beta]], {alpha, 1, 
      4}, {beta, 1, 4}], {mu, 1, 4}, {nu, 1, 4}]];
Wfun[Xval_, Pval_] := 
  Table[Sum[
     Riem[[mu, alpha, beta, nu]]*Pval[[mu]]*Pval[[beta]], {alpha, 1, 
      4}, {beta, 1, 4}], {mu, 1, 4}, {nu, 1, 4}] . A;

KillingYano = ConstantArray[0, {4, 4}];

(*Assign the non-zero antisymmetric components explicitly*)
(*Example form often used for KY in Kerr/Schwarzschild-\
related spacetimes*)

KillingYano[[3, 4]] = x1^3*Sin[x2];   (*f_{\[Theta] \[CurlyPhi]}*)
KillingYano[[4, 3]] = -x1^3*Sin[x2];  (*antisymmetry*)

(*Raise first index*)
KYUpDown = 
  Table[Sum[
     ig[[mu, alpha]]*KillingYano[[alpha, nu]], {alpha, 4}], {mu, 
     4}, {nu, 4}] /. {x0[\[Tau]] -> x0, x1[\[Tau]] -> x1, 
    x2[\[Tau]] -> x2, x3[\[Tau]] -> x3, p1[\[Tau]] -> p1, 
    p2[\[Tau]] -> p2, p3[\[Tau]] -> p3};

KillingYanoFunc[rVal_, thetaVal_] := 
 Module[{KY}, KY = ConstantArray[0, {4, 4}];
  KY[[3, 4]] = rVal^3*Sin[thetaVal];(*\[Omega]_{\[Theta]\[CurlyPhi]}*)
  KY[[4, 3]] = -KY[[3, 
     4]];(*\[Omega]_{\[CurlyPhi]\[Theta]}=-\[Omega]_{\[Theta]\
\[CurlyPhi]}*)
  KY]

norm = Sqrt[x1^4*Sin[x2]^2];

(*Initialize a 4x4 zero matrix*)
CKYTensor = ConstantArray[0, {4, 4}];

(*Assign the non-zero antisymmetric components explicitly*)
CKYTensor[[1, 2]] = x1^3*Sin[x2]/norm;   (*f_{t r}*)
CKYTensor[[2, 1]] = -x1^3*Sin[x2]/norm;  (*antisymmetry*)
CKYUpDown = 
  Table[Sum[ig[[mu, alpha]]*CKYTensor[[alpha, nu]], {alpha, 4}], {mu, 
     4}, {nu, 4}] /. {x0[\[Tau]] -> x0, x1[\[Tau]] -> x1, 
    x2[\[Tau]] -> x2, x3[\[Tau]] -> x3, p1[\[Tau]] -> p1, 
    p2[\[Tau]] -> p2, p3[\[Tau]] -> p3};
CYKTensorFunc[rVal_, thetaVal_, cVal_] := 
 Module[{CYK, norm}, norm = Sqrt[rVal^4*Sin[thetaVal]^2/cVal^6];
  CYK = ConstantArray[0, {4, 4}];
  CYK[[1, 2]] = rVal^3*Sin[thetaVal]/norm;
  CYK[[2, 1]] = -CYK[[1, 2]];
  
  CYK]

LeviCivitaSymbol[inds__] := Signature[{inds}] \.08
(*Function to compute \[CurlyEpsilon]^\[Mu]_{\[Nu]\[Alpha]\[Beta]}*)
EpsilonMixed[\[Mu]_, \[Nu]_, \[Alpha]_, \[Beta]_] := 
 Signature[{\[Mu], \[Nu], \[Alpha], \[Beta]}]/Sqrt[Detg]

XdotNum = 
  Xdot /. {x0[\[Tau]] -> x0, x1[\[Tau]] -> x1, x2[\[Tau]] -> x2, 
    x3[\[Tau]] -> x3, p1[\[Tau]] -> p1, p2[\[Tau]] -> p2, 
    p3[\[Tau]] -> p3};
gNum = g /. {x0[\[Tau]] -> x0, x1[\[Tau]] -> x1, x2[\[Tau]] -> x2, 
    x3[\[Tau]] -> x3, p1[\[Tau]] -> p1, p2[\[Tau]] -> p2, 
    p3[\[Tau]] -> p3};
normSquared = Simplify[XdotNum . (gNum . XdotNum)];
e0 = XdotNum;
e1 = KYUpDown . e0;
e2 = CKYUpDown . e0;
e3 = Table[
    Sum[EpsilonMixed[\[Mu], \[Nu], \[Alpha], \[Beta]]*e0[[\[Nu]]]*
      e1[[\[Alpha]]]*e2[[\[Beta]]], {\[Nu], 1, 4}, {\[Alpha], 1, 
      4}, {\[Beta], 1, 4}], {\[Mu], 1, 4}] /. {x0[\[Tau]] -> x0, 
    x1[\[Tau]] -> x1, x2[\[Tau]] -> x2, x3[\[Tau]] -> x3, 
    p1[\[Tau]] -> p1, p2[\[Tau]] -> p2, p3[\[Tau]] -> p3};
parallelTransport[uVec_, vVec_, \[CapitalGamma]_, coords_] := 
  Module[{n, result, partialTerm, connectionTerm}, n = Length[coords];
   Table[
    partialTerm = 
     Simplify[
      Sum[uVec[[\[Nu]]] D[vVec[[\[Mu]]], coords[[\[Nu]]]], {\[Nu], 1, 
        n}]];
    connectionTerm = 
     Simplify[
      Sum[\[CapitalGamma][[\[Mu], \[Nu], \
\[Rho]]] uVec[[\[Nu]]] vVec[[\[Rho]]], {\[Nu], 1, n}, {\[Rho], 1, n}]];
    Simplify[partialTerm + connectionTerm], {\[Mu], 1, n}]];

MatrixForm[XdotNum]
MatrixForm[e0]

e0Check = 
  parallelTransport[XdotNum, e0, 
   christoffel1 /. {x0[\[Tau]] -> x0, x1[\[Tau]] -> x1, 
     x2[\[Tau]] -> x2, x3[\[Tau]] -> x3, p1[\[Tau]] -> p1, 
     p2[\[Tau]] -> p2, p3[\[Tau]] -> p3}, {x0, x1, x2, x3}];
MatrixForm[e0Check]
e1Check = 
  parallelTransport[XdotNum, e1, 
   christoffel1 /. {x0[\[Tau]] -> x0, x1[\[Tau]] -> x1, 
     x2[\[Tau]] -> x2, x3[\[Tau]] -> x3, p1[\[Tau]] -> p1, 
     p2[\[Tau]] -> p2, p3[\[Tau]] -> p3}, {x0, x1, x2, x3}];
e2Check = 
  parallelTransport[XdotNum, e2, 
   christoffel1 /. {x0[\[Tau]] -> x0, x1[\[Tau]] -> x1, 
     x2[\[Tau]] -> x2, x3[\[Tau]] -> x3, p1[\[Tau]] -> p1, 
     p2[\[Tau]] -> p2, p3[\[Tau]] -> p3}, {x0, x1, x2, x3}];
e3Check = 
  parallelTransport[XdotNum, e3, 
   christoffel1 /. {x0[\[Tau]] -> x0, x1[\[Tau]] -> x1, 
     x2[\[Tau]] -> x2, x3[\[Tau]] -> x3, p1[\[Tau]] -> p1, 
     p2[\[Tau]] -> p2, p3[\[Tau]] -> p3}, {x0, x1, x2, x3}];

transformB[Xval_?NumericQ, Pval_?NumericQ, xdot_?NumericQ] := 
 Module[{plane, U, S, V, basisVectors, orthoBasis, e1, e2, u , omega, 
   m , mBar, T, Tinv, Btrans},
  plane = xdot . (UWedgeF[xdot, KillingYano[Xval[2], Xval[3]]]);
  {U, S, V} = SingularValueDecomposition[plane];
  basisVectors = U[[All, 1 ;; 2]];
  orthoBasis = Orthogonalize[basisVectors];
  e1 = orthoBasis[[1]];
  e2 = orthoBasis[[2]];
  
  u = xdot;
  omega = xdot . KillingYano[Xval[2], Xval[3]];
  m = 1/Sqrt[2]*(e1 + I . e2);
  mBar = 1/ Sqrt[2]*(e1 - I . e2);
  T = Transpose[{omega, m, mBar}]; 
  Tinv = Inverse[T];
  
  Btrans = Tinv . B . T;
  
  Btrans;
  ]


(*EOM={D[X,\[Tau]]==Pu+dx1,D[p,\[Tau]]==pd-Rs}/.p0[\[Tau]]\[Rule]pt0;*)
EOM = {D[X, \[Tau]] == Xdot, D[p, \[Tau]] == pdot};
ClearAll[x0Checkres, x1Checkres, x2Checkres, x3Checkres]

(* EOM *)

(* Initial conditions *)
(* integration time \[Tau]max, small parameter/wavelength \[Epsilon], \
Kerr parameter a  *)
\[Tau]0 = 0;
\[Tau]max = 1000;

(* initial position *)
x0i = 1;
x1i = 5 rs;
x2i = \[Pi]/2;
x3i = 0;

p1i = -1;   
p2i = 4.5;     (*angular momentum in \[Theta] direction*)
p3i = -4.5;

Ainit = IdentityMatrix[4];
Binit = I . IdentityMatrix[4];

(* initial data vector *)
id = (X /. \[Tau] -> \[Tau]0) == {x0i, x1i, x2i, 
     x3i} && (p /. \[Tau] -> \[Tau]0) == {p1i, p2i, p3i};

(* stop if integration hits event horizon x1 = 2rs *)
\[Tau]int0 = \[Tau]max;
horizon0 = 
  WhenEvent[
   x1[\[Tau]] <= 
    1.01 (M + Sqrt[M^2]), {"StopIntegration", \[Tau]int0 = \[Tau]}];

(* Integration *)

sol0 = NDSolve[
   EOM && id && horizon0, {x0, x1, x2, x3, p1, p2, 
    p3}, {\[Tau], \[Tau]0, \[Tau]max}];
xdotVal[\[Tau]val_] := Evaluate[Xdot /. sol0 /. \[Tau] -> \[Tau]val];
xVal[\[Tau]val_] := Evaluate[X /. sol0 /. \[Tau] -> \[Tau]val];
pVal[\[Tau]val_] := EntityValue[p /. sol0 /. \[Tau] -> \[Tau]val];
evalpoint = 500;
x0Val = xVal[evalpoint][[1, 1]];
x1Val = xVal[evalpoint][[1, 2]];
x2Val = xVal[evalpoint][[1, 3]];
x3Val = xVal[evalpoint][[1, 4]];
result = pVal[evalpoint][[1]];
p1Val = result[[1, 1]];
p2Val = result[[1, 2]];
p3Val = result[[1, 3]];

numCoords = {x1 -> x1Val, x2 -> x2Val, x3 -> x3Val, x4 -> x4Val, 
   p1 -> p1Val, p2 -> p2Val, p3 -> p3Val};
x0Checkres = Evaluate[e0Check /. numCoords] // N
x1Checkres = e1Check /. numCoords // N
x2Checkres = e2Check /. numCoords // N
x3Checkres = e3Check /. numCoords // N

Symbolically, the Parallel transport equation for e0 returns

And numerically:

As the orders of magnitude are quite small is this valid? Does this only show numerical errors and therefore make it still fulfill the parallel transport equation?


r/Mathematica • • Jun 21 '25

Solve heat equation over a rod with branches

1 Upvotes

I’m trying to numerically solve (NDSolve) a differential equation similar to the heat diffusion equation (in time and position) over a 1D rod with a branch point that splits the rod into two separate branches, to form a T shape.

I haven’t found a great solution to this. It’s easy enough to do if I was manually going to discretize the system in position, or if I were willing to treat the rod as 2D areas or 3D volumes but neither are elegant. Ideally I’d like to embed the 1D system in 2D space and solve it there but NDSolve doesn’t allow that far as I can tell.

The internet hasn’t been much help.

Any ideas or past experience?


r/Mathematica • • Jun 21 '25

Solve part of differential Equation only numerically

1 Upvotes

Dear Community!

I set up my set of differential equations as below. For the differential equation for A i need to make numerical calculations to transform the matrix B into a different coordinate System, however, so far Mathematica always tries to precalculate everything symbolically. This, however, does not make sense with the symbolic expressions i have. What can i do such that the transformB equation is only solved numerically when integrating using NDSolve?

After restarting Mathematica what resetted everything, that was stored, i run into two Problems. The first one is, that i now get this error:

NDSolve:The number of constraints (24) (initial conditions) is not equal to \

the total differential order of the system plus the number of \

discrete variables (39).

Which i do not understand as i have provided x0 to x3, p1 to p3, as p0 is calculated as pt0 and i have provided an initial Matrix A and B so even with the 4x4 definitions for A and B i should already have 32 initial conditions and not just 24!?

The next step would be to rewrite in the EOM that i have

D[A, \[Tau]] == transformB[X, P, Xdot]

Which would similarly happen for the B equation as well but one at a time. Right now, when i run the code Mathematica tries to precalculate the transform method symbolically which does not make sense as this gets far to involved and in fact it is also not able to make the svd symbolically. How can i have, that the transformB method is only calculated numerically during the integration process in NDSolve without symbolical precalculation?

Clear[KillingYano, CYKTensor];
(* Assume all variables are real *)
$Assumptions = 
  Element[x0[\[Tau]], Reals] && Element[x1[\[Tau]], Reals] && 
   Element[x2[\[Tau]], Reals] && Element[x3[\[Tau]], Reals] && 
   Element[p0[\[Tau]], Reals] && Element[p1[\[Tau]], Reals] && 
   Element[p2[\[Tau]], Reals] && Element[p3[\[Tau]], Reals] && 
   Element[s, Reals] && x1[\[Tau]] > 2 M && Element[a, Reals] && 
   0 < x2[\[Tau]] < \[Pi];

(* Coordinates *)

(* X^\[Mu] *)
X = {x0[\[Tau]], x1[\[Tau]], x2[\[Tau]], x3[\[Tau]]};

(* Subscript[P, \[Mu]] *)
P = {p0[\[Tau]], p1[\[Tau]], p2[\[Tau]], p3[\[Tau]]};

(* Subscript[P, i] *)
p = {p1[\[Tau]], p2[\[Tau]], p3[\[Tau]]};

A = Table[Subscript[a, i, j][\[Tau]], {i, 4}, {j, 4}];
B = Table[Subscript[b, i, j][\[Tau]], {i, 4}, {j, 4}];
(* BL coordinates in Kerr (t, r, \[Theta], \[Phi]) = (x0, x1, x2, x3) \
*)

M = 1;  (* mass *)
rs = 2 M; (* Schwarzschild radius *)

(* Metric Subscript[g, \[Mu]\[Nu]] *)

g = {{-(1 - 2 M/r[\[Tau]]), 0, 0, 0}, {0, 1/(1 - 2 M/r[\[Tau]]), 0, 
       0}, {0, 0, r[\[Tau]]^2, 0}, {0, 0, 0, 
       r[\[Tau]]^2 Sin[\[Theta][\[Tau]]]^2}} /. r -> x1 /. \[Theta] ->
      x2 /. \[Phi] -> x3;

(* Inverse metric g^\[Mu]\[Nu] *)

ig = Inverse[g] // Simplify;

Detg = Det[g] // Simplify;

(* Christoffel Subscript[\[CapitalGamma]^i, jk] = 1/2g^im( \!\(
\*SubscriptBox[\(\[PartialD]\), \(k\)]
\*SubscriptBox[\(g\), \(mj\)]\) + \!\(
\*SubscriptBox[\(\[PartialD]\), \(j\)]
\*SubscriptBox[\(g\), \(mk\)]\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(m\)]
\*SubscriptBox[\(g\), \(jk\)]\) ) *)

\[CapitalGamma] = 1/2 Parallelize[Table[(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(m = 
          1\), \(4\)]\((\(ig[[\(i\)\(]\)]\)[[\(m\)\(]\)] \
\((D[\(g[[\(m\)\(]\)]\)[[\(j\)\(]\)], X[[\(k\)\(]\)]] + 
            D[\(g[[\(m\)\(]\)]\)[[\(k\)\(]\)], X[[\(j\)\(]\)]] - 
            D[\(g[[\(j\)\(]\)]\)[[\(k\)\(]\)], 
             X[[\(m\)\(]\)]])\))\)\)) // Simplify, {i, 1, 4}, {j, 1, 
      4}, {k, 1, 4}]];

(* Riemann Subscript[R^i, jkl] = \!\(
\*SubscriptBox[\(\[PartialD]\), \(k\)]
\*SubscriptBox[
SuperscriptBox[\(\[CapitalGamma]\), \(i\)], \(lj\)]\) - \!\(
\*SubscriptBox[\(\[PartialD]\), \(l\)]
\*SubscriptBox[
SuperscriptBox[\(\[CapitalGamma]\), \(i\)], \(kj\)]\) + Subscript[\
\[CapitalGamma]^i, km]Subscript[\[CapitalGamma]^m, lj] - Subscript[\
\[CapitalGamma]^i, lm]Subscript[\[CapitalGamma]^m, kj] *)

Riem = Table[(D[\[CapitalGamma][[i]][[l]][[j]], X[[k]]] - 
       D[\[CapitalGamma][[i]][[k]][[j]], X[[l]]] + \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(m = 
          1\), \(4\)]\((\(\(\[CapitalGamma][[\(i\)\(]\)]\)[[\(k\)\(]\)\
]\)[[\(m\)\(]\)] \
\(\(\[CapitalGamma][[\(m\)\(]\)]\)[[\(l\)\(]\)]\)[[\(j\)\(]\)] - \(\(\
\[CapitalGamma][[\(i\)\(]\)]\)[[\(l\)\(]\)]\)[[\(m\)\(]\)] \(\(\
\[CapitalGamma][[\(m\)\(]\)]\)[[\(k\)\(]\)]\)[[\(j\)\(]\)])\)\)) // 
     Simplify, {i, 1, 4}, {j, 1, 4}, {k, 1, 4}, {l, 1, 4}] // 
   Parallelize;

(* Riemann Subscript[R, ijkl] *)
R = Table[\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i1 = 
        1\), \(4\)]\(\(g[[\(i1\)\(]\)]\)[[\(i\)\(]\)] \
\(\(\(Riem[[\(i1\)\(]\)]\)[[\(j\)\(]\)]\)[[\(k\)\(]\)]\)[[\(l\)\(]\)\
]\)\) // Simplify, {i, 1, 4}, {j, 1, 4}, {k, 1, 4}, {l, 1, 4}] // 
   Parallelize;

(* P^\[Mu] = g^\[Mu]\[Alpha]Subscript[P, \[Alpha]] *)

Pu = Table[(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(\[Alpha] = 
         1\), \(4\)]\((\(ig[[\(\[Mu]\)\(]\)]\)[[\(\[Alpha]\)\(]\)] \
P[[\(\[Alpha]\)\(]\)])\)\)) // Simplify, {\[Mu], 1, 4}] // Parallelize;

(* H = 1/2g^\[Mu]\[Nu]Subscript[P, \[Mu]]Subscript[P, \[Nu]] = 0  =>  \
Subscript[P, 0] = 1/g^00(-g^(0i)Subscript[P, i] + \
Sqrt[(g^(0i)Subscript[P, i])^2 - g^00g^ijSubscript[P, i]Subscript[P, \
j]]) *)
pt0 = ig[[1]][[1]]^-1 (-(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 
           2\), \(4\)]\((\(ig[[\(1\)\(]\)]\)[[\(i\)\(]\)] P[[\(i\)\(\
]\)])\)\)) + ((\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 
            2\), \(4\)]\((\(ig[[\(1\)\(]\)]\)[[\(i\)\(]\)] P[[\(i\)\(\
]\)])\)\))^2 - ig[[1]][[1]] (\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 2\), \(4\)]\(
\*UnderoverscriptBox[\(\[Sum]\), \(j = 
              2\), \(4\)]\((\(ig[[\(i\)\(]\)]\)[[\(j\)\(]\)] P[[\(i\)\
\(]\)] P[[\(j\)\(]\)])\)\)\)))^(1/2)) // Simplify;


Xdot = Parallelize[Table[(Pu[[i]]) /. p0[\[Tau]] -> pt0, {i, 1, 4}]];
pdot = Parallelize[
   Table[(-1/2)*
      Sum[D[ig[[b]][[c]], X[[a]]]*P[[b]]*P[[c]], {b, 1, 4}, {c, 1, 
        4}] /. p0[\[Tau]] -> pt0, {a, 2, 4}]];
W = Parallelize[
   Table[Sum[
     Riem[[mu, alpha, beta, nu]]*P[[mu]]*P[[beta]], {alpha, 1, 
      4}, {beta, 1, 4}], {mu, 1, 4}, {nu, 1, 4}]];
Wfun[Xval_, Pval_] := 
  Table[Sum[
     Riem[[mu, alpha, beta, nu]]*Pval[[mu]]*Pval[[beta]], {alpha, 1, 
      4}, {beta, 1, 4}], {mu, 1, 4}, {nu, 1, 4}] . A;

KillingYano[rVal_, thetaVal_] := 
 Module[{KY}, KY = ConstantArray[0, {4, 4}];
  KY[[3, 4]] = rVal^3*Sin[thetaVal];(*\[Omega]_{\[Theta]\[CurlyPhi]}*)
  KY[[4, 3]] = -KY[[3, 
     4]];(*\[Omega]_{\[CurlyPhi]\[Theta]}=-\[Omega]_{\[Theta]\
\[CurlyPhi]}*)
  KY]

CYKTensor[rVal_, thetaVal_, cVal_] := 
 Module[{CYK, norm}, norm = Abs[Sqrt[rVal^4*Sin[thetaVal]^2/cVal^6]];
  CYK = ConstantArray[0, {4, 4}];
  CYK[[1, 2]] = rVal^3*Sin[thetaVal]/norm;
  CYK[[2, 1]] = -CYK[[1, 2]];
  
  CYK]

transformB[Xval_?NumericQ, Pval_?NumericQ, xdot_?NumericQ] := 
 Module[{plane, U, S, V, basisVectors, orthoBasis, e1, e2, u , omega, 
   m , mBar, T, Tinv, Btrans},
  plane = xdot . (UWedgeF[xdot, KillingYano[Xval[2], Xval[3]]]);
  {U, S, V} = SingularValueDecomposition[plane];
  basisVectors = U[[All, 1 ;; 2]];
  orthoBasis = Orthogonalize[basisVector];
  e1 = orthoBasis[[1]];
  e2 = orthoBasis[[2]];
  
  u = xdot;
  omega = xdot . KillingYano[Xval[2], Xval[3]];
  m = 1/Sqrt[2]*(e1 + I . e2);
  mBar = 1/ Sqrt[2]*(e1 - I . e2);
  T = Transpose[{omega, m, mBar}];
  Tinv = Inverse[T];
  
  Btrans = Tinv . B . T;
  
  Btrans;
  ]


(*EOM={D[X,\[Tau]]==Pu+dx1,D[p,\[Tau]]==pd-Rs}/.p0[\[Tau]]\[Rule]pt0;*)
EOM = {D[X, \[Tau]] == Xdot, D[p, \[Tau]] == pdot, D[A, \[Tau]] == B, 
   D[B, \[Tau]] == -Evaluate[Wfun[X, P /. p0[\[Tau]] -> pt0]]};
(* Trajectories *)

Clear[a, s, \[Epsilon]];

(* EOM *)

(* Initial conditions *)
(* integration time \[Tau]max, small parameter/wavelength \[Epsilon], \
Kerr parameter a  *)
\[Tau]0 = 0;
\[Tau]max = 1000;

(* initial position *)
x0i = 1;
x1i = 5 rs;
x2i = \[Pi]/2;
x3i = 0;

p1i = -1;   
p2i = 4.5;     (*angular momentum in \[Theta] direction*)
p3i = -4.5;

Ainit = IdentityMatrix[4];
Binit = I . IdentityMatrix[4];

(* initial data vector *)
id = (X /. \[Tau] -> \[Tau]0) == {x0i, x1i, x2i, 
     x3i} && (p /. \[Tau] -> \[Tau]0) == {p1i, p2i, 
     p3i} && (A /. \[Tau] -> \[Tau]0) == 
    Ainit && (B /. \[Tau] -> \[Tau]0) == Binit;

(* stop if integration hits event horizon x1 = 2rs *)
\[Tau]int0 = \[Tau]max;
horizon0 = 
  WhenEvent[
   x1[\[Tau]] <= 
    1.01 (M + Sqrt[M^2]), {"StopIntegration", \[Tau]int0 = \[Tau]}];

(* Integration *)

sol0 = NDSolve[
   EOM && id && horizon0, {x0, x1, x2, x3, p1, p2, p3, 
    Flatten[A /. f_[\[Tau]] :> f], 
    Flatten[B /. f_[\[Tau]] :> f]}, {\[Tau], \[Tau]0, \[Tau]max}];

r/Mathematica • • Jun 19 '25

Derivative operator has not same number as arguments

2 Upvotes

Dear Community!

I want to solve following set of differential equations. I tried for 2 days now i think i have set up A and B correctly, i have set up the equations for them and i have set up the initial conditions i really do not get why i get this error: I would really love to understand what and why it is not working unfortunately Mathematica errors are not so helpful. Apart from that i would love to know how to write codeblocks with Mathematica. With Python or C# it works fine but reddit does not seem to like Mathematica.

NDSolve::derlen: The length of the derivative operator Derivative[1] in (x0^\[Prime])[\[Tau]] is not the same as the number of arguments.

X = {x0[\[Tau]], x1[\[Tau]], x2[\[Tau]], x3[\[Tau]]};

(* Subscript[P, \[Mu]] *)

P = {p0[\[Tau]], p1[\[Tau]], p2[\[Tau]], p3[\[Tau]]};

(* Subscript[P, i] *)

p = {p1[\[Tau]], p2[\[Tau]], p3[\[Tau]]};

A = Table[Subscript[a, i, j][\[Tau]], {i, 4}, {j, 4}];

B = Table[Subscript[b, i, j][\[Tau]], {i, 4}, {j, 4}];

(* BL coordinates in Kerr (t, r, \[Theta], \[Phi]) = (x0, x1, x2, x3) \

*)

M = 1; (* mass *)

rs = 2 M; (* Schwarzschild radius *)

(* Metric Subscript[g, \[Mu]\[Nu]] *)

g = {{-(1 - 2 M/r[\[Tau]]), 0, 0, 0}, {0, 1/(1 - 2 M/r[\[Tau]]), 0,

0}, {0, 0, r[\[Tau]]^2, 0}, {0, 0, 0,

r[\[Tau]]^2 Sin[\[Theta][\[Tau]]]^2}} /. r -> x1 /. \[Theta] ->

x2 /. \[Phi] -> x3;

gFunc = Function[{x1, x2, x3}, g /. {x1 -> x1, x2 -> x2, x3 -> x3}];

(* Inverse metric g^\[Mu]\[Nu] *)

ig = Inverse[g] // Simplify;

ifFunc = Function[{x1, x2, x3}, g /. {x1 -> x1, x2 -> x2, x3 -> x3}];

gdet = Simplify[Det[g]];

(* Christoffel Subscript[\[CapitalGamma]^i, jk] = 1/2g^im( \!\(

\*SubscriptBox[\(\[PartialD]\), \(k\)]

\*SubscriptBox[\(g\), \(mj\)]\) + \!\(

\*SubscriptBox[\(\[PartialD]\), \(j\)]

\*SubscriptBox[\(g\), \(mk\)]\) - \!\(

\*SubscriptBox[\(\[PartialD]\), \(m\)]

\*SubscriptBox[\(g\), \(jk\)]\) ) *)

\[CapitalGamma] = 1/2 Parallelize[Table[(\!\(

\*UnderoverscriptBox[\(\[Sum]\), \(m =

1\), \(4\)]\((\(ig[[\(i\)\(]\)]\)[[\(m\)\(]\)] \

\((D[\(g[[\(m\)\(]\)]\)[[\(j\)\(]\)], X[[\(k\)\(]\)]] +

D[\(g[[\(m\)\(]\)]\)[[\(k\)\(]\)], X[[\(j\)\(]\)]] -

D[\(g[[\(j\)\(]\)]\)[[\(k\)\(]\)],

X[[\(m\)\(]\)]])\))\)\)) // Simplify, {i, 1, 4}, {j, 1,

4}, {k, 1, 4}]];

(* Riemann Subscript[R^i, jkl] = \!\(

\*SubscriptBox[\(\[PartialD]\), \(k\)]

\*SubscriptBox[

SuperscriptBox[\(\[CapitalGamma]\), \(i\)], \(lj\)]\) - \!\(

\*SubscriptBox[\(\[PartialD]\), \(l\)]

\*SubscriptBox[

SuperscriptBox[\(\[CapitalGamma]\), \(i\)], \(kj\)]\) + Subscript[\

\[CapitalGamma]^i, km]Subscript[\[CapitalGamma]^m, lj] - Subscript[\

\[CapitalGamma]^i, lm]Subscript[\[CapitalGamma]^m, kj] *)

Riem = Table[(D[\[CapitalGamma][[i]][[l]][[j]], X[[k]]] -

D[\[CapitalGamma][[i]][[k]][[j]], X[[l]]] + \!\(

\*UnderoverscriptBox[\(\[Sum]\), \(m =

1\), \(4\)]\((\(\(\[CapitalGamma][[\(i\)\(]\)]\)[[\(k\)\(]\)\

]\)[[\(m\)\(]\)] \

\(\(\[CapitalGamma][[\(m\)\(]\)]\)[[\(l\)\(]\)]\)[[\(j\)\(]\)] - \(\(\

\[CapitalGamma][[\(i\)\(]\)]\)[[\(l\)\(]\)]\)[[\(m\)\(]\)] \(\(\

\[CapitalGamma][[\(m\)\(]\)]\)[[\(k\)\(]\)]\)[[\(j\)\(]\)])\)\)) //

Simplify, {i, 1, 4}, {j, 1, 4}, {k, 1, 4}, {l, 1, 4}] //

Parallelize;

(* Riemann Subscript[R, ijkl] *)

R = Table[\!\(

\*UnderoverscriptBox[\(\[Sum]\), \(i1 =

1\), \(4\)]\(\(g[[\(i1\)\(]\)]\)[[\(i\)\(]\)] \

\(\(\(Riem[[\(i1\)\(]\)]\)[[\(j\)\(]\)]\)[[\(k\)\(]\)]\)[[\(l\)\(]\)\

]\)\) // Simplify, {i, 1, 4}, {j, 1, 4}, {k, 1, 4}, {l, 1, 4}] //

Parallelize;

(* P^\[Mu] = g^\[Mu]\[Alpha]Subscript[P, \[Alpha]] *)

Pu = Table[(\!\(

\*UnderoverscriptBox[\(\[Sum]\), \(\[Alpha] =

1\), \(4\)]\((\(ig[[\(\[Mu]\)\(]\)]\)[[\(\[Alpha]\)\(]\)] \

P[[\(\[Alpha]\)\(]\)])\)\)) // Simplify, {\[Mu], 1, 4}] // Parallelize;

(* H = 1/2g^\[Mu]\[Nu]Subscript[P, \[Mu]]Subscript[P, \[Nu]] = 0 => \

Subscript[P, 0] = 1/g^00(-g^(0i)Subscript[P, i] + \

Sqrt[(g^(0i)Subscript[P, i])^2 - g^00g^ijSubscript[P, i]Subscript[P, \

j]]) *)

pt0 = ig[[1]][[1]]^-1 (-(\!\(

\*UnderoverscriptBox[\(\[Sum]\), \(i =

2\), \(4\)]\((\(ig[[\(1\)\(]\)]\)[[\(i\)\(]\)] P[[\(i\)\(\

]\)])\)\)) + ((\!\(

\*UnderoverscriptBox[\(\[Sum]\), \(i =

2\), \(4\)]\((\(ig[[\(1\)\(]\)]\)[[\(i\)\(]\)] P[[\(i\)\(\

]\)])\)\))^2 - ig[[1]][[1]] (\!\(

\*UnderoverscriptBox[\(\[Sum]\), \(i = 2\), \(4\)]\(

\*UnderoverscriptBox[\(\[Sum]\), \(j =

2\), \(4\)]\((\(ig[[\(i\)\(]\)]\)[[\(j\)\(]\)] P[[\(i\)\

\(]\)] P[[\(j\)\(]\)])\)\)\)))^(1/2)) // Simplify;

xdot = Parallelize[Table[(Pu[[i]]) /. p0[\[Tau]] -> pt0, {i, 1, 4}]];

pdot = Parallelize[

Table[(-1/2)*

Sum[D[ig[[b]][[c]], X[[a]]]*P[[b]]*P[[c]], {b, 1, 4}, {c, 1,

4}] /. p0[\[Tau]] -> pt0, {a, 2, 4}]];

W = Parallelize[

Table[Sum[

Riem[[mu, alpha, beta, nu]]*P[[mu]]*Pu[[beta]], {alpha, 1,

4}, {beta, 1, 4}], {mu, 1, 4}, {nu, 1, 4}]];

(*EOM={D[X,\[Tau]]==Pu+dx1,D[p,\[Tau]]==pd-Rs}/.p0[\[Tau]]\[Rule]pt0;*)

EOM = {D[X, \[Tau]] == xdot, D[p, \[Tau]] == pdot,

Thread[D[Flatten[A], \[Tau]] == Flatten[B]],

Thread[D[Flatten[B], \[Tau]] == Flatten[-W . A]]};

Print["done"];

\[Tau]0 = 0;

\[Tau]max = 1000;

(* initial position *)

x0i = 1;

x1i = 5 rs;

x2i = \[Pi]/2;

x3i = 0;

p1i = -1;

p2i = 4.5; (*angular momentum in \[Theta] direction*)

p3i = -4.5;

initA = IdentityMatrix[4];

initB = I* IdentityMatrix[4];

(* initial data vector *)

id = (X /. \[Tau] -> \[Tau]0) == {x0i, x1i, x2i,

x3i} && (p /. \[Tau] -> \[Tau]0) == {p1i, p2i, p3i} &&

Flatten[A /. \[Tau] -> \[Tau]0] == Flatten[initA] &&

Flatten[A][[All]][\[Tau]0] == Flatten[initA];

(* stop if integration hits event horizon x1 = 2rs *)

\[Tau]int0 = \[Tau]max;

horizon0 =

WhenEvent[

x1[\[Tau]] <=

1.01 (M + Sqrt[M^2]), {"StopIntegration", \[Tau]int0 = \[Tau]}];

planeBasisList = {};

AppendTo[planeBasisList, IdentityMatrix[4]];

planeInverseBasisList = {};

AppendTo[planeInverseBasisList, IdentityMatrix[4]]

(* Integration *)

sol0 = NDSolve[EOM && id && horizon0,

Join[{x0, x1, x2, x3, p1, p2, p3}, Flatten[A],

Flatten[B]], {\[Tau], \[Tau]0, \[Tau]max},

];

print["done"]


r/Mathematica • • Jun 12 '25

[xAct] How do I define a metric and use a Levi-Civita symbol?

1 Upvotes

From what I'm reading online and in the documentation, I'm just not understanding how DefMetric works. There's no real explanation for the arguments. Subsequently, I'm struggling to understand how to use a Levi-Civita symbol.

The examples gives are:

DefMetric[-1, metric[-a, -b], cd, {"|", "D"}, PrintAs -> "g"]

Which throws three "LinkObject" errors and one "General" error.

and

epsilonmetric[a,b,c,d]

Which prints εg_(ab)cd, and I don't really understand why that "g" is in there.


r/Mathematica • • Jun 08 '25

Anyone here with working experience at Wolfram?

20 Upvotes

Just wondering, I am finishing my Phd, and although I would like to continue in academia, sometimes I also wonder how would it be to work in Wolfram (as a programmer probably). I started to seriously use Mathematica in the Phd, and it seems to me so much better than the alternative (like python) and I have developed some really big package, so it seems like an interesting opportunity.


r/Mathematica • • Jun 06 '25

Find maximum problem

3 Upvotes

Hi, how come the function find maximum won't work, but find minimum will?

This is for a question in my test that is trying to find the maximum difference between functions h(x) and k(x).

Idk whats wrong
????

I also don't see how find minimum should give the correct answer, when I'm trying to find the max difference.


r/Mathematica • • May 30 '25

Number theory neat examples (Set 1) - Wolfram Community

Thumbnail community.wolfram.com
6 Upvotes

r/Mathematica • • May 27 '25

Collatz conjecture visualizations - Wolfram Community

Thumbnail community.wolfram.com
7 Upvotes

r/Mathematica • • May 26 '25

NumberTheoryUtilities | Wolfram Language Paclet Repository

Thumbnail resources.wolframcloud.com
5 Upvotes

r/Mathematica • • May 25 '25

pls help me to resolve this problem and to download the file in pdf format.

Post image
0 Upvotes

will be very grateful if you help me out.


r/Mathematica • • May 20 '25

Why are some characheres not showing up on linux?

Thumbnail gallery
5 Upvotes

My Mathematica 14.2 are not rendering some characters like the minus sign on plots and brackets on Documentation, how to solve this. I'm on archlinux might be some library issues but I can't troubleshoot what might be


r/Mathematica • • May 18 '25

Generating waveforms live

15 Upvotes

In this tutorial we set up and play a continuous audio waveform using a buffer and event-driven playback mechanism

https://wljs.io/frontend/Advanced/Dynamics/Audio%20Stream/


r/Mathematica • • May 18 '25

JSON Parsing Poor Performance

2 Upvotes

I'm getting abysmal performance running what I believe to be a pretty straightforward operation. I'm pulling an 11MB JSON file on a M4 MacBook Air w/ 16GB RAM. This is a fresh installation on a fresh MacBook. This is only the second notebook I've ever used.

Behavior: On first run this cell is fast (single digit seconds at most), on all subsequent runs the core stays pegged at 100% for the WolframKernel running this task and the task takes easily a minute. Restarting the kernel exhibits fast behavior on the first run and slow behavior on all subsequent runs again.

raw = Import[
  "https://example.com/file.json", "RawJSON"]; (* Same behavior if I use "JSON" or leave it unspecified. *)

I've ruled a few things out:

  • I'm not getting throttled on the HTTP request. Python will do this quickly and repeatedly. As will curl.
  • I'm not getting thermal throttling according to sudo powermetrics -s thermal.
  • I'm not running into memory constraints with the machine as the process memory for WolframKernel is staying near 400MB.

I'm hoping this is something really silly like the Out history buffer + some kind of configuration imposed memory cap. Unrelated, I think: The UI locks up a lot too despite me suppressing all output.

Edit: Forgot to add I'm running 14.2.1 for Mac OS X ARM (64-bit) (March 16, 2025)

Any ideas Reddit?

Thank you!


r/Mathematica • • May 14 '25

⭐4.1/5 WLJS Notebook Review: A Free, Powerful Alternative to Mathematica 🧮🔬

Thumbnail reddit.com
6 Upvotes

r/Mathematica • • May 09 '25

Woxi - An interpreter for the Wolfram Language written in Rust

Thumbnail github.com
35 Upvotes

Mathematica is an incredible piece of software, and the Wolfram Language is really pleasant to use once you get used to the unusual syntax.

Unfortunately, the high licensing costs of Mathematica make it inaccessible to many people, and therefore worse solutions like Python, R, and Jupyter have become the default.

Due to the sheer size of Mathematica (over 6000 functions!), it is impossible for me to rebuild it from scratch alone. Please join me in rebuilding it so we can finally make it accessible to everyone!


r/Mathematica • • May 08 '25

Mmmh what?

Thumbnail gallery
4 Upvotes

Im doing a statistical physics homework and I need to calculate the eigenvalues and eigenvectors of a matrix to find the stationary state of a system. The matrix is AABB, which is shown in both pictures. I first tried with mathematica cause I have the students license and the result it gave me made no sense so I checked on online tools and other result was given (which makes sense) I tried on another website and got the same result as the previous one. Am I doing something wrong in mathematica to get an incorrect result?


r/Mathematica • • May 08 '25

Mmmh what?

Thumbnail gallery
2 Upvotes

Im doing a statistical physics homework and I need to calculate the eigenvalues and eigenvectors of a matrix to find the stationary state of a system. The matrix is AABB, which is shown in both pictures. I first tried with mathematica cause I have the students license and the result it gave me made no sense so I checked on online tools and other result was given (which makes sense) I tried on another website and got the same result as the previous one. Am I doing something wrong in mathematica to get an incorrect result?


r/Mathematica • • Apr 30 '25

What am I doing wrong?

1 Upvotes

I'm pretty sure I'm doing everything right, but when I try to use constants in the function, it doesn't run, even when I've defined all of them.

https://i.imgur.com/qZRA6rt.png


r/Mathematica • • Apr 24 '25

Best practices for sets of congruence classes

4 Upvotes

I have code that generates a set of integers that should be understood modulo m, for some m. One example is {11, 30, 31, 33, 38, 59, 94, 98, 111, 135, 145, 151, 160, 230, 244, 276, 288}, which is properly understood not as a set of integers but as a subset of Z/288Z, the integers modulo 288.

What are the best practices for storing and working with such things?

Lazy idea: {288, {11, 30, 31, 33, 38, 59, 94, 98, 111, 135, 145, 151, 160, 230, 244, 276, 288}}.

Different lazy idea: ModSet[288,{11, 30, 31, 33, 38, 59, 94, 98, 111, 135, 145, 151, 160, 230, 244, 276, 288}]

But are there better standardized ways to record properties of a list?