Questions about Mathematica's rule-based replacement functionality, including functions such as Rule (->), Replace, ReplaceAll (/.) and ReplaceRepeated (//.).

learn more… | top users | synonyms (4)

5
votes
2answers
154 views

How to “ReplaceAll” using a set of conditional expressions instead of a list of rules [closed]

Suppose that I have two linear functions f[x_] := f0 + f1 x g[x_] := g0 + g1 x and a (possibly rather complicated) set of conditional expressions, obtained ...
5
votes
2answers
115 views

Trouble with Replacements [duplicate]

I have the following line of code: Plus @@ Table[p[x], {x, 0, 20}] /. p[x_] -> Boole[MemberQ[{0, 5}, x]] The first part of this produces ...
5
votes
1answer
95 views

How can I substitute something using only map?

For example, I have the equation x^2 + y^2 == x*y and I want to apply the rule y -> s*x I can do it easily by ...
5
votes
2answers
598 views

Speeding up mathematica by subsitituting numerical values

According to this Wolfram Blog post, one can speed up Mathematica code by substituting numerical values as soon as possible. How does one substitute before the main expression is evaluated? The ...
4
votes
4answers
129 views

Rearrange graph

I have to rearrange graph described by j = {1 -> 2, 2 -> 3, 3 -> 1}; appling some function to all vertices on the right so rearrange[j] output should ...
4
votes
2answers
141 views

Confusing ReplaceRepeated & Hold

I've been bootstrapping myself to the very alien world of Mathematica and there came my first WTF moment: ...
4
votes
3answers
122 views

Simplifying expression with patterns (Replace)

Q1: I need to simplify some algebraic expression, according to particular pattern, namely: \begin{equation*} x^m = \left\{ \begin{array}{ll} 1 & \text{if } m \text{ is even} \\ x & \text{if ...
4
votes
3answers
123 views

Appending to sublists using “anchor” values

Say I have a set of lists that look like this: ...
4
votes
1answer
136 views

replace items in a list with items in a another list

Suppose I have a list A={x1,x2,x3}, a list B={3,4,5}, and a function f[x]=x1+x2+x3. Is there ...
4
votes
1answer
167 views

How to speed up calculations with large number of replacement rules?

In my program I have an expression which looks like: ...
4
votes
3answers
601 views

Replace function and its derivatives found with Solve

If I have an equation eq = f[x] + g[x] == 0 and I solve for f[x] sol = Solve[eq,f[x]] ...
4
votes
1answer
153 views

Replace rule does not match

I have the following expression -3 I Ez Re[Ex] + 3 I Ex Re[Ez] And I wish to write this in the form ...
4
votes
2answers
108 views

BezierCurve in EdgeShapeFunction fails when used on multiedges graph

I would like to create a network graph with curved edges. To do this, I wrote this function: ...
4
votes
2answers
417 views

Replace expressions by self defined symbols

I have a list of expressions like the following: ...
4
votes
2answers
224 views

Returning Replacement Rules in a function and using it later on

I would like a function which returns replacement rules as some internal mathematica functions are doing and use the results in other functions. I could not find any information on that and I'm ...
4
votes
2answers
84 views

Creating functions from output of other calculations

Apologies in advance if the title is vague, I'm not really sure what to call this. I have a function (call it 'foo') that generates a largeish polynomial, and it is natural to make the variables be ...
4
votes
1answer
103 views

Replacing a combination of terms

I have a quite simple question. I have an expression that looks similar to $T=\frac{1}{a_1}-\frac{1}{a_2}+1$ and I'd like Mathematica to use $A=\frac{a_1}{a_2}$ to reformat the expression (if ...
4
votes
1answer
255 views

Replace rule also matching complex numbers

I'm having the replace rule: rule = {-g_ x_^4 - 2 g_ x_^2 y_^2 - g_ y_^4 -> -g (x^2 + y^2)^2}; Now, ...
4
votes
0answers
162 views

Is everything in Mathematica ultimately stored as a rule?

My current understanding about Mathematica is that everything, at the lowest level, ends up as replacement rules. First question: is this true? Second question: does Mathematica "gloss over" these ...
3
votes
3answers
551 views

How to replace all occurrences of an element in a list?

Consider the following: list={1,2,2,2,3}; I would like to replace all 2 with the string "Test". ...
3
votes
2answers
141 views

Can I ask Mathematica to replace the arguments of my functions with a replacement rule?

I have an equation that I evaluate at some point (let's say $x=1$) that have terms of the form f[1,y] D[g[1,y],{y,2}] Is there an easy way to replace [1,y] by ...
3
votes
4answers
188 views

Replace symbols with values without evaluation

If I have assigned values to a, b and c: ...
3
votes
3answers
62 views

Operations on expressions within Hold [duplicate]

I want to convert {Hold[1 + 2], Hold[3 + 4], Hold[5*6]} into Hold[{1 + 2, 3 + 4, 5*6}]. I have tried ...
3
votes
1answer
109 views

$Post with ReplaceAll [closed]

I'd like to have a ReplaceAll applied to every output in my notebook. When I try this using $Post, e.g. by writing ...
3
votes
3answers
685 views

Changing variables algebraically

Suppose one has two functions, $y(x)$ and $z(x)$, and one seeks to obtain $y(z)$ by substituting $x(z)$ into $y(x)$. Can this be done in a single step? Or must $z(x)$ first be inverted independently? ...
3
votes
1answer
83 views

Replace rule with function? Derivatives don't evaluate

Say I have an expression (call it expr) involving a function, f[x]. I'd like to be able to evaluate that for a particular choice of f[x] without setting that choice for the whole session. I thought to ...
3
votes
1answer
90 views

obtaining real roots of negative numbers in a long expression [duplicate]

I have a complicated symbolic expression which contains many terms like $(a/(a-2))^{1/m}$, where $a/(a-2)$ could be positive or negative after replacement. I only need the real root after ...
3
votes
1answer
141 views

Shortening the syntax {var1, var2} /. # & /@ list

I noticed that I often turn out to have a list of substitutions in the form ...
3
votes
2answers
212 views

Creating a Nearest function programmatically

I've been trying to work out how to create a Nearest function programmatically. My goal is to produce something similar to this, a hand-assembled function: ...
3
votes
2answers
157 views

Efficient way of setting up a rule

I tried to define a simple rule defining how λ acts on ψ[n]: myrule1 = λ ψ[n_] -> α[n + 1] ψ[n + 1]; The result I get is ...
3
votes
1answer
155 views

Pattern matching a multivariate derivative

I can extract the parameters of a single variable derivative with the rule D[f[x], x] /. Derivative[order_][fcn_][variable_] -> {order, fcn, variable} ...
3
votes
1answer
134 views

Set promotes Rule to RuleDelayed?

In mentally building a model of how Mathematica works, I naively expected Set to create a Rule in a symbol's ...
3
votes
1answer
53 views

Replacing multiple instances of a symbol with different variables and assignments

I have an expression of the form: expr=x+y+n+Sin[n]+n^2 I would like to replace each of the variables n with a different ...
3
votes
1answer
83 views

some confusion about ReplaceRepeated

This is Right. NestWhile[Log, 100, # > 1 &] {100} //. {x_} /; x > 1 :> {Log[x]} 100. //. x_ /; x > 1 :> Log[x] This is wrong,why? ...
3
votes
1answer
128 views

Is there a way to simplify this replacement rule

I have a rather big expression which contains many sums of form q x + w y. I know that y is much smaller than ...
3
votes
3answers
254 views

StreamPlot using NDSolve gives error messages

StreamPlot[{x[0], x'[0]} /. NDSolve[{x''[t] == 1, x'[0] == v0, x[0] == x0}, {x, x'}, {t, 0, 1}], {x0, -2, 2}, {v0, -2, 2}] The above code gives me the errors ...
3
votes
1answer
99 views

How can I avoid this ReplaceAll trap? [duplicate]

I am having some difficulties with the ReplaceAll function. When I write: {1, 2, 3, 4, 5} /. x_?OddQ -> x^2 I expect: ...
3
votes
1answer
146 views
3
votes
2answers
94 views

truncating terms in symbolic expression basing on their values

I need to simplify big symbolic solution of equations system by truncating terms that are orders of magnitude less then others in subexpressions like a + b. The solution is big fraction with sum of ...
3
votes
1answer
172 views

Replace elements in a list

Suppose that I have a list: ...
3
votes
1answer
60 views

Have transformation rules to be explicitly set?

I think this problem is not the hardest one, but I can't hit on it. I want to NSolve an equation. For this equation I use a transformation rule "ampTimes". First I used it like this: ...
3
votes
0answers
60 views

Why doesn't ReplaceAll work on Image? [duplicate]

ReplaceAll seems not to reach inside Image, and I don't see why it shouldn't. Here is an example. Create a black and white image ...
2
votes
3answers
131 views

Slow image processing using ReplaceAll

Can anyone explain me, why this line of simple code is so slow? And can it be improved somehow? ...
2
votes
2answers
348 views

How to do substitution of variable with power in Mathematica?

I want to replace $x^{i+1}$ with $z_i$. EDIT: I have some latex equations, and wish to import them to MMa. How to replace x_i (NOT $x_i$) with $x[i]$ But it seems the underscore "_" has a special ...
2
votes
3answers
117 views

How to manipulate log arguments?

A common problem I have with Mathematica is needing to manipulate the arguments of logarithms. Typically, I need to take ratios of the log arguments Mathematica gives me by default. That's to say ...
2
votes
2answers
303 views

Replacing values of a function

I have recently starting using Mathematica and have recently come to what seems as an impasse in my understanding of the language. If this is too "tell me how to do it" I would certainly understand. ...
2
votes
4answers
186 views

Extracting RHS from Rule

In an example from another question, the rhs of a list of rules can be simply extracted: ...
2
votes
2answers
80 views

Converting a date to a calendar quarter

I have a list that includes dates. I need to convert each date to be the last day of the quarter that it's in. So for example I want {2013,7,1,0,0,0} to become {2013,9,30,0,0,0}. Actually I don't ...
2
votes
1answer
107 views

Confused behavior using Dispatch to sort one list relative to another

I have a need to sort the entries of one list into the same sequence as the entries in a reference list. Because I have rather large lists involving strings, I'm trying to use Dispatch to accomplish ...
2
votes
2answers
127 views

How to plot complex values given by Solve

Update Solve[N[Table[BernoulliB[n, z], {n, 10, 10}] == 0]] ...