# Questions tagged [replacement]

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

1,025 questions
Filter by
Sorted by
Tagged with
36 views

### ReplaceAll and complex numbers

I am currently trying to "canonicalize" my expressions with complex numbers. One thing I want to do is to replace expressions such as x x* (where * denotes complex ...
49 views

### How to get ReplaceAll to happen before function call

I have a problem I am attempting to solve with NMinimize. I have a scoring function, which will give me a final score. The problem has many variables, so instead of having them as parameters, they ...
124 views

### How to replace $x$ and $x^2$ with different value?

I have an expression now expr = x + x^2 what I want to do is to replace $x$ with $k\times p$ , and $x^2$ with $[k\times p + k\times(k - 1)\times p^2]$. The ...
51 views

### Hiding variable names in function definitions

I have a problem where Table and similar functions' substitution variable name can conflict with names in my function it self. Here's a minimal example ...
42 views

### Replace does not substitute in function

I have the following expression with functions ...
28 views

### ReplaceAll in a vector

I have the following code: ...
70 views

### Variable substitution like usual maths

So... I'm trying to get some simplifications on equations using own defined variables, but the tries I gave didn't result on what I wanted. The problem: I have 2 variables defined as: ...
57 views

### Replacement patterns

I wanted to ask if there was a way to quickly replace multiples of the same thing, instead of writing it so many times, for example: ...
36 views

### Replace sub-expressions matching a pattern unless located under specific heads

Suppose I have some rule, e.g. Re[x_] :> (x + Conjugate[x])/2. I want to apply it to all matching sub-expressions in some expression, excluding any sub-...
24 views

### Contracting the KroneckerDelta should bring it back

I would like to define a function $$V[\phi] = \sum_{1\leq i,j,k,l\leq dim}\lambda_{ijkl} \phi_i\phi_j\phi_k\phi_l,$$ with $\lambda_{ijkl}$ defined by contractions of the type $\delta_{ij}\delta_{kl}$...
100 views

### Add a suffix to the end of every number in a string [duplicate]

I have some polynomial e.g. $2x^2 + 3x + 4$ In Mathematica I can define this, and convert it to a string simply enough ...
66 views

### All possible replacements of an expression, considering each term separately

Say I have an expression: f[a, f[a, b]] I would like to replace each a with either x or <...
52 views

### Replacement Help, Patterns, Rules?

I have been trying to replace multiples of one thing in an expression, for example: (E^(4 s - 2 z) + E^(8 s - 4 z) + E^(12 s - 6 z)) //. {(4 s - 2 z) -> meow} However, I end up with the ...
73 views

### Identify complex expression and replace

I'm having trouble finding a way (if there is) to analyse an expression, identify a complex pattern in it and then replace it with another thing. I think it will be easier to explain my problem with ...
39 views

### Drop-down menu to globally change definition of a function t[x]?

If have a function t[x_] which is used extensively in a long notebook, but which has a variety of definitions, and which is used in a variety of ways. I'm looking ...
81 views

### Replacement rule with all possible permutations

I have a list with expressions like {1,3,5,a,c,b,3,d} I want to generate all outputs with permutations of {a,b,c,d}, so for example a rule like ...
51 views

### defining rule for derivative on arbitrary function

Is there a way in Mathematica for defining rules for derivatives on undefined functions? For example, I would like to define a rule such that for any function $f(x,y)$, when I take the $x$-derivative ...
33 views

### Replacement rules for partial derivatives

I have a system of PDEs, and it contains terms of the form $A^{(n,m)}(x,t)$, which is shorthand for $$\frac{\partial^n}{\partial x^n}\frac{\partial^m}{\partial t^m}A(x,t),$$ (ie ...
50 views

### How to use replace for only one term in a long expression in Mathematica?

I am doing some Cauchy integration and in some intermediate steps I have something that looks like that: (..many_terms...+I*\epsilon(k1-p1))/k1 where ...
42 views

### How to construct multible rules for replacement at once?

After generating a list for which I used //N, I want to replace some values with the exact one. What I had in mind is to generate something like: ...
217 views

### A “Replace” sort problem. Basic but haunts me

Here's my problem when doing my codes. I need an output similar to {{p->1},{p->2},{p->3},{p->4}},where p is a ...
83 views

### ReplaceAll does not work

Why ReplaceAll does not work in this situation? ...
67 views

### How can I ask the Wolfram Language to tell me if a hypothetical expression with a particular head would be atomic or not?

Integers in the Wolfram Language are atomic expressions, while uses of the Map function are not atomic. But if you didn't know this, how could you find out using only the Wolfram Language? The best I ...
373 views

### Turning arguments into exponents

I'd like to replace every instance of L[stuff], e.g., L[-1,-2,-2] in an expression with ...
75 views

### How do I replace a double asterisk Noncommutative multiplication symbol in an output?

For a better overview in my output, I would like to replace ** with nothing. Since foobar /. ** -> "" does not work I ask for help. The ** is the ...
194 views

### How to find the positions of replaced elements in a list

I have two sets of elements a={3, 4.8, 5, 4.2, 3.6, 2.3, 4.5, 3.7} b={2.8, 4.7, 5.3, 3.5, 4.7, 2.8, 4.2, 3.4} and I create ...
44 views

### Replace rules change dimension

This is absolutely driving me crazy, I don't know if this is a bug or what, but replace rule sometimes change dimension of my data. Say I have data: ...
38 views

### Extracting list from a list of lists obtained using Solve function

After solving a set of equations I got a solution like the following using Solve[] function. ...
45 views

### Unconstrained pattern evaluated by a constrained replacement rule

I'm working with a notebook that defines NonCommutativeMultiply on a defined set of symbols, called operators. Semantically, ...
30 views

### Stepwise simplification

I have a really, really complicated set of string simplification rules (only it doesn't actually work on strings, I use stuff like ...
15 views

### KeyAbsent when using Association in a replacement [duplicate]

I am trying to replace derivatives with other functions. I expected as = Association[fx -> dfx] Derivative[fx][x] /. Derivative[y_][_] -> as[y] to ...
58 views

### Find and replace particular multiplication of factors in big multiplication

I have a sum of terms where each is a multiplication of several factors. For example my expression in FullForm is ...
33 views

### Replace at given levels, but ignore subexpressions with specified heads

I'm implementing a function tacit1 that constructs a pure function by applying all Symbols to ...
39 views

### SequenceReplace is incorrectly modifying the list when a pattern partially matches

(Below is a big simplification of my actual use-case which reproduces the problem with SequenceReplace. Please assume my use of ...
30 views

### Replace series with restrictions

I am trying to implement a code where I have the following problem: Consider I have the following series, which I can express as follows:  \sum_{j<k} x_j^4 x_k^4 = \frac{(\sum_j x_j^4)^2 - \...
47 views

### I need the product of two functions be understood as a function itself (a Dirac delta example):

Define: In:= Dirac /: Dirac[x_, y_] f_[x_] := f[y] This works like Dirac's Delta. For example, In:= h[x] Dirac[x,y] Out:= h[y] perfect! However if I ask: In:= h[x] k[x] Dirac[x,y] Out:= h[y] ...
88 views

### How to simplify an expression by substitution?

Sometime Solve and DSolve give a finial form of the solution, which is fine in may applications. However, in some cases, one may ...
46 views

### Pattern ReplaceAll warning

Why does the following code give me a warning, but then still work? a1={{1,2},{3,1}} a2={"foo","bar"} a1/.{x_Integer,y_Integer}->{x,a2[[y]]} After a "Part: ...
53 views

### Creating an elementwise replacement rule

I apologise for the simple question, but I've trying to learn to write my code in a more idiomatic way, after having learned Mathematica mostly through trial and error for a long time. Say I have ...
36 views

### Construction of symmetric function / matching function argument patterns

[Edited for clarification after comment] Suppose that the function f is defined as the linear combination of several translated/reflected functions starting from the primitive f0[x,y,z] ...
32 views

### Constructing sequence of function with elements depending on the previous one / recursion

Starting from a function $\theta0[x\_ , y\_ , z\_ ]$, the terms $\varphi[x\_ , y\_ , z\_ ]$ constitutes a sequence of functions that is constructed as follows: Order 1 (one function and its ...
58 views

### Replace Integrate with NIntegrate

I have a symbolic integral with a pretty complex expression. I need it in symbolic form because I want to compute its derivative. ...
37 views

### First ReplaceAll, then evaluate an expression

Here is a simple example using ReplaceAll: rvec[] /. rvec -> {x, y} I think Mathematica first tries to evaluate ...
32 views

### Could not able to replace some terms in my final expresion

I have three equations. The third one is the sum of the first and the second. I wanted to replace some terms in the third equation but it is not happening. I don't know why ...
54 views

### Expressing D[1/D1, x] in terms of D1 when D1 = 1 + x

For simplicity, taking D1 to be equal to 1 + x, I want to evaluate the derivative of 1/D1 (...
134 views

### Pattern test: _Integer vs _?IntegerQ [closed]

While checking out the different pattern replacement methods in this question, I have a query regarding the basic difference between the following two replacements: ...
35 views

### Using rule with derivative

Assuming I have an expression like: D[a[t, r], r] + D[D[a[t, r], r],r] + D[D[a[t, r], t],r] I would like to change variables, by expression everything in terms of ...
59 views

### How to substitute integral operators into polynomials?

Suppose I have a polynomial $a_0+a_1 f(x,t) + a_2 f(x,t)^2 + ....$. In code, a0 + a1 y + a2 y^2 + a3 y^3 /. y :> Integrate[Subscript[y, k] E^(I k y), k] <...