Tag Info

Hot answers tagged

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] := ...


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 ...


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 ...


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 : ...


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. ...


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 = ...


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]]} & /@ ...


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 ...


4

What takes a long time is formatting the output. Even though just a small blue box is shown, the output is in fact very large, and Mathematica probably tries to process it before deciding that it's just too big to show. AbsoluteTiming only measures the time taken to compute something (by the kernel), not the time taken to format it (by the front end, and, ...


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 = ...


3

A more efficient method to calculate the B-Spline curve is Cox-De Boor algorithm Implementation (* Do binary search *) biSearch[knots_, {low_, high_}, u0_] := With[{mid = Floor[(low + high)/2]}, If[u0 < knots[[mid]], {low, mid}, {mid, high}] ] (* Search the index of span [u_i,u_i+1]) *) searchSpan[{knots_, deg_}, u0_] := First@ NestWhile[ ...


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 ...


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]]} ...


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___] := ...


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] ...


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 *) , ...


2

One approach is to construct the rows with a head other than list, say row, then accumulate the rows as a linked list, and finally transform the linked list into a simple list of sublists of the form wanted. Here is an example. Row constructor. newRow[] := With[{rs = RandomSample[Range[11]]}, row[rs[[;; 3]], rs[[4 ;; 6]], {rs[[7]]}, rs[[8 ;;]]]] ...


1

Maybe there's something I do not understand because this seems fast to me. I used Do instead of For, since it is faster: (nodes = ConstantArray[0, {200000}]; Do[ nodes[[ji]] = List[{8, 3, 2}, {1, 2, 3}, {8}, {8, 3, 3, 2}], {ji, 200000}];) // AbsoluteTiming (* {0.13, Null} *) But it's not even twice as fast as For: (nodes = ConstantArray[0, ...


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 >= ...


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. ...


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]]



Only top voted, non community-wiki answers of a minimum length are eligible