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

learn more… | top users | synonyms (4)

8
votes
2answers
761 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
1answer
231 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
201 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
177 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
300 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 ...
7
votes
5answers
3k 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}} ...
7
votes
4answers
446 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
3answers
303 views

Safely nesting RuleDelayed

Say I have the following, straightforward-seeming functions: ...
7
votes
3answers
465 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: ...
7
votes
4answers
204 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
2k 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
162 views

Probablity weighted ReplaceAll

I'm looking for an elegant way of doing a probability weighted replacement rule, something like this ...
7
votes
3answers
183 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
242 views

Is there a faster way to Map an Association?

Consider mapping an existing Association in a manner such as this: ...
7
votes
1answer
522 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
120 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
114 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
106 views

Why does ToRules return a Sequence?

Why does ToRules return a Sequence expression? Why doesn't it return directly what ...
7
votes
1answer
191 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
5answers
334 views

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

Considering we have a list of rules: ...
6
votes
4answers
326 views

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

Considering we have a list of rules: ...
6
votes
5answers
221 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
539 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
401 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
211 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
394 views

Conditionally replace rows in lists

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

How to make Missing[] invariant for some functions without redefining them

Missing[...] "is a symbolic object, with no default evaluation rules defined." Missing data can be tagged and enables use of filters like DeleteMissing. So it's worthwhile to normalize various types ...
6
votes
2answers
266 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
5answers
137 views

Setting value of a rule

How do I change the value of a rule? For example, If I have a = x->3, how do I reset the value of x/.a to 4, so that ...
6
votes
2answers
247 views

Apply Trig Identity to Transform Sum of Sinusoids into Phase-Shifted Cosine Function

I'm trying to solve for the output of a simple RC low-pass filter, with Cos[w t] input. The convolution of the input with the RC transfer function gives me the right output, but I'd like to be able to ...
6
votes
1answer
213 views

Better replacement rules to perform this change of variables?

In cosmology, it is common to work with both a normal time variable t and a “conformal” time variable τ, which is defined such ...
5
votes
4answers
285 views

ReplaceAll is not working correctly

The line of code -E^(-I k) x /. {-x -> -x, -E^(-I k) x -> -E^(I k) x} is not properly replacing the exponential of (-I k) with the exponential of (I k), as ...
5
votes
3answers
2k 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". ...
5
votes
4answers
316 views

Long list of similar rules

Given the following: ...
5
votes
4answers
269 views

How to split a list with respect to its nested sublists?

I have a problem with the following task: having some nested list list = {{k6},{k1,k3,k5},{k7}} I would like to replace all sublists by their permutations and ...
5
votes
3answers
397 views

Simple image processing very slow

I have compared the speed of some simple image processing routines between Mathematica an IDL. 1. Reading of a grey scale png image of (720,577) pixels: The image is: Mathematica: ...
5
votes
3answers
329 views

Replace x, but not x[a] [duplicate]

I want to replace all occurences of the symbol x with x[a], as long as it is not in the form ...
5
votes
1answer
325 views

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

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

Assign output of solve to variables [duplicate]

I have this input: Solve[x^2 + 3 x + 2 == 0, x] which gives this output: {{x -> -2}, {x -> -1}} I want the first x ...
5
votes
2answers
122 views

ReplaceAll performance problem: packed arrays on the LHS are unpacked when the RHS is too long

The following is one of the most mysterious performance problems I encountered and came up while extending the booleval function mentioned here. I am looking for a ...
5
votes
1answer
196 views

How can I make a “self-referential” replacement operation self-contained?

Fairly often I find use for replacement rules that call themselves on the right-hand side of the rule, e.g.: ...
5
votes
1answer
198 views

Conditional string replacement

I have a list of text and need to replace an element based on the text that appears in the element before it. Example of my list: ...
5
votes
1answer
277 views

Embedding StreamPlot in Graphics3D

I am trying to visualise the action of the infinitesimal generator of a Lie group on the solution of a 2-ODE system drawn in the 3D space (t,y1,y2): t is time and y1,y2 are the two dependent ...
5
votes
2answers
121 views

Why does Rule behave like this?

Why does {x, x^2, x^3, a, b} /. x^n_ -> f[n] result in {x, f[2], f[3], a, b} when ...
5
votes
2answers
167 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: ...
5
votes
2answers
94 views

Replace expression in series expansion [duplicate]

A test case: I'm trying to replace an expression inside a series expansion: Series[f[x],{x,x0,4}] ./ (x-x0)->h but it still returns ...
5
votes
1answer
168 views

Simplifying a b = 1 in non-commutative products?

Yesterday, I try to define some rules for non-communicative multiplication, here. I find some quite helpful rules, but things still do not work properly; the distributive rule can't be applied (e.g.: ...
5
votes
3answers
282 views

When I use Rule, why doesn't this work?

k.k.k //. {k.k -> k} works perfectly, so the output is just k. But, when I use ...
5
votes
1answer
500 views

Apply color function to matrix plot

I want to apply the following color rules to a large table of numbers. I have extracted a part of the table to use as an example. I would like to assign 5 different colors to 5 different ranges of ...