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

learn more… | top users | synonyms (4)

5
votes
2answers
92 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 ...
7
votes
3answers
95 views

Transformation rule for arbitrary number of argument expressions

I am failing to write a Mathematica transformation rule that replaces e.g. f[a + 3, b + 3, c + 3] with f[a, b, c] + 3 for an ...
0
votes
0answers
61 views

Why does the replacement /. {1 -> x} make sense?

Why does the replacement expr /. {1 -> x} in which the number 1 is replaced by the symbol ...
0
votes
0answers
35 views
14
votes
5answers
313 views

Apply multiple functions to parts of a nested list [duplicate]

data = {{1, a, x, "one"}, {2, b, y, "two"}, {3, c, z, "three"}} I want to apply a list of four functions {f, g, h, m} one ...
3
votes
1answer
67 views

Rule for derivatives

I have a differential equation of the form $$f'(x)=g(x,f(x))$$ (where $g$ is has a known explicit form, e.g. $x f(x)$) and I have an expression which contains several derivatives of $f$. I want to ...
1
vote
1answer
73 views

Tensors constructed using KroneckerDelta's - and/or displaying KroneckerDelta as a matrix

I am doing some work in elasticity and as a result am working with tensors. In particular, I would like to calculate the contraction of a fourth order tensor (the stiffness tensor) with a second order ...
2
votes
1answer
32 views

Replacing expressions with variables [closed]

I have a large matrix where the entries are rational in Jacobi elliptic functions e.g. I'd like to replace the elliptic functions by variables, e.g. ...
0
votes
0answers
40 views

Generate rule that replaces if string matches pattern

I have let's say a function f[x], but for x I pass a string. And I want to replace that string with a number, if that string ...
6
votes
2answers
147 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 ...
4
votes
0answers
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 ...
0
votes
1answer
77 views

Module output rules like Solve FindRoot etc built-in functions

How do I print rules {x -> values} as an output from my module? I'd like it formatted just like those from the built-in functions, so that users can use Out[ ] or % to directly apply the rules. ...
2
votes
1answer
63 views

Replace part on string in list

I have a list with string and integers and want to replace one word in the string with an integer. For Example if I have ...
1
vote
0answers
42 views

ReplaceAll and derivatives

I have the following coming from a computation, $$h_1 (u,v,w) = \frac{1}{v}\partial_v g(u,v,w)$$ and in Mathematica I have different quantities in terms of $h_1$ and its derivatives like $\...
3
votes
2answers
85 views

Generating all permutations of labels in an expression

I have some very long and complex expressions which involve a set of $n$ variables, and I want to be able to permute the labels of the variables. I will give a simple example, instead of my awful ...
4
votes
2answers
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?
1
vote
2answers
119 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 ...
8
votes
4answers
165 views
4
votes
0answers
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: ...
0
votes
0answers
34 views

Gradient Descent's Update Rule for Multiparameter Functions

I'm sure this has been answered before, but I haven't been able to find it. I'm trying to manually implement gradient descent for some function, f, with a vector of any length. I define the update ...
1
vote
0answers
79 views

Speed differences between Table, Replace, Array, Map, and AssociationMap

EDIT: Actually, it looks like part of this is bogus—Array doesn't actually work on this input (I'm often a little shaky on what kind of input, exactly, works for ...
1
vote
1answer
33 views

Substitute when hold is on

I have an expression for $div$ with Lame coefficients. I want to look at the $div$ in different coordinate systems. So I want to substitute the Lame coefficients not letting Mathematica to actually ...
4
votes
3answers
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 ...
12
votes
2answers
205 views

Opposite of Alternatives in a Rule

Mathematica provides Alternatives, to match one of several patterns. In a rule, this is used as p1|p2|p3:>replacement. I ...
1
vote
0answers
41 views

Using Replacement Rule on Part of List? [closed]

Why can't I use a replacement rule to replace a variable name standing in for the index of a list? For example: myList={1,2,3,4,5} myList[[index]]/.index->3 ...
6
votes
5answers
682 views

How to replace a space by a comma

in this expression = "{1 2 3 4 5 6 7 8}", I want to replace each space by a comma , to get ...
1
vote
1answer
53 views

Is there parallel/simultaneous substitution in Mathematica?

I know this is simple but I'm not proficient at Mathematica and cannot seem to find it online or in the help. Also, Replace[] says it does the assignments in order, ...
-1
votes
1answer
38 views

Substitution in a power transformed in product

This could be thought as a duplicate question but it's in fact an extension of an old section. 1 + (a (α + β))/(b^2 + c^2) + ((α + β)^2 a^2)/(b^2 + c^2) could ...
0
votes
2answers
46 views

How to replace the argument of a function?

For the following expression, b'[x y/z]+z b''[x y/z] I want to replace the argument of b such that the above expression becomes ...
0
votes
1answer
39 views

Replace argument of delayed function before it evaluates [duplicate]

How should I use replace so that f[x_] := x^2 Replace[f[3], 3 -> 1] OUT = 9 evaluates to 1, instead of 9? This way, the replacement of the argument is done ...
3
votes
1answer
155 views

Is this a bug in NCAlgebra library?

In course of my work with NCAlgebra package for Mathematica, http://math.ucsd.edu/~ncalg/ , I encountered unexpected behavior of replacements. I am using ...
6
votes
2answers
261 views

How to program a click panel for chemical elements (like a periodic table)?

The command ColorData["Atoms", "Panel"] indicates RGBColors of atoms by clicking a panel. As I don't want to reinvent the ...
2
votes
0answers
66 views

Find and Replace in Notebook using a Pattern [closed]

I would love to be able to use the power of Mathematica's patterns to find and replace within my code. I have searched for this on google, and so far I've come up with this: http://reference.wolfram....
4
votes
1answer
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 ...
2
votes
1answer
87 views

replacement rule doesn't work

I define a function which counts the number of negative elements in a list as follows: g[x1_, x2_] := Count[{x1, x2}, _?Negative] Plugging in values shows that ...
3
votes
2answers
114 views

Replace non-zero elements with symbol

I have a very large matrix (tensor) in Mathematica with some zero and non-zero elements. I am interested in replacing the non-zero elements with some symbol or a 1 so that it is easier to view the ...
0
votes
0answers
42 views

Replace higher order derivatives given lower order derivative

I have an unknown function f[x1,x2] whose partial with respect to x1 is identically zero. I would like to make sure that ...
1
vote
1answer
45 views

How to define a Reduce function in order to define two parameters afterwards

I'm trying to evaluate numerically an equilibrium for a system of ODEs: dA = c A + b B - a A dB = d B + a A - b B So I define a list of known parameters: ...
1
vote
1answer
41 views

Why doesn't recursive transformation stop when rules no longer match?

In an exercise where I was trying to pick out consecutive words starting with the same letter, i.e. I wanted: ...
1
vote
1answer
71 views

Replace a variable with a list of numbers [closed]

How can I replace a variable with a list of numbers? Example Suppose I have the following list of values V = Table[i, {i, 1, 6.8, 0.19}]; and I want to ...
0
votes
1answer
87 views

Solve equation with two different sets of replacement rules

I want to solve eqn for c0 in the following code. I want to do it with both replacement rules. I realize I could just use solve ...
6
votes
3answers
230 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
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 ...
2
votes
2answers
463 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
112 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
40 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
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 ...
9
votes
1answer
205 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 ...
0
votes
1answer
36 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 ...