Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9100 > unrolled thread

replace one rule in a list of rules

Started byKathryn Isaac <kathryn.isaac@gmail.com>
First post2012-03-17 07:54 +0000
Last post2012-03-20 07:24 +0000
Articles 7 — 7 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  replace one rule in a list of rules Kathryn Isaac <kathryn.isaac@gmail.com> - 2012-03-17 07:54 +0000
    Re: replace one rule in a list of rules Ray Koopman <koopman@sfu.ca> - 2012-03-18 07:50 +0000
      Re: replace one rule in a list of rules nanobio9 <kuokan.liang@gmail.com> - 2012-03-21 10:46 +0000
    Re: replace one rule in a list of rules Murta <rodrigomurtax@gmail.com> - 2012-03-18 07:51 +0000
    Re: replace one rule in a list of rules Peter Pein <petsie@dordos.net> - 2012-03-18 07:56 +0000
      Re: replace one rule in a list of rules Alan <alan.isaac@gmail.com> - 2012-03-19 10:00 +0000
      Re: replace one rule in a list of rules Christoph Lhotka <christoph.lhotka@fundp.ac.be> - 2012-03-20 07:24 +0000

#9100 — replace one rule in a list of rules

FromKathryn Isaac <kathryn.isaac@gmail.com>
Date2012-03-17 07:54 +0000
Subjectreplace one rule in a list of rules
Message-ID<jk1fv4$515$1@smc.vnet.net>
I have a list of simple rules. I want to repeatedly replace one of
them.  Is the best way to simply prepend the new rules, or is there a
"prettier" approach?

Thanks,
Alan Isaac

[toc] | [next] | [standalone]


#9120

FromRay Koopman <koopman@sfu.ca>
Date2012-03-18 07:50 +0000
Message-ID<jk4443$e1f$1@smc.vnet.net>
In reply to#9100
On Mar 17, 12:54 am, Kathryn Isaac <kathryn.is...@gmail.com> wrote:
> I have a list of simple rules. I want to repeatedly replace one of
> them.  Is the best way to simply prepend the new rules, or is there
> a "prettier" approach?
>
> Thanks,
> Alan Isaac

If all you want to change is the right-hand side of the rule
then use RuleDelayed with a dummy right-hand-side variable:

rlist = {a -> a1, b :> bx};
bx = b1; {a,b}/.rlist
bx = b2; {a,b}/.rlist

{a1,b1}
{a1,b2}

[toc] | [prev] | [next] | [standalone]


#9182

Fromnanobio9 <kuokan.liang@gmail.com>
Date2012-03-21 10:46 +0000
Message-ID<jkcbhk$qet$1@smc.vnet.net>
In reply to#9120
I also believe that Ray's solution with SetDelayed is the most general
and elegant way out. Once you delayed the set rule, you can implement
the rule in anyway, anytime later, to meet your specific need. You
will need to learn more about SetDelay and do exercises by yourself to
master it, though.

Best,
Kuo Kan Liang

> On Mar 17, 12:54 am, Kathryn Isaac <kathryn.is...@gmail.com> wrote:
>
> > I have a list of simple rules. I want to repeatedly replace one of
> > them.  Is the best way to simply prepend the new rules, or is there
> > a "prettier" approach?
>
> > Thanks,
> > Alan Isaac
>
> If all you want to change is the right-hand side of the rule
> then use RuleDelayed with a dummy right-hand-side variable:
>
> rlist = {a -> a1, b :> bx};
> bx = b1; {a,b}/.rlist
> bx = b2; {a,b}/.rlist
>
> {a1,b1}
> {a1,b2}

[toc] | [prev] | [next] | [standalone]


#9122

FromMurta <rodrigomurtax@gmail.com>
Date2012-03-18 07:51 +0000
Message-ID<jk4460$e1p$1@smc.vnet.net>
In reply to#9100
Can you sent an toy code exemple?
I didn't get your point.
tks
Murta
On Mar 17, 4:54 am, Kathryn Isaac <kathryn.is...@gmail.com> wrote:
> I have a list of simple rules. I want to repeatedly replace one of
> them.  Is the best way to simply prepend the new rules, or is there a
> "prettier" approach?
>
> Thanks,
> Alan Isaac

[toc] | [prev] | [next] | [standalone]


#9132

FromPeter Pein <petsie@dordos.net>
Date2012-03-18 07:56 +0000
Message-ID<jk44fj$e40$1@smc.vnet.net>
In reply to#9100
Am 17.03.2012 08:54, schrieb Kathryn Isaac:
> I have a list of simple rules. I want to repeatedly replace one of
> them.  Is the best way to simply prepend the new rules, or is there a
> "prettier" approach?
>
> Thanks,
> Alan Isaac
>

You can apply pattern matching to rules:

In[12]:= rules = Thread[{a,b,c} -> {x,y,z}]
Out[12]= {a -> x, b -> y, c -> z}
In[13]:= newrules = rules /. HoldPattern[b -> _] :> b -> newy
Out[13]= {a -> x, b -> newy, c -> z}

[toc] | [prev] | [next] | [standalone]


#9146

FromAlan <alan.isaac@gmail.com>
Date2012-03-19 10:00 +0000
Message-ID<jk704a$107$1@smc.vnet.net>
In reply to#9132
Using Murray's approach I end up with (for example):

Clear[a, b, c]
rules = Thread[{a, b, c} -> {0.1, 0.1, 0.1}]
Table[rules /. ((b -> _) -> (b -> i/10.)), {i, 0, 10}]

Using Peter's approach I end up with (for example):

Clear[a, b, c]
rules = Thread[{a, b, c} -> {0.1, 0.1, 0.1}]
Table[(rules /. HoldPattern[b -> _] :> b -> i/10.), {i, 0, 10}]

Both appear to meet my needs. I'm not sure what the real
difference between the two is.

Thanks,
Alan


Thanks!
Alan

[toc] | [prev] | [next] | [standalone]


#9168

FromChristoph Lhotka <christoph.lhotka@fundp.ac.be>
Date2012-03-20 07:24 +0000
Message-ID<jk9bbg$bnd$1@smc.vnet.net>
In reply to#9132
Dear Alan,

if your simple examples below are what you need I would avoid to replace 
rules
in a list but rather define the rules (which are needed to be changed) using
parameters or patterns:


Clear[a, b, c]
Table[{a -> 0.1, b -> i/10., c -> 0.1}, {i, 0, 10}]

For example if the rules are controlled by an external parameter,
you could define a rule depending on i as:

rul[i_]:={a -> 0.1, b -> i/10., c -> 0.1}

etc...

and use it in the following way in your expressions:


Table[{a,b,c}/.rul[i],{i,0,10}]


If the rule depends on the lhs of the above (the rule depends
on the expression it will be used with) I would use patterns
instead:

Clear[rul]
rul={a->0.1,b:>b/10, c->0.1}

{a,b,c}/. rul

Hope that helps,

Best,

Christoph






On 03/19/2012 10:57 AM, Alan wrote:
> Using Murray's approach I end up with (for example):
>
> Clear[a, b, c]
> rules = Thread[{a, b, c} ->  {0.1, 0.1, 0.1}]
> Table[rules /. ((b ->  _) ->  (b ->  i/10.)), {i, 0, 10}]
>
> Using Peter's approach I end up with (for example):
>
> Clear[a, b, c]
> rules = Thread[{a, b, c} ->  {0.1, 0.1, 0.1}]
> Table[(rules /. HoldPattern[b ->  _] :>  b ->  i/10.), {i, 0, 10}]
>
> Both appear to meet my needs. I'm not sure what the real
> difference between the two is.
>
> Thanks,
> Alan
>
>
> Thanks!
> Alan
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web