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
140 views

StringReplace, ReplaceAll and Rule interact in a bizarre way

I have the following pair of things: ClearAll[foo, labeledFoo]; labeledFoo = {"FooBarBazQuux", foo}; This works like you'd expect: ...
3
votes
2answers
85 views

Replacing a function in defined expressions

I'm having a little trouble with substituting functions into an expression. A minimal example is as follows: ...
0
votes
1answer
42 views

Append subscript to variable name [duplicate]

I'm importing equations from Word document and transforming them into input form. But subscripts look ugly. Is is possible to programmatically append subscript to variable name? e.g. ...
0
votes
0answers
42 views

How can I represent this automorphism of Q(i,a)?

Let $a$ represent some real irrational number. I am trying to perform computations with an automorphism $f:\mathbb{Q}(i,a)\to\mathbb{Q}(i,a)$ which fixes $\mathbb{Q}(i)$ pointwise and maps $a$ to $i ...
1
vote
0answers
36 views

How can I use the Rules returned by FindFit? [on hold]

When I evlauate data = RandomReal[10, {5, 10, 2}]; fit[i_] := FindFit[data[[i]], a Sin[x] + b, {a, b}, x] fit[1] I get something like: ...
11
votes
4answers
226 views

Replacing parts of a held expression with held parts of another expression

I'm applying the Trott-Strzebonski technique in order to replace inside a held expression. ...
0
votes
0answers
85 views

Calculating the Lyapunov Exponent/Spectrum for the Duffing Oscillator

Some fellow students and I are trying to calculate and plot the Lyapunov spectrum for the Duffing oscillator (currently a test approach). Each of us has Mathematica knowledge based purely on past ...
0
votes
2answers
94 views

How to partition three years of data into daily samples

I'm trying to find mean of temperature data list. The temperature data list has 3 hours steps which means I have 365*8=2920 temperature measurements per year. My data are temperatures of 13 years. ...
2
votes
1answer
63 views

Using Total on a list supplied by a rule

I'm confused as to why Total complains about a nonatomic expression here: ...
1
vote
1answer
47 views

How can I get the functionality of a command ReplaceAllList?

I'm trying to use ReplaceList to match subexpressions. I expected the command: ReplaceList[{p, {p, p}}, {x_, y_} -> x y] to output two results, equivalent to: ...
0
votes
1answer
61 views

Replace one order of derivative inside higher one

How could I replace for example: Derivative[2][f][x] by Derivative[1][g][x] where ...
0
votes
1answer
43 views

Replacing powers of $n$-th partial sums in the output of a function with $S[i]$ for $i = 1,\ldots,n$

I've written some simple code that, for various $N \geq 1$ and $k = 1,\ldots,N+1$ computes the $n$-th derivative of $\log S_N$ where $S_N(z) = \sum_{k=0}^N \frac{1}{k!}z^k$ as follows: ...
2
votes
1answer
42 views

Ordering for Pattern matching in Subscripts

I have some transformation rules acting on subscripted variables. When I match the a product the order matters. ...
0
votes
1answer
82 views

Replacement rules in combination with pure functions to make a change of variables

I have defined my function in this way w = f[r,Θ] ; After some calculations i obtained my results with respect to the previous function. For instance: ...
8
votes
2answers
315 views

Using ReplaceAll on SparseArray

I'm using SparseArray in a notebook in which I am doing complex conjugation manually, i.e. writing $\sqrt{-1}$ as i and applying ...
1
vote
1answer
94 views

Matrix substitution

I'm newbie in Mathematica and would like to ask one simple question. Let's say I have matrix with some elements == -1 ...
2
votes
1answer
58 views

Applying rules to rewrite expressions that contain patterns

I'm trying to write a rule that can rewrite a pattern that exists in another expression. For some reason, this is proving to be more challenging than I expected ... most likely because I'm not totally ...
1
vote
0answers
30 views

How to apply an rule to factors matching a certain form

Suppose we have many expressions involving irrational ArcTan expressions (such as ArcTan[Sqrt[3],5]) along with other irrational factors (such as ...
0
votes
2answers
76 views

Replace matrix with conditions

I want to replace the values of 1. in slot under the condition that the year and month in basevalue and ...
0
votes
1answer
42 views

Solve equation with two different sets of replacement rules

I want to solve eqn for c0. I want to do it with both replacement rules. I realize I could just use solve twice, once for subsCoke, and once for subsPepsi. I'm trying to do it in just one Sovle to ...
1
vote
1answer
38 views

Commutative algebra by generators and quadratic relations

My apologies if this has been asked before. Please also be gentle, as this is my first question here. I am trying to define a commutative algebra by generators and relations in the following way. ...
0
votes
0answers
28 views

Block, With, Module, can anyone help me understand the distinction? [duplicate]

I've been working heavily with Block[] for months in my research so I've read the documentation for the seemingly similar commands With[], Block[], and Module[]. Personally, I've had by far the ...
10
votes
7answers
2k views

How do I replace a variable in a polynomial?

How do I substitue z^2->x in the following polynomial z^4+z^2+4? z^4+z^2+4 /. z^2->x ...
1
vote
2answers
88 views

How to get a rule for, e.g., (x + y) to apply to (-x - y), etc.?

For example, the substitution (x + y) -> s fails here: In[1]:= (-x - y) /. (x + y) -> s Out[1]= -x - y Of course, I ...
2
votes
2answers
103 views

Generating an editable table that can be used as input

Say I have a number $n_{max}$. I want a command to generate a table like 1 1 2 2 3 3 4 4 ... nmax nmax where the second column is editable, and this table can be ...
1
vote
3answers
111 views

Conditional Check on Matrix

Suppose I have the following matrix (i,j): ...
0
votes
0answers
65 views

Back Linear Substitution

I have an equation set Ax=b and A(n*m),n<m . To lower down the dimension, I express some vars by other vars, which gives ...
0
votes
0answers
18 views

Manipulating named patterns inside replacement rules [duplicate]

When I evaluate the following expression, {a -> "value1", b -> "value22"} /. {Rule[x_, y_] -> Rule[x, y]} I get ...
3
votes
0answers
60 views

Why doesn't ReplaceAll work on Image? [duplicate]

ReplaceAll seems not to reach inside Image, and I don't see why it shouldn't. Here is an example. Create a black and white image ...
1
vote
2answers
63 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?
4
votes
1answer
137 views

replace items in a list with items in a another list

Suppose I have a list A={x1,x2,x3}, a list B={3,4,5}, and a function f[x]=x1+x2+x3. Is there ...
0
votes
0answers
71 views

Patterns and replacements [closed]

I basically need to read off informations from an expression in order to apply some recursion relations. The terms I encounter have a form like $f(k) = (1+ k_{\alpha}^{n1} k_\beta^{m1} ...
1
vote
2answers
80 views

How to “reversely” replace definitions?

I have the following problem at hand. 1.) I have a list of definitions like ...
5
votes
5answers
498 views

Replace values which obey certain criteria

I'd like to replace all values in a list which obey one or more criteria with another value. Example: Replace all values>30 by 30. data={{3,35},{2,7}} ...
0
votes
2answers
90 views

Handling output that was returned unevaluated from a function

When evaluating functions, including those described in this post, how do Mathematica users handle values returned from a function that are unevaluated? For example (based on 1): ...
2
votes
1answer
29 views

Table doesn't replace inside conditionals (If, Which) [duplicate]

This: Table[If[cond, y], {y, 1, 3}] outputs this: {If[cond, y], If[cond, y], If[cond, y]} The ...
6
votes
3answers
273 views

How to make all numbers equal to one in a Series?

I have many outputs of one-dimensional Series expansions for which I am only interested in the general tending with the variable. For example, I would like to be able to transform something like ...
5
votes
2answers
156 views

How to “ReplaceAll” using a set of conditional expressions instead of a list of rules [closed]

Suppose that I have two linear functions f[x_] := f0 + f1 x g[x_] := g0 + g1 x and a (possibly rather complicated) set of conditional expressions, obtained ...
1
vote
2answers
63 views

How do I use a passed-in symbol as a tag to define UpValues?

ClearAll@foo; foo[tag_] := ( tag /: tag[i_] := tag[[i]] ); a = {7, 8, 9}; foo[a]; a[2] I wanted the above to output 8, ...
1
vote
1answer
52 views
1
vote
1answer
128 views

How to properly substitute values into variables?

I have an array of functions in terms of a[0] through a[n] and I wish to replace the a[]'s with previously calculated values p[0] through p[n], as in below: ...
1
vote
1answer
42 views

How to substitue local variables into expression with free variables?

I'm wondering how to do the following: expr = L + R; foo[L_, R_] := expr; foo[1, 2] (* L + R *) and have it substitute the function variables ...
7
votes
3answers
139 views

Converting StringJoin to StringForm inside Hold

Let's say I have a piece of code: Hold[{code1, "asdad " <> ToString[testa] <> " adsd " <> ToString[testb], code2}] (*MWE ofc*) ...
3
votes
2answers
95 views

truncating terms in symbolic expression basing on their values

I need to simplify big symbolic solution of equations system by truncating terms that are orders of magnitude less then others in subexpressions like a + b. The solution is big fraction with sum of ...
3
votes
1answer
61 views

Have transformation rules to be explicitly set?

I think this problem is not the hardest one, but I can't hit on it. I want to NSolve an equation. For this equation I use a transformation rule "ampTimes". First I used it like this: ...
3
votes
1answer
85 views

Replace rule with function? Derivatives don't evaluate

Say I have an expression (call it expr) involving a function, f[x]. I'd like to be able to evaluate that for a particular choice of f[x] without setting that choice for the whole session. I thought to ...
3
votes
4answers
188 views

Replace symbols with values without evaluation

If I have assigned values to a, b and c: ...
3
votes
1answer
83 views

some confusion about ReplaceRepeated

This is Right. NestWhile[Log, 100, # > 1 &] {100} //. {x_} /; x > 1 :> {Log[x]} 100. //. x_ /; x > 1 :> Log[x] This is wrong,why? ...