# Tagged Questions

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

210 views

### How do I write a function that can be used in a rule to modify both sides of an equation? [duplicate]

I sometimes need fine grain control over equations in Mathematica in order to help me understand how to solve a problem manually. A greatly simplified example of a session might be something like ...
186 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: ...
501 views

### Piping and Replacement

I haven't been able to figure out how to use piping with certain functions. Specifically, I would like to apply a replacement at the end of a pipe, like the following: ...
496 views

### Converting a list of replacement rules into a list of real values [duplicate]

The following command outputs a list of replacement rules. ...
145 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. ...
518 views

### Replacing values of a function

I have recently starting using Mathematica and have recently come to what seems as an impasse in my understanding of the language. If this is too "tell me how to do it" I would certainly understand. ...
122 views

### Replacing a combination of terms

I have a quite simple question. I have an expression that looks similar to $T=\frac{1}{a_1}-\frac{1}{a_2}+1$ and I'd like Mathematica to use $A=\frac{a_1}{a_2}$ to reformat the expression (if ...
186 views

### Efficient way of setting up a rule

I tried to define a simple rule defining how λ acts on ψ[n]: myrule1 = λ ψ[n_] -> α[n + 1] ψ[n + 1]; The result I get is ...
356 views

### Make Mathematica wait before replacing?

How can I force Mathematica to completely evaluate a subexpression before using a replacement rule? For example, consider the following definition (for the series of ...
173 views

### simplification rule with symmetry

What rule can you think of to replace the first line by the second? I have a bunch of expression like these, which I would like to simplify. All dependent cross terms must be written in the same ...
223 views

### A Sierpinski-ish structure, via 2D automata

The project is originally based on a puzzle proposed by EIORU at http://www.ptt.cc/bbs/puzzle/M.1342451949.A.00F.html (若您懂得讀中文 / if anyone happen to read Chinese^^) last July. The descriptions, ...
165 views

### Apply rule only if applicable? Check rule applicability? Custom unapplicable rule?

How to avoid recursion in the following example? ...
41 views

### How do I replace a variable with the answer from a solve function in the form k-> number + variables? [duplicate]

Possible Duplicate: Using the result of Solve in subsequent calculations extract values from replacement list I am trying to define a variable (k1) as ...
113 views

### Confused behavior using Dispatch to sort one list relative to another

I have a need to sort the entries of one list into the same sequence as the entries in a reference list. Because I have rather large lists involving strings, I'm trying to use Dispatch to accomplish ...
1k 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 ...
223 views

### Modify a sub-part of a sub-list only

I have this list: a = {{{0, 0}, {1, 7}, {2, 0}, {3, 2}, {4, 7}}, {{0, 0}, {1, 0}, {2, 1}, {3, 2}, {4, 7}}} and I'd like to transform it to this: ...
336 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 ...
239 views

### Transformation rule on a list of non-negative integers

I am trying to create a transformation rule that takes a list of non-negative integer values of any length, finds a non-zero entry in the list, adds 1 to all preceding numbers, subtracts 1 from the ...
268 views

### How to use Solve rules as a function body?

Solve gives me an expression which I want to use as the body of a function. Rather than run Solve every time inside my function ...
281 views

### How to extract a specific element from EXIF photographic metadata?

It's now possible to import photographs into Mathematica and import the EXIF data at the same time: i = Import["ExampleData/coneflower.jpg", "ImageWithExif"] ...
261 views

### Replacing product expressions - named vs. unnamed pattern?

Suppose I have an expression of the form a b c d + x y z The FullForm of this is ...
206 views

### Generating all matrices with 1 (possibly) replaced by -1

I have a matrix $M$, whose dimension I am unsure of, which has only $\lbrace0,1\rbrace$ entries. I would like to generate all the possible matrices that result from changing (some subset) of the $1$'s ...
163 views

### Extract a value from a list of InterpolatingFunction

By running the code n = 5; U[t_] := Table[Subscript[u, i][t], {i, 0, n}]; lines = NDSolve[{eqns, initc}, U[t], {t, 0, T}] I get the output ...
82 views

### Extract a List of Co-ordinates from a List of Complex Solutions

Let's say I have this list of solutions from a previous computation: ...
156 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}, ...
334 views

### Extracting RHS from Rule

In an example from another question, the rhs of a list of rules can be simply extracted: ...
86 views

### What exactly happens when adding a Blank after a built-in function used in the rules of ReplaceAll?

Well, this question is just for curiosity, personally I never use ReplaceAll like this: ...
534 views

### Using ReplaceAll on SparseArray

I'm using SparseArray in a notebook in which I am doing complex conjugation manually, i.e. writing $\sqrt{-1}$ as i and applying ...
177 views

### Confusing ReplaceRepeated & Hold

I've been bootstrapping myself to the very alien world of Mathematica and there came my first WTF moment: ...
481 views

### Transform an expression into a graph that can be plotted with TreeGraph (not TreeForm)

I would like to plot an expression (like TreeForm does), but using the new TreeGraph functionality. ...
2k 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 ...
240 views

### Is it possible to specify a context-sensitive, “depth-agnostic” rewrite rule?

I'm using the term "depth-agnostic" in this post to describe structural patterns featuring a "target" sub-pattern that can occur "at any depth" relative to one or more "context" sub-patterns. As a ...
315 views

### Indexed replacement

Is there some equivalence of MapIndexed for rules? Consider a substitution on 3 a + 4 b + 21 c + .... The first integer I want to replace by 1, the second ...
415 views

### Conditionally replace rows in lists

I have a sequence of matrices/lists ...
222 views

### Why does the name of a pattern affect the result of a transformation rule?

I was playing a little dirty, trying to get a template for defining a Curl[] operator. I wrote the following rule, which I knew was going to spit out a few ...
3k views

### Replace values which obey certain criteria

I'd like to replace all values in a list which obey one or more criteria with another value. Example: Replace all values>30 by 30. data={{3,35},{2,7}} ...
174 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. ...
288 views

### Creating a Nearest function programmatically

I've been trying to work out how to create a Nearest function programmatically. My goal is to produce something similar to this, a hand-assembled function: ...
3k views

### How to use results of NDsolve[] for further solving of ODEs?

I have a system of ODEs with 10 eqns. I can solve the first 5 independently. How can I use those results to solve for the remaining 5? An easy example would be $\dot{x}=f(x), \quad \dot{y}=g(x,y)$ ...
405 views

### How to manipulate nested expressions?

I have the following expression, only slightly complicated, and I'd like to do two things: Plot the elements of the lists (though messy, all are in the same ...
234 views

### Pattern matching a multivariate derivative

I can extract the parameters of a single variable derivative with the rule D[f[x], x] /. Derivative[order_][fcn_][variable_] -> {order, fcn, variable} ...
1k views

### Mathematica for Computer Scientists

Background I've coded in C/Scheme for 10 years. I have a BS CS. I've recently become very interested in pattern-matching (lisp's match macro), and term rewriting systems, which brought me to ...
338 views

### Returning Replacement Rules in a function and using it later on

I would like a function which returns replacement rules as some internal mathematica functions are doing and use the results in other functions. I could not find any information on that and I'm ...
638 views

### How to do substitution of variable with power in Mathematica?

I want to replace $x^{i+1}$ with $z_i$. EDIT: I have some latex equations, and wish to import them to MMa. How to replace x_i (NOT $x_i$) with $x[i]$ But it seems the underscore "_" has a special ...
320 views

### Safely nesting RuleDelayed

Say I have the following, straightforward-seeming functions: ...
158 views

### Set promotes Rule to RuleDelayed?

In mentally building a model of how Mathematica works, I naively expected Set to create a Rule in a symbol's ...
231 views

### Leave expression tree formatting the same when a replacement rule is applied

If I type e1 := 3 x + 2 (1 + y) z // TreeForm, I get an expression tree. If I then type e1 /. x -> 3, I get another ...
1k views

### Replace function and its derivatives found with Solve

If I have an equation eq = f[x] + g[x] == 0 and I solve for f[x] sol = Solve[eq,f[x]] ...