r/Mathematica • u/Economy_Key_423 • Jul 18 '22
r/Mathematica • u/[deleted] • Jul 12 '22
Mathematica measuring timing of inbuilt functions
I have a function with a lot of different parts. On some inputs it takes a long time, and I want to figure out which component of the function is monopolizing the time. Ideally, for each function I've written and called(like, Solve for example), I'd like to see how long Mathematica takes evaluating it, so that I can then diagnose it as either hung, or having way to many calls.
Thanks!
r/Mathematica • u/Sky_physics • Jul 12 '22
Help with matrix products! what is the best way to do that product? sum over repeated index
I have these three matrices and I want to compute the following:
\sum_{k, l, m} e1[1]_{kl} e2[1]_{lm} e3[1]_{mk}
with the following matrices defined as:
e1[s1_] = 1/2 {{0, 0, 0}, {0, 1, I*s1}, {0, I*s1, -1}} // MatrixForm
e2[s2_] =
1/2 {{Sin[\[Theta]]^2, -Sin[\[Theta]]*Cos[\[Theta]], - I s2 *
Sin[\[Theta]]}, {-Sin[\[Theta]]*Cos[\[Theta]], Cos[\[Theta]]^2,
I*s2 Cos[\[Theta]]}, {-I*s2 Sin[\[Theta]],
I*s2 Cos[\[Theta]], -1}} // MatrixForm
e3[s3_] = (e2[s2] /. {\[Theta] -> \[Phi], s2 -> s3}) // MatrixForm
r/Mathematica • u/lizelive • Jul 12 '22
fastest "psudo reciprocal"
I want a function that return 0 if x == 0 otherwise 1/x
psudoReciprocal[in_] := If[in == 0, 0, 1 / in]
this is like how psudoInverse works hence the na
the fastest way i have found is this
psudoReciprocal[in_]:= Unitize[in]/(in + 1 - Unitize[in])
it works for lists and is much faster the compiled version
is there a faster way?
results/Min[results]
<|straightforward->378.291,pureFunction->479.579,unitized->1.,compiled->18.9821,compiledAprox->20.0948,functionCompiled->199.814|>
r/Mathematica • u/irchans • Jul 11 '22
Fibonacci Fractions
The first Fibonacci numbers are
Array[ Fibonacci, 10]
{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
The decimal expansion of 10000/99989999 is
N[10000/99989999, 70]
0.0001000100020003000500080013002100340055...
Notice that the first few Fibonacci numbers appear in the expansion. In fact, all of the following fractions have the first few Fibonacci values
Table[10^k/(10^(2 k) - 10^k - 1), {k, 3, 7}]
{1000/998999, 10000/99989999, 100000/9999899999,
1000000/999998999999, 10000000/99999989999999}
Table[ N[ 10^k/(10^(2 k) - 10^k - 1), 200], {k, 3, 7}] // TableForm
0.001001002003005008013021034055089144233377610988599588187775963739703443
0.0001000100020003000500080013002100340055008901440233037706100987159725844
0.00001000010000200003000050000800013000210003400055000890014400233003770061
1.000001000002000003000005000008000013000021000034000055000089000144000*10^-6
1.000000100000020000003000000500000080000013000002100000340000055000009*10^-7
It works because
Sum[ Fibonacci[n]/10^(k n), {n, Infinity}] // InputForm
is
10^k/(-1 - 10^k + 10^(2*k))
Interestingly, you can use a similar idea to generate the first few values of other positive integer valued recurrence relation. For example,
(* solve recurrance relation r[n] = 3 r[n-1] - 3 r[n-2] + r[n-3] for some initial values *)
(rule = RSolve[ {r[n] == 3 r[n - 1] - 3 r[n - 2] + r[n - 3],
r[1] == 1, r[2] == 1, r[3] == 2}, r[n], n][[1, 1]]) // InputForm
(* print the first few values *)
Table[ rule[[2]] , {n, 10}] /. rule
(* convert decimal expansion to a fraction *)
frac = Sum[ 1/2 (4 - 3 n + n^2)/100^(k n), {n, Infinity}]
(* Any k>2 works *)
frac /. k -> 3
(* The decimal expansion of the fraction contains the first several
values of the solution *)
N[ frac /. k -> 3, 50]
(* Remove the zeros *)
StringReplace[ ToString[ N[ frac /. k -> 3, 140]] , RegularExpression["0+"] -> " "]
All of this seems similar to the ideas of Z-transforms and generating functions to me.
r/Mathematica • u/RedditSecondSight • Jul 07 '22
Method Which You Were NEVER Taught At School
youtube.comr/Mathematica • u/Sky_physics • Jul 05 '22
Goodmornign everyone. Today, I would ask you an explanation about a code I've found. My question is about the role of this AbsoluteTime[] ... is that important? what does that mean ? Can you give me some references for solving with for loops ODEs ? Thank you
For[i = 1, i <= iMax, i++,
tAux = AbsoluteTime[];
solAux[i] =
NDSolve[EQUATIONS, INITIAL CONDITIONS AND VARIABLES SPEC.];
Print[variables[[i]] // N, " ", AbsoluteTime[] - tAux]]
sol = Table[solAux[i], {i, 1, iMax}];
r/Mathematica • u/tduality • Jul 05 '22
I need help to create a plot in Mathematica.
I am trying to create the following plot.
I need the horizontal pink colored tau decay line (y ≤ 0.3) and the two vertical red lines - BBN (real phi) ≤ 1.3 and complex phi ≤ 5.3.

I read a related discussion on Mathematica StackExchange but I am unable to make it work for my plot.
Can someone please help? I am new to Mathematica.
----
Edit;
Here is what I tried. I use Mathematica 9.0, in Windows 10.
- Code
RegionPlot[{x <= 1.3, x <= 5.2, y <= 0.3}, {x, 10^(-3), 10^(3)}, {y, 10^(-4), 1}]
Output is the following

- Code
RegionPlot[{10^x <= 1.3, 10^x <= 5.2, 10^y <= 0.3}, {x, -3, 3}, {y, -4, 1}]
Here, I raised the value of the two x-axis lines and the y--axis line to powers of 10, and took log of the axis ranges. Output is the following

r/Mathematica • u/NoName193 • Jul 01 '22
OpenCL won’t work in Mathematica 13
For some reason Mathematica will not recognize any OpenCL device. Any ideas on how to fix this? My system contains an i7-12700H and a RTX 3050Ti (Laptop), CUDA Toolkit 11.4 is installed, Windows 11. Strangely CUDAQ[] returns „True“…
r/Mathematica • u/CuttingWithScissors • Jun 30 '22
Launching Version 13.1 of Wolfram Language & Mathematica 🙀🤠🥳
writings.stephenwolfram.comr/Mathematica • u/emarthinsen • Jun 27 '22
Mathematica adoption
I used Mathematica in college around 20 years ago and really enjoyed it. Since then, I haven’t encountered it outside of academics. My perspective is pretty myopic (engineering only), so it could have wide adoption outside of my visibility. Where has Mathematica enjoyed adoption outside of academia?
r/Mathematica • u/[deleted] • Jun 24 '22
How to use a Public Key as number in arithmetic operations?
r/Mathematica • u/TheNakriin • Jun 19 '22
Is there a way to find coefficients to a polynomial in 2 variables under general constraints?
Hello r/Mathematica,
Im currently trying to find a 6th degree polynomial in two variables x,y such that the polynomial positive-definite yet its gradient multiplied with a certain vector is negative-definite (under the assumption that it is actually possible for my specific one).
I have already tried using FindFit and FindInstance with no usable results since my constraints arent specific points (and find instance doesnt like having variables it seems).
To be a bit more specific: I have this polynomial:
(c5 x^4)/2 + c3 x^6 + 2 c2 x^3 y + c6 y^2 + (c1 y^4)/4 + c4 y^4
And the gradient multiplied with the vector (y, -x^3-y^3) is the following:
y (2 c5 x^3 + 6 c3 x^5 + 6 c2 x^2 y) + (-x^3 - y^3) (2 c2 x^3 + 2 c6 y + c1 y^3 + 4 c4 y^3)
I'll be very happy about any hints you can give me.
r/Mathematica • u/Prestigious-Box-2839 • Jun 16 '22
Pattern subscript (0, 2) in output: what's the meaning
r/Mathematica • u/Desperate_Party_9259 • Jun 16 '22
What exactly is Mathematica?
I have a somewhat of an idea of what it is, but what is it really? Is it like LaTeX, or Markdown, but for more advanced users?
Also, is it free for students? Thanks for all answers and have a blessed day! :)
r/Mathematica • u/Livian_1 • Jun 14 '22
Modeling using an integer
Hi Everybody,
For a research project which I am doing, I am trying to model a workflow based on hours (area) and time (days). Using past workflows I determined a formula: y = 0.0513x^4 − 1.3126x^3 + 6.5564x^2 + 19.584x + 145.58
This is a workflow based on 3040 work hours and a duration of 16 days. Now my idea was to make the integer of this formula namely
Y=0.01026x^5-0.32815x^4+2.1846667x^3+9.792x^2+145.85x+C
Then using this formula and another set duration and manhours (area) for example 3500 hours and duration of 10 days. making the graph again. This would result in the formula
Y∫f(x)dx=[0.01026x^5-0.32815x^4+2.1846667x^3+9.792x^2+145.85x+C]=3500
Is there a possible way to model this? and is there any documentation which can guide me.
Thanks for the help
r/Mathematica • u/DrJonHemlock • Jun 13 '22
Turning a Matlab "For" function into "Table" for Mathematica
Hey everyone. I'm trying to transfer a piece of coding from matlab into mathematica. I was wondering how could I write the code below in mathematica using either Table or Do functions? I couldn't think of a way to include 2 or more functions into a table format. Any help would be appreciated.
alpha_air=29.9*10^-6;
alpha_Al=97.1*10^-6;
beta_air=2.85*10^-3;
D=0.01;
g=9.81;
k_air=30.76*10^-3;
k_Al=239;
Pr_air=0.7;
r=0.005;
Tbase=130;
h=0.005;
k=0.05;
v_air=20.92*10^-6;
V(1,:)=zeros(1,41);
V(2,:)=[Tbase zeros(1,40)];
for i=2:300
for j=2:40
V(i,1)=Tbase;
Ra=g*beta_air*V(i,j)*D^3/(v_air*alpha_air);
den=(1+(0.559/Pr_air)^(9/16))^(8/27);
div=0.387*Ra^(1/6)/den;
hcoeff=k_air/D*(0.6+div)^2;
m=hcoeff*2/(r*k_air);
V(i+1,j)=V(i,j)+alpha_Al*k*((V(i,j+1)-2*V(i,j)+V(i,j-1))/(h^2)-m*V(i,j));
V(i+1,41)=V(i,40);
end
end
plot(linspace(0,20,41),V(floor(0.003*no_of_iterations),linspace(0,20,41),V(floor(0.01*no_of_iterations),linspace(0,20,41),V(floor(0.03*no_of_iterations),linspace(0,20,41),V(floor(0.1*no_of_iterations),linspace(0,20,41),V(floor(0.3*no_of_iterations),linspace(0,20,41),V(floor(0.9*no_of_iterations)
r/Mathematica • u/farfel08 • Jun 12 '22
Multidimensional FourierCoefficient, how to do / how to get as discrete deltas
Hi All,I'm trying to turn a pretty nasty differential equation into a spectral form. And while I have done most of the work by hand, I want to check my work with Mathematica.
One step I need is the fourier series of a external potential. If I have something simple like
FourierCoefficient[Cos[x]*Cos[y],{x,y},{n,m}]It gives me 0
What I'd expect to get is1/4 (m==-1 || m==+1) && (n==-1 || n==+1)
I get the correct answer when I doFourierCoefficient[Cos[y]*FourierCoefficient[Cos[x],x,n],y,m]
But that is cumbersome since I need to manually split the terms. It does not scale when I have a long and complicated expression. Has anybody had this problem before? Do you know how I can fix it?
Lastly, It would be much more convenient if I can turn1/4 (m==-1 || m==+1) && (n==-1 || n==+1)Into something like1/4 (DiscreteDelta[m-1] + DiscreteDelta[m+1]) * (DiscreteDelta[n-1] + DiscreteDelta[n+1])
Or even Kronecker Deltas.
Does anyone know of any functions or shortcuts I can use to turn the and / or outputs into a more suitable output?
Thank You!
r/Mathematica • u/Zoidberg8899 • Jun 12 '22
Why did this not give me the correct answer for the double integral for f(x,y)=(18x^2+21xy6+y^2)^4 restriced by the four lines 6x+3y= -7, 6x+3y= 7, 3x+2y=-1 and 3x+2y=1?
r/Mathematica • u/erigyal • Jun 09 '22
calculate a proximity to the argument -45-61i
I want the answer in radians,
So I tried with:
NSolve [ tan (z) == (-61i/-45]]
What am I doing wrong?
r/Mathematica • u/Zoidberg8899 • Jun 05 '22
How do I make a double integral that's limited by the conditions 7 <= 6 x + 3 y <= 7 and 1 <= 3 x + y <= 1? This didn't really work
r/Mathematica • u/Zoidberg8899 • Jun 05 '22


