# Tagged Questions

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

326 views

### Pattern does not match with Orderless head

Why does the following pattern matching not succeed? ...
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) : <...
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 ...
90 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 ...
172 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: ...
75 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? ...
102 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 ...
55 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] ...
63 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 ...
196 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 ...
87 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 ...
77 views

### Replace All using a list

I have a more complicated case however a basic example 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 ...
137 views

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

I imported data from .csv file and it looks like: ...
90 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 ...
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: ...
78 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: ...
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 <...
58 views

### How to rationalize real number in string?

Considering we have: in = "F 18/10" How can I make this to be: Thanks!
364 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 ...
174 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 ...
100 views

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

Given the following line of code, ...
81 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 -> 1/...
61 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 ...
148 views

### How to check keys and values in nested Associations?

Considering we have a an association: ...
69 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 ...
75 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 ...
56 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 ...
125 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 ...
65 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 ...
75 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.
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 ...
63 views

### Using HoldForm for a changing list

I have a 'list' of the form an expansion: F = (A[1] + A[2])(B[1] + B[2]), such that F[[1]] produces the result ...
133 views

### why does replacement rule not work

Why does the following replacement rule not work? term = x^2/y^2 + x^3/y^3 term /. u_^p_/v_^p_ -> z^p I expected it to return ...
124 views

### How to apply ReplaceAll to denominator?

Here is the simple task. In reality, it's far more complicated, but it comes down to this. I want to take the expression $\frac{y}{x^2}$ and replace $x^2$ with $k$. This doesn't work: ...
54 views

### Mathematical symbol and name for Symbolic Replacement

(Originally from math.SE, but might be better here) I'm searching for a mathematical symbol, that describes the symbolic replacement done for instance in Mathematica: ...
145 views

### Making ReplaceAll faster

I have a list of functions ncalc that is calculated by ...
54 views

### Pattern matching & simple replacement

I have a question about an expression that I want to modify. Here is the expression: ...
248 views

### Why is Block significantly faster than ReplaceAll for inputting variable values

Given that the documentation for FindMinimum uses ReplaceAll for its various examples of what to do with the found solution to ...
283 views

### Is there a programmatic equivalent of Edit > Find…?

Most things that can be done via the front-end interface in Mathematica can also be accomplished by some function. Is that the case for find-and-replacing? That is, is there some code I could execute ...
100 views

### Applying ReplaceAll to I (imaginary unit) [closed]

Why does I/.I->-I return -I but Exp[-I]/.I->-I yields ...
144 views

### Why does it matter at what point I replace?

I need to find four orthogonal linear combinations of complicated functions, that vanish at four different points. I use (LK4 is defined below, but its shape should have nothing to do with my problem)...
84 views

### Why does ReplaceAll behave so oddly?

ReplaceAll can behave a bit oddly. Consider ...
49 views

### What particular situation do we have to use RuleDelayed after replacement? [closed]

On the answer of How to impose normalization condition (not initial condition) on DSolve or NDSolve? k = vars /. sol /. cs[[1]] /. C[_] :> 0 // N; I see that ...
60 views

### Mathematica does not replace \[ImaginaryI] inside Hold

Mathematica 10.1 does not replace I (i.e. \[ImaginaryI], the square root of -1) inside functions with the Hold attribute. Example: ...
121 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 ...
83 views

### Replace symbols in output without evaluation

I found the Wolfram example for ExpandNCM but I want a more human-readable output form. I want to evaluate ExpandNCM, for example with ...
72 views

### Matrix iteration failing after second step

I am calculating 11x6 matrix using different logics as in below code. Problem is in Do loop in below code where I have two different criteria to update matrix "fd". I am expecting logic in IF which ...