# Tagged Questions

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

213 views

### How to make a ReplaceAll Rule for adjacent items in a list?

I would like to make only one pass of a list of 2D items and replace adjacent items that have matching $x$ or $y$ coordinates with a series of linearly interpolated points between the two points. My ...
439 views

### How can I replace the Greek symbols in an expression with their plain text names?

Suppose I have an expression with Greek symbols in it. How would I convert that expression to another expression where the Greek symbols have been replaces with their names in plain text? I've ...
199 views

### Fast implementation of Metropolis-Hastings update with multiple sets

This is an optimisation question rather than something I am unable to do. I have a naive, primitive working version of the code (I submit it below) but I am trying to make it work faster. I am asking ...
172 views

### What kind of patterns in a list of rules would benefit from being Dispatched?

I have a long list of rules for manipulating expressions. They are of the form: ...
359 views

### Performance of Dispatch and lists of Rules

The issue outlined here seems to be no longer present in version 10, if you use a Dispatch table! This because a Dispatch table ...
108 views

### MapAt with Replace(All) causing kernel crash

Bug introduced in 10.1 and fixed in 10.2 I wanted to roll a function that works like /. but only acts on the very first match it encounters. ...
328 views

### Indexed replacement

Is there some equivalence of MapIndexed for rules? Consider a substitution on 3 a + 4 b + 21 c + .... The first integer I want to replace by 1, the second ...
321 views

### Unexpected behavior of rule matching a pattern

I am a beginner exploring the world of Mathematica. I expected the following code T[6, 5, 4, 1, 2, 3] /. {T[a___, 1, b___] -> Length[List[b]]} should return ...
225 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 ...
362 views

### Safely nesting RuleDelayed

Say I have the following, straightforward-seeming functions: ...
266 views

### Applying a transformation rule on an Image

I haven't been able to figure out why the following doesn't work: ...
607 views

### Piping and Replacement

I haven't been able to figure out how to use piping with certain functions. Specifically, I would like to apply a replacement at the end of a pipe, like the following: ...
212 views

### How do I write a function that can be used in a rule to modify both sides of an equation? [duplicate]

I sometimes need fine grain control over equations in Mathematica in order to help me understand how to solve a problem manually. A greatly simplified example of a session might be something like this:...
248 views

### Why is Block significantly faster than ReplaceAll for inputting variable values

Given that the documentation for FindMinimum uses ReplaceAll for its various examples of what to do with the found solution to ...
295 views

### Replacing product expressions - named vs. unnamed pattern?

Suppose I have an expression of the form a b c d + x y z The FullForm of this is ...
256 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 ...
809 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 ...
120 views

### Why is a longer pattern matched rather than a short pattern?

I was reading the Mathematica V5 Book and am a bit perplexed on the behavior of a particular pattern matching example where it seems to be choosing a longer pattern rather than a shorter pattern. The ...
254 views

### Transformation rule on a list of non-negative integers

I am trying to create a transformation rule that takes a list of non-negative integer values of any length, finds a non-zero entry in the list, adds 1 to all preceding numbers, subtracts 1 from the ...
207 views

### Representing a value in an output as a self defined variable

I'm trying to find a way to have Mathematica always represent a numerical value as a self defined variable that I define using lhs=rhs. For example, if I execute <...
192 views

### Substituting values in 3j symbols

I believe these three expressions should give the same answer of -1/Sqrt[3], but the first gives +1/Sqrt[3] whereas the second ...
85 views

### On an existing notebook, how to transform all “double brackets” into “DoubleBracket”

Sometimes, having all double brackets [[...]] transformed into \\[LeftDoubleBracket] ... \\[RightDoubleBracket] can really ...
140 views

### Why does ToRules return a Sequence?

Why does ToRules return a Sequence expression? Why doesn't it return directly what ...
310 views

### Mma10 mystery: Failure of pure function substitution with Interpretation?

Bug introduced in 10.0.0 and fixed in 10.0.1 I had code to display with subscripts from Displaying index as subscript on output: e.g. C[i] -> C_i with Notation[...] or Interpretation[..]? which ...
233 views

### Lowering the degree of an polynomial with an assumption that the polynomial has a factor x^2+ax+b

Let's assume $x^{10}+x^5+1$ has a factor $x^2+ax+b$ Then, If $x^2=-ax-b$, $x^{10}+x^5+1 =0$. If we successively apply $x^2=-ax-b$ to $x^{10}+x^5+1$, we can make $x^{10}+x^5+1$ to degree 1, ...
474 views

I have a list of strings and I would check if there is an empty string element, that's "", because I need to replace it with a "0". I try the following ...
153 views

### Replacing elements in lists of a nested list

I have the following nested list ...
210 views

### Generating all matrices with 1 (possibly) replaced by -1

I have a matrix $M$, whose dimension I am unsure of, which has only $\lbrace0,1\rbrace$ entries. I would like to generate all the possible matrices that result from changing (some subset) of the $1$'s ...
168 views

### A replacement question

I have: {{θ -> 0}, {θ -> (2 π)/ 3}, {θ -> π}, {θ -> (4 π)/3}} I would like to create a list of points $(\cos\theta,\sin\theta)$ using each of the ...
280 views

### Obtaining the number of iterations used in ReplaceRepeated

ReplaceRepeated has an option MaxIterations which indicates Mathematica is keeping track of the number of iterations. Is there ...
4k 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)$ ...
188 views

### How to prevent substitution in a part of an expression?

Background: I am trying to write a program that can execute scheme in Mathematica. Now I have things like {"+", 3, {"-", 2, {"'", {"*", 1, 3}}}}. The syntax ...
167 views

### Probablity weighted ReplaceAll

I'm looking for an elegant way of doing a probability weighted replacement rule, something like this f[x_] := x /. {1,2,3,4} -> {50%{ 4,3,2,1} ,50%{1,3,2,4}} ...
195 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*) ...
122 views

### Using two rules for the same variable

I am a newbie in the field so please not angry. I am reading the tutorial below and having a problem. Link: How to Create and Use Rules From the link: If you give two rules for the same ...
187 views

### Remove leading and trailing zeros from a list

This functions and gives the desired result: {1, 0, 2, 0, 0} /. {x___, 0 ...} :> {x} {1, 0, 2} But with leading zeros it doesn't: ...
349 views

### Is there a faster way to Map an Association?

Consider mapping an existing Association in a manner such as this: ...
585 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: ...
130 views

### Why does an inert ReplaceAll modify this Dataset?

I cannot see any reason for this behavior besides a bug, but I've been wrong before so I put it before the community: ...
135 views

### Is Replace doing double work?

In an answer to this question, rm -rf used the function Replace with level spefication to do a series of replacements inside out. As far as I can see, the order of substitutions when more than one ...
201 views

### What is the principle behind Dispatch?

I want to know what Dispatch actually does to rule the list. Why is it so fast? The documentation says Dispatch generates a dispatch table which uses hash ...
925 views

### Replacing Tan[x] with Sin[x]/Cos[x]

Why doesn't this work? (1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Sin[x]/Cos[x] I get back: ...
673 views

### How to replace a space by a comma

in this expression = "{1 2 3 4 5 6 7 8}", I want to replace each space by a comma , to get ...
464 views

### How to switch from List of rules to Association? [duplicate]

Considering we have a list of rules: ...
398 views

### How to delete Missing[“NotAvailable”] from list of rules? [duplicate]

Considering we have a list of rules: ...
330 views

### Long list of similar rules

Given the following: ...
250 views

### How to program a click panel for chemical elements (like a periodic table)?

The command ColorData["Atoms", "Panel"] indicates RGBColors of atoms by clicking a panel. As I don't want to reinvent the ...
712 views

### How to make all numbers equal to one in a Series?

I have many outputs of one-dimensional Series expansions for which I am only interested in the general tending with the variable. For example, I would like to be able to transform something like <...