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


Groups > comp.soft-sys.math.mathematica > #6545

Re: Pattern replacement

From Bob Hanlon <hanlonr357@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Pattern replacement
Date 2011-11-19 11:57 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <ja85jb$imn$1@smc.vnet.net> (permalink)
References <201111181250.HAA07952@smc.vnet.net>

Show all headers | View raw


(data = Table[-1/n + u, {n, 5}])

{-1 + u, -(1/2) + u, -(1/3) + u, -(1/4) + u, -(1/5) + u}

data /. Rational[-1, q_] + x_ -> q*x - 1

{-1 + u, -1 + 2 u, -1 + 3 u, -1 + 4 u, -1 + 5 u}

data /. k_ + x_ -> -x/k - 1

{-1 + u, -1 + 2 u, -1 + 3 u, -1 + 4 u, -1 + 5 u}

data /. k_?(Element[1/#, Integers] &) + x_ -> -x/k - 1

{-1 + u, -1 + 2 u, -1 + 3 u, -1 + 4 u, -1 + 5 u}


Bob Hanlon


On Fri, Nov 18, 2011 at 7:50 AM, Dr. Wolfgang Hintze <weh@snafu.de> wrote:
> My questions are rather elementary.
>
> I wish to replace expressions of the form
>
> -1/2+u by 2u-1
> -1/3+u by 3u-1
>
> and so on.
>
> Trying the replacement
>
> (1)    (-1/2+u)/.(-1/q_ + u) -> (q x-1)
>
> does not work, i.e. leaves the original expression unchanged.
>
> Using FullForm[] to reveal the structure gives
>
> (2)    FullForm[Rational[-1, 2] + u]
>
> Now the replacement in explicit form works:
>
> (3)    (-1/2+u)/.(Rational[-1, 2] + u) -> (q u -1)
>        Out: -1+q*u
>
> But this is not a nice way to write it.
>
> My questions are
>
> 1) Are there better ways to implement the replacement?
> 2) How do I "undo" FullForm[], i.e. which command produces -1/2+u from
> Rational[-1,2] + u ?
>   (I can do it by ToExpression["Rational[-1,2] + u"] but maybe there
> is another way avoiding going to strings).
>
> Thanks in advance
> Wolfgang
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Pattern replacement Bob Hanlon <hanlonr357@gmail.com> - 2011-11-19 11:57 +0000

csiph-web