Linked Questions

4 votes
1 answer
157 views

How can I avoid this ReplaceAll trap? [duplicate]

I am having some difficulties with the ReplaceAll function. When I write: {1, 2, 3, 4, 5} /. x_?OddQ -> x^2 I expect: <...
Pedro's user avatar
  • 129
1 vote
2 answers
258 views

Why doesn't ToString work in ReplaceAll? [duplicate]

I'm trying to use ReplaceAll in a subscript. However, it doesn't seem to work as expected if I want to use the output the subscript in a ...
JeffDror's user avatar
  • 1,870
5 votes
2 answers
161 views

Trouble with Replacements [duplicate]

I have the following line of code: Plus @@ Table[p[x], {x, 0, 20}] /. p[x_] -> Boole[MemberQ[{0, 5}, x]] The first part of this produces ...
RoyalTS's user avatar
  • 249
1 vote
2 answers
236 views

Make a partition with a rule [duplicate]

I wrote this line of code : {{1, 1, 1}, {1, 1}} /. ((x___ /; Length[x] > 2) -> Partition[x, 2, 1]) generates this error : Partition::pdep: Depth 1 ...
Doedalos's user avatar
  • 616
2 votes
1 answer
119 views

Rule vs RuleDelayed in the context of StringReverse [duplicate]

{"test", 3} /. s_String :> StringReverse[s] {"test", 3} /. s_String -> StringReverse[s] The second line gives the error: StringReverse: String expected ...
GambitSquared's user avatar
3 votes
1 answer
138 views

How to localize symbols on the right-hand side of Rule when using ReplaceAll? [duplicate]

I am frustrated by the following: {1, 2, 3.0} /. a_Integer -> a + 1 The a on the left side of the arrow is localized and ...
eldo's user avatar
  • 48k
1 vote
1 answer
161 views

How to use EvenQ[] in patterns? [duplicate]

This works: (w[1] + w[2] + w[3]) /. w[s_] -> If[s == 2, w[s], 0] (* w[2] *) This doesn't: ...
Jerry Guern's user avatar
  • 4,553
1 vote
0 answers
248 views

Problem in evaluating nested anonymous functions [duplicate]

It seems that I have found a bug (or a fundamental problem in the design of the programming language of Mathematica) with functions defined as rules. Define the function g as g(1)=2 and g(2)=1. We can ...
Mauri Ericson Sombowadile's user avatar
0 votes
1 answer
152 views

Sum of Sequence inside a replace rule? [duplicate]

I'd like a replacement rule that replaces derivatives with their total order. I tried ReplaceAll[Derivative[x__] -> Plus[x]][Derivative[1, 1]] but it doesn't ...
Sampo Smolander's user avatar
0 votes
1 answer
80 views

What's wrong with my rule? [duplicate]

Consider a simple code to form a SparseArray: Q = 5; a = Table[i, {i, Q}]; SparseArray[{{j_} /; j > 2 -> a[[j]]}, {Q}] Part::pspec: Part specification j ...
Yasha Gindikin's user avatar
1 vote
1 answer
54 views

Replacing with Elements from a List [duplicate]

I have a fairly complicated expression with nested lists and I want to substitute a given string for each integer. Here's a simple example where ind is my integer-valued data structure and names is ...
bill s's user avatar
  • 68.3k
2 votes
0 answers
59 views

pattern matching and applying a function, not working as expected [duplicate]

Consider the following code G[x, y] /. a_[b__] -> Total[a @@@ Permutations[List[b]]] I expect it to give G[x, y]+G[y,x] ...
Srivatsan Balakrishnan's user avatar
1 vote
0 answers
41 views

Replace date pattern [duplicate]

I have a list, such as the following: li = {"2017-10-16", 10.24, 10.71, 10.18, 10.61, 10.61, 16802200} Extracting the first element ...
sunspots's user avatar
  • 259
0 votes
0 answers
38 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 ...
verse's user avatar
  • 1,287
0 votes
0 answers
36 views

Puzzling transformation rule output [duplicate]

My problem should be a trivial one but for the life of me I can't get it to work. This is my data (10 rows, each made up of a DateList and a list of 5 numeric values corresponding to that date); <...
RobertNathaniel's user avatar

15 30 50 per page