Questions tagged [replacement]
1636 questions
23
votes
1 answer
Is there a difference between Replace with parameter "All" and ReplaceAll
Here is my extremly basic understanding of Replace and ReplaceAll.
This post is also a way for me to check if I understood the mechanism behind, if you see mistakes in my explanation don't hesitate to correct me !
Replace is a function that will…
StarBucK
- 2,164
- 1
- 10
- 33
21
votes
4 answers
Replace custom functions, leave built in functions untouched?
I have three expressions a[x, y], b[x, y], c[x, y] that act as placeholders for functions of two variables x,y. Consider the following substitution:
a[x, y]/(b[x, y] c[x, y]) /. f_[x1_, y1_] :> f[2 x1, 3 y1]
a[2 x, 3 y]/(64 b[x, y]^3 c[x,…
Kagaratsch
- 11,955
- 4
- 25
- 72
19
votes
2 answers
Why Infinity /. _?(# < 6 &) -> 0 replaces Infinity with ComplexInfinity?
Suppose I define a matrix:
mat = {
{Infinity, 4, 9},
{6, Infinity, 10},
{8, 9, Infinity}
};
Now I want to replace all values smaller than 6 with 0,
mat /. _?(# < 6 &) -> 0
However, that ends up replacing Infinity with…
a06e
- 11,327
- 4
- 48
- 108
18
votes
1 answer
How to ReplaceAll except within specified heads
ReplaceAll[expr, rule] and ReplaceRepeated[expr, rule] search through all subexpressions of expr applying rule where they match.
Are there built-in Mathematica functions that do the same thing, except not make any replacements inside specified Heads…
QuantumDot
- 19,601
- 7
- 45
- 121
15
votes
7 answers
How to assign a list of numbers into letter grades
Here is my list:
collection = {76.6256, 51.9264, 50.238, 14.4203, 80.9205, 12.2036, 2.39568,
38.2747, 12.4422, 29.9621}
collection /. {x_ /; x < 50 -> "F"}
and this gives me
{76.6256, 51.9264, 50.238, "F", 80.9205, "F", "F", "F",…
Sultan of Swing
- 913
- 1
- 6
- 19
15
votes
3 answers
substitution rules excluding subscripted variables
Suppose I have an expression containing p,Subscript[p,1],Subscript[p,2],etc. I want to replace p with a numerical value while keeping the other two variables intact. How can I do it?
If I naively use /.{p->0.5} for example, I will get…
wdg
- 1,189
- 9
- 18
12
votes
1 answer
Using PatternSequence on its own in ReplaceAll?
Say I have this list:
list = {0, 1, 2, 0, 1, 3, 0, 1, 4}
and say I want to remove consecutive sequences of 0 and 1.
I'd normally go for this:
list //. {before___, 0, 1, after___} :> {before, after}
{2,3,4}
But a more concise way would be the…
Teake Nutma
- 5,981
- 1
- 25
- 49
12
votes
4 answers
“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}
Output is
{{1},3.5}
Since Mathematica evaluate the rhs immediately, I think the “correct”…
luyuwuli
- 2,814
- 19
- 25
12
votes
9 answers
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 integer by 4, and the i'th integer by i^2.
1 a + 4 b + 9 c + ....
How would you do…
sjdh
- 7,757
- 5
- 37
- 47
11
votes
1 answer
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 codes to specify which sets of rules need actually be scanned for a particular input…
matheorem
- 17,132
- 8
- 45
- 115
11
votes
4 answers
Is there a way to replace only once if possible by trying the rules sequentially in order?
Is there a ReplaceOnce which does only one replacement if possible by trying the rules sequentially in order. Consider the following as an example:
ReplaceOnce[{"May","5","May","5"},{"May"->1,"5"->2}]
should…
user13892
- 9,375
- 1
- 13
- 41
11
votes
4 answers
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:
(x^2 + y + Log[z])^2 /. {y -> x/2} // Expand /. {z -> E^x}
Also, is there a…
jmbejara
- 427
- 5
- 14
9
votes
3 answers
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 rewritten in terms of the values $T_j = \frac\pi{j}…
JT_NL
- 983
- 1
- 6
- 16
8
votes
3 answers
Safely nesting RuleDelayed
Say I have the following, straightforward-seeming functions:
makeARuleDelayed[a_, b_] :=
With[{anotherRule = makeAnotherRuleDelayed[b]},
v : a[b] :> ((v == 1 - v) /. anotherRule)];
makeAnotherRuleDelayed[x_] :=
v : h_[x] :> foo[h, x];
If I…
Pillsy
- 18,498
- 2
- 46
- 92
7
votes
3 answers
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 values in this list. That is, I want a simple way to convert to:
$$\{(\cos 0,\sin 0),…
David
- 14,883
- 4
- 44
- 117