Linked Questions

4
votes
1answer
151 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: <...
1
vote
2answers
208 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 ...
5
votes
2answers
154 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 ...
1
vote
2answers
205 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 ...
3
votes
1answer
136 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 ...
2
votes
1answer
99 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 ...
1
vote
1answer
115 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: ...
1
vote
0answers
202 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 ...
0
votes
1answer
132 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 ...
0
votes
1answer
74 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 ...
1
vote
1answer
43 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 ...
2
votes
0answers
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] ...
1
vote
0answers
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 ...
0
votes
0answers
36 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 ...
0
votes
0answers
35 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); <...
0
votes
0answers
29 views

Replacement[] and Apply[], aren't commutative? [duplicate]

in my notebook, I have a function which output is of the kind: zzz[0,{a,b,c},{d,e,f,g},OptionsPattern] Then, I need to use this output in another form ...
0
votes
0answers
16 views

KeyAbsent when using Association in a replacement [duplicate]

I am trying to replace derivatives with other functions. I expected as = Association[fx -> dfx] Derivative[1][fx][x] /. Derivative[1][y_][_] -> as[y] to ...
12
votes
4answers
640 views

“Strange” behavior of Rule

According to the Help lhs->rhs evaluates rhs immediately. How to understand the output of the following code? ClearAll@x; {1, 3.5} /. x_?IntegerQ -> {x} ...
18
votes
1answer
513 views

Why is ReplaceAll behaving like this?

I'm learning to use the ReplaceAll function and I found the behavior of which is quite confusing. For Sqrt[f[x, y]] /. f[___] -> u Mathematica returns <...
3
votes
2answers
200 views

Localizing Pattern Names

When I want to define a function that uses a pattern, how should I localize the pattern name (i.e., x_ localizing the symbol x)? ...
6
votes
1answer
1k views

String pattern search and replacement — it works, but produces error messages only the first time it is called

I defined a function in Mathematica to process a pre-generated string. More specifically, I'd like to process some parts of C codes generated from MMA. Basically what my function does is to expand the ...
2
votes
1answer
1k views

message about string expected in pattern [duplicate]

...
2
votes
1answer
223 views

How to make Simplify/Expand work in patterns?

I am trying to calculate deriv of a complicated function and store the values for reuse. But the FullSimplify in my definition doesn't seem work all the time: ...
2
votes
1answer
232 views

Confusion over blank sequence in pattern matching

My overall goal was to produce a list of heads used in a graphics output. For an example I used: ...
0
votes
2answers
125 views

Merging two variables in one

I was looking for a way of, given two variables called A and B, obtaining a new variable called AB. My idea was doing simple pattern matching, e.g. ...
3
votes
1answer
181 views

Rule for derivatives

I have a differential equation of the form $$f'(x)=g(x,f(x))$$ (where $g$ is has a known explicit form, e.g. $x f(x)$) and I have an expression which contains several derivatives of $f$. I want to ...
2
votes
1answer
88 views

how to manipulate pattern-matched lists for replacements

Consider the following simplified code snippet: line=Line[{#, 2 #} & /@ Range[10]] (* arbitrary line *) line /. {Line[x_List] -> Line[Most@x]} ...
2
votes
1answer
71 views

Why is head evaluation inconsistent in pattern replacement?

I noticed the following unexpected result: In[90]:= "foo" /. x_String -> Head[x] Out[90]= Symbol Why is it that Head[x] ...
2
votes
1answer
45 views

Why is the variable x not locally scope in the StringExpression?

Clear[x] StringCases["abcadcacb","a"~~x_~~"c"->x] {b,d} ...
0
votes
0answers
20 views

How to define a function that utilizes the Range function? [duplicate]

How can I define a function that uses the Range function in its definition. A very simple example would be f[x_]=Range[x] However if I try that it outputs "Range ...