Stack Exchange Network

Stack Exchange network consists of 175 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
1answer
33 views

Replacing with Elements from a List [duplicate]

I have a fairly complicated expression with nested lists and I want to substitute a given string for each integer. Here's a simple example where ind is my integer-valued data structure and names is ...
1
vote
0answers
21 views

Moving to the last placeholder with inputautoreplacements

I have had a long-standing problem when I try to use my inputautoreplacements in a text cell. The replacements I have made include boxes that include placeholders. When in the text cell, you type ...
4
votes
1answer
90 views

A single argument pattern definition applies to multiple-argument patterns?

Consider defining a pattern rule, such as myFun[x_]:=x As far as I understand Mathematica syntax, this rule means ...
1
vote
3answers
91 views

Substitution in expressions

As the title suggests, I want in an expression to to some substitutions. For example in the expression $\mathcal{L}^+_0+\mathcal{L}^-_0+\mathcal{L}^+_0+\mathcal{L}^-_0+\mathcal{L}_1$ I want to ...
0
votes
1answer
45 views

Replacing a derivative [closed]

If I want to calculate e.g. $\frac{dr(t)}{dt}$ then in Mathematica we can write e.g. , answer = D[r[t],t] This outputs r'[t] ...
0
votes
1answer
37 views

constructing Matrix from coefficients of solution & assigning 0 for empty Cells [on hold]

i tried to construct a matrix from the coefficients of my solution, but problem that; 1; how can i get coefficients out of Solve 2; some Matrix elements are empty so i must assign 0 in those empty ...
0
votes
1answer
46 views

Replace constant in summation function using replace or If or /

Hi i how do i replace constant for k in this summation. For following: Even 2*n Even replace k by a Even 2*n+1 Odd replace k by b Here is the code ...
5
votes
1answer
46 views

Inconsistent application of replacement rule

I want to use a replacement rule to change log[var] into var inside a function. A minimal example follows. In one case it ...
4
votes
2answers
91 views

Finitely many repeated replacements

I'm looking for good practices to replace a given function recursively n times by itself with its arguments. The following example (for n=2) hopefully makes it clear: ...
0
votes
1answer
46 views

Using the Replace function [closed]

How can I replace two variables at once with Replace[] ? Can't find how to do this. I tried Replace[x^2+y^2, {x -> a, y -> b}] but does not work...
7
votes
2answers
123 views

Replacement Rule for Functions

Attempting to write a rule that will substitute an undetermined function of several variables by some other undeterminate functions (also, of the same several variables). I can do it in an inelegant ...
2
votes
3answers
144 views

How to avoid Replace substituting subscripts?

I have a polynomial in $\rho$ with many parameters (I will omit the polynomial code in the Mathematica expressions below because it is too long): $$ \rho ^3 J (\mu +1) \left[(A \delta \tau _0+B (1-\...
0
votes
1answer
39 views

Substitution inside an expression [duplicate]

I'm trying to get mathematica to replace an expression with a reduced form. I wanted to replace $(k/w)$ with $h$ in a particular expression, and I've tried something like: ...
5
votes
1answer
225 views

Neglect higher order derivatives in expression

Say I have an expression of this sort: Derivative[5][x1][t]+Derivative[3][x2][t]+x1[t]+Derivative[2][x1][t] and so on... I would like to ask Mathematica to ...
1
vote
2answers
48 views

Replacement rule for terms that are separated by operators?

My main problem is how one can create a "transcending between operators rule" in the same term according to the minimal example bellow. Suppose I need to perform a replacement of the form ...
1
vote
1answer
43 views

Question about replacement and plot

I want to plug a value into the following solution which I defined. ...
1
vote
3answers
56 views

How to apply a continious substitution reduction of (-1 + 3 phi - 2 phi^3 - 6 phi^4 - 20 phi^6 + 6 phi^7 + 3 phi^8 + phi^9)//.{phi^3->1-phi^2}

I have the answer to $(-1 + 3 phi - 2 phi^3 - 6 phi^4 - 20 phi^6 + 6 phi^7 + 3 phi^8 + phi^9)/.{phi^3->1-phi^2}$, that is not the problem. I am looking for a method that continues to apply this ...
4
votes
1answer
188 views

Setting Some Elements in a Matrix to zero

Assuming I have the a 5x7 matrix below: b = ((1, 4, 9, 16, 25, 4, 2), (36, 49, 64, 81, 100, 5, 1), (121, 144, 169, 196, 225, 8, 9), (256, 289, 324, 361, 400, 9, 5), (441, 484, 529, 576, 625, ...
2
votes
2answers
67 views

Elements Replacement

Please I need help on how to replace elements in a matrix with another elements. For example, I have a (5,5) matrix, and I like to replace elements from All rows and column 3 to 5, with zero. such ...
2
votes
1answer
117 views

Notation package with Traditional Form

I symbolize subscripted variables with the form TraditionalForm, to enable easy use of TeXForm as suggested here: ...
1
vote
2answers
32 views

Replacing a subset of variables in a polynomial [closed]

Let's say I have a polynomial in n variables, e.g. x^3 + xyz + z^2, where n=3 in this case. How can I replace all of the variables in the equation with a certain number? I tried things like ...
1
vote
2answers
39 views

Replacement rule for a function and its derivative

I have this equation: EOM1 = Subscript[x1, t, t] + ω1^2*x1 /. Subscript[x1_, m__] :> D[x1[x, t], m] /. x1 -> x1[x, t] The problem with the ...
1
vote
1answer
58 views

Convert String in Expression to Integer

I have reviewed many string replacement Q&A but I have been unable to apply them to this case (MWE): ...
2
votes
0answers
26 views

Using Min & Max of an Interval in a replacement [closed]

I want to use a replacement rule to transform a list of equations. The following works as expected: ...
3
votes
2answers
96 views

Convert defined variables to rule list

I'd like to make a function that takes a list of variables and returns a corresponding rule list with the current values of the variables. E.g. ...
1
vote
0answers
40 views

Why doesn't `ReplaceAll` work in this situation? [closed]

I can work around this by using With, but it seems really odd to get the following: ...
2
votes
3answers
45 views

Using rule output as function

Im trying to define a function using the output from RSolve, however i cant get it to work properly. I have a RSolve that output something like: ...
1
vote
2answers
59 views

Having problems with using TableForm to format a table

I want to make a table of various values of the logistic curve given by l[x_] := 1/(1 + E^((-k)*(x - \[Alpha]))). In basic form, this is dead easy. However, I want ...
1
vote
2answers
66 views

Why does this replacement rule for imaginary unit not work? [closed]

I'm trying to replace imaginary unit in an expression with a real unit: E^(-I y) /. I -> 1 E^(-I y) As you can see, in ...
1
vote
1answer
41 views

ReplaceAll giving different answer than actual replacement

I have an expression, and I wanted to evaluate it for a certain value of a variable. However, /. gives a calculation error, whereas manually replacing the variable with the value succeeds. Why? $$\...
1
vote
3answers
70 views

How to make functional rules not depend on an argument?

Consider the following: rule = f[x] -> Sin[x] if I now do, say: f[y] /. rule this does not work, because the rule wants ...
1
vote
1answer
21 views

Merging values of a list of rules where some keys occur more than once

I have a list of rules resembling rules = {{a -> {1, 2, 3}}, {b -> {2, 3, 4}}, {c -> {3, 4, 5}, c -> {4, 5, 6}}} How can I neatly rearrange this to ...
1
vote
0answers
45 views

Do replacement only on certain specific forms

If I want to replace, say, a with a1 + a2 in an expression, I can write ...
1
vote
3answers
86 views

Is there a possibility of adding a second condition, or an else clause to $/;$?

Let tally=RandomInteger[{0, 1}, 10]; In the code below, I'm trying to output a list where each component $C_n=\sum^n_{i=1} \text{tally}_i / n$ The problem with my ...
0
votes
1answer
64 views

Algebraic substitution for polynomial simplification

Consider a polynomial $p(x,y)$ and we want to simplify $p(x,y_0)$ where $y_0$ is a root of some other polynomial $q(y)$. In Maple I would use something like: ...
1
vote
2answers
47 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
40 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
320 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
18 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
48 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
33 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
45 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
76 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
177 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
101 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
339 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
42 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
63 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
48 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
53 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 ...