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

learn more… | top users | synonyms (4)

0
votes
1answer
141 views

How to replace multiple variables at once

I would like to make a replacement "c1 C1 -> F1, c2 C2-> F2...." such that the expression like this "c1 C1 c2 C2 c3 C3 c4 C4" becomes "F1 F2 F3 F4". I used the standard replacement method: ...
0
votes
1answer
89 views

How to transform the polynomial in this way?

I have two functions f and g and a polynomial ...
0
votes
2answers
168 views

Replace all elements of a list

I have a set that looks like: s = {{1, 2, 3} -> 9, {4, 1, 9} -> 9, {3, 7, 3} -> 1}; Now, I want to replace each element of the set by the lhs of "...
0
votes
0answers
90 views

Alternate method of conjugation using replaceall

Mathematica really struggles to simplify complex conjugates of complicated expressions - even given assumptions for every symbol in the expression being real. Often times I've found that I can ...
2
votes
1answer
69 views

Convert Integer to Numeric with Replacement rules

I have a long list of triples, each looking something like {AGO, 1988, 2345.23}. Some of these, however, have an integer in the third spot, like this: ...
11
votes
2answers
191 views

ReplaceRepeated seemingly omits some rules

Is there a simple way in Mathematica to prevent rule reordering? Let me give an example: ...
1
vote
0answers
59 views

Object depth different in replacement rule [closed]

I access the first part of an expression with head foo without a problem: foo[arg][[1]] arg Then when I try to use this ...
0
votes
0answers
54 views

How to make this particular replacement involving exp

I apologize if similar questions have already been asked, but I have some difficulties with replacements in Mathematica and I would like to make a substitution like ...
1
vote
2answers
118 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 ...
2
votes
3answers
160 views

Slow image processing using ReplaceAll

Can anyone explain me, why this line of simple code is so slow? And can it be improved somehow? ...
2
votes
2answers
98 views

Converting a date to a calendar quarter

I have a list that includes dates. I need to convert each date to be the last day of the quarter that it's in. So for example I want {2013,7,1,0,0,0} to become {2013,9,30,0,0,0}. Actually I don't ...
0
votes
1answer
88 views
7
votes
1answer
173 views

What is the principle behind Dispatch?

I want to know what Dispatch actually does to rule the list. Why is it so fast? The documentation says Dispatch generates a dispatch table which uses hash ...
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, ...
0
votes
1answer
100 views

replace is not happening for specific vector valued expression

Given x[t_] := {Subscript[x, 1][t], Subscript[x, 2][t]} following replace works fine x[t] /. x[t] -> 0 0 but following ...
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
2answers
84 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 ...
0
votes
0answers
43 views

is there a way to speed up rule replace? [duplicate]

I am currently doing calculation with 7 parameters, each parameter set corresponding to a value. And I store the data in the format as a list of rule, like following ...
0
votes
2answers
127 views

Replace doesn't replace the denominator [duplicate]

I have the following expression in Mathematica -((E^(2 - 2 Sqrt[(x[1] - x[2])^2]) (x[1] - x[2]))/Sqrt[(x[1] - x[2])^2]) I want to replace anything of the ...
2
votes
1answer
122 views

Derivation after substitution not working

I have a code that will output an expression in form of a functions like this ...
3
votes
3answers
71 views

Operations on expressions within Hold [duplicate]

I want to convert {Hold[1 + 2], Hold[3 + 4], Hold[5*6]} into Hold[{1 + 2, 3 + 4, 5*6}]. I have tried ...
0
votes
1answer
126 views
0
votes
1answer
85 views

Function generating a rule with a Patterns: e.g. toRule[ lhs, rhs] into lhs[x_] -> rhs [duplicate]

I want to create a rule as a function of z from a left and right hand side. My (non-working) code and usage is: ...
5
votes
0answers
183 views

Is everything in Mathematica ultimately stored as a rule?

My current understanding about Mathematica is that everything, at the lowest level, ends up as replacement rules. First question: is this true? Second question: does Mathematica "gloss over" these ...
11
votes
4answers
290 views

How to replace every possible $A+B$ and $AB$ in expansion of $(A+B)^{10}-A^{10}-B^{10}$ with $x$ and $y$, respectively?

I would like to replace every possible $A+B$ and $AB$ in expansion of $(A+B)^{10}-A^{10}-B^{10}$ with $x$ and $y$, respectively. How to do it with the simplest code in Mathematica? For example, ...
4
votes
2answers
114 views

BezierCurve in EdgeShapeFunction fails when used on multiedges graph

I would like to create a network graph with curved edges. To do this, I wrote this function: ...
2
votes
2answers
130 views

How to plot complex values given by Solve

Update Solve[N[Table[BernoulliB[n, z], {n, 10, 10}] == 0]] ...
4
votes
3answers
138 views

Simplifying expression with patterns (Replace)

Q1: I need to simplify some algebraic expression, according to particular pattern, namely: \begin{equation*} x^m = \left\{ \begin{array}{ll} 1 & \text{if } m \text{ is even} \\ x & \text{if ...
4
votes
4answers
140 views

Rearrange graph

I have to rearrange graph described by j = {1 -> 2, 2 -> 3, 3 -> 1}; appling some function to all vertices on the right so rearrange[j] output should ...
1
vote
1answer
76 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 ...
9
votes
1answer
229 views

error when defining a compiled function with ReplacePart

I always have difficulty in writing a non-trivial compiled function. I'm using Mathematica 9. Please see the following code ...
0
votes
1answer
68 views

Manipulate that remembers its own input code and can show it by request

This question is a further question to my previous one. The solution for that question works well as long as the original input Manipulate code is still provided in ...
1
vote
2answers
168 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 ...
3
votes
1answer
144 views

Is there a way to simplify this replacement rule

I have a rather big expression which contains many sums of form q x + w y. I know that y is much smaller than ...
0
votes
1answer
91 views

Problem with List Dimension Outer Map [duplicate]

I am currently handling a big list which has the following dimension: {6, 3, 4, 2, 2, 1, 8} where the last number 8 represent a ...
3
votes
1answer
93 views

obtaining real roots of negative numbers in a long expression [duplicate]

I have a complicated symbolic expression which contains many terms like $(a/(a-2))^{1/m}$, where $a/(a-2)$ could be positive or negative after replacement. I only need the real root after ...
3
votes
3answers
331 views

StreamPlot using NDSolve gives error messages

StreamPlot[{x[0], x'[0]} /. NDSolve[{x''[t] == 1, x'[0] == v0, x[0] == x0}, {x, x'}, {t, 0, 1}], {x0, -2, 2}, {v0, -2, 2}] The above code gives me the errors ...
1
vote
2answers
233 views

How to debug error triggered by replacement rule?

The second line below produces the errors shown: ...
0
votes
1answer
133 views

Checking differential equation solution

I have this differential equation: eq=D[ψ[r],r]+A*ψ[r]-(B/(A*r))ψ[r]==0; And DSolve sucessfully solves it: ...
2
votes
2answers
158 views

Applying rules with functions to expressions with derivatives

I have a function that I want to see applied, potentially within a derivative. The use of delayed vs. non-delayed rules seems irrelevant. See the following: ...
2
votes
3answers
144 views

How to manipulate log arguments?

A common problem I have with Mathematica is needing to manipulate the arguments of logarithms. Typically, I need to take ratios of the log arguments Mathematica gives me by default. That's to say ...
4
votes
3answers
139 views

Appending to sublists using “anchor” values

Say I have a set of lists that look like this: ...
1
vote
3answers
112 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 ...
0
votes
1answer
118 views

How to change variable that has been defined in PDE?

I have a PDE function that contains a variable (named ka). I want to plot the result of my PDE in 1 plot using several different ka values. However, I'm not sure about how to write down this equation. ...
3
votes
2answers
170 views

Can I ask Mathematica to replace the arguments of my functions with a replacement rule?

I have an equation that I evaluate at some point (let's say $x=1$) that have terms of the form f[1,y] D[g[1,y],{y,2}] Is there an easy way to replace [1,y] by ...
0
votes
2answers
166 views

Writing intelligent replacement rules for symbols

I am learning about quaternions right now, and I wanted to play around with some concepts I am learning. e.g. take rotation of a point about an axis with specified angle. ...
4
votes
1answer
195 views

How to speed up calculations with large number of replacement rules?

In my program I have an expression which looks like: ...
9
votes
2answers
418 views

Simple problem with Manipulate and Plot

Would anyone have an idea why the following doesn't work: rule = {z -> x^2 + 2 x + y}; Manipulate[ Plot[z /. rule, {x, 0, 10}], {{y, 2, "y"}, 1, 5} ] But ...
20
votes
1answer
588 views

replacement rules from a pattern and a matching expression

(This seems to be a very basic necessity in a language having pattern-matching capabilities such as Mathematica, yet I struggled for many hours to find a common solution to this problem.) Suppose ...
2
votes
1answer
150 views

Add Pattern to List Removing Duplicates

Lets say I have a list of patterns ...