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

learn more… | top users | synonyms (4)

7
votes
1answer
168 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
45 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
90 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
167 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: ...
5
votes
5answers
204 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 ...
2
votes
1answer
101 views
0
votes
1answer
52 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 ...
9
votes
1answer
108 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
84 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
211 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
179 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 ...
5
votes
3answers
176 views

How to replace an element in a list with an element in another list with matching indices

I have a list like this l = {1 + 2*s, 2 + 3*s, 4 + 1*s}, and I have calculated s = {1, 2, 3}. I need to replace ...
2
votes
1answer
91 views

How avoid error message with Range?

Evaluating Range[1, n] /. n -> 3 produces an error message ...
0
votes
2answers
177 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 ...
1
vote
1answer
229 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 ...
19
votes
2answers
305 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) : ...
0
votes
1answer
73 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
88 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 ...
0
votes
1answer
128 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: ...
1
vote
1answer
67 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? ...
1
vote
1answer
101 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 ...
1
vote
2answers
52 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
62 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 ...
2
votes
3answers
179 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
75 views

Replace All using a list

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

Replace empty spaces with zero (0.0) after Import .csv data

I imported data from .csv file and it looks like: ...
0
votes
2answers
82 views

Replacement rules do not apply for an expression inside a root

Say, you have an expression which looks like this: myexpr = (mess)+(some other mess)/( (mess)^3+(some other mess)^2 )^(1/3) Here the 'mess' in the name of terms ...
4
votes
1answer
117 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: ...
2
votes
2answers
76 views

Trouble simplifying using Replace

I have the following expression: w.a.b.r+w.c.d.r I want to get: w.(a.b+c.d).r So far I used ReplaceRepeated: ...
2
votes
2answers
114 views

Replace elements in some region in a list

I want to replace some elements in list. Assume that we have A={{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}} and we want to replace elements ...
1
vote
1answer
58 views

How to rationalize real number in string?

Considering we have: in = "F 18/10" How can I make this to be: Thanks!
5
votes
4answers
339 views

ReplaceAll is not working correctly

The line of code -E^(-I k) x /. {-x -> -x, -E^(-I k) x -> -E^(I k) x} is not properly replacing the exponential of (-I k) with the exponential of (I k), as ...
2
votes
4answers
168 views

Symbolic cut-off of high-order terms

I know that I can cut-off high-order terms of a $1$-variable polynomial P = a0 + a1*x + a2*x^2 + a3*x^3 + a4*x^4 + a5*x^5; simply by doing for example ...
1
vote
1answer
71 views

How to change a point to circle in a Graphics expresion [closed]

Given the following line of code, ...
1
vote
1answer
77 views

Apply solution rules to new functions as parameters [duplicate]

Update: I put an screenshot of how my code and problem look like at the end. Say that Mathematica solved a system of equations and got two answers {{a -> 3, b -> c, d -> 0}, {a -> -3, b -> 0, c -> ...
0
votes
4answers
58 views

How to trim out sequence of blank-spaces?

Considering we have few strings: s1 = "dasbdk " s2 = "asdnkal asn knkl nkn dvklsn " s3 = " jbjkbasd nklnkln " Q1: How can the blank-spaces in ...
0
votes
0answers
91 views

Replacing function in Sum

I have this expression, which, after execution, gives these $F[s,r]$ in output. $$ S_{1,1}=\sum _{s=1}^{16} \sum _{r=1}^{16} F(s,r) \text{Tr}\left[G_r.G_1.G_s.G_1\right] $$ Here is a piece of ...
2
votes
3answers
134 views

How to check keys and values in nested Associations?

Considering we have a an association: ...
1
vote
1answer
61 views

Concatenated rule applications (applying rules to rules correctly)

I am trying to apply a series of individual rules onto an expression sequentially, but also not have to write out the repeated ReplaceAll functions each time I want ...
0
votes
1answer
72 views

Replacing every nth comma in string output with “\n” [closed]

Feels like a strange question to ask, but I basically have a string with sets within sets, and it seems like the simplest formatting solution to seeing the data without needing to import everything ...
2
votes
1answer
55 views

Conditional replacement syntax

Why does the following expression not do what I expect Array[a, 6, 0] /. a[__?MemberQ[{1, 2}, #] &] -> 0 I expect ...
2
votes
1answer
107 views

What's the difference between functions and replacement?

I used to thought that calling a function is equal to applying a rule, i.e. applying a rule of f[x_]->sth. is equal to calling a function f with definition f[x_]:=sth. However, given ...
0
votes
1answer
63 views

Setting a range of values to one value [closed]

My question is perhaps worded a little vaguely, but it's a problem I'm running into defining my own function. I'm drawing some shapes with variable height, and sometimes this height is 0. For it still ...
0
votes
1answer
74 views

Substituting inside a differential equation

x y''[x]+2y'[x]+l^2 x y[x]==0/.y[x]->Cos[x] I think above code shows what I want to do quite clearly. How do I make it happen. Only y[x] is replaced.
1
vote
1answer
108 views

A puzzling result of the `ReplaceAll` [duplicate]

I tried to do something below Log[PDF[NormalDistribution[m, s], x]] /. Log[u_[x__] ] :> Plus @@ Log[List[x]] which gives a desirable result that splits the ...