Hot answers tagged

23 votes

Reverse Lookup: getting keys from values

PositionIndex[assoc][<value>] For example: ...
user1066's user avatar
  • 15.6k
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 ...
Leonid Shifrin's user avatar
19 votes

Reverse Lookup: getting keys from values

One way would be to "invert" the association and then look up values in the result: ...
WReach's user avatar
  • 68.4k
18 votes
Accepted

When is the righthand side of a RuleDelayed evaluated?

There seems to be a subtlety in the way delayed rules are used. Have a look at the following: ...
Fred Simons's user avatar
  • 10.1k
17 votes

Evaluating expressions by applying corresponding rules stored in another list

MapThread[ReplaceAll,{{x,x^2,x^3},{x->1,x->2,x->3}}]
cvgmt's user avatar
  • 63.4k
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 ...
Αλέξανδρος Ζεγγ's user avatar
13 votes
Accepted

Pattern matching Association in rules

The problem can be seen in simpler expressions: MatchQ[<||>, <||>] (* True *) MatchQ[<||>, HoldPattern[<||>]] (* False *) This behaviour ...
WReach's user avatar
  • 68.4k
12 votes

Pattern Matching for Rules

Under the Possible Issues tab of the Cases documentation. Use HoldPattern to treat the rule itself as a pattern: ...
Quantum_Oli's user avatar
  • 7,874
12 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 ...
Mr.Wizard's user avatar
  • 270k
12 votes
Accepted

Add the values in the rule

Merge[Total]@{a -> 1, a -> 2, b -> 3, b -> 4}
rnotlnglgq's user avatar
  • 3,690
11 votes
Accepted

Using two rules for the same variable

As far as I understand it, once a subexpression gets replaced, it can't get replaced again. Try this: a*b /. {a -> a, a -> 1, b -> c} resulting in <...
march's user avatar
  • 22.9k
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: ...
WReach's user avatar
  • 68.4k
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 ...
Szabolcs's user avatar
  • 233k
11 votes
Accepted

Is there a simple way to apply a function to the RHS of a substitution?

...
Bob Hanlon's user avatar
  • 151k
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 ...
WReach's user avatar
  • 68.4k
10 votes

How to make a ReplaceAll Rule for adjacent items in a list?

...
Dr. belisarius's user avatar
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 ...
Edmund's user avatar
  • 41.7k
10 votes

Reverse Lookup: getting keys from values

Pick may be worthy of consideration: ...
Mr.Wizard's user avatar
  • 270k
10 votes

Pattern matching Association in rules

To @WReach's examples, one can add these: MatchQ[<||>, Unevaluated@<||>] MatchQ[<||>, HoldPattern[Evaluate@<||>]] (* False True *) It ...
Michael E2's user avatar
  • 233k
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]
Kuba's user avatar
  • 136k
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 ...
NonDairyNeutrino's user avatar
10 votes
Accepted

Delete zeros from a sparse array

You can just apply SparseArray to a SparseArray object to normalize it. For your example: ...
Carl Woll's user avatar
  • 130k
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: ...
Mr.Wizard's user avatar
  • 270k
10 votes

Extract all Rules from Plot function

You can get the options associated with the plot theme "Detailed" using the (undocumented) internal function ...
kglr's user avatar
  • 384k
10 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: ...
Syed's user avatar
  • 41.5k
9 votes
Accepted

How to change symbols in differential equation with a single rule?

Your question suggests that you are not aware that f[x] and f'[x] have very different internal representations. ...
halirutan's user avatar
  • 112k
9 votes
Accepted

How to prevent substitution in a part of an expression?

This is IMO one of the cases where local rules are not a good match for the problem, and one is better off using custom expression parsers. The problem with rules is that the whole expression is ...
Leonid Shifrin's user avatar
9 votes

How do I encode this replacement rule?

Function - just feed it the 5 x 5 array, returns updated array: ...
ciao's user avatar
  • 25.6k
9 votes
Accepted

Simple rule does not work

You can use patterns in your replacement rules: ...
Gyebro's user avatar
  • 386
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}: <...
kglr's user avatar
  • 384k

Only top scored, non community-wiki answers of a minimum length are eligible