23
votes
22
votes
Accepted
Is this a bug in Replace?
This is not a bug. However, this is a great opportunity to shed some light on the inner workings of Replace. What happens, in short, is that it first fully ...
19
votes
Reverse Lookup: getting keys from values
One way would be to "invert" the association and then look up values in the result:
...
18
votes
Evaluating expressions by applying corresponding rules stored in another list
MapThread[ReplaceAll,{{x,x^2,x^3},{x->1,x->2,x->3}}]
14
votes
Accepted
A simple problem about a Rule example
Pattern matching is performed based on the form of an expression, not its (mathematical) meaning. Using jargon, pattern matching is performed syntactically, not semantically.
Specifically, here ...
14
votes
Accepted
I'm not able to delete -> in {{x -> a}, {x -> b}, {x -> c, x -> d}}
DeleteCases does not by default operate on heads. You can set the Heads option to True to ...
13
votes
Accepted
Pattern matching Association in rules
The problem can be seen in simpler expressions:
MatchQ[<||>, <||>]
(* True *)
MatchQ[<||>, HoldPattern[<||>]]
(* False *)
This behaviour ...
12
votes
Accepted
11
votes
Why and how does RuleDelayed evaluate to itself?
RuleDelayed is unconventional in that it strips any number of occurrences of Unevaluated from the right-hand side:
...
11
votes
Can't understand one paragraph in Power Programming with Mathematica
He is simply saying that the first argument of Set is not evaluated before Set creates the definition.
However, the sub-parts ...
11
votes
Accepted
11
votes
Accepted
StringReplace with multiple patterns
Short Version
Order matters when specifying replacement rules. Rules are tried from left-to-right. Each rule will attempt to match and replace as much of the string as possible before moving on to ...
11
votes
Evaluating expressions by applying corresponding rules stored in another list
I would use MapThread as it is the function designed for this purpose; but here is another solution step by step:
...
11
votes
How can I change the expression x1 to x[1] in a simple way?
I added xy to exp0 just to prove there's no xy problem :) as well as a two-digit index:
<...
10
votes
Reverse Lookup: getting keys from values
You may use Position on an Association to return the Key (or keys) that holds a value.
With ...
10
votes
10
votes
Pattern matching Association in rules
To @WReach's examples, one can add these:
MatchQ[<||>, Unevaluated@<||>]
MatchQ[<||>, HoldPattern[Evaluate@<||>]]
(*
False
True
*)
It ...
10
votes
Accepted
Joining all the keys of a list of rules that point to matching values
There are many ways, here's one:
KeyValueMap[#2 -> # &] @ GroupBy[rules, Last -> First, Flatten]
10
votes
Accepted
How can I get new random values with each blank in ReplaceAll?
You can simply change the Rule -> to RuleDelayed :> to give
...
10
votes
Accepted
Delete zeros from a sparse array
You can just apply SparseArray to a SparseArray object to normalize it. For your example:
...
10
votes
Accepted
Convert a list of lists into a list of rules
Why not
list = {{a, b}, {c, d}, {e, f}};
Rule @@@ list
{a -> b, c -> d, e -> f}
This will work as well as
...
10
votes
StringReplace with multiple patterns
WReach covered this well, but in supplement consider using the third parameter of StringReplace to see how a replacement evolves:
...
10
votes
Extract all Rules from Plot function
You can get the options associated with the plot theme "Detailed" using the (undocumented) internal function ...
10
votes
Accepted
Manipulating rules
a = {x -> 2, y -> 3};
b = {x -> 3, y -> 5};
<|a|> + <|b|> I // Normal
{x -> 2 + 3 I, y -> 3 + 5 I}
...
9
votes
Accepted
9
votes
Is there a simple way to apply a function to the RHS of a substitution?
Additional alternatives:
You can use MapAt to map myFunction at positions {All, -1, -1}:
<...
9
votes
Accepted
9
votes
Evaluating expressions by applying corresponding rules stored in another list
We define the following lists:
list = {x, x^2, x^3};
rules = {x -> 1, x -> 2, x -> 3};
And then we can run
Map
...
9
votes
Accepted
Smart way to remove rules from lists, leaving just values, specifically from FindMinimum output
I want to add this reply in response to the comment written under the OP.
The function Values does not work, as all the elements of the list are not rules. (the ...
9
votes
Manipulating rules
{a, b} = {{x -> 2, y -> 3}, {x -> 3, y -> 5}};
Normal @ Merge[Apply @ Complex] @ {a, b}
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
rule × 360replacement × 196
pattern-matching × 58
list-manipulation × 56
simplifying-expressions × 23
functions × 18
expression-manipulation × 16
associations × 16
equation-solving × 14
evaluation × 14
syntax × 13
string-manipulation × 11
assignment × 10
calculus-and-analysis × 9
symbolic × 9
function-construction × 8
plotting × 7
differential-equations × 7
core-language × 7
matrix × 6
programming × 6
summation × 6
filtering × 6
scoping × 6
argument-patterns × 6