New answers tagged procedural-programming
0
The issue is not θ vs x. It's PolarPlot vs. Plot. They both have the HoldAll attribute. This is to prevent a value that the variable, either θ or x, might have outside the plot call from replacing the symbolic variable. While the plot functions both have the HoldAll attribute, they do different things with their arguments. This is undocumented, and one ...
1
Like this?
g1[x_] = x*x + 1;
g2[x_] = Log[x];
g3[x_] = 2 Cos[x];
funclist = "g" <> ToString[#] & /@ {1, 2, 3} // ToExpression;
Plot[#[x] & /@ funclist, {x, -5, 5}]
Better way:
g[1][x_] = x*x + 1;
g[2][x_] = Log[x];
Plot[g[#][x] & /@ {1, 2}, {x, -5, 5}]
Actually, it's not recommended to do that in Python. eval is evil. You could ...
1
Clear["Global`*"]
pltData = {{Sin[x], {0, 2 Pi}, {-1, 1}},
{y*Cos[y], {-2 Pi, 2 Pi}, {-6, 6}},
{{Sqrt[5^2 - x^2], -Sqrt[5^2 - x^2]}, {-5, 5}, {-5, 5}},
{{z^2 - z - 6}, {-3, 4}, Automatic}};
Column[Plot @@@ ({#[[1]],
Insert[#[[2]], Variables[Level[#[[1]], {-1}]][[1]], 1],
PlotRange -> #[[3]]} & /@ pltData)]
8
Welcome to MMA SE! What the code above does is simply repeatedly overwrite the value of A. In each iteration of the loop, it evaluates A = i + j, so the A at the end of the loop is simply i + j for the last i, j in the loop.
You could do a For loop where you initialize A to a table, and then set different parts of A, e.g. A[[i,j]] = "*". That's not ...
3
SeedRandom[1];
dates = Sort[RandomSample[With[{first = Now},
DateRange[first, DatePlus[first, Quantity[1000, "Minutes"]], "Minutes"]], 20] ];
events = RandomChoice[{3, 1} -> {"in", "out"}, 20];
names = RandomWord["Noun", 20];
data = Transpose[{names, events, dates}];
Grid @ data
Prepend dates ...
3
hex = CirclePoints[{1, Pi/6}, 6];
spikes = CirclePoints[{2, Pi/6}, 6];
core = Join[hex, spikes];
center = NearestNeighborGraph[core, {3, 1 + 10^-2},
DistanceFunction -> (EuclideanDistance @@ N[{##}] &)];
Δ = 1;
firstlayer = Join @@ Table[FullSimplify @
(k RotationTransform[j Pi/3, {0, 0}]@{Δ + 2 Sqrt@ 3, 0} + # & /@ core),
{k, {-1,...
0
Here is an example of MarcoB's suggestion:
AbsoluteTiming[
TimeConstrained[Print[Pause[1]; 1], 3];
TimeConstrained[Print[Pause[5]; 10], 3];
TimeConstrained[Print[Pause[2]; 100], 3];
]
You can think of each Print statement as one iteration of the loop. Example output of the code:
1
100
{6.03724, Null}
This shows that it skipped the second Print ...
6
Try CellEpilog:
SetOptions[EvaluationNotebook[], CellEpilog :> (x = 0)]
3
Here are a couple of further suggestions to make the code faster and a bit shorter.
n = 160;
t = 100;
id = IdentityMatrix[n, SparseArray, WorkingPrecision -> MachinePrecision];
A = 0.5 ((DistanceMatrix[Range[1., n]] + id)^-3 - id);
M = 2. (A + id);
F = LinearSolve[M];
iprv = ConstantArray[0., {n, t}];
ParallelDo[
Block[{m, k, Psi, B, h1, h2, H, h, u, ...
answered Mar 20 at 9:15
Henrik Schumacher
87.6k66 gold badges137137 silver badges250250 bronze badges
4
To get the matrix inversion to work on machine numbers just replace
F = Inverse[0.0 + M];
That brings the running time to under 150 seconds on my MBPro -- replace the Do line by
Timing@Do[
to have Mathematica tell you about running time.
You can indeed shave a few seconds off by moving the matrix definition and inverse out of the Do loop, but not much.
Top 50 recent answers are included
Related Tags
procedural-programming × 264list-manipulation × 35
programming × 20
functions × 19
iterators × 17
matrix × 15
functional-style × 15
function-construction × 14
table × 12
differential-equations × 11
equation-solving × 11
performance-tuning × 11
infinite-loop × 11
plotting × 10
scoping × 10
parallelization × 9
random × 8
recursion × 8
code-review × 7
iteration × 7
calculus-and-analysis × 6
export × 5
evaluation × 5
variable-definitions × 5
mathematical-optimization × 4