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

learn more… | top users | synonyms (4)

9
votes
1answer
189 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 ...
9
votes
1answer
171 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: ...
9
votes
1answer
356 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 ...
9
votes
1answer
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. ...
8
votes
7answers
326 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 ...
8
votes
2answers
316 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 ...
8
votes
2answers
223 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 ...
8
votes
3answers
355 views

Safely nesting RuleDelayed

Say I have the following, straightforward-seeming functions: ...
8
votes
3answers
265 views

Applying a transformation rule on an Image

I haven't been able to figure out why the following doesn't work: ...
8
votes
3answers
588 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: ...
8
votes
2answers
210 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 ...
8
votes
1answer
247 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 ...
8
votes
2answers
285 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 ...
8
votes
1answer
251 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 ...
8
votes
2answers
798 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 ...
8
votes
2answers
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 ...
8
votes
1answer
248 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 ...
8
votes
2answers
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 ...
8
votes
3answers
187 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 ...
8
votes
1answer
81 views

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

Sometimes, having all double brackets [[...]] transformed into \\[LeftDoubleBracket] ... \\[RightDoubleBracket] can really ...
8
votes
1answer
307 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 ...
8
votes
0answers
149 views
7
votes
4answers
471 views

Strange answer from StringReplace

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 ...
7
votes
4answers
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 ...
7
votes
3answers
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 ...
7
votes
3answers
3k 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)$ ...
7
votes
2answers
165 views

Probablity weighted ReplaceAll

I'm looking for an elegant way of doing a probability weighted replacement rule, something like this ...
7
votes
3answers
191 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*) ...
7
votes
1answer
117 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 ...
7
votes
1answer
168 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: ...
7
votes
1answer
327 views

Is there a faster way to Map an Association?

Consider mapping an existing Association in a manner such as this: ...
7
votes
1answer
574 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: ...
7
votes
1answer
129 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: ...
7
votes
2answers
134 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 ...
7
votes
2answers
130 views

Why does ToRules return a Sequence?

Why does ToRules return a Sequence expression? Why doesn't it return directly what ...
7
votes
1answer
199 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 ...
6
votes
3answers
864 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: ...
6
votes
5answers
442 views

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

Considering we have a list of rules: ...
6
votes
4answers
617 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 ...
6
votes
4answers
373 views

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

Considering we have a list of rules: ...
6
votes
5answers
229 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, ...
6
votes
3answers
688 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 ...
6
votes
3answers
464 views

Replacement rule only matches part of expression

I have the result of a calculation (which is too long to mention here) and it gives ...
6
votes
3answers
218 views

What's wrong with naming a rule?

Please have a look at the code below. The second Manipulate uses the name rl of the replacement rule given in the first line, ...
6
votes
3answers
268 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 ...
6
votes
2answers
189 views

How can you resize all the images in a notebook section?

I've pasted a large number of photos and they are too big which makes scrolling hard. Is there a way to automatically have Mathematica size them to 10% or something reasonable without have to click on ...
6
votes
2answers
331 views

Matrix exponential via Cayley-Hamilton Theorem

I'm attempting to calculate the exponential of a matrix via Cayley-Hamilton theorem. (Following the "concrete example" from http://en.wikipedia.org/wiki/Cayley%E2%80%93Hamilton_theorem) I am having ...
6
votes
1answer
505 views

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

In my program I have an expression which looks like: ...
6
votes
2answers
447 views

Conditionally replace rows in lists

I have a sequence of matrices/lists ...
6
votes
3answers
865 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 ...