The Stack Overflow podcast is back! Listen to an interview with our new CEO.

# Tag Info

0

Here is another solution which should reproduce the default behavior exactly: SetOptions[EvaluationNotebook[], {CellProlog :> AbortProtect[ Module[{nb = EvaluationNotebook[], evalCell = EvaluationCell[], cells, pos, cellsBelow, len}, If[And[CurrentValue[#, GeneratedCell], CurrentValue[#, CellAutoOverwrite]] &@ NextCell[], ...

1

Currently I have come to a much more complicated solution than I wished, which completely depends on the automatic grouping behavior (which I temporarily switch on): SetOptions[EvaluationNotebook[], {CellProlog :> AbortProtect[ Module[{nb = EvaluationNotebook[], evalCell = EvaluationCell[], cells, pos, grouping, groupingRules}, If[...

0

Mathematica is written in C/C++ itself. That means it cannot run faster than C/C++. (Assuming of course the faster implementations are used for each algorithm).

61

High-level languages, like Mathematica, have a high overhead for executing each command/instruction. However, they also typically include commands/instructions that solve a larger and more complex task than those in low-level languages. To take a concrete example, in C, we can add two numbers. In Mathematica, we can add two arrays directly. If we want to do ...

6

Yes, putting the function call in twice will cause it to be evaluated twice. You can visualize this by putting a Print inside the function, for example: f[x_] := (Print["In f"]; x) g[x_] := f[x]^2 + 2*f[x] + 1 g[2] (* In f *) (* In f *) (* 2 *) You can see that it went to f twice. To avoid that, you can assign a temp variable: g2[x_] := Module[{t =...

4

You could do something like G[x_] := a^2 + 2 a + 1 /. a -> F[x] or G[x_] := #^2 + 2 # + 1 &@F[x]

1

I would like to contest the claim that For problems that have more than 6 separate components to them: ... For such problems, using 12 kernels is ALWAYS unambiguously faster, and significantly so. I cannot repeat the benchmarks shown in the original post. Therefore I created a small toy benchmark that everyone can try, and that can in principle benefit ...

6

I can't comment of the specific integral you have. But on a test suite that contains 14,944 integrals, the following shows the average time (in seconds) used to solve each integral for different Mathematica versions Version 12 timing is not completed yet. This shows that version 9.0 was fast compared to newer versions. But this does not necessarily mean ...

6

Here are timings obtained with versions 12.0, 11.3 and 8.0.4 on the same machine: It is apparent that between versions 11.3 and 12.0 we have significant degradation of performance for simple integrals as I recently notices in another answer. I recommend reporting this to the technical support.

4

This is an extended comment to address the conditions for which the default in the Piecewise applies: Clear["Global*"]; ineq = r[3] + γ r[4] > r[2] + γ r[3] && r[2] + γ (r[3] + r[5]) < r[3] + γ (r[2] + r[4]); reg = ImplicitRegion[ ineq, {{r[1], 0, 1}, {r[2], 0, 1}, {r[3], 0, 1}, {r[4], 0, 1}, {r[5], 0, 1}}]; (int[γ_] = ...

3

Here's an idea similar to Carl Woll's that's a little faster: sQ[n_] := FractionalPart@Sqrt[n + 01] == 0; Here are some timing runs similar to @fgrieu's: timeRun[f_] := Module[{a, m}, a = (2^1024 - 3^644)^2; m = (2^1024 - 3^644)^2 + 9; First@ AbsoluteTiming@ Do[f[n], {n, m - 200000, m}] ] timeRun2[f_] := First@ AbsoluteTiming[ Do[ f /@ ...

3

The following is optimized for large values. The main idea is to reduce the integer tested modulo a product of small primes less than 64-bit, so that the cost is low and linear with the bit size of the argument, and filter the remainder using precomputed Jacobi tables to weed out all except very few (1/11595) of the non-squares. SqQ::usage = "SqQ[n] is ...

2

You can also use BinCounts using {{a,b}} as bin specification: BinCounts[list, {{a, b}}] or a combination of Clip, Unitize and Total: Total @ Unitize @ Clip[list, {a, b}, {0, 0}] Note: This is slower than Henrik's approach using Dot+ UnitStep combination.

4

If you are trying to do this operation repeatedly for the same list but different endpoints, then a faster alternative is to use Nearest. The basic idea is the following (I increased the size of the random list so that the speed difference is more readily apparent): SeedRandom[0] list=RandomReal[{0,1}, 10^5]; nf = Nearest[Sort @ list -> "Index"]; //...

2

(Not as fast as that by Henrik) Count[Clip[list, {a, b}, {0., 0.}], Except[0.]]

4

Total@Boole[a <= # <= b & /@ list]

5

This isn't as fast as the UnitStep method given by Henrik, but for many applications having the most efficient method isn't necessary. Length @ Select[list, Between[{a, b}]] or Count[list, _?(Between[{a, b}])]

9

n = 10000; list = RandomReal[{0, 1}, n]; a = 0.3; b = 0.6; UnitStep[list - a].UnitStep[b - list]

5

This is the actual data, everything else is derived from it: {T, W, H} = {1000, 100, 100}; matT = RandomReal[10, {T + 1, 2 T + 1}]; data = RandomReal[10, {W, H}]; u = RandomReal[{0, 1}, T + 1]; v = Table[RandomComplex[n + n I], {n, -T, T}]; That is how you compute the result: matX = TensorProduct[u, data]; matY = TensorProduct[v, data]; result = Total[...

11

This is the actual problem, condensed in only a couple of lines: m1 = 10; m2 = 10; m3 = 10; m4 = 10; dims = {10, 10, 10, 10}; grids = { Subdivide[0., 1., m1 - 1], Subdivide[0., 1., m2 - 1], Subdivide[-6., 0., m3 - 1], Subdivide[0., 4., m4 - 1] }; origrid = Tuples[grids]; leng = Length[origrid]; pt = DeveloperToPackedArray[{0.45, 0.03, -4.089,...

6

Matrix construction First of all, it might be a good idea to use memoization for building the matrices because this avoids recursive calls to A. By using SparseArrays, we can generate the matrices for much higher n: (*define matrices required for recursion relation*) Id[n_] := IdentityMatrix[Fibonacci[n - 2], SparseArray]; Zm[n_] := SparseArray[{}, {...

1

Since the system is in the form $v'(t)=a.v(t)$ where $a$ ia a constant matrix and $v(t)$ is a vector, it can be solved with MatrixExp: {barray, marray} = CoefficientArrays[Flatten@ρprime /. replace3, var] sol = MatrixExp[marray t, {1, 0, 0, 0, 0, 0, 0, 0, 0}]; // AbsoluteTiming (* {23.7716, Null} *) Be careful the symbolic solution is rather large: ...

2

This works and provides expected resulst as far as I'm aware: Clear[A, F, f, p, Ff, S, X, Y, d, j, k]; A[p_, k_, q_] := p/((2^k)*(2*q + 1)); F[p_, n_] := Table[A[p, k, q], {k, 0, Floor[Log[2, n]]}, {q, 0, Floor[(n - 1)/2]}]; f[n_, a_, b_] := p /. Table[ Solve[a <= A[p, k, q] <= b, p, Integers], {k, 0, Floor[Log[2, n]]}, {q, 0, ...

2

Just to get you started, the first part of the code where you solve for p can be written as f[n_, a_, b_] := Cases[ Flatten[ Table[ Solve[a < p/((2^k) (2 q + 1)) < b, p, Integers], {k, 0, Floor[Log[2, n]]}, {q, 0, Floor[(n - 1)/2]} ] ], Rule[x_, y_] -> y] Test f[2, 1, 5] (* {2, 3, 4, 3, 4, 5, 6, 7, 8, 9} *) Notice, I ...

16

Edit 2019-09-28: OK, now I got a full picture: It is the changes of SVD algorithm in MKL that affect speed of MatrixRank[] in Mathematica. Particularly, the SVD routine dgesdd got speed boost only in MKL version 11.2, hence also the Mathematica. A picture (Hum...a table) worth a thousand words: This table shows the implementations of the functions in ...

3

l1 = {2, 2, 3, 3, 7}; M = 4; l2 = Table[Length[Select[l1, # < n &]], {n, 1, M}] {0, 0, 2, 4} UnitStep + Total Total[1 - UnitStep[l1 - #] & /@ Range[M], {2}] (* or Total[1 - UnitStep[l1 - #]] & /@ Range[M] *) {0, 0, 2, 4} Clip + Unitize + Total Total[1 - Unitize@Clip[l1, {#, ∞}, {0, 1}] & /@ Range[ M], {2}] (* or Total[1 - ...

6

list = RandomReal[{0, 100}, 100000]; Accumulate[BinCounts[list, {0, Ceiling[Max[list]], 1}]]

6

The BoolEval package can be used here to get a good combination of performance and clean syntax: << BoolEval Table[BoolCount[list < n], {n, 5}] {0, 0, 2, 4, 4}

8

A severe problem is that Table generates an unpacked array, a thing that is often very annoying. When converting to an image, it is packed automatically (one can check that, e.g., with DeveloperPackedArrayQ[ImageData[Image[dat]]]). And because many functions work faster on packed arrays than on unpacked ones, this increases the performance. A vectorized ...

10

Is there any faster way available? There is, if you're willing to to use a different definition of "local maximum". If a local maximum is any value at least as high as any value in a 11x11 neighborhood, then you can use UnitStep[dat - Dilation[dat, 5]], which is about 16 times faster for your data: RepeatedTiming[MaxDetect[dat];][[1]] 0.016 ...

9

Why is MaxDetect so much slower when Chop is applied? Because Chop returns exact zeros what prevents packing of the matrix attempted by MaxDetect. You can detect an attempt to pack the array with Trace: Trace[MaxDetect[{{1.}}], Developer`ToPackedArray] // Flatten Applying N after Chop gives almost the same speed as without Chop because now the matrix can ...

6

A little bit faster than @kglr 's tricky answer is Position[dat, Max[dat]]

2

image=Import["https://i.stack.imgur.com/GZcUT.jpg"] c=1/2; ImageForwardTransformation[image, Through[{Re,Im}[((#[[1]]+I #[[2]])-c)/(Conjugate[c]*(#[[1]]+I #[[2]])-1)]]&, Background->1,DataRange->{{-1,1},{-1,1}},PlotRange->{{-1,1},{-1,1}}]//AbsoluteTiming Compile transformation function expr=ReIm[((#[[1]]+I #[[2]])-c)/(Conjugate[c]*(#[[...

Top 50 recent answers are included