# Tag Info

1

This expression, designated exp for convenience, can be simplified substantially as follows. num = Map[FullSimplify[#] &, Numerator[exp]]; Map[FullSimplify[#, θ[_] ∈ Reals && ϕ[_] ∈ Reals] &, Denominator[exp] /. Abs[z_]^2 :> FullSimplify[Abs[z]^2, θ[_] ∈ Reals && ϕ[_] ∈ Reals]; /. Abs[z_]^2 :> z^2] den = ...

5

Let's assume you have the following points a-f, which form the line segments ab and cd and ef. a = {0, 0}; b = {3, 3}; c = {1, .5}; d = {2, 1.5}; e = {0, 2}; f = {2, 0}; In order to test whether they intersect you can use RegionIntersection. First form lines from your points, then use RegionIntersection on the two lines. Compare the intersection to an ...

4

As this is special-functions question, I feel justified in using a bit of heavy artillery. Here goes nothing... In effect, what the OP seems to want to do is to evaluate $$\sum_{n=1}^\infty \frac{(q^{n+1};q)_\infty}{(q^n;q)_\infty} q^{n-1}$$ where $(a;q)_n$ is the $q$-Pochhammer symbol by approximating it with its partial sums. However, there is a more ...

10

This is best I can do so far. The system is linear so LinearSolve is a natural thing to try. arrays = CoefficientArrays[eqs, vars] (* {SparseArray[< 2 >, {9}], SparseArray[< 35 >, {9, 9}]} *) solv1 = Thread[ vars -> LinearSolve[arrays[[2]], -arrays[[1]], Method -> "CofactorExpansion"]]; // AbsoluteTiming (* {0.28347, Null} ...

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

3

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

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

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

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

Top 50 recent answers are included