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

learn more… | top users | synonyms (4)

2
votes
2answers
296 views
2
votes
1answer
125 views

How to transform an expression using algebraical instead of pattern rules [duplicate]

I would like to transform rules algebraically. A very simple example would be: - k^2 - 2 k x + x^2 /. {2*k -> 1} This transforms to: - $$k^2-2 k x+x^2$$ ...
2
votes
1answer
144 views

#[[1]]//FullForm = 1?

I have a set of symbolic algebraic expressions that I'm trying to get some speed into. To illustrate the issue, I'll use a simple form like { k0 X, k1 Y, k1 Z}, ...
2
votes
1answer
33 views

Table doesn't replace inside conditionals (If, Which) [duplicate]

This: Table[If[cond, y], {y, 1, 3}] outputs this: {If[cond, y], If[cond, y], If[cond, y]} The ...
2
votes
0answers
103 views

Generalization of a rule to N arguments

I am trying to apply a series expansion on a function x[t1,t2,...tn], with an expansion parameter a. For ...
1
vote
4answers
180 views

How to find all symbols in an expression and perform an operation on them?

========== motivation =========== Suppose, for example, I have an incoming expression like m = (3-I)x + 4(x y)/(Cos[y]) I want to find all the symbols in ...
1
vote
1answer
124 views

Simple rule appears not to work

I have the following simple code ...
1
vote
2answers
67 views

Replacing a specific pattern of values at level 2 in a list

I've a list of lists, tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}} and each nested list has the same length, What is the proper way to ...
1
vote
3answers
115 views

Replace inside a Held expression separate context

How do I replace content inside a Held expression without local values coming through? For example if you run the following code you get g[2] instead of ...
1
vote
2answers
160 views

Using patterns in ReplaceAll to extract elements from tuples at arbitrary depths

Please consider the following: I need to extract from MyList all last parts of each 3-tuple. ...
1
vote
2answers
174 views

How to replace subscript symbols

I have an expression with subscripted variables. I would like to replace all those terms with other symbols. For example, $P_1 + P_2^3 \rightarrow P1 + P2^3$ I used the following code but it doesn't ...
1
vote
2answers
74 views

Changing all link destinations in a notebook

I have a bunch of cells containing text and hyperlinks. i.e. of the form: ...
1
vote
3answers
88 views

How can you form a variable from a function and its arguments?

Is there a way to make the general replacement foo[x,y,z]->fooxyz so that I can treat the x_i as variables so that I will obtain foofgh from the following: ...
1
vote
3answers
173 views

Help me to find a way to do this repetitive stuff

I'm trying to do this: ...
1
vote
3answers
121 views

How to ReplaceAll independent of depth?

I have a big list which in principle can be described as this more compact list : ...
1
vote
4answers
98 views

Replacing more than one element from a Sublist

The following expression allows me to extract the value of x and y: ...
1
vote
2answers
91 views

How to define a rule to simplify the notation for derivatives of chain rule?

Suppose that $u(x,y)$ is a function, and $x=r \cos t$, $y= r \sin t$, then we can define a new function $v(r,t):=u(x,y)$ with $x,y$ replaced by the above equations. It is quite easy to compute $v$'s ...
1
vote
1answer
102 views

Matrix/tensor addition behaving funny under replacement?

I have two matrices I want to add, and one of the matrices is a tensor product of two vectors. I've used a SetDelayed to define the summed matrix, because I want to evaluate it for different values of ...
1
vote
2answers
88 views

How to “reversely” replace definitions?

I have the following problem at hand. 1.) I have a list of definitions like ...
1
vote
2answers
241 views

How to debug error triggered by replacement rule?

The second line below produces the errors shown: ...
1
vote
2answers
98 views

Looking for an elegant way to solve a (system of) ODEs/functional equations with undetermined coefficients

I want to solve an ODE using undetermined coefficients/guess-and-verify and am looking for an elegant way to use this technique. I am running into a few ugly warts. Here is a basic implementation for ...
1
vote
3answers
152 views

Conditional Check on Matrix

Suppose I have the following matrix (i,j): ...
1
vote
2answers
89 views

How to get a rule for, e.g., (x + y) to apply to (-x - y), etc.?

For example, the substitution (x + y) -> s fails here: In[1]:= (-x - y) /. (x + y) -> s Out[1]= -x - y Of course, I ...
1
vote
2answers
64 views

How do I avoid vector variables being treated as scalars in the course of replacements?

I often work with a mixture of symbolic and concrete values as I develop teaching materials. I introduce variables symbolically and then need to instantiate them to specific values while continuing ...
1
vote
1answer
204 views

Equating and solving equations that have been generated using table[]

I am in the process of trying to use Mathematica to find the coefficients of transmission and reflection for the wavefunction of a beam of particles tunnelling across any number of identical potential ...
1
vote
1answer
1k views

Converting polar coordinate expression to cartesian coordinates

An intermediate step in my analysis requires me to work in polar coordinates, but I would like to convert the results back into cartesian coordinates. The conversion is very simple but tedious for ...
1
vote
1answer
72 views

How can I write a function which takes another function of a variable and the variable itself as arguments and gives a list of critical points?

This is an exercise from Power Programming with Mathematica by Wagner. I need a function which takes a function and a variable as arguments, like this: critpts[f_,x_]:= and then on the rhs I've got ...
1
vote
1answer
34 views

Automatic substitution of fractions

I have several equations with many variables, and here and there I would like to do a variable substitution. For example, in the equation: a (b + 42 c / d) I ...
1
vote
1answer
78 views

Nest Replace Function with different rules

I have a table of data that I want to filter. Any value of zero in the table means that value was not calculated, so it should get populated with the tag "NC". Next the table is filtered for all ...
1
vote
4answers
69 views

Replace a sum of squared variables by a new variable squared

Is there any way to tell Mathematica to replace $\kappa^2 + k_z^2 \rightarrow k^2$ in an expression? I want to make a result more readable, e.g. something like this: $$\frac{2 k_z^3 \sqrt{k_z^2 + ...
1
vote
1answer
107 views

Matrix substitution

I'm newbie in Mathematica and would like to ask one simple question. Let's say I have matrix with some elements == -1 ...
1
vote
1answer
45 views

Different ordering for KroneckerDelta?

I have an expression like this: Assuming[m [\Element] Integers && n [\Element] Integers, 1/2 I J n (4-m n+n^2) KroneckerDelta[m,-n]]//FullSimplify Now, ...
1
vote
1answer
143 views

Improving the plotting time of a very complicated expression

The quatity $R$ is defined by a complicated expression in terms of $a_1,a_2,\ldots,a_n$. Now each $a_i$ is a function of $x$, $a_i(x)$. I would like to know what is the most efficient way to ...
1
vote
3answers
117 views

Auto simplifications of power products

Mathematica automatically simplifies Exp[a]Exp[b] to Exp[a+b]. The problem is now that I can't do this ...
1
vote
2answers
42 views

How do I format expressions so that replacement rules correctly apply to exponents? [duplicate]

I have a series of rules that I would like to apply to a series of expressions with variables, but cannot get Mathematica to correctly recognize that these rules should apply when my variables have ...
1
vote
1answer
67 views

How can I get the functionality of a command ReplaceAllList?

I'm trying to use ReplaceList to match subexpressions. I expected the command: ReplaceList[{p, {p, p}}, {x_, y_} -> x y] to output two results, equivalent to: ...
1
vote
1answer
46 views

Commutative algebra by generators and quadratic relations

My apologies if this has been asked before. Please also be gentle, as this is my first question here. I am trying to define a commutative algebra by generators and relations in the following way. ...
1
vote
1answer
55 views

How do I extend (prepend or append to) an existing TagSetDelayed rule? [duplicate]

I have a TagSetDelayed rule associated with, say, tag: ...
1
vote
1answer
46 views

How to substitue local variables into expression with free variables?

I'm wondering how to do the following: expr = L + R; foo[L_, R_] := expr; foo[1, 2] (* L + R *) and have it substitute the function variables ...
1
vote
2answers
120 views

Why does using ReplaceAll twice produce a different result?

Why does this: Solve[a x^4 + b x^3 + c x^2 + d x + e == 0, x] /. {2 c^3 - 9 b c d + 27 a d^2 + 27 b^2 e - 72 a c e -> F} produce a different result from ...
1
vote
1answer
77 views

An equivalent in Mathematica [duplicate]

Let's say I have a $n$x$m$ matrix $M$ in Matlab and I want to substitute the elements of $M$ that exceed $15$ for $-1$. In Matlab I just write M(M>15)=-1 and ...
1
vote
1answer
264 views

Arbitrary depth patterns/rules

Temporary message: I am now really confused. I am not sure how using Power and Unevaluated together works in the examples below. While answering this question, I stumbled upon the following. We ...
1
vote
1answer
100 views

given n lists, get list of applications of function to n-tuples

Is there a built in function or an easy rule to do the following transformation? H[{A[0], A[1], ...},{B[0], B[1], ...},...] into ...
1
vote
1answer
126 views

Match and replace subexpression

I would like to replace the expresion $(n1-1)$ to $z1$ in following: In[1] := 2*n1*p[n1-1,n2] Out[1] := 2*(z1+1)*p[z1,n2] I have tried ...
1
vote
1answer
152 views

Efficient way to give symbolic elements symmetry

I have a symbolic expression that represents a set of operators. These operators have quite a bit of symmetry to simplify expressions: ...
1
vote
0answers
38 views

How can I use the Rules returned by FindFit? [closed]

When I evlauate data = RandomReal[10, {5, 10, 2}]; fit[i_] := FindFit[data[[i]], a Sin[x] + b, {a, b}, x] fit[1] I get something like: ...
1
vote
0answers
37 views

How to apply an rule to factors matching a certain form

Suppose we have many expressions involving irrational ArcTan expressions (such as ArcTan[Sqrt[3],5]) along with other irrational factors (such as ...
1
vote
2answers
124 views

Replace a column in table with a list of numbers?

I have a Table of values e.g. {{x,y,z},{x,y,z},{x,y,z}…} How do I replace the the "z" column with a List of values?
1
vote
0answers
61 views

How to simplify expressions

I read several similar posts but I have not found a solution yet. However, I think that my problem should be quite easy. So here we are. Relations ...
1
vote
0answers
44 views

Best way to power series expand in multiple variables? [duplicate]

A power series expansion of a multivariate function can be performed with the Series command, which performs each expansion consecutively. The results can be a bit ...