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

learn more… | top users | synonyms (4)

3
votes
1answer
64 views

Is the replacement operator associative?

In mathematics, a given operation $\circ$ is said to be associative if there is equality $$(f\circ g)\circ h=f\circ(g\circ h)$$ for any $f$, $g$ and $h$. Intuitively, this means ...
5
votes
5answers
419 views

Random character change in a random place

I would like to ask for help in a Random Replace of character. For example: ORANGE and change 1 character randomly (random place and random character from ORANGE) in it into any character of ORANGE, ...
2
votes
1answer
50 views

Replacing elements in a list if contains X (string)

I want to replace T1 with T2 if the list contains X (It's a string with something more) or else return unevaluated. Input:...
0
votes
2answers
36 views

How to replace a hole mathematical expression? [closed]

I have the following function: Input: f=-a n^2/v^2 +v/n I want to Replace v/n by v_new ...
0
votes
1answer
93 views

Getting mathematica to factor out constants and apply euler's identity to a table

I'm doing some quantum mechanics homework that I'm going to eventually generalize to higher spin states, so the solutions are going to need to be decently independent of the dimensions of the table ...
4
votes
1answer
146 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 ...
7
votes
3answers
168 views

A replacement question

I have: {{θ -> 0}, {θ -> (2 π)/ 3}, {θ -> π}, {θ -> (4 π)/3}} I would like to create a list of points $(\cos\theta,\sin\theta)$ using each of the ...
4
votes
2answers
64 views

Type error before replacement rule is applied

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

Replace instanced variables within module-return functions for dynamic Tooltip

Currently I am stuck with the issue of renaming an instanced variable like r1$38655 to a global one. The function is created within a "localizeAll" Module. Here's ...
2
votes
2answers
138 views

Replacement rule not having desired effect

So I have this replacement rule that tends to work in most cases, except occasionally in cases like this: ...
8
votes
1answer
78 views

On an existing notebook, how to transform all “double brackets” into “DoubleBracket”

Sometimes, having all double brackets [[...]] transformed into \\[LeftDoubleBracket] ... \\[RightDoubleBracket] can really ...
6
votes
2answers
187 views

How can you resize all the images in a notebook section?

I've pasted a large number of photos and they are too big which makes scrolling hard. Is there a way to automatically have Mathematica size them to 10% or something reasonable without have to click on ...
2
votes
1answer
54 views

Using strings in defining rules

I am very new using Mathematica and I would like to define a rule of replacement which operates as below, keeping freedom for what concern the pre-factors that can appear in the equation: $$e^{\pm ix} ...
1
vote
0answers
26 views

Why variable in SparseArray can't be replaced? [duplicate]

SparseArray[{1, 1} -> t] /. t -> 1 Still contains t. Why t is not replaced by 1?
2
votes
1answer
56 views

how to change Symbol to other symbol?

I have Input =a**b**c I want to have output such as {a,b,c} So I want to change **->, but replace code does not work. How ...
0
votes
1answer
86 views

Using NDSolve initial conditions as parameters for a function and then a replacement rule

I am trying to make a function based on two initial conditions of a set of differential equations: ...
2
votes
1answer
75 views
1
vote
1answer
103 views
3
votes
1answer
62 views

Simplifying an expression with complex variables

The other day I was trying to simplify the following expression of $w \in \mathbb{C}$ and $|w| < 1$: $$f(w) = \left|\frac{2}{(\frac{w+1}{1-w}+1)^2}\right|^{-\Delta} \cdot \frac{1}{\left|{\rm ...
3
votes
1answer
118 views

Remove leading and trailing zeros from a list

This functions and gives the desired result: {1, 0, 2, 0, 0} /. {x___, 0 ...} :> {x} {1, 0, 2} But with leading zeros it doesn't: ...
1
vote
0answers
43 views

Collect fails to collect all powers of my variable [closed]

I am fairly new to mathematica and have not used it to really code anything at all, but just used it as an kind of advanced calculator. My issue is the following: I need to extract the power of a ...
3
votes
1answer
87 views

How could I perform this replacement?

I essentially have something of the form pm[1, 2] pm[3, 4] pm[5, 6] c[{1, 2, 5, 6}, "+"] c[{3, 4}, "-"] I want to add next to each pm a corresponding plus or ...
1
vote
2answers
151 views

Find the explicit solution of the integral

I want to calculate the integral: assuming that we are familiar with the relation: The answer should be: The code that I've used is the following: ...
4
votes
5answers
171 views

Converting set of characters into numbers using list of replacement rules

I am trying to convert a text, as an example "aaaa" into a set of numbers as defined in a list of replacement rules, for example letterstonum . letterstonum takes in letters a to z and returns the ...
1
vote
0answers
59 views

How can I perform this replacement? [closed]

I want to take an expression of the form p[1,2]p[3,4] and have an output of something like {{1,2},{3,4}} If I do the ...
0
votes
1answer
50 views

How to do this replacement without a for loop?

I'm new to Mathematica and I'm trying to get past my desire to use For loops for everything; can somebody tell me a simple way to make this replacement? I want my input to be ...
0
votes
2answers
171 views

Approximate UnitStep for use in Enterprise CDF

The following Manipulate code displays the VectorPlot for all functions. (This code is a subset of a more extensive notebook which is based on the structure you see below. Consequently, I need to ...
9
votes
1answer
107 views

MapAt with Replace(All) causing kernel crash

Bug introduced in 10.1 and fixed in 10.2 I wanted to roll a function that works like /. but only acts on the very first match it encounters. ...
5
votes
1answer
79 views

How to transform abstract finite group to permutation group?

It seems that Mathematica only has group functionality for permutation groups? Then there is a step to transform the abstract finite one to a permutation one. As an example, consider the following ...
9
votes
3answers
210 views

How to make a ReplaceAll Rule for adjacent items in a list?

I would like to make only one pass of a list of 2D items and replace adjacent items that have matching $x$ or $y$ coordinates with a series of linearly interpolated points between the two points. My ...
5
votes
3answers
177 views

How to change symbols in differential equation with a single rule?

Problem Suppose I have a differential equation such as $\qquad f'(x)+f(x)=0$ and I want to change it into $\qquad y'+y=0$ My Method Using ...
2
votes
1answer
85 views

How avoid error message with Range?

Evaluating Range[1, n] /. n -> 3 produces an error message ...
1
vote
1answer
206 views

Duplicate Mathematica functionality in Mathematica clone or lisp [closed]

What is the easiest way to accomplish the following in a Mathematica clone or in any version of Lisp? Also it doesn't appear in any lisps have a similar replace function. ...
0
votes
1answer
29 views

Substitute ratios

Suppose that I have the expression (x+y+z)/y. I want to substitute the rules Phix -> x/y, Phiz ->z/y. Is there a way to ...
15
votes
1answer
210 views

Pattern does not match with Orderless head

Why does the following pattern matching not succeed? ...
4
votes
0answers
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) : ...
1
vote
1answer
95 views

How to assert that a list of variables is real in a nice and clean way

You can use the function Element to say e.g. $x,y,z\in\mathbb{R}$ via Element[x | y | z, Reals]. Now if I have a list of ...
0
votes
1answer
71 views

Changing notations for probability moments with subscripts

Suppose I have have two kinds of variables $X_{\alpha_j}, X_{\beta_k}$ in the form of Subscript[x,Subscript[α,j]] and ...
0
votes
1answer
86 views

Overriding the behavior of Dot

I would like to override the behavior of Dot, so that Dot[u,v] will return a scalar instead of a 1x1 matrix in the case that the ...
1
vote
1answer
63 views

Replace symbol with numerical value

How do I get the same result, i.e., how can I get Mathematica to replace a with 1? ...
6
votes
2answers
316 views

Matrix exponential via Cayley-Hamilton Theorem

I'm attempting to calculate the exponential of a matrix via Cayley-Hamilton theorem. (Following the "concrete example" from http://en.wikipedia.org/wiki/Cayley%E2%80%93Hamilton_theorem) I am having ...
1
vote
2answers
43 views

Replace function with multiple functions while respecting the inputs

I want to replace a function with either one or two functions and also combine their arguments. Say I have an original expression expr = u[i + 1] r[i] r[i-1] ...
3
votes
0answers
61 views

Why is the evaluation of f different using rules and using arguments to it as a function? [closed]

I will provide all the code I used at the end of the question. I was trying to evaluate a function f with the following signature (which I'll give the details at ...
3
votes
3answers
106 views
2
votes
3answers
159 views

How does one unpack the contents of a list while using rules to substitute values for each variable?

My intention is having a large collection of variables (symbols to be more specific) in a list and substituting all the variables at once with a value using rule instead of going through each one, one ...
0
votes
1answer
86 views

Using Simplify to perform substitutions [closed]

I have a matrix element $k_x$ and I want to substitute it as $-i\kappa$, I tried ...
2
votes
2answers
74 views

Replace All using a list

I have a more complicated case however a basic example is; ...
4
votes
1answer
118 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 ...