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
1
vote
0answers
20 views

Difference in pattern matching between Cases and ReplaceAll

I don't understand why there is a difference in the pattern matching between x a /. coef_*x^pow_. :> {pow, coef} and ...
2
votes
1answer
51 views

How to get the result in terms of a previously defined expression?

Say I have a vector X[n_] := Table[Subscript[x, i], {i, 1, n}] and a matrix of connections: ...
6
votes
1answer
118 views

How can I get the progressive application of list of rules?

I have a very long list of rules, and using this list of rules I want to see how some elements evolve as I successively apply the next rules on the previous rule. Here is the simpler version of my ...
0
votes
0answers
27 views

Problem with /. not assigning value [closed]

If I run: NSolve[Sqrt[2]*Sqrt[x - Exp[x] + 10000] == 10, x] {{x -> -9950.}, {x -> 9.20625}} As expected. However to assign the values to x I tried running: ...
4
votes
1answer
92 views

Turn definitions into rules

I know that this seems to be unnecessary, but here I have a bunch of assignments, like around 50, and I want to create rules for all these to have the freedom to apply these rules only on certain ...
0
votes
2answers
57 views

How to solve for y for a set of implicit (in x and y) linear equations for use in Plot?

From a matrix m={{1, 1}, {-1, 1}}, a vector b={1, 2}, and a list of variables vars={x,y} we ...
0
votes
1answer
24 views

Is there a better syntax to obtain the derivative of a function as a function/operator? (e.g. for simple plotting of derivtives of functions, etc.)

When trying to plot a derivative of a function, (I take Sin as a simple example), the naive approach ...
3
votes
1answer
78 views

Replace multiple variables using a list?

I want to calculate sum of derivatives of expression F = Exp[(x*z - 1) f*b + (x - 1) (1 - f) b] from n-th to (n-k)-th, say k=4. The dummy way which works is: ...
1
vote
1answer
47 views

Write function in terms of a combined variable [closed]

I am working with recursive convolutions of complex functions (fourier transforms), so, I calculate convolution integrals of functions of the form $$g(iy) = \int_{-\infty}^\infty f(ix)f(i(y-x))dx, \ \ ...
5
votes
2answers
124 views

How do I replace specific sequences of a sublist without changing the level of the list?

Suppose I have a list of multiple sublists: testlist={{1,1,1,0,0,0,1,1,1,0,1,1},{1,0,0,1,1,1,1,0,1,1,1,0}} I want to replace given sequences without creating new ...
0
votes
0answers
64 views

How can I shuffle the indices of certain variables?

I am recursively defining rational expressions in two sets of variables $x_i$ and $y_i$. Specifically, for $m<n$, I am interested in replacing all degree-$m$ monomials of the following form: $$\...
1
vote
1answer
22 views

Error: Root::npoly: not a polynomial in #1 [closed]

I have a $45\times 45$ matrix $H$ and its elements depend on $Ca$. I want to find its eigenvalues (which will also depend on $Ca$). I do this using: ...
1
vote
2answers
43 views

How to define a series of substituting rules?

suppose I have a long list of many variables: alist = { u, v, w, ... }. At some point, I have another combination of variables depending on { u, v, w, etc.},e.g., alpha = 2 * u + v beta = v + w^2. And ...
7
votes
4answers
190 views

How to use each replacement only once in a list of replacements

I have a list of replacements and some list they are acting on. However, some RHS of the replacements rules are identical (the LHS is not). I want to use those rules one after another (perhaps cycling ...
1
vote
2answers
42 views

How to match a formula in different level

Solve[4 n^4 + 4 n + 3 == 0] The result has many $$ \sqrt[3]{\frac{1}{2} \left(1+i \sqrt{3}\right)} $$ so I want to replace them with a letter, for example , ...
1
vote
2answers
48 views

Swap two numbers - but only in a particular context

I have a list of terms such as {2*s[1,2]^2, 3*s[3,5]^4, ...} and I would like to have a replacement that swaps $2 \leftrightarrow 4, 3 \leftrightarrow 1$, but only ...
0
votes
1answer
23 views

How to define the correct replacement

I would like to replace a specific expression denrule={Den1[vk]^e1_*Den2[l]^e2_*Den3[k+l-v*\[Omega]]^e3_*Den4[k]^e4_*Den5[kl]^e5_->j[MI,e1,e2,e3,-e4,-e5]}; The ...
0
votes
1answer
57 views

How do I make a substitution to an expression inside a derivative? [duplicate]

Pardon if I didn't ask the question correctly. Still trying to figure out the language. I have the following expression: $$energy=\frac{1}{2}R_s^{'}[t]^2==\frac{4}{3}G\pi\rho R_s[t]^2$$ ...
4
votes
1answer
58 views

Generate a list of rules from two rules

I have two lists: a = {a1,a2,a3} b = {b1,b2,b3} and I want to generate a list of replacement rules ...
2
votes
1answer
33 views

Formula: setting parameters vs replacement, different results?

I have a question that may sound stupid and have an easy answer - but I currently do not see it. I have defined a function of four variables, $f[a,b,c,d]=...$, in what I think is correct Mathematica ...
4
votes
2answers
126 views

Drop unecessary arguments

I have a very odd question. I have a complicated expression that is proportional to dx and dy. I want to drop everything that is not dx^(n) *Exp[..], dy^(n)*Exp[..], dx^(n)*dy^(n)*Exp[..]. For the ...
1
vote
1answer
33 views

Replacing a submatrix of a matrix

Minding the fact that In[102]:= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}[[1 ;; 2, 1 ;; 2]] Out[102]= {{1, 2}, {4, 5}} is the upper left submatrix. I would like to do the ...
1
vote
2answers
43 views

Substituting values from an association thread into a table of expressions

I have asked a question here. Yet I am trying to do the same procedure with AssociationThread Suppose I have the following function which is a normal distribution: <...
5
votes
1answer
46 views

Substituting elements of lists into an expression

Suppose I have the following lists, as = {1, 2, 3, 4}; bs = {10, 20, 30, 40}; and the following expression: ...
4
votes
0answers
114 views

A Curiosity: What is DispatchQ?

As shown below, my autofill in Mathematica showed an interesting function: DispatchQ. Yet, it does not appear in the Mathematica Documentation Center, a search for ...
2
votes
1answer
70 views

Substitute Expression in for Derivative?

I am trying to create a notebook to evaluate a line integral Line Integral on SO(3). Everything is going fine up to this point: ...
1
vote
1answer
69 views

Removing unphysical solutions of differential equations

I am solving differential equations in a physical context and I would like to remove 'unphysical solutions'. In my case, these correspond to non-polynomial solutions. My program outputs general ...
8
votes
4answers
312 views

Inverting replace direction

I just have a simple problem. I have a list such that, rep = { a1 -> b1, a2 -> b2, a3 -> b3}; I want to invert replace direction such that, ...
2
votes
2answers
54 views

Applying a function when using ReplaceAll

For example, I have a list m={{1,-1,3},{-2,1,-3},{-2,3,3}} and a function f[x_]=-x^2. What I want to do is to apply ...
2
votes
2answers
80 views

Slow replacement rule while combining symbolic summands

Edit: I attempted to use the method of repeatedly using Replace instead of ReplaceRepeated as suggested by Mr.Wizard, but the ...
2
votes
5answers
364 views

How to replace every element in a list?

I have a list s:={1, 2, 4, 7, 8} and I wish to replace every element in the list with Range[Max[s]]. I know that I could ...
2
votes
1answer
60 views

Listing numbers whose digit sum is greater or equal 5

I just started using Mathematica and at the moment I'm learning some things. As you can see in the headline, I want to make a list of numbers (up to 100) whose digit sum is greater or equal 5. I ...
5
votes
2answers
157 views

Replace a randomly chosen part of an expression

Given an arbitrary Mathematica expression, how can I choose a random part of the expression and replace it with another symbol of my choosing? For example, given $a x^3 +2y\cos(x)- \tanh(x^{y+3})/(x^...
5
votes
1answer
55 views

Dispatch not working properly with Times

Dispatch seems to work incorrectly when the head of the lhs of the replacement rule is Times. In Mathematica 10.0 and 12.0 we ...
0
votes
1answer
23 views

How to substitute part of expression with function variable (Goal: create function that solves expression numerically for one of the variables)

I have a complicated expression that contains N variables. I want to create a function of N-1 that solves for one of those variables Toy example: ...
2
votes
1answer
52 views

Write repeated expressions within a rule more simply

Is it possible to shorten the length of a (delayed) rule (i.e. lhs :> rhs) when there are expressions which repeat several times? Those expressions involve ...
2
votes
1answer
39 views

Replacement rule does not recognize parity?

All of the following ...
1
vote
0answers
39 views

Finding analytic expression with replacement rules

I have a function defined as f[k_]:= Maximize[{(x^2/(1+x^2))((1-x^(2k))/(1+x^(2k+1)))^2, 0 <= x <= 1}, x] I would like to find an analytic form of $f$ as a ...
3
votes
2answers
105 views

Replacement in a While loop

I want substitute m for n in n = -3; While[True, If[2 n^2 + n - 6 == 0, Break[]]; n += 1/2];n...
3
votes
2answers
40 views

Replacement rules which for elements of a vector which meet conditions

I'm trying to create a function which will replace elements of a list with their values modulo 1, i.e. Mod[x,1], but only if ...
2
votes
1answer
53 views

Replace a family of coefficients by a function in a sum

I would like to do something very simple, i.e. replace the coefficients in a sum by a function: c[1] + c[2] + c[3] /. {c[s] -> (s + 1)/(2 (2 s + 1))} ...
3
votes
1answer
48 views

Using Replace for derivative

I have a small issue in understanding the workflow of Replace /. When I give this input: (y''[x]+y[x]) /. y[x] -> 1 I am getting just ...
0
votes
1answer
30 views

Replace the values of the second position in a list given two conditions

Suppose I have the following list: https://pastebin.com/zFin7kkB and I have a given value of maxDh (let's say 0.5) . How can I replace only the values in the ...
0
votes
1answer
67 views

Resolving a sum after making a substitution

Consider the following sum as expressed by $s3$: ...
6
votes
3answers
190 views

replacing a sub-partition (unordered list; multiset)

I am working on a bijection for integer partitions and would appreciate help with two steps. First, I want to replace each occurrence of (4, 2, 1, 1) with (3, 3, 2). One complication is that the 4, 2,...
3
votes
1answer
49 views

Replacing an indexed term

Consider the expression: s[i_, n_] := Sum[(e[c[j]] + e[z[j]])*(l[i, j] + m[i, j]), {j, 1, n}] I'm trying to replace e[c[3]] by c[3] and have attempted ...
0
votes
0answers
32 views

Expanding complex expression and replacing variables in the result

When I'm finished expanding my expression I would like the result to be expressed again in terms of the original variables (like α, S1, T1, T2). I've tried replace, ...
1
vote
2answers
65 views

Replacement Rule Line Thickness

I have a plot with imported data that I have used a replacement rule to make the data a line rather than individual points. However, I want to increase the thickness of that line. How do I do this? I ...
2
votes
0answers
53 views

How to match exponents that can be 0?

I want to do some transformation of the following expression ...
3
votes
1answer
108 views

Replace derivative terms with finite difference symbols

I would like to replace the derivative terms in an expression with their finite difference approximations. Note, I don't want to actually evaluate the approximations at a given point, just display the ...

1
2 3 4 5
24