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
7
votes
1answer
205 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
73 views

ReplaceAll does not work

Why ReplaceAll does not work in this situation? ...
1
vote
2answers
64 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
368 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
66 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
188 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
40 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
35 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
42 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
29 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
54 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
37 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
29 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
46 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
84 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
42 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
47 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
33 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
30 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
53 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
35 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
53 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 (...
3
votes
1answer
128 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
34 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
57 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
269 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
64 views

Pattern matching failed [closed]

I want to replace Θ[z + m τ] by a[m] Θ[z]. So I consider the pattern ...
1
vote
0answers
54 views

How to replace an expression in an entire notebook?

I have a frequently occurring complex expression, let's for simplicity say $a+b$ which I want to replace with let's say $c$ in all the mathematical expressions following this Rule. It is simply like ...
0
votes
0answers
33 views

How to replace all the derivatives of a particular function?

For a given expression, such as: exp=p[x]+p'[x]+p''[x]...Derivative[n][p][x] I want to replace all the derivatives of p[x] to ...
1
vote
1answer
34 views

Show result of matrix operation in terms of user defined matrices

I have defined some matrices K[0],K[1],...,K[9] and S[1],...
0
votes
1answer
86 views

Substitution into integral [duplicate]

I have to do a substitution (u = pi/2-x) into an definite integral, which I have defined as f[u]. When I evaluate my code, no ...
1
vote
1answer
51 views

Replace second derivative with product rule

Suppose that after some calculations, I find a[x]*D[g[x],x,x]+b[x]*D[h[x],x,x]+c[x]*D[j[x],x] What sort of operations do I need to do in Mathematica such that ...
0
votes
1answer
29 views

Substitution of an expression into another expression

I'm trying the following Mathematica command to simplify an expression by substitution of a known constant, but it's not performing the substitution. Any ideas why? ...
1
vote
1answer
65 views

Replace every n-th part of list [closed]

I have a list: b = ConstantArray[1, 50]; And I would like to replace every say 5th element with 2: I came up with: ...
6
votes
2answers
546 views

How to use ReplaceAll on an expression that contains a rule

I know I have seen this before but can't seem to locate it today. I am trying to take an expression that has some rules in it and replace one of that parts that contain a rule using ...
2
votes
2answers
72 views

Replacing with pattern on built-in symbols?

I have: f[a, b] /. f[x__] -> g[x, 0] // InputForm (* Out: g[a, b, 0] *) Which is what I expect, but when I use a built in symbol instead of ...
0
votes
1answer
74 views

Replace expressions outside certain functions

I'm trying to replace variables outside of certain functions. More precisely, in the standard ones like Sin substitution should work while in undefined ...
1
vote
1answer
39 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
24 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
101 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
131 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
49 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 [closed]

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
54 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
65 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
95 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
71 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...