Tag Info

New answers tagged

0

My guess is you are running into the same issue as was reported here. A possible workaround is to give NMinimize an explicit PostProcess option value, e.g."PostProcess" -> {"KKT"} (this goes inside the Method option setting).


3

Maybe the fastest is... just to compile the OP code sameQ = Compile[{{arr, _Integer, 1}}, Module[{IsConst = True, i}, For[i = 1, i <= Length[arr], i++, If[arr[[i]] != arr[[1]], IsConst = False; Break[]; ]]; IsConst], CompilationTarget -> "C", "RuntimeOptions" -> "Speed"]; arr = ConstantArray[1, 10000000]; ...


3

Not sure why people don't like DeleteDuplicates :) Length@DeleteDuplicates@arr > 1 for me this is faster than other methods (using V9 OS X tonight)


2

This problem is in a manner an converse of How do you check if there are any equal arguments(even sublist) in a list? and is related to How do I check if any element in a list is positive? As with both of those there is a choice in approach of either scanning the entire light with a fast, vectorized operation, or providing for an early-exit behavior. Which ...


5

You can Apply (@@) Equal to your list: constantQ = Equal@@#&; constantQ @ {0,1,0,0} (* False *) or constantQ2 = Max@#==Min@# &; constantQ3 = Variance@#==0; constantQ4 = Length@DeleteDuplicates@# == 1 &; See also: SameQ constantQ5 = SameQ @@ #& Equal @@ {1, 1., 1} (* True *) SameQ @@ {1, 1., 1} (* False *) ...


3

If[Length[Tally[arr]] == 1, IsConst = True, IsConst = False]


4

belisarius left a hint in a comment on how to solve this problem more efficiently, but I'm going to take it at face value and try to optimize your code. The pattern matcher is slow, so you don't want to use the pattern matcher in any kind of loop generally. On my computer your code takes 3.87 seconds to execute, whereas ParallelSum[ n ...


3

The given integral can be integrated exactly and quickly. Integrate[fermitotal[beta, k, mu, delta] k^2, {k, 0, 20}] // AbsoluteTiming N@Last[%] (* {0.006876, 8000/3} 2666.67 *) But since, presumably, the OP's actual use-case is not, I'll comment on the set up and the relation of OP's choices to speed. Remarks on the OP's option settings Since it's a ...


1

I'm afraid that this is a bit puzzling indeed. If I use your example and run the NIntegrate without all the fancy options I get {0.006944, 2666.67}. So I assume it has something to do with your options. Leaving out the method, but leaving everything else in place, slows down things dramatically, but it is still doable: {0.561119, 2666.666666667}, ...


0

How about this: fermitotal[beta_?NumericQ, k_?NumericQ, mu_?NumericQ, delta_?NumericQ] := With[{ee = Eigenvalues[{{k^2 - mu, delta}, {delta, -k^2 + mu}}]}, 1/(1 + Exp[beta*ee[[1]]]) + 1/(1 + Exp[beta*ee[[2]]]) ]; nTotal[beta_?NumericQ, mu_?NumericQ, delta_?NumericQ] := NIntegrate[fermitotal[beta, k, mu, delta] *k^2, {k, 0, 20}] nTotal[50, 1, 1/10] // ...



Top 50 recent answers are included