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
1answer
226 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
174 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
286 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
2k 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
439 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
290 views

Safely nesting RuleDelayed

Say I have the following, straightforward-seeming functions: ...
7
votes
3answers
441 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
156 views

Probablity weighted ReplaceAll

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

Is there a faster way to Map an Association?

Consider mapping an existing Association in a manner such as this: ...
7
votes
1answer
117 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
111 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
103 views

Why does ToRules return a Sequence?

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

How to switch from List of rules to Association?

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

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

Considering we have a list of rules: ...
6
votes
5answers
217 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
499 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
391 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
205 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
377 views

Conditionally replace rows in lists

I have a sequence of matrices/lists ...
6
votes
3answers
755 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
183 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
5answers
130 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
235 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
507 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
1answer
209 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
3answers
1k 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
315 views

Long list of similar rules

Given the following: ...
5
votes
3answers
363 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
4answers
261 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
321 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
293 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
300 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
117 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
195 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
178 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
264 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
118 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
91 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
153 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
271 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
469 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 ...
5
votes
1answer
279 views

Why does this pattern for combining two lists require triple underscores (___) to work?

I understand that __ is a list of one or more elements, and that ___ is 0 or more elements, but when I try this rule with 2 ...
5
votes
1answer
196 views

Generating replacement rules programatically

I would like to generate sets of replacement rules programatically for predefining some permutations. So a function ...
5
votes
1answer
400 views
5
votes
2answers
222 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 ...