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

learn more… | top users | synonyms (4)

2
votes
0answers
53 views

Optimization of recursive functions by using the //. operator

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
43 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
83 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
30 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
52 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
77 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
35 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
54 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
105 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
73 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
70 views

Replace All using a list

I have a more complicated case however a basic example is; ...
4
votes
1answer
102 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
40 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
50 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
84 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
70 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
98 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
51 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
285 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
142 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
59 views
1
vote
1answer
69 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
53 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
67 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
94 views

How to check keys and values in nested Associations?

Considering we have a an association: ...
1
vote
1answer
49 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
61 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
44 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
81 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
57 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
65 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 ...
1
vote
1answer
50 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 ...
1
vote
2answers
90 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 ...
3
votes
3answers
71 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: ...
0
votes
0answers
37 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: ...
1
vote
1answer
133 views

Making ReplaceAll faster

I have a list of functions ncalc that is calculated by ...
2
votes
1answer
48 views

Pattern matching & simple replacement

I have a question about an expression that I want to modify. Here is the expression: ...
8
votes
1answer
231 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 ...
10
votes
1answer
259 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 ...
0
votes
1answer
78 views

Applying ReplaceAll to I (imaginary unit) [closed]

Why does I/.I->-I return -I but Exp[-I]/.I->-I yields ...
1
vote
1answer
140 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 ...
1
vote
1answer
83 views

Why does ReplaceAll behave so oddly?

ReplaceAll can behave a bit oddly. Consider ...
1
vote
1answer
40 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 ...
2
votes
1answer
52 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: ...
0
votes
1answer
59 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 ...
1
vote
1answer
48 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 ...
0
votes
0answers
67 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 ...
0
votes
1answer
52 views

Replace element of matrix based on rule which invloves other element of same matrix

I am trying to replace 1s row of matrix d iteratively from right by multiplying last element in 1st row with (1-int*dt) to generate 2nd last element. The replacement continues till I reach the 1st ...
0
votes
0answers
25 views

Convert the output of solve into equality [duplicate]

I know this is has been asked before but seriously, none of the ones I checked helped because they referred to a single variable assignation. Apologies if this is a duplicate. I have many variables ...