# Tagged Questions

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

180 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 ...
248 views

### How to set multiple parameters to zero?

Say I have the matrix A = {{a,b,c,d,e}, {f,g,h,j,k}} and I want to set some of these to zero. I have been doing this via ...
188 views

### Confusing ReplaceRepeated & Hold

I've been bootstrapping myself to the very alien world of Mathematica and there came my first WTF moment: ...
137 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 ...
229 views

### Replace elements in matrix to make it symmetric using automated procedure

I am generating a matrix using (mat2 = Array[Subscript[a, ##] &, {4, 4}]) // MatrixForm but would like to force Mathematica to make it symmetric. Thereby I ...
229 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 }...
166 views

### Appending to sublists using “anchor” values

Say I have a set of lists that look like this: ...
240 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 ...
1k views

### Replace symbols with values without evaluation

If I have assigned values to a, b and c: a=7; b=5; c=6; ...
139 views

### ReplaceRepeated as a Looping construct?

There are a lot of discussions about looping constructs (notable example here). Wolfram language guides, as usually, contain most relevant links. Looping constructs guide has the following part on ...
128 views

### Substitute a function's definition for its name

I'm having trouble with a function which is supposed to work on another function. Simplified version: ...
292 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: ...
131 views

### How can one match a function in a replacement rule

I'm trying to write a delayed replacement rule that is capable of matching both the functions and their parameters, like ...
91 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 ...
66 views

### Type error before replacement rule is applied

When I type a <> "0" /. {a -> "1"} I get the error ...
172 views

### Replacing one element of list

I have a list of sequences of x's and y's. I want to switch the last position to the "opposite" symbol - if ...
131 views

### How can I avoid this ReplaceAll trap? [duplicate]

I am having some difficulties with the ReplaceAll function. When I write: {1, 2, 3, 4, 5} /. x_?OddQ -> x^2 I expect: <...
899 views

### Replace expressions by self defined symbols

I have a list of expressions like the following: ...
105 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 ...
140 views

### How to transform a list (of sums) into list of summands

Suppose you have the list list={a,Sin[b+c],d+c}. What I want is to transform all elements of list with head ...
374 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 ...
123 views

### Why doesn't FullSimplify find simpler expression given a set of transformation rules?

I've defined a set of rules, which I want to use with FullSimplify, as follows: ...
106 views

### Question about Replacements and String Manipulations

I'm trying to get better at Mathematica and in the book I'm using (by Nancy Blachman) is the following question (which apparently was taken from another book: The Art and Science of C) which reads: ...
89 views

### Better use of pattern matching in replacing elements of a list

I have a list of reals and I'm replacing those which are contained in a given interval with two different values depending on wheter the selected reals are positive or negative. I came up with this ...
197 views

### Trying to get Arg[1 + I a] -> ArcTan[a]

Can Mathematica evaluate Arg[1+ I a] when a is a positive real in order to get ArcTan[a]? ...
178 views

### Remove redundant parameters from equation

I have random expressions like (b1 x + b2 x)/(b3 + b4/b5 x) + Sin[b6 x] where $x$ is a variable and {b1, ..., b6} are parameters to be fitted. This expression is ...
188 views

### Replacing a function in defined expressions

I'm having a little trouble with substituting functions into an expression. A minimal example is as follows: ...
587 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?
151 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 ...
125 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 possible)...
63 views

### Composition of rules: a->b + b->c = a->c

I am trying to compose two rules to form a new one: {a -> b, b -> d} and {b -> c} should become ...
388 views

### Replace rule also matching complex numbers

I'm having the replace rule: rule = {-g_ x_^4 - 2 g_ x_^2 y_^2 - g_ y_^4 -> -g (x^2 + y^2)^2}; Now, ...
82 views

### Obtain a minimal set of parameters from a large number of rules

Problem statement I have a matrix $$D(\vec{k})=\sum_\vec{R}\tilde{D}(\vec{R})\cos(\vec{k}\cdot\vec{R})$$ where $\vec{R}$ is summed over all atoms positions and $\tilde{D}$ are 3x3 matrices. $D(k)$ is ...
73 views

### Why isn't SparseArray working in this case?

This works: SparseArray[{{1, 1} -> 1, {2, 2} -> 2}]\ (* {{1, 0}, {0, 2}} *) This works: ...
92 views

### Optimization of recursive functions by using the //. operator [duplicate]

In some cases, recursion in Mathematica (without storing values) is not very efficient. For example, the recursive implementation of Fibonacci numbers (version which doesn't store the values) : <...
197 views

### Change last column of a matrix

Given a (rectangular) matrix of integers, i.e. RandomInteger[4, {10, 3}] I want to apply the following rules only to the last column ...
113 views

### How to select the value of a substitution rule for a particular variable?

Say I have the following lists of rules: ...
236 views

This is a question about ReplacePart but if there's a better that's great. If I have a list a= {1,2,3,4,5} and I want to replace elements ...
247 views

### Replace all symbols in a matrix with their numbered forms

I have a matrix made mostly from "x" with a few 0's thrown in: ...
331 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 ...
274 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? ...
202 views

### How to Update 2D List based on a binary array

I have 3 lists: numbers = {{1, 2}, {3, 4}, {5, 6}}; toUpdate = {{1, 0}, {0, 0}, {1, 1}}; newValues = {{10, 20}, {30, 40}, {50,60}}; I have to update the list <...
209 views

### ReplaceImageValue and ReplacePixelValue extremely slow

I have a grey scale image: and I am doing the following: bin = Binarize[image, 0.18] ...
554 views

### Using ReplaceAll to replace a head

Why would this work to replace the head b with newHead: ...
378 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 ...
505 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 [x,...
184 views

### Replace expression, but only “outside” of a function [duplicate]

I have a horrid expression, which is basically a sum of exponents with coefficients. Here's a taste: What I want to do is to replace ks with another expression, ...
76 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 ...
Suppose one has two functions, $y(x)$ and $z(x)$, and one seeks to obtain $y(z)$ by substituting $x(z)$ into $y(x)$. Can this be done in a single step? Or must $z(x)$ first be inverted independently? ...