Tag Info

Hot answers tagged

10

You can have a single rule using Alternatives (|)... {a, b, c, d, e, f, g, h} /. x : (a | c | e | f) -> 12 Furthermore you can construct the rule on the fly... {a, b, c, d, e, f, g, h} /. x : Alternatives@@{a, c, e, f} -> 12 As noted by @Kuba below there is no requirement for the pattern to have a name (x) so... {a, b, c, d, e, f, g, h} /. (a | ...


8

The use of Rule (->) does cause the right-hand-side to be evaluated but if a pattern Symbol (such as n) remains its left-hand-side match will still be substituted in. Observe what happens when f[n] evaluates to different expressions. No n present in evaluated form: f[n] = 7; {x, x^2, x^3, a, b} /. x^n_ -> f[n] {x, 7, 7, a, b} Note that only ...


7

Use two rules in the replacement with a dummy rule to replace exponents with themselves. Along the lines of. (ks-foo*bar) Exp[ks^foo-bar] /. {x:Exp[___]->x, ks->Y} So anything that is an exponent is 'caught' by the first rule and the kx replacement rule doesn't get a look in.


6

Here's one way: letters = Alternatives@@Map[ToExpression, CharacterRange["α", "ω"]]; a /. Rule[l : letters, x_] :> Rule[l, x /Degree] {x -> 51.0169, y -> 51.0169, γ -> 157.082} The degree sign is equivalent to Degree in Mathematica, so the number will be converted back to radians if we put it in there. However if it's just for display ...


5

Similar approach to Mr Wizard's but using a silly trick with pure functions rather than the auxiliary function f: Replace[{{1, 2, 3}, {-∞, 1, 2}, {1, 2, ∞}, {-∞, 1, 2, ∞}, {-∞, ∞}, {∞}, {-∞}, {}}, {a : (-∞ | PatternSequence[]), Shortest[x___], b : (∞ | PatternSequence[])} :> {#2 &[a, Unevaluated[], -∞], x, #2 &[b, Unevaluated[], ∞]}, {1}] ...


5

Let me relax rules a bit just to write some compact code without external functions. I can add -∞ and ∞ and delete double infinities Replace[{{1, 2, 3}, {-∞, 1, 2}, {1, 2, ∞}, {-∞, 1, 2, ∞}, {-∞, ∞}, {∞}, {-∞}, {}}, {mid___} :> ({-∞, mid, ∞} /. {x_, x_, y___} :> {y} /. {y___, x_, x_} :> {y}), {1}] (* {{-∞, 1, 2, 3, ∞}, {1, 2, ∞}, {-∞, 1, 2}, {1, ...


5

I set out to condense the rules shown in the question by use of "vanishing patterns" but I found it rather difficult. The best I could come up with is this: f[x_ | __] := x Replace[ {{1, 2, 3}, {-∞, 1, 2}, {1, 2, ∞}, {-∞, 1, 2, ∞}, {-∞, ∞}, {∞}, {-∞}, {}}, {a : -∞ ..., Shortest[s___], b : ∞ ...} :> {f[a, -∞], s, f[b, ∞]}, {1} ] {{-∞, 1, 2, 3, ∞}, ...


5

Update The first method I recommended does not work properly. I fooled myself because I have a $PrePrint definition by default. However following your update I think you do not need ReplaceAll. Instead try Block: e := Integrate[1/(x^3 + 1), {x, 0, n}]; Block[{n = 2.0}, e] 1.09 Not the use of SetDelayed to keep the definition from evaluating ...


4

Unevaluated[Limit[(c^(1 - g) - 1)/(1 - g), g -> d]] /. d -> 1 Hold[Limit[(c^(1 - g) - 1)/(1 - g), g -> d]] /. d -> 1 // ReleaseHold With[{d = 1}, Limit[(c^(1 - g) - 1)/(1 - g), g -> d]] Limit[(c^(1 - g) - 1)/(1 - g), g -> #] &@1 Block[{Limit}, SetAttributes[Limit, HoldAll]; Limit[(c^(1 - g) - 1)/(1 - g), g -> d] /. d ...


4

You asked for "more elegant or shorter" and Ymareth gave you exactly that. However it is worth noting that with long Alternatives this comes at a performance cost. Please observe: Needs["GeneralUtilities`"] set = Range[20000]; fn1[x_] := set /. (Alternatives @@ x -> -1) fn2[x_] := set /. (Dispatch @ Thread[x -> -1]) BenchmarkPlot[{fn1, fn2}, ...


4

I like using Thread for this kind of rules {a, b, c, d, e, f, g, h} /. Thread[{a, c, e, f} -> Table[12, {4}]] As @kguler suggests there is no need having a Table and keeping track of the length of the list also for a single value. So the following is better! {a, b, c, d, e, f, g, h} /. Thread[{a, c, e, f} -> 12]


4

To understand how you end up with a length of 11 for your usedsubsets you must consider not only the traversal order(1)(2)(3) in which the expression is scanned for pattern matching but the evaluation order as well. Much like Map as I summarized in Scan vs Map vs Apply Replace does not perform a sequential evaluation* but performs all replacements and then ...


3

This is what I get in Mathematica 10.0.2: In[1]:= img= Import["http://i.stack.imgur.com/natsI.png"]; bin=Binarize[img,0.18]; AbsoluteTiming[pos=PixelValuePositions[bin,1];] AbsoluteTiming[resultImage=ReplacePixelValue[bin,pos->Red];] Out[3]= {0.004004,Null} Out[4]= {2.661534,Null} The performance of ReplacePixelValue and ...


3

expr = {{1, 2, 3}, {-∞, 1, 2}, {1, 2, ∞}, {-∞, 1, 2, ∞}, {-∞, ∞}, {∞}, {-∞}, {}}; Not general but useful: Flatten[Replace[Split[{-∞, ##, ∞}], {x_, x_} :> Sequence[], {1}]] & @@@ expr Not working if in the list are repeated elements already. Also Flatten should be restricted if we are dealing with more complex structures.


3

Try the following. Let this: expr = (ks - foo1*bar1) Exp[ks^foo1 - bar1] + (ks - foo2*bar2) Exp[ ks*foo2 - bar2]; This pos = Position[expr, Exp[__]]; returns the position of exponents in it. Then this: expr2 = MapAt[ReplaceAll[#, ks -> kg] &, expr, pos] /. ks -> Y /. kg -> ks (* E^(-bar1 + ks^foo1) (-bar1 foo1 + Y) + E^(-bar2 + ...


3

It is very old function. It was introduced in V1.0 in 1988. It was used in the following way: So Sequence header was very useful. Ref (Section 3.4.2).


1

The most clean formulation, I believe, is “add an element and delete a pair if one occures”. Thus, I would just use something similar to idempotentAppend[{most___, x_}, x_] := {most}; idempotentAppend[l_List, x_] := Append[l, x]; idempotentPrepend[{x_, most___}, x_] := {most}; idempotentPrepend[l_List, x_] := Prepend[l, x] with “idempotent” in the ...


1

I suspect this is a way of letting you know that ToRules doesn't have an appropriate head to use for the conversion. Or put another way when a head isn't supplied Sequence is used by default. Consider: Delete[{1, 2}, 0] {1, 2} /. _[x__] :> x Sequence[1, 2] Sequence[1, 2] ToRules converts And to List: ToRules[a == 1 && b == 2 && ...


1

greekdegrees = x_ /; MemberQ[ { γ , α }, x ] :> Quantity[ x /Degree , "Degree" ] ; { x , γ } /. greekdegrees /. {x -> 51.0169, y -> 51.0169, γ -> 2.74159} Unfortunately the trig functions don't recognize the degree quantity, so to use it you'll need to do this kind of thing: Sin[QuantityMagnitude[UnitConvert[γ]]]


1

I think that's because n is on both sides of ->, so it's treated like a function. So for x^2, the rules says, make it a function, with 2 as its input. {x, x^2, x^3, a, b} /. x^n_ -> f[n] {x, f[2], f[3], a, b} {x, x^2, x^3, a, b} /. x^n_ :> f[n] {x, f[2], f[3], a, b} Now we define the function f[z_] f[z_] := Sqrt[z] // N {x, x^2, x^3, ...



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