Skip to main content

Questions tagged [rule]

Questions about transformation rules in Mathematica--constructs that let you set, or programmatically modify, local values for expressions.

Filter by
Sorted by
Tagged with
24 votes
3 answers
2k views

Converting hierarchies of rules to associations

The impossibility of MatchQ-ing inside and Association MatchQ-ing Associations (MMA 10) explains a problem I have. I would like ...
John Jowett's user avatar
23 votes
2 answers
1k views

Difference between Association and Dispatch

In the example below, Association has a different behavior from Dispatch: ...
Murta's user avatar
  • 26.3k
22 votes
9 answers
4k views

Reverse Lookup: getting keys from values

Lists of Rules or Associations are both a useful way of mapping between values. It also ...
SumNeuron's user avatar
  • 5,422
22 votes
1 answer
677 views

What does ImageSize -> 1 -> 1 mean?

On the page http://www.wolfram.com/language/gallery/blur-faces-in-an-image/ the example code has ImageSize -> 1 -> 1. I am unfamiliar with this chaining of ...
PeterR's user avatar
  • 1,485
19 votes
1 answer
939 views

Is this a bug in Replace?

Consider the following minimal examples. This one works as expected, replacing trees starting from the inside with a numerical value. ...
István Zachar's user avatar
17 votes
2 answers
625 views

ReplaceRepeated seemingly omits some rules

Is there a simple way in Mathematica to prevent rule reordering? Let me give an example: ...
whackyusername1's user avatar
17 votes
1 answer
324 views

When is the righthand side of a RuleDelayed evaluated?

The documentation on RuleDelayed states that lhs :> rhs represents a rule that transforms lhs to rhs, evaluating rhs only after the rule is used. Consider the ...
Fred Simons's user avatar
  • 10.2k
16 votes
1 answer
476 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: ...
Pillsy's user avatar
  • 18.5k
16 votes
4 answers
638 views

ReplaceAll with Associations instead of Rules

I have just noticed that x /. <|x -> 123|> evaluates to 123 In other words, the association seems to behave as a ...
Gustavo Delfino's user avatar
12 votes
5 answers
493 views

Evaluating expressions by applying corresponding rules stored in another list

I have a list of expressions: {x,x^2,x^3} and a list of rules {x->1,x->2,x->3} How can I get ...
Patchouli Nine's user avatar
12 votes
5 answers
1k views

Select a particular rules out of a list

rule = {beta -> 4, alpha -> 2, x -> 4, z -> 2, w -> 0.8} Select[rule, beta] Select[rule, beta &] Select[rule, beta -> _ &] How would I ...
Chen Stats Yu's user avatar
12 votes
2 answers
598 views

Pattern matching Association in rules

I would appreciate an explanation of how Associations behave in pattern matching, especially when matching rules, and using HoldPattern. Consider the following: ...
ZeitPolizei's user avatar
12 votes
6 answers
742 views

Combinations of multiple matching patterns

I wonder if there is any nice way to combine DownValues (or any other suitable rule-/pattern-/function -based Mathematica construct) when multiple patterns match an expression. Let me explain what I ...
Sascha's user avatar
  • 8,489
12 votes
2 answers
265 views

Why and how does RuleDelayed evaluate to itself?

It seems that RuleDelayed has some behaviour we cannot reproduce. It is not just a symbolic wrapper. Consider this: ...
masterxilo's user avatar
  • 5,739
12 votes
1 answer
290 views

Practice notebook for Pure Functions & Rules-Patterns

Are there practice notebooks or exercises for practising using pure functions and rule-patterns? I've completed "An Elementary Introduction to the Wolfram Language" and "Fast Introduction for ...
I should change my Username's user avatar
11 votes
5 answers
514 views

Isomorphic lists

Is there a function that outputs True if lists are isomorphic (there exists a rule that changes the first list into the second one and inverse of the rule changes ...
azerbajdzan's user avatar
  • 19.7k
11 votes
2 answers
1k views

Can't understand one paragraph in Power Programming with Mathematica

In the chapter 7 of the Power Programming with Mathematica, there is a paragraph discussing the behavior of Set- functions: (page 205. (pdf version p. 224)) ...
Eric's user avatar
  • 1,191
10 votes
4 answers
422 views

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

Suppose I have solved a system of equations, resulting in a solution of the form: ...
user366202's user avatar
9 votes
2 answers
808 views

StringReplace with multiple patterns

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns. The problem is to translate a string into Morse code, ...
abcd's user avatar
  • 483
9 votes
3 answers
278 views

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

I would like to make only one pass of a list of 2D items and replace adjacent items that have matching $x$ or $y$ coordinates with a series of linearly interpolated points between the two points. My ...
Edmund's user avatar
  • 42.4k
9 votes
4 answers
467 views

Create Nested List from tabular data

Im trying to create a nested list from tabular data, then onward into a nested association. I can get part way with the help of this answer but it only works for the first level. Repeated use of this ...
Gordon Coale's user avatar
  • 2,341
9 votes
5 answers
986 views

Setting value of a rule

How do I change the value of a rule? For example, If I have a = x->3, how do I reset the value of x/.a to 4, so that ...
Yituo's user avatar
  • 1,409
9 votes
1 answer
426 views

Better replacement rules to perform this change of variables?

In cosmology, it is common to work with both a normal (a.k.a. “cosmic”) time variable t and a “conformal” time variable τ, which ...
thecommexokid's user avatar
9 votes
1 answer
137 views

SequenceCases does something odd with RuleDelayed arguments

I have a list of stuff. list = {a, b, b, a, b, a, a, a, b, a}; I want to split it up into subsequences where a single a is ...
Pillsy's user avatar
  • 18.5k
8 votes
6 answers
710 views

Smart way to remove rules from lists, leaving just values, specifically from FindMinimum output

I would like to know if there is a direct way to remove the rules from a list of rules. I know that, simply, this can be done with: ...
Joshua Salazar's user avatar
8 votes
5 answers
504 views

How can I change the expression x1 to x[1] in a simple way?

I have one expression, exp0=x1+x9/8+ x4 x5 I want to get exp1=x[1]+x[9]/8+x[4] x[5] Is there a very simple way to do that?
Orders's user avatar
  • 1,247
8 votes
5 answers
417 views

Joining all the keys of a list of rules that point to matching values

Suppose I have a list of rules whose keys are not of identical structure (in my case, the keys are either lists of at least one symbol or symbols alone): ...
user170231's user avatar
  • 1,621
8 votes
4 answers
425 views

How to prevent substitution in a part of an expression?

Background: I am trying to write a program that can execute scheme in Mathematica. Now I have things like {"+", 3, {"-", 2, {"'", {"*", 1, 3}}}}. The syntax ...
vapor's user avatar
  • 7,921
8 votes
2 answers
481 views

A simple problem about a Rule example

The documentation says that "Rule represents a rule that transforms lhs to rhs" and gives the following example: ...
user55777's user avatar
  • 671
8 votes
2 answers
1k views

How to replace a variable that was assigned a value before

Question How to make the replacement $f[x] \to f[y]$ for an abitrary function $f$ if $x$ was assigned a value before? Example 1 How can I make the replacement $x +1 \to y + 1$ if I have assigned ...
sjdh's user avatar
  • 7,777
8 votes
2 answers
307 views

Why does ToRules return a Sequence?

Why does ToRules return a Sequence expression? Why doesn't it return directly what ...
Szabolcs's user avatar
  • 236k
8 votes
2 answers
237 views

Given x ⊕ x =5,x ⊕ (y ⊕ z) = (x ⊕ y)+z-5 what is 2009 ⊕ 1949

There is an operation "⊕" that satisfies $x ⊕ x =5$ $x ⊕ (y ⊕ z) = (x ⊕ y)+z-5$ what is 2009 ⊕ 1949? I think it is feasible to solve with Mathematica, but my code doesn't give a useful ...
expression's user avatar
  • 5,652
7 votes
6 answers
776 views

How to replace an element in a list with an element in another list with matching indices?

I have a list like this l = {1 + 2*s, 2 + 3*s, 4 + 1*s}, and I have calculated s = {1, 2, 3}. I need to replace ...
BetterEnglish's user avatar
7 votes
4 answers
2k views

How to find all symbols in an expression and perform an operation on them?

========== motivation =========== Suppose, for example, I have an incoming expression like m = (3-I)x + 4(x y)/(Cos[y]) I want to find all the symbols in ...
Steve's user avatar
  • 1,153
7 votes
4 answers
286 views

Convert a list of lists into a list of rules

I have some trouble converting an imported list looking like this: {{a, b}, {c, d}, {e, f}} into a list of rules looking like this: ...
user3483676's user avatar
7 votes
2 answers
463 views

How can I get new random values with each blank in ReplaceAll?

Is there any way to get different values for different blanks with ReplaceAll[]? For example, I would like to do something like: ...
Jmeeks29ig's user avatar
  • 1,447
7 votes
2 answers
1k views

Pattern Matching for Rules [duplicate]

Consider a set of rules, e.g. {a -> aa, b -> bb, c -> cc, d -> dd, e -> ee} I want to remove from this list all patterns of the form e->_ and to ...
Adalbert Hanßen's user avatar
7 votes
2 answers
193 views

Probablity weighted ReplaceAll

I'm looking for an elegant way of doing a probability weighted replacement rule, something like this f[x_] := x /. {1,2,3,4} -> {50%{ 4,3,2,1} ,50%{1,3,2,4}} ...
William Grunow's user avatar
7 votes
2 answers
874 views

Replacement Rule for Functions

Attempting to write a rule that will substitute an undetermined function of several variables by some other undeterminate functions (also, of the same several variables). I can do it in an inelegant ...
user3755605's user avatar
7 votes
1 answer
434 views

Using two rules for the same variable

I am a newbie in the field so please not angry. I am reading the tutorial below and having a problem. Link: How to Create and Use Rules From the link: If you give two rules for the same ...
emnha's user avatar
  • 2,111
7 votes
2 answers
189 views

Is Replace doing double work?

In an answer to this question, rm -rf used the function Replace with level spefication to do a series of replacements inside out. As far as I can see, the order of substitutions when more than one ...
Fred Simons's user avatar
  • 10.2k
6 votes
6 answers
787 views

Convert a list of rules into 2 lists of integers

If I have a list of n number of rules, what is a concise way of breaking the data into 2 lists of the first and second part of the rule? This works, but seems like an unnecessary amount of code to ...
Schwarz's user avatar
  • 363
6 votes
5 answers
281 views

Add the values in the rule

{ a -> 1, a -> 2, b -> 3, b -> 4 } You can see that both a and b have two values, so how do I add these two vaule?Like this ...
我心永恒's user avatar
  • 1,572
6 votes
5 answers
726 views

Manipulating rules

I tried searching in the docs in "Transformation Rules and Definitions", however there wasn't anything of help. So imagine I have these two rules: ...
Confuse-ray30's user avatar
6 votes
4 answers
360 views

Long list of similar rules

Given the following: ...
MathLind's user avatar
  • 1,697
6 votes
2 answers
399 views

I'm not able to delete -> in {{x -> a}, {x -> b}, {x -> c, x -> d}}

Here is what I have done. Input DeleteCases[{{x -> a}, {x -> b}, {x -> c, x -> d}}, Rule, {2}] Output ...
kile's user avatar
  • 1,671
6 votes
4 answers
112 views

Applying transformation rule for corresponding indices?

I have two lists, that looks like below list1 = {a, x, b, x, x, c}; list2 = {2, 3, 4, 5, 6, 7}; Any time there is an x in the ...
user106860's user avatar
6 votes
4 answers
307 views

How to scope `Pattern` labels in rules/set?

Module[{x}, f@x_ = x; p@x_ := x; {x, x_, x_ -> x, x_ :> x} ] ?f ?p gives ...
masterxilo's user avatar
  • 5,739
6 votes
3 answers
265 views

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

Problem Suppose I have a differential equation such as $\qquad f'(x)+f(x)=0$ and I want to change it into $\qquad y'+y=0$ My Method Using rule = f[x] -> y, I ...
PureLine's user avatar
  • 1,320
6 votes
1 answer
757 views

Simplifying a b = 1 in non-commutative products?

Yesterday, I try to define some rules for non-communicative multiplication, here. I find some quite helpful rules, but things still do not work properly; the distributive rule can't be applied (e.g.: <...
van abel's user avatar
  • 1,235

1
2 3 4 5
8