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

learn more… | top users | synonyms (4)

1
vote
2answers
94 views

How to force a replacement in an order that I want?

This is a follow-up to this question. I have a big sum, with lots of terms of the form ...
6
votes
3answers
214 views

What's wrong with naming a rule?

Please have a look at the code below. The second Manipulate uses the name rl of the replacement rule given in the first line, ...
4
votes
2answers
232 views

How do I encode this replacement rule?

I'm pulling a 5x5 array from a larger array. The small sub-array (call it S) can contain any combination of the symbols A, B or 0. What I'd like to do is: if the array contains any matching pairs ...
2
votes
2answers
453 views

What is the meaning of “/.”? [closed]

I've looked to the built-in help of Mathematica, but I still can't understand the usage of /., like in the following expression: ...
3
votes
1answer
90 views

Replace list element with element from another list based on condition

I Have a list on integers and another list of "duplicates". E.g. start={{3},{1},{2},{4}}; duplicates={{1,4,7},{5,6}} I would like to substitute each element of ...
2
votes
0answers
37 views

Replacement rule of pure function not working [closed]

The following code works g[x_, y_] := x^2 + y Thread[xxx[{1, 3, 4}, {10, 20, 30}]] /. xxx -> g I would like to use ...
4
votes
3answers
117 views

Alternative forms to Table for iterating over replacement rules

I have a multivariate polynomial x. I get coefficients of various monomials using CoefficientRules, which returns a list of ...
9
votes
1answer
188 views

Fast implementation of Metropolis-Hastings update with multiple sets

This is an optimisation question rather than something I am unable to do. I have a naive, primitive working version of the code (I submit it below) but I am trying to make it work faster. I am asking ...
5
votes
2answers
249 views

Lagrangian to Hamiltonian

I want to go from Lagrangian description to Hamiltonian one. Using the example given by Mathematica I do something like : ...
0
votes
1answer
34 views

Restricting the evaluation of variables [closed]

Hopefully you understand my question. I m calculating inverse kinematics of a robot and each subsequent value is dependant on the one before. For simple porting into c++ I would like something like ...
2
votes
0answers
37 views

Replacement rule for summation with Kronecker delta

I want to perform some symbolic computations in index notation, without explicit reference to sums, so that in expressions of the type f[i,j]g[j,k] summation over ...
0
votes
1answer
35 views
1
vote
1answer
50 views

Rearrange the list with some rules

I am trying to solve physic problem on operators which are not commute. However, I am not good at coding, so I am having some problem with Mathematica code. Let's define my list such that ...
1
vote
0answers
35 views

Pretty equations in HoldForm[]

I have been generating equations programmatically, resulting in lists that look like this: {Log[2,(4^4)^3, 3 (Mod[2,4] 4), Log[root[2, 3], 4^4]} where ...
1
vote
1answer
69 views

How to replace a variable in a function?

I'm new to Mathematica and am using it for algebraic manipulation. I'm trying to figure out how to replace a variable in a user-defined function. I defined a derivative function like this: ...
19
votes
2answers
307 views
0
votes
0answers
41 views

Using ParametricPlot to Display result of Maximize

One would expect that: Plot[f[x],{x,0,1/2}] and ParametricPlot[{x,f[x]},{x,0,1/2}] would always yield identical outputs. ...
6
votes
3answers
169 views

How to prevent substitution in a part of an expression?

Background: I am trying to write a program that can execute scheme in Mathematica. Now I have things like {"+", 3, {"-", 2, {"'", {"*", 1, 3}}}}. The syntax ...
2
votes
2answers
79 views
6
votes
2answers
109 views

What is the Difference Between Rule (->) and RuleDelayed (:>)?

I am new to Mathematica so this question may seem rudimentary. Sorry for that! :) I want to implement the well-known property of Kronecker's Delta $$\Sigma_{i=1}^{n} a_i \delta_{ij}=a_j,\quad 1 \le ...
1
vote
1answer
77 views

Replacing negative components of a matrix with zeros [closed]

How can I replace the negative components of a matrix with zeros?
-2
votes
1answer
26 views

How to renew a value when a value is larger than it, counting the time of the record being broken at the same time [duplicate]

I have a given value and is it possible that after some calculation, when the calculated result is larger than the given value, the given value can be set as the value that is larger than it? Other ...
58
votes
3answers
2k views

Replacement inside held expression

I wish to make a replacement inside a held expression: f[x_Real] := x^2; Hold[{2., 3.}] /. n_Real :> f[n] The desired output is ...
0
votes
1answer
47 views

Extract solutions of linear system to variables

I need to output the answers from solve into the variables named just as they were named in the solve equations. I have checked out this thread Assign the results from a Solve to variable(s) but ...
3
votes
1answer
24 views

Possible incompatibility between ReplaceAll and LaplaceTransform

I have a large symbolic expression containing many terms of the form, LaplaceTransform[u2[z], z, s], with various functions for the first argument. I wish to ...
0
votes
1answer
66 views
1
vote
0answers
31 views

How Can I combine the equations' results in a table without “x->” [closed]

For example: I have a series of functions: F[x_,o_]=x+o And I want to combine the result into a table with o=0.3,0.6,0.9.. ...
5
votes
5answers
205 views

Converting set of characters into numbers using list of replacement rules

I am trying to convert a text, as an example "aaaa" into a set of numbers as defined in a list of replacement rules, for example letterstonum . letterstonum takes in letters a to z and returns the ...
0
votes
1answer
34 views

Replacement of a group of variables in trigonometric expression

I would like to make simplification of trigonometric expressions by using a replacement a group of variables. Here the equation I have to simplify : ...
0
votes
2answers
58 views

replace a part of equation by a symbol

I have an equation: $$ \frac{d\text{$\phi $0}}{dx} \left(-\frac{\text{A1}(x) \frac{d\text{A0}}{dx}}{\text{A0}(x)^2}+\frac{\frac{d\text{A1}}{dx}}{\text{A0}(x)}+\frac{\text{B1}(x) ...
4
votes
2answers
96 views

How to put replacement rules into a package

I have a replacement rules list, but its quite long and therefore I would like to make a standalone package of it. I tried but haven't succeeded. A simple example of my approach is ...
1
vote
1answer
58 views
5
votes
1answer
104 views

How to make a postordered ReplaceAll that operates just once per subtree?

The answers to this question provide code for a function I'll call postOrderReplaceAll to replace from the inside-out: ...
1
vote
0answers
52 views

A kind of function optimization problem [duplicate]

I have the following problem. Out of a calculation I got the four functions shown at the end of the post. These functions depends on many variables, qk, pk etc (all of them are actually scalar ...
1
vote
3answers
240 views

How to select elements from a list by condition?

Considering I have a list: F = {f1, f2, f3, f4} I want to perform exponent on the list F = Power[#, 2]& /@ F Now I ...
11
votes
2answers
95 views

Replace and Association

I'm surprised that I can't find this question already, so perhaps I'm missing something obvious. I would like to take a list of associations and remove any with value ...
1
vote
2answers
59 views

Using a rule to increase precision [closed]

I'm sure this is a very simple question, but I can't work out how to do it. Construct a rule to set x=0.32 to a precision of 20 digits. The rule is to be applied ...
4
votes
2answers
116 views
0
votes
0answers
24 views
3
votes
2answers
73 views

Replacing symbol with vector

Consider example: expr := {-1, 1} * value expr /. {value -> {10, 20}} (* {{-10, -20}, {10, 20}} *) however, I expect to have something like ...
3
votes
2answers
38 views

Setting initial conditions of a domain grid?

So I'm writing some code to try and produce a cellular automaton which will model an oscillatory chemical reaction, but I'm trying to set up the initial conditions of the grid so that I can vary them ...
1
vote
0answers
36 views

replace a^2 and b^2 with a different rule to ab [closed]

I have an expression and need to replace all instances of $x^2$ with $a$, all instances of $y^2$ with $b$ and all instances of $xy$ with $0$, but I only know how to replace $x$ or $y$ individually. ...
0
votes
1answer
52 views

Filter pattern from a list

I have group elements, that I'd like to condense down according to group rules. Take the modular group. This has generators $S,T$ that satisfy the groups relations $S^2 = I, (ST)^3 = I$. So given a ...
0
votes
1answer
50 views

Rule for multiplying of Kronecker-delta-type functions

I am trying to introduce a rule of multiplication of entries of a matrix which would have the following property: delta[k,i]*delta[k,j]=delta[k,i]*delta[i,j] and ...
1
vote
1answer
47 views

How to substitude a^n->D[u[t,x],{x,n}] for n=0,1,2,

I have expression which have terms a, a^2, a^3, ..., How can I replace a^n -> D[u[t,x],{x,n}] for n=0,1,2,....
1
vote
1answer
76 views

Why does using replacement rules give a different result than setting the variable? [duplicate]

These two methods should give the same result, but one terminates when the result is unchanged (to machine precision) while the other continues for the full 100 allowed cycles. Why? ...
7
votes
1answer
168 views

Remove leading and trailing zeros from a list

This functions and gives the desired result: {1, 0, 2, 0, 0} /. {x___, 0 ...} :> {x} {1, 0, 2} But with leading zeros it doesn't: ...
0
votes
1answer
55 views

Issue with replacement rules screwing up a matrix [closed]

I am pretty new to using Mathematica as more than a calculator. Essentially my problem is that I am trying to calculate a quantity vector quantity $x=x_0 - A^{-1}F(x_0)$ where $x_0$ is a 3-entry ...
4
votes
1answer
87 views

Why a part of this expression, similar to another, is left unchanged?

An expression has one of its part replaceable and one, strictly similar, that isn't. Please, can you point at the difference ? I can't pinpoint the complication ... The FullForm of ...