Questions tagged [replacement]

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

Filter by
Sorted by
Tagged with
0
votes
0answers
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 ...
1
vote
1answer
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 ...
7
votes
3answers
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 ...
2
votes
2answers
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 ...
0
votes
1answer
42 views

Replace does not substitute in function

I have the following expression with functions ...
0
votes
0answers
28 views

ReplaceAll in a vector

I have the following code: ...
1
vote
1answer
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: ...
2
votes
1answer
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: ...
2
votes
1answer
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-...
0
votes
0answers
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}$...
2
votes
2answers
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 ...
4
votes
3answers
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 <...
2
votes
1answer
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 ...
3
votes
3answers
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 ...
1
vote
1answer
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 ...
2
votes
3answers
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 ...
0
votes
1answer
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 ...
3
votes
1answer
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 ...
0
votes
0answers
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 ...
2
votes
1answer
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: ...
7
votes
1answer
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 ...
3
votes
1answer
83 views

ReplaceAll does not work

Why ReplaceAll does not work in this situation? ...
1
vote
2answers
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 ...
5
votes
2answers
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 ...
2
votes
2answers
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 ...
3
votes
2answers
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 ...
1
vote
2answers
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: ...
2
votes
1answer
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. ...
2
votes
1answer
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, ...
1
vote
1answer
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 ...
0
votes
0answers
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[1][fx][x] /. Derivative[1][y_][_] -> as[y] to ...
1
vote
2answers
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 ...
1
vote
1answer
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 ...
2
votes
1answer
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 ...
1
vote
1answer
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 - \...
1
vote
2answers
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] ...
0
votes
3answers
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 ...
0
votes
1answer
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: ...
2
votes
2answers
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 ...
2
votes
1answer
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] ...
0
votes
1answer
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 ...
0
votes
0answers
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. ...
0
votes
2answers
37 views

First ReplaceAll, then evaluate an expression

Here is a simple example using ReplaceAll: rvec[[2]] /. rvec -> {x, y} I think Mathematica first tries to evaluate ...
0
votes
0answers
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 ...
0
votes
5answers
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 (...
4
votes
1answer
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: ...
1
vote
1answer
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 ...
0
votes
1answer
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] <...
1
vote
1answer
283 views

Replacement using pattern matching?

Starting with the expression eq1 = 5 Exp[1 + 2 x + 3 x^2 + 4 x^3]; I want to implement the following transformation: ...
2
votes
1answer
65 views

Pattern matching failed [closed]

I want to replace Θ[z + m τ] by a[m] Θ[z]. So I consider the pattern ...