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

learn more… | top users | synonyms (4)

32
votes
3answers
445 views

Replace inside Held expression

I wish to make functional replacement inside Held expression like this: ...
13
votes
6answers
2k views

Replacing composite variables by a single variable

To replace a single variable by another variable, one can simply use the the replace all (/.) operator (e.g., ...
10
votes
7answers
2k views

How do I replace a variable in a polynomial?

How do I substitue z^2->x in the following polynomial z^4+z^2+4? z^4+z^2+4 /. z^2->x ...
19
votes
2answers
1k views

Extracting values from nested rules in JSON data

I have been using Mathematica to analyse some data from the StackExchange API. It is conveniently available in JSON form, which Mathematica interprets as replacement rules. However, some of the rules ...
19
votes
2answers
804 views

Mathematica for Computer Scientists

Background I've coded in C/Scheme for 10 years. I have a BS CS. I've recently become very interested in pattern-matching (lisp's match macro), and term rewriting systems, which brought me to ...
12
votes
1answer
321 views

Transform an expression into a graph that can be plotted with TreeGraph (not TreeForm)

I would like to plot an expression (like TreeForm does), but using the new TreeGraph functionality. ...
5
votes
5answers
498 views

Replace values which obey certain criteria

I'd like to replace all values in a list which obey one or more criteria with another value. Example: Replace all values>30 by 30. data={{3,35},{2,7}} ...
9
votes
3answers
805 views

Replace expressions with symbols

First of all: I'm new to mathematica, so I would appreciate it if the answers are quite complete. I have the result of calculation that is expressed in $\sin$ and $\cos$. Now, all of these can be ...
7
votes
3answers
139 views

Converting StringJoin to StringForm inside Hold

Let's say I have a piece of code: Hold[{code1, "asdad " <> ToString[testa] <> " adsd " <> ToString[testb], code2}] (*MWE ofc*) ...
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: ...
10
votes
3answers
388 views

How to properly project a Graphics object consisting of line primitives

My problem starts from attempts at depicting overlapping lines of different thickness, i.e. ...
9
votes
2answers
366 views

Simple problem with Manipulate and Plot

Would anyone have an idea why the following doesn't work: rule = {z -> x^2 + 2 x + y}; Manipulate[ Plot[z /. rule, {x, 0, 10}], {{y, 2, "y"}, 1, 5} ] But ...
11
votes
4answers
269 views

How to replace every possible $A+B$ and $AB$ in expansion of $(A+B)^{10}-A^{10}-B^{10}$ with $x$ and $y$, respectively?

I would like to replace every possible $A+B$ and $AB$ in expansion of $(A+B)^{10}-A^{10}-B^{10}$ with $x$ and $y$, respectively. How to do it with the simplest code in Mathematica? For example, ...
6
votes
6answers
758 views

How to simplify a complicated Sum in terms of power Sums?

For example, I have: $a=\sum _{r=1}^n x_r \left(\left(\sum _{i=1}^n x_i-x_r\right){}^2-\sum _{i=1}^n x_i^2\right)$ ...
7
votes
3answers
212 views

Applying a transformation rule on an Image

I haven't been able to figure out why the following doesn't work: ...
4
votes
2answers
421 views

Replace expressions by self defined symbols

I have a list of expressions like the following: ...
20
votes
1answer
511 views

replacement rules from a pattern and a matching expression

(This seems to be a very basic necessity in a language having pattern-matching capabilities such as Mathematica, yet I struggled for many hours to find a common solution to this problem.) Suppose ...
9
votes
3answers
513 views

How to implement a regular grammar?

What is the most simple, elegant way of implementing a rewrite-system defined as: $$ \begin{aligned} \Sigma &= \{a_1, a_2, a_3, ...\} \\ N &= \{A_1, A_2, A_3, ...\} \\ \{\alpha_1 , ...
8
votes
1answer
157 views

Performance of Dispatch and lists of Rules

First of all, I have seen the question "Why is this parallel evaluation with Dispatch[] so slow?", but that seems to be a different matter. My question arose when I tried to answer this question by ...
11
votes
4answers
226 views

Replacing parts of a held expression with held parts of another expression

I'm applying the Trott-Strzebonski technique in order to replace inside a held expression. ...
12
votes
1answer
362 views

rule-based implementation of an algorithm

When I first started learning about rule-based programming with Mathematica, I tried to translate this algorithm for computing the convex hull of a set of 2-D points in $O(n \log(n))$ time, to use ...
9
votes
2answers
200 views

How to extract a specific element from EXIF photographic metadata?

It's now possible to import photographs into Mathematica and import the EXIF data at the same time: i = Import["ExampleData/coneflower.jpg", "ImageWithExif"] ...
7
votes
1answer
141 views

StringReplace, ReplaceAll and Rule interact in a bizarre way

I have the following pair of things: ClearAll[foo, labeledFoo]; labeledFoo = {"FooBarBazQuux", foo}; This works like you'd expect: ...
7
votes
2answers
206 views

Why does the name of a pattern affect the result of a transformation rule?

I was playing a little dirty, trying to get a template for defining a Curl[] operator. I wrote the following rule, which I knew was going to spit out a few ...
10
votes
4answers
307 views

“Strange” behavior of Rule

According to the Help lhs->rhs evaluates rhs immediately. How to understand the output of the following code? ClearAll@x; {1, 3.5} /. x_?IntegerQ -> {x} ...
5
votes
3answers
1k views

How to use results of NDsolve[] for further solving of ODEs?

I have a system of ODEs with 10 eqns. I can solve the first 5 independently. How can I use those results to solve for the remaining 5? An easy example would be $\dot{x}=f(x), \quad \dot{y}=g(x,y)$ ...
4
votes
3answers
604 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]] ...
9
votes
3answers
213 views

substitution rules excluding subscripted variables

Suppose I have an expression containing p,Subscript[p,1],Subscript[p,2],etc. I want to ...
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} ...
1
vote
0answers
60 views

Abridging Lists of Equations within NDSolve, Manipulate [duplicate]

I have a pretty little NDSolve function that I've finally decked out with manipulatable (by sliders) coefficients, which changes the graph in real time. However, I ...
8
votes
2answers
612 views

Mathematica rule substitution memory

First I present the specific programming question, and then I provide further background details. Given an expression in Mathematica, say f=3x+7y, there are two ...
7
votes
1answer
195 views

Is it possible to specify a context-sensitive, “depth-agnostic” rewrite rule?

I'm using the term "depth-agnostic" in this post to describe structural patterns featuring a "target" sub-pattern that can occur "at any depth" relative to one or more "context" sub-patterns. As a ...
7
votes
3answers
218 views

Safely nesting RuleDelayed

Say I have the following, straightforward-seeming functions: ...
6
votes
1answer
286 views

ReplaceAll inside an Iterator

I'd like to apply a set of rules to an expression defining the iterators of the table, like this: ...
6
votes
3answers
576 views

Arithmetic operation on the value returned by Solve

I want to modify the value that is returned by Solve. Example: SolC1 = Solve[Sin[ π x / L] == 1, x] {{x -> L/2}} I need to add n to x so that x becomes ...
3
votes
2answers
143 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
1answer
173 views

Replace elements in a list

Suppose that I have a list: ...
3
votes
2answers
158 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
3answers
554 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
3answers
688 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? ...
2
votes
2answers
219 views
1
vote
2answers
115 views

Why does using ReplaceAll twice produce a different result?

Why does this: Solve[a x^4 + b x^3 + c x^2 + d x + e == 0, x] /. {2 c^3 - 9 b c d + 27 a d^2 + 27 b^2 e - 72 a c e -> F} produce a different result from ...
1
vote
2answers
125 views

How to replace subscript symbols

I have an expression with subscripted variables. I would like to replace all those terms with other symbols. For example, $P_1 + P_2^3 \rightarrow P1 + P2^3$ I used the following code but it doesn't ...
0
votes
1answer
235 views

Arbitrary depth patterns/rules

Temporary message: I am now really confused. I am not sure how using Power and Unevaluated together works in the examples below. While answering this question, I stumbled upon the following. We ...
-1
votes
1answer
78 views

How to make rules for changing parameters

I have one boring problem with changing parameters in printing output. If I have equation ...