Tag Info

Hot answers tagged

14

A few things are happening in there. First, Infinity is being replaced by DirectedInfinity[1] in order to perform the comparison. In fact Infinity is just an abbreviation of DirectedInfinity[1]. You can see it by doing: Infinity // FullForm (* DirectedInfinity[1] *) Next, the comparison takes place. It's done in the same way Mathematica always does, ...


9

Use Replace instead of ReplaceAll with the option Heads -> False. Replace[x[a] + x, x -> y, {0, Infinity}, Heads -> False] {0, Infinity} here is a level specification which tells Replace to replace everywhere, just like ReplaceAll. You can drop Heads -> False because it's the default setting for Replace, but I wanted to point out the option ...


5

At some point, someone might want to write a good question about how to debug patterns that do not work as (naively) expected. When I was first learning about patterns, I found it frustrating and confusing. Now, I wonder what was the big deal. It can still be difficult to find the right pattern, just as there are difficult problems in every area. The gap ...


4

This is strongly related to Plot draws list of curves in same color when not using Evaluate but the specific issue there has to do with styling of lines rather than evaluation itself. In this case the question is why the curve scale ends up different. For that you need to understand how Plot works. It is akin to Block over the plot variables. Now ...


4

myfun[x_] := c /. x myfun /@ {case1, case2, case3} (* {4, 3, 2} *) But please note that if you inadvertently assign a value to the symbol c, it goes astray and can't be repaired by tricks done only on myfun[] since it "corrupts" your cases lists. Considering the above, perhaps it is safer to work with Formal symbols if you really need this kind of ...


3

An alternative to @belisarius' use of formal symbols to overcome the problem caused by c having been assigned a value case1 = {a -> 1, b -> 3, c -> 4, e -> 5}; case2 = {c -> 3, a -> 1, w -> 2}; case3 = {x -> 5, y -> 2, z -> 0, c -> 2}; myfun[x_] := Cases[x, (c -> val_) :> val][[1]] myfun /@ {case1, case2, case3} ...


3

Try thinking of doing substitutions on a list. You want the beginning of the list and then a couple of elements and then the rest of the list. If the beginning of the list and the next element satisfies a condition then you want the substitution to give you a modified list. G[2] = {2, 4}; G[i_] := Nest[Join[#, G[i - 1]] &, G[i - 1], Prime[i] - 1]; g3 = ...


2

The problem is the rule you are trying to apply looks like this: {x1, x2} -> {{1, 1}, {-1, 1}} and of course {x1, x2} is not present in the expression. Try this: With[{x = {x1, x2}}, Grad[f@x, x] /. Thread[x -> #] & /@ {{1, 1}, {-1, 1}}] {{{2, -2}, {Cos[1], -Sin[1]}}, {{-2, -2}, {Cos[1], -Sin[1]}}}


2

For simplification you can also try to define a new function or use pure-function: Evaluate@Grad[f[{#1, #2}], {#1, #2}] & @@@ {{1, 1}, {-1, 1}}


2

I don't understand exactly how it work but as the help say "With replaces symbols in expr only when they do not occur as local variables inside scoping constructs". So you should use rules on x1 and x2 like this With[{x = {x1, x2}}, Grad[f[x], x] /. {x2 -> {1, 1}, x1 -> {-1, 1}}]


1

because the x you want to replace is in the last level of the expression you can use: Replace[Sin[x] + x[a] + x, x -> y, {-1}] (*y + Sin[y] + x[a]*)


1

to get: 1+2+3 you can use any of: exp /. Thread[syms -> (Defer /@ val)] exp /. Thread[syms -> (HoldForm /@ val)] Defer[Evaluate@exp] /. Thread[syms -> val] HoldForm[Evaluate@exp] /. Thread[syms -> val]



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