Hot answers tagged


You seem to be re-evaluating the eigenvalues at every point. Just use this definition: Clear[Eval,kx, ky, kz]; Eval[kx_, ky_, kz_] = FullSimplify[ Eigenvalues[H[kx, ky, kz] + Subscript[H, 1][kx, ky, kz]]]; Then the plots will be faster. This will symbolically evaluate the eigenvalues once, and the variables kx, ky, kz get substituted into the ...


The two examples in this question relate to two different aspects of pattern matching. I will start with the simpler to understand and intentional aspect, which is the second example. g[2] /. g[ 1 + (1|other) ] -> post (* g[2] *) In the above, the pattern doesn't match, and it can never match. g[2] has one argument. Since Plus is OneIdentity, 2 ...


I think an acceptable solution is to Thread over Alternatives: Basic solution: SetAttributes[f, Flat]; f[a, b, c] /. Thread[f[a, f[b, c] | other], Alternatives] -> post post Though, it won't be very helpful in more complex situations: f[a | b, f[b, c | h]]. General solution (experimental) tupplesOver[ f[a | g, f[b, c | h] | other], ...


I think this is what you are looking for: BooleanTable[Xor[p, p || q], {p}, {q}] // TableForm


Personally, I find this behavior somewhat surprising -- in particular, I would intuitively expect the following patterns to be completely equivalent: somePattern[..., a|b, ...] somePattern[..., a, ...] | somePattern[..., b, ...] While that may seem a natural expectation I do not believe the documentation ever states that they are. Nowhere can I ...


After discussing with Kuba, I conjecture the following: Patterns involving alternatives are not evaluated further when attempting to match each alternative That is, somePattern[..., a|b, ...] originally evaluates as if a|b is a black box. Then, during pattern matching, the pattern does not evaluate any further when a|b is replaced by a and b in turn. To ...


If you are expecting Clear[x] to clear definitions of x, don't expect anything more from Clear[s]. So you can go with: X = 5; s = "X"; Clear[#] &@s X X Or, as pointed by Albert Retey, with Clear[Evaluate@s]. Maybe you don't want to use strings, then: X = 5; s = ToExpression["X", StandardForm, Unevaluated]; Clear[#] &@s X works too. Some ...


If $a$ is already defined SeedRandom[123]; a = RandomInteger[{1, 9}, 6] (* {8, 5, 1, 3, 7, 8} *) and Length[a]>=m then m = 4; a[[;; m]] = ConstantArray[0, m]; a (* {0, 0, 0, 0, 7, 8} *) However, if $a$ not already defined then create with length $m$ of zeros. m = 4; a = ConstantArray[0, m] (* {0, 0, 0, 0} *) Hope this helps.


I'm not sure that memoization applies to OPs question. In a direct test, the order doesn't seem to make much of a difference in execution time.


Will this simple method work for you? m = 5; Table[a[i] = 0, {i, m}];

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