Tag Info

New answers tagged

2

For the given example where no values in b are zero before the mask one can use: Divide[mask*a, b] Note the use of explicit Divide for optimum performance. Timings: (* your data *) (r1 = Quiet[c/d /. Indeterminate -> 0]); // AbsoluteTiming // First (r2 = Divide[mask*a, b]); // AbsoluteTiming // First r1 == r2 4.578262 0.212012 ...


6

SparseArray can help, given the size and nature of the mask. It's slightly faster to convert c and d to sparse arrays than to convert a and b. mask = SparseArray@RandomChoice[{0, 0, 1}, {n, n}]; First@AbsoluteTiming[ c = mask a; d = mask b; Quiet[foo2 = Block[{Indeterminate = 0}, SparseArray[c] / SparseArray[d]]] ] (* 0.151565 *) Compare: ...


2

c/(d /. (0 | 0. -> Infinity));


6

ans2 = c/(1 - Sign@d + d); // AbsoluteTiming {0.7630000, Null} The above answer only works for specific example in the question i.e. it only handles positive lists and identical mask. For more general cases one can use: n = 1000; (* a and b contain non-positive elements now *) origin := RandomInteger[{-9, 9}, {n, n}] a = origin; b = origin; (* c and ...


7

SetAttributes[f, Listable]; f@__ := 0 f[a_, b_] /; b != 0 := a/b First@AbsoluteTiming[f[c, d];] Edit On the same spirit, this is 50% faster: SetAttributes[g, Listable]; g[_, 0] := 0 g[a_, b_] := Divide[a, b] First@AbsoluteTiming[g[c, d];]


2

Applying the method I described in: How to define even permutations correctly?: fW[a_List] := With[{p = Permutations @ Range @ Length @ a}, Dot[Signature /@ p, func /@ Extract[a, p ~Partition~ 1]] ] This is faster than both of kguler's functions (which are in turn faster than belisarius's code): f1 = Function[{k}, Total[Map[Signature[ #[[Ordering ...


1

Try this: First[Timing[ all = Graph[g, EdgeStyle -> Thread[Rule[ Function[set, If[Length[set] > 1, Alternatives @@ set, First@set]] /@ #, c[[;; numColors]]]]] & /@ p]]


3

mapping[set_] := Dispatch@Thread[set -> Range@Length@set] input = {a, b, c}; Total[Map[Signature[# /. mapping[input]] function[#] &, Permutations[input]]] (* function[{a, b, c}] - function[{a, c, b}] - function[{b, a, c}] + function[{b, c, a}] + function[{c, a, b}] - function[{c, b, a}]*)


4

This is much faster in my machine: el = Sort /@ EdgeList[g]; eSF[pos_, vert_, kset_] := {c[[Position[kset, Sort@vert][[1, 1]]]], Line[pos]} genG[el_, kSet_] := System`Graph[el, EdgeShapeFunction -> (eSF[##, kSet] &), VertexLabels -> "Name"] all1 = genG[el, #] & /@ p; // Timing (* {0.790625, Null} *) The "new" PropertyValue[] is nice, but a ...


6

input = {b, a, c}; Perhaps Total[Map[Signature[ #[[Ordering @ input]] ] func[#] &, Permutations[input]]] (* -func[{a, b, c}] + func[{a, c, b}] + func[{b, a, c}] - func[{b, c, a}] - func[{c, a, b}] + func[{c, b, a}] *) or Total[Map[Signature[#]Signature[input] func[#] &, Permutations[input]]] (* same result *) or Signature[input] ...


0

I wrote my thesis "Implementation of an algorithm for verifying the non-negativity of a multilinear function in a hypercube" about this here (password is "sal" and username "sal"). The key algorithm is not specified in the publication, focusing only on the key implementation idea with the hypercubes -- this was a requirement by my instructor. Shortly I ...


5

Use AbsoluteTime :- DateObject[Min[Map[AbsoluteTime, DateRange[DateObject[{2000, 1, 1}], DateObject[{2014, 10, 1}], "Month"]]]]


1

OK, now let me make use of the experience got from this answer. With the help of CompilationTarget -> "C" and RuntimeOptions -> "Speed" (both are indispensable), the Table approach turns out to be the most elegant and fast and universal. The 1D case: n = 10^6; a = RandomReal[1, n]; pxz = Compile[{{n, _Integer}, {a, _Real, 1}}, Table[If[n/4 + 1 ...


0

Denominators of first sequence: A003418, which are symmetric sequences of greatest divisors <= ii. The denominators of the differences are these greatest divisors. See here and here. Edit: I missed that ii=9 is 840 instead of 2520 in the first sequence and is a zero instead of 9 in the second. So, the symmetry stops at 9. I'll leave this answer in case ...


3

my stab at it, unfortunately only a marginal improvement in time: ii=13; Clear[a, b]; b = FoldList[Times, 1, Table[ Exp[MangoldtLambda[n]], {n, 2, ii}]]; a = Prepend[Table[ Limit[ Zeta[s] Total[Exp[Divisors[n]]^(s - 1) MoebiusMu[Divisors[n]]], s -> 1], {n, 2, ii}], 1] ; Monitor[aa = Prepend[Table[ ...


2

I realized now that I included unnecessary many terms of the Dirichlet inverse of the Euler totient. Therefore a better program is: ii = 13 aa = Range[ii]*0; Monitor[Do[ Clear[A, a, b, n, k]; b = Table[Product[Exp[MangoldtLambda[n]], {n, 1, k}], {k, 1, nn}]; a = Table[ If[n == 1, 1, Limit[Zeta[s] Total[ Exp[Divisors[n]]^(s - ...


4

You can use ParallelCombine for this task: plot = Plot[#, {x, -10, 10}, PlotPoints -> 10] &; funcs = {Sin[x], Cos[x], Sinc[x]}; g = ParallelCombine[plot, funcs, Show] Be aware that the expressions in funcs are not held in this example. Consider using Formal Symbols for the plot variables. You can add styling with post-processing, e.g.: ...


3

Plot by itself is not parallelizable using Parallelize. You can plot each curve in a different kernel using ParallelTable and then Show the results together Show[ ParallelTable[ Plot[ Sin[a x] , {x, 0, Pi} , PlotRange -> {-1, 1} ], {a, {1, 2, 3}}]] You may need to use DistributeDefinitions so the sub-kernels know the definitions of your ...


1

The performance issue is that you are dynamically building several levels of tables into tables into a plot function into a manipulate. This is very slow and what you are looking for is to build the table outside the plot and outside the manipulate and only do the indexation inside. However (!!) it seems to me there is a very straightforward solution to ...


11

Okay, this is a bit of an embarassment. Here is a very small modification of the original code. I simply made explicit option settings, made a denominator to Sin explicitly real, that kind of thing. My tests show the same timing as the original, give or take an iota. ie = 200; ez = ConstantArray[0., {ie + 1}]; hy = ConstantArray[0., {ie}]; fdtd1d = ...


14

There are several reasons. Firstly the built-in function has some minor overhead to check the arguments and call the appropriate internal function depending on whether the first argument is a list, a sparse array or an association. Secondly, with a packed array, LengthWhile uses compilation in an attempt to increase performance. There is some overhead in ...


14

Your test is quite synthetic: you take only few first elements. If you you have longer sequence of positive elements then build-in LengthWhile is faster lst = RandomInteger[{-1, 30000}, 100000]; rst1 = LengthWhile[lst, # >= 0 &]; // AbsoluteTiming rst2 = lengthwhile[lst, # >= 0 &]; // AbsoluteTiming rst1 == rst2 (* {0.096340, Null} *) (* ...


6

SubsetQ is implemented in top-level using Complement[a, b] === {}. It has some overhead because it has to treat associations specially, plus it has to go through the requisite error-handling rigmarole. But has the same time complexity in the length of the first argument: But this is on the shortlist of functions to reimplement in C when we have time. ...


1

EDIT After comments from RunnyKine... Just another approach and timing: subs[u_, v_] := Length@Intersection[u, v] == Length@v Performance: BenchmarkPlot[{count1[#, list] &, count2[#, list] &, Tally@Map[Function[x, subs[x, list]], #] &}, RandomInteger[100, {100000, #}] &, PowerRange[1, 1000], "IncludeFits" -> True, Frame -> ...


7

Okay, I'll go first. This is not an answer per se to the post, but more an invitation to write a fast sorting code for machine reals. That way we can get some sense of what might be feasible (showing timings of existing implementations would also be useful; I leave that for others). In Mathematica. Using Compile, of course. The point is to illustrate a few ...


6

I was able to speed up your code by a factor of 47,500 times faster than original. First, note that you can get a fairly good speedup just by eliminating the superfluous nested Table and Sum operators: n = 999; ak = RandomReal[{1, 10}, {1000, n}]; pimatk = RandomReal[{1, 10}, {1000, n}]; fikyj = RandomReal[{1, 10}, {1000, n}]; bk = RandomReal[{1, 10}, ...


2

Update Your code appears to be from yesterday's question: My Baum-Welch algorithm is very slow. Is it due to Mathematica? Answer The nested Table[Table[Tables and nested Sum[Sum[s not only make the code hard to read, but may also have a performance hit. Without a clue as to what ak, pimatk, fikyj, bk are, and how big they are, and what n is, this is ...


2

A very clear description of a Mathematica implementation of BW is given by Robert J Frey here. He seems to not have performance issues.



Top 50 recent answers are included