# Tag Info

## New answers tagged performance-tuning

4

With modest preprocessing we get a factor of 9 or so for large inputs just by chunking into 12 bit pieces and using a compiled lookup function. m = 12; Timing[tmLookup = Table[Mod[Total[IntegerDigits[j, 2]], 2], {j, 0, 2^m - 1}];] (* Out[49]= {0.00157, Null} *) Some of the option settings are probably overkill. tmLookupCSmall = With[{tmtable = ...

22

I can't take much credit for this answer--I hadn't even got version 10.2 installed until J. M. commented to me that these functions could be written efficiently in terms of the Hamming weight function. But, it is understandable that he doesn't want to write an answer using a smartphone. The definition of the built-in ThueMorse is: ThueMorse[n_Integer] := ...

4

Note that Piecewise functions are a special case in Integrate if the integral is in the form of an indefinite integral. (One can add a constant as needed to adjust for a different starting point, but in the OP's example, it is unnecessary.) This evaluates relatively quickly: foo = Piecewise[{(t - #[[1]])^2, #[[1]] <= t && t < #[[2]]} & /@ ...

3

First thing I would note is that you should Integrate the function analytically before plotting it. To do so you should add your assumptions like already commented by other users (note that it is important to use set (=) instead of set delayed(:=) to do the integration only once): f[t_] = Piecewise[{(t - #[[1]])^2, #[[1]] <= t && t < #[[2]]} ...

1

Essentially the same as Simon Woods's comment: SeedRandom[1] data = Partition[Sort[Join[{0}, RandomReal[{0, 10}, 20], {10}]], 2, 1]; You don't need the And with the inequality f[t_] = Piecewise[{(t - #[[1]])^2, #[[1]] <= t < #[[2]]} & /@ data]; You need to add an assumption to the Integrate for it to evaluate Plot[Evaluate@Assuming[x >= ...

4

My keyboard is broken. So here is fast answer (on Mathematica 9); more later... Here is your input: dim = 3; g = RandomReal[{0, 1}, {dim, dim}]; F = RandomReal[{0, 1}, {dim, dim, dim, dim}]; Now multiply four g's and the F. Use TensorProduct[g, g, g, g, F] (don't run this yet--it's slow) to generate the rank 12 tensor (unrepeated indices). Now ...

1

Here's a possible solution. It's working (really well) for 1 particle, but needs to be checked for two particles, I guess due to minor mistakes. Basically, I changed approach and turned to a matrix notation. Let's start from the one-particle system. We simply write the state as a vector, and the operators as matrices, being very careful about the indexing. ...

6

First, here's an image from the docs which we'll use for testing: img = Import["http://i.stack.imgur.com/bzkJM.png"] Going with the definition given in the IPP, here is a remapping method based on the use of ImageValue[]: Options[ImageRemap] = {Padding -> 0, Resampling -> "Bilinear"}; ImageRemap[img_Image, xm_?MatrixQ, ym_?MatrixQ, opts : ...

3

This is an answer to Q1 and partly Q4, really. I can't test your Fortran version at the moment, but it would be an interesting comparison. You can improve the performance of f4 compared to f1 and f2 by setting RuntimeOptions -> "Speed". Clearly the change in runtime settings (mainly "CatchMachineIntegerOverflow" it seems...) from the defaults has a ...

4

In your declaration of the new compiled function, you omitted to include the type of some variables. The Part::pspec error has actually come up very recently in this forum. I will refer to that Q&A for a full discussion, but the short answer is that you want to "inject" your matrix into the compiled function so that Part can properly operate with it. ...

0

This works best with lists, could you check the validity of this sequence? \[Psi] = (1/Sqrt[2^n])*{{1}, {1}, {1}, {1}, {1}, {1}, {1}, {-1}, {1}, {1},{1}, {-1}, {1}, {-1}, {-1}, {1}}; \[Phi] = -\[Pi]; l1 = Table[\[Phi] + i, {i, \[Pi]/180, \[Pi], \[Pi]/180}]; l2 = Table[i, {i, \[Pi]/180, \[Pi], \[Pi]/180}]; CT = CoordinateTransformData["Spherical" -> ...

2

Without knowing how psi is defined, I can't fully answer, but a few points. Try to avoid For loops and use Do instead. So instead of writing For[\[Theta] = \[Pi]/180, \[Theta] < \[Pi], \[Theta] += \[Pi]/180, For[\[Phi] = -\[Pi] + \[Pi]/180, \[Phi] <= \[Pi], \[Phi] += \[Pi]/180, (* some code here *) ] ] write Do[ (* some code here *) , ...

12

I remember reading somewhere that everytime I use =, Mathematica copies an expression in the memory (which may be slow and inefficient). This is not quite true, as written here. Mathematica uses a copy-on-write behaviour, i.e. it will only create an actual copy of a datastructure if you modify it. Example: a = {1,2,3}; As this is evaluated, first ...

1

You can use ColorReplace: image = Import["http://i.stack.imgur.com/Pgppv.png"]; dx = 40; dy = 40; redblack = ColorReplace[image, {Red -> White, Black -> White, _ -> Black}]; box = BoxMatrix[Floor[{dx, dy}/2]]; ImageAdjust[ImageConvolve[redblack, box]]

4

As long as you're only counting/summing pixel values in a sliding window, you don't have to recalculate the whole sum for every pixel. You can just "count" red/black pixels in a "1x1 window" and then use a moving average or "box" filter to sum over neighborhoods. First calculate two images where every red/black pixel is 1, other pixels 0: image = ...

3

This answer is effectively a generalization of the approach by halirutan and Pickett. Here, I present a function that when given a list of colors, a list of positions and colors, or a color gradient known to ColorData[], it yields a listable compiled function effectively equivalent to Blend[]: makeCompiledBlend[colors : (_String | _List), opts___] := ...

18

We can take advantage of the fact that IntegerDigits is very fast when the base is large. But not too large: no bigger than $2^{63}-1$ on a 64-bit system or $2^{31}-1$ on a 32-bit one, because Mathematica's machine integers are signed. Additionally, non-power-of-two bases require more work to get the result than just partitioning a bit-string, and are ...

2

Update It might be worthwhile just specifying PlotPoints. For instance, if we use Energies[_, _, x_, y_] := SparseArray[ {Band[{1, 2}] -> Cos[x] Sin[y], Band[{2, 1}] -> Cos[x] Sin[y]} , {3, 3}] as our set of test-matrices, then Plot3D[Evaluate @ Sort @ Eigenvalues @ Energies[W, q, x, y] , {x, 0, 1}, {y, 0, 1} , PlotPoints -> 10] ...

Top 50 recent answers are included