Tag Info

Hot answers tagged

12

For readers who didn't read all the comments, the slowdown is due to a lack of packing of tb, whereas RandomReal returns packed arrays when more than 250 elements are generated. The reason why packing tb fails is because some elements have different precision than others, and (I think?) ToPackedArray requires arrays to be of homogeneous type. To fix this, ...


8

If your operation can be converted to a canonical ranking rather than a pairwise comparison then you can use MaximalBy introduced in version 10. If not a good approach to a single pass through a list is Fold. Here is a function using that: foldMax[list_, p_] := Fold[If[p[##], ##] &, list] This proves to be faster in some cases than using Ordering ...


6

Just for fun, here is the pattern based version mylst2[K_] := ReplaceList[ ConstantArray[0, K], {a___, x_, b___, y_, c___} :> {a, 1, b, 2, c} ]


5

You can increase the performance by transposing your matrix and using Dot without explicit [[...]] << Developer` n = 2; m = 6; matrix = Table[Sin[1.0*i*j], {i, m}, {j, n}, {k, n}]; matrix3 = ToPackedArray@Transpose@matrix; vector = N@Transpose@{IdentityMatrix[n]}; vectorHC = ConjugateTranspose@vector; Flatten[vectorHC.matrix3.vector, {{1, 3, 2, 4, ...


5

It looks like you want to plot the phase-only information of a complex function. Using the following helper functions for plotting the phase-only information complex functions: hue = Compile[{{z, _Complex}}, {Mod[3 π/2 + Arg[z], 2 π]/(2 π), 1, If[Abs[z] > 10^-3, 1, 0]}, CompilationTarget -> "C", RuntimeAttributes -> {Listable}]; ...


4

This is too long for a comment, but here's a comparison between Matematica 10.0.2 and MATLAB R2014b on OS X, using MATLink. There is no appreciable difference between their performance. Mathematica 10 performs significantly better than Mathematica 9 due to updated MKL libraries. Both MATLAB and Mathematica rely on the MKL for matrix multiplications. ...


4

mylst2[K_] := Map[ ReplacePart[#, FirstPosition[#, 2] -> 1] &, Permutations[PadRight[{2, 2}, K]] ] This might not be what you want for K == 0. But it has much better complexity (quadratic vs exponential).


3

Adding elements to a growing list is slow in general. We get much better performance out of Mathematica if we treat data in chunks, and use high level functions as much as we can. This usually translates to a functional style of programming, as opposed to procedural programming. Do, While and For as therefore best to try to avoid altogether, in favor of ...


1

In your example it may be acceptable to merely Clip the input to mymod: mymod[ Clip[ Array[Exp[-(#)^2./10.] &, 500, {-100., 100.}], {$MinMachineNumber, $MaxMachineNumber} ] ] 9.8885 In a form to reapply, along with Message generation: catch::foo = "argument clipped"; catch[fn_][args__] := With[{clipped = Clip[{args}, {$MinMachineNumber, ...


1

I propose a silly workaround, instead of a workable explanation: Get["tb.dat"]; xls = Range[-500, 500, 1000/(1000 - 1)] // N; test[tb_] := (Re[Conjugate[#].(xls*#)] & /@ tb;) // AbsoluteTiming; test[RandomComplex[{0., 1. + I}, Dimensions[tb]]] (* {0.002002, Null} *) test[tb] (* {0.040038, Null} *) test[tb + ConstantArray[0. + 0. I, Dimensions@tb]] (* ...



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