MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Replace and ReplaceAll -- simple application

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105949] Re: [mg105922] Replace and ReplaceAll -- simple application
  • From: "David Park" <djmpark at comcast.net>
  • Date: Sun, 27 Dec 2009 02:26:38 -0500 (EST)
  • References: <26882784.1261873319266.JavaMail.root@n11>

expr = 1. (y1 - 1. yr);

Look at the FullForm.

expr // FullForm 
Times[1.`,Plus[y1,Times[-1.`,yr]]]

There is a 1. And a -1. and you must have a replacement for each of them.

expr /. {1. -> 1, -1. -> -1} 
y1 - yr 

expr /. a : (1. | -1.) -> Sign[a] 
y1 - yr 


Another possible construction is:

expr // Rationalize 
y1 - yr 

I don't know how you arrived at the initial expression, but if possible you
could try to enter only exact expressions (avoiding numbers with decimal
points, called approximate numbers). Enter approximate numbers only when you
are absolutely forced to. Try to keep all equations symbolic, free of
approximate numbers. Also keep them free of units. Then once you have solved
the equations (if you can do that) then substitute data values with
approximate numbers and units.


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  


From: amannuc1 [mailto:Anthony.J.Mannucci at jpl.nasa.gov] 

I am doing some manipulations in Mathematica and end up with
constructs such as "1." in the expressions. I want to convert these to
"1" and thereby eliminate them. However, these examples don't seem to
work. This is Mathematica 7 on OS X.

In[40]:=
Replace[1.(y1-1.yr),1.->1]
Out[40]= 1. (y1-1. yr) (* Nothing happens *)

In[39]:=
ReplaceAll[1.(y1-1.yr),1.->1]
Out[39]= y1-1. yr (* Only first one is replaced *)

Any help is appreciated. Thanks.

-Tony




  • Prev by Date: Re: problem with EvenQ
  • Next by Date: Re: Figure quality
  • Previous by thread: Re: Replace and ReplaceAll -- simple application
  • Next by thread: Re: Replace and ReplaceAll -- simple application