Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [replacement]

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

1
vote
2answers
36 views

Question about replace command

I want to replace the common part in the equation by different value. Here is my equation ...
2
votes
1answer
30 views

Using OverBarred variables

Mathematica does not seem to treat variables as a unit. Consider the following: rule = a -> Function[{x}, OverBar[a][x]] test = a[x] /. rule; test /. rule It ...
7
votes
5answers
310 views

Unexpected behaviour of Nothing inside List inside Association

When evaluating the following input: <|a -> {c, d, e}|> /. d -> Nothing I get (expression 1): ...
0
votes
0answers
16 views

Plotting multiples figures using rules [duplicate]

Consider the following: rule = {a -> x+1, b -> x^2, c -> 3x^3+1} Plot[{a, b, c} /. rule, {x, 0, 1}] This, unfortunately, does not color each plot with a ...
0
votes
1answer
34 views

Use of module and nested variables [closed]

Consider the following: rule = x -> 2 para + 1 funct[para_] := Module[{x}, x /. rule] this unfortunately does not work. If I put the content of "rule" ...
0
votes
1answer
44 views

Replacement of the patterns in the replacement

I want to make such changes: $(A\, p + B\, q)[i] \to A\, p[i]+B\, q[i]$ But the replacement below can not do this ...
0
votes
1answer
29 views

Expression replacement with variable exponents

I have an expression that consists of a large sum of variables of powers like: X[1]^(2p) X[2]^2 + X[1]^2 X[2]^(2p) + X1[1]^(1+p) X[2]^(1+p) ... Now I would like ...
1
vote
1answer
43 views

Replacing powers with ReplaceRepeated

I have question about ReplaceRepeated. Consider $\qquad\mathsf{FullSimplify}\left[\left\{\frac{q^5-q^4}{q^5-2 q^4+2 q^2-q}\right\}\text{//.}\, \left\{q^3\to 1\...
0
votes
4answers
70 views

Implementing a Dirac Delta like function symbolically

I have an arbitrary expression where some terms are multiplied with a Dirac delta 'like' function d(x,y). For example, e^(ax+by+cz).d(x,y).d(a,c) + e^(ax+by+cz).d(x,y)+e^(ax+by+cz) I need to get ...
10
votes
3answers
171 views

Perform localized, evaluation-leak free replacements

Consider the following expression: expr = Hold[ { f[ {1, {Print@1}} ], g[ {{{Print@1}}} ] } ]; I'm looking for a way to ...
3
votes
1answer
94 views

“Smart” substitution of subexpressions

I have the following question. An expression, which I want to simplify contains several subexpressions which appear quite frequently all over the place. To optimize simplification I would like to ...
14
votes
1answer
327 views

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 ...
1
vote
1answer
39 views

Replace an If by a combination using replacement rules

Consider the following code : tttest = a^2 + If[a > 0, a, -a] a^2 + If[a > 0, a, -a] I would like to replace my If function by something like fonction @ ...
0
votes
2answers
62 views

Replacement rules with list as matching

How can I use replacement rules to match and work with a list. For example, {1, {2, 3}} /. {{a_, b_} -> ((a + # &) /@ b)} outputs ...
2
votes
2answers
47 views

Replace a an expression in Exponential only but not in other terms?

I want to replace the A in the Exponent only but when I use the replace rule it also replaces the Derivative of A. ...
2
votes
2answers
51 views

Expand a recursion relation

Hermite Polynoms have the following recursion relation: $$ H_{n+1}(x) = 2x H_n(x) - 2n H_{n-1}(x), $$ which can be rearranged to be $$ x H_n(x) = \frac{1}{2} H_{n+1}(x) - n H_{n-1}(x).$$ Is it ...
0
votes
1answer
61 views

Pattern matching on Orderless functions

I'm trying the following pattern matching command: ReplaceList[Hold[b + a], Hold[x___ + y___] -> {{x}, {y}}] And Mathematica returns: ...
2
votes
1answer
43 views

ReplaceAll rules for Power function

I have the following function: eW = fL11(u1L^r + u2L^r) + fM11(u1M^r+ u2M^r) + fH11(u1H^r + u2H^r) The full form of eW is then: ...
3
votes
2answers
104 views

Replace $ z z^* $ by $ |z|^2 $

I have a long expression involving different complex numbers. I want to force Mathematica to replace z Conjugate[z] by Abs[z]^2 ...
0
votes
3answers
64 views

Transformation rule for a sum of functions

When applying the rule: exp[c[x] + d[x] + e[x]] //. exp[a_[x] + b_[x]] -> exp[a[x]] + exp[b[x]] I would like to get ...
1
vote
2answers
68 views

Replacement of the variables

I have lots of functions of the form $-x_4^2+x_0 x_4-x_1 x_3-x_5 x_7+x_1 x_{13}+x_2 x_{22}$ (this is $eq_4$ see below). I want to replace each pair $x_i x_j$ by $y_l=x_i x_j$. How is it possible to do ...
2
votes
1answer
33 views

Defined equality does not show in ContourPlot

I have some function definitions that ultimately define an equality through Solve and Replace: ...
2
votes
2answers
38 views

Symmetry of a Dot with functions with arguments

Seemingly an easy question, but I'm trying to avoid using a straight replacement rule. As an example, I could take a term like p[i].p[j]-p[j].p[i] where this ...
2
votes
1answer
46 views

Replacing values in a dataset

Could someone explain why ...
2
votes
0answers
35 views

Fold/ NestList/ etc in MCMC loop for optimisation

I am trying to simplify and optimise an old loop code I wrote. Since Fold and NestWhile use the results of the last iteration in the new iteration, I would like to use them in order to get rid of Do ...
1
vote
1answer
33 views

Replacement into a list

I have the following polynomials: pol = {x^2 + 2 x^3 + 4 x^7, x + 2 x^5 + 4 x^6, 3 x^6 + 2 x^9}; and following list ...
4
votes
1answer
148 views

Efficient way to add new rules to a sublist within a nested list of rules

I often build nested lists (as a way to keep data sets organised) with some form like: ...
3
votes
4answers
102 views

How can I replace a function? Something like f[x] /. f -> -g. ReplaceAll doesnt seem to work properly when there is a minus sign

How can I replace a function with minus another function? This: Sin[x] /. Sin -> Cos works just fine. But this: ...
1
vote
1answer
73 views
2
votes
1answer
70 views

How do you find a sub-graph and replace it with another subgraph?

I saw a video where SW shows how he has a large graph and some rules for finding a given subgraph in a graph and replacing it with another subgraph. How would one go about doing this in Mathematica? ...
5
votes
1answer
34 views

Overriding TwoWayRule for in-place element swapping

I'd like to make \[TwoWayRule] work for things other than graph edges, for example, for swapping i.e. Replace, ReplaceAll, StringReplace, etc: ...
2
votes
2answers
169 views

Wrestling with substitution rules

Summary I am struggling with substitution rules. Example Here are several cases which are problematic: ...
2
votes
1answer
35 views

Replace rule using a wildcard

I have a complicated expression which I want to simplify following a rule. For example, if I want to make the following replacement: A[t] - B*A[t - d1] = Y[t] ...
2
votes
1answer
56 views

Understanding conditional replacement

I have encountered recently a replacement and I tried to look up documentation but could not quite find anything and hope to get some explanation from experts. The replacement is the following: ...
0
votes
1answer
35 views

Substituting values

I have a little program that should find values that Solve a system of polynomic equations and then substitute those values function. It solves the system but then, the substitution doesn't happen. I ...
3
votes
4answers
240 views

Basic substitution

I am relatively new to Mathematica, so I am sure this is basic, but I lack the basic terminology to be able to google it. How can I make this command work? ...
5
votes
1answer
40 views

how to comfortably concatenate substitutions

Often I have to do perform a succession of substitutions on some expression, and in a notebook the way I know to do that is ...
6
votes
3answers
142 views
1
vote
1answer
24 views

Substitution of expressions for the output

I have a code which gives this kind of output : W = Sum[v[j]*b[j], {j, 1, 3}]; AA = (v[1]*b[1] + v[2]*b[2] + v[3]*b[3])*2 + 3 ; I know that if i want to ...
3
votes
2answers
176 views

Substituting 0 for even and 1 for odd natural numbers using EvenQ[] [closed]

I am trying to define a substitution that gives 1 for odd numbers and 0 for even numbers. My guess was (in a particular example) ...
0
votes
0answers
21 views

Define a function using pattern matching and repeated replacement to sum the elements of a list.(exercise in Paul Wellin's Book)

I am reading Paul Wellin "Essentials of Programming in Mathematica". I have a question about his answer to 4.1 Exercise 5. 4.1 Exercise 5: Define a function using pattern matching and repeated ...
1
vote
0answers
31 views

ReplaceAll does nothing with the rgb values of a picture [closed]

In the book "Programming with Mathematica: An Introduction" at the beginning of the chapter on Patterns and rules an example is given where the colors of an image are transformed according to ...
1
vote
1answer
37 views

Discrepancy between ReplaceList and ReplaceAll?

Consider the following ClearAll[eqv, not, neqv, p, q, r] ReplaceAll[eqv[neqv[p, q], r], neqv[p_, q_] :> not[eqv[p, q]]] ...
1
vote
1answer
41 views

Why does ReplaceAll match pattern while MatchQ fails

Why does the following pattern with MatchQ fail MatchQ[1+Sin[1] I, a_Complex /; Im[a] > 0] (* False *) Whereas, the same ...
0
votes
1answer
41 views

How can I apply If or Which in a multiple Line list?

I have a multiple Line list ...
1
vote
1answer
29 views

Replace function arguments multiple times?

I have a very large function f[x_,y_,z_] and it's too cumbersome to type it directly. So I wish to break the definition and use (...
5
votes
1answer
106 views

Replace targeted elements in first level of list only

I have a list where some elements are strings or sublists: ...
0
votes
3answers
98 views

Replacements within a mathematica expression?

Say I have many expressions of the form $$\text{expr} = \frac{1}{AB(C+D)^n E^m}$$ where $A,B,C,D$ and $E$ are symbols and $n,m$ arbitrary powers. In order to convert between the mathematica output and ...
4
votes
3answers
98 views

Repeated replacing elements in the list

I have a large list of the following form {{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1} and I want to combine all of the pairs ...
4
votes
3answers
355 views

How to replace a comma “,” with “||” in a list

I have a list of equations: dene = {x1== 0.2, x2== 0.4, x3==0.5}; I would like to convert this list into a rule: ...