New answers tagged

8

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


4

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


3

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


8

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


4

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


2

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


1

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.


1

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.


1

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


2

The following also works SetAttributes[S3, HoldFirst]; S3[expr_, indices__] := With[{ex = Unevaluated[expr]}, (Sum @@ Join[{ex}, Map[{#, 1, 2} &, {indices}]])]


2

One possibility: a[i_, j_] := D[u[i], u[j]] ClearAll@S2 SetAttributes[S2, HoldFirst] S2[expr_, indices__] := Sum @@ Join[Inactivate@{expr}, Map[{#, 1, 2} &, {indices}]] // Activate; S2[b[i, j], i, j] S2[a[i, j], i, j]


2

Ok. Here comes the Inactivate, Mathematica 10's powerful feature. Inactivate could not solve injecting expression into Hold. But you mentioned you actually want to use this to inject Do iterator in Compile. This can be done directly by Inactivate without Hold stuff. Use this Activate[Inactivate[Compile[{}, Do[code, iterators]]] /. iterators -> ...


1

I like march's idea of automation but I think at the moment his updated code is not working. Perhaps this will serve the purpose: SetAttributes[heldDistribute, HoldFirst] heldDistribute[expr_] := Unevaluated[expr] /. Cases[Unevaluated[expr], x_Symbol :> (HoldPattern[x] :> Defer[x]), {-1}, Heads -> False] // Distribute Now with the ...


6

So many ways: Apply[Unique, (patt :> #) & @ Table[{}, {3}], {2}] (patt :> Evaluate@Table[foo[], {3}]) /. foo -> Unique Hold[Unique[]][[Range[3]^0]] /. _[x__] :> (patt :> {x}) (patt :> {##}) & @@ Table[Unevaluated @ Unevaluated @ Unique[], {3}] (Function @@ {patt :> Evaluate@Table[#[], {3}]}) @ Unique All produce: patt ...


6

$userInt = 5; With[{ x = Table[Inactive@Unique[], {i, $userInt}] }, rules = {_Integer :> x} // Activate ] {_Integer :> {Unique[], Unique[], Unique[], Unique[], Unique[]}} {1} /. rules {{$27, $28, $29, $30, $31}}


9

This can be a good case for a Block trick: Block[{Unique}, With[{x = Table[Unique[], {i, 1, 3}]}, rules = {patt :> x}] ] (* {patt :> {Unique[], Unique[], Unique[]}} *)


3

With[{x = Table[HoldForm@Unique[], {i, 1, 3}]}, rules = {patt :> x}] patt /. (ReleaseHold@rules)



Top 50 recent answers are included