Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This is maybe a silly question, but I could not think of any logical reason why it is not simplified automatically.

I was working with a big linear equation system with some constrained inequalities and noticed a lot of "1. xyz" terms.

I tried Simplify[1. x == x] which gives True, but 1. x stays 1. x. I'm using the simple rule Rule[1., 1] to simplify the "1. xyz" terms in my equation system; it just looks somehow "better" (in my opinion).

Function Times was last modified in version 3, so this behaviour should exist for a long time now.

What is the logical reason behind?

share|improve this question
4  
This has been asked more than once before but I can't find it. In short, 1. is an inexact number, i.e. not 1, just an approximation of 1 with an associated precision. 1. x won't simplify to x==1*x because it is only approximately equal to it. In most cases it's better not to use inexact numbers for symbolic calculations. –  Szabolcs Jun 13 at 13:53
1  
You can Round or Rationalize the result to an appropriate precision to eliminate 1. or 0. inexact coefficients. –  Bob Hanlon Jun 13 at 14:08

3 Answers 3

up vote 1 down vote accepted

A generalization of your rule approach:

  {1., 2.5, 5, 1.0001, 2.9999999999999999} /. {x_?MachineNumberQ /; 
                                              Chop[Abs[Round[x] - x]] == 0 :> Round[x]}

{1, 2.5, 5, 1.0001, 3}

Note Chop[] is not strictly necessary but serves as a reminder we are doing a floating point approximate comparison (and you can specify a non-default tolerance if you want )

share|improve this answer

The comments already point out that for a general symbol like x it wouldn't be justified to replace 1. x by x. The reason is that x is unknown and therefore generically could be an exact number for which multiplication by an inexact number (1.) would violate the equality.

So you should tell Mathematica that your variable x is restricted to values for which it's OK to multiply by 1. without worrying about potential change from exact to inexact. Here is one way to do this:

x /: 1. x := x

This says the same thing as you expressed in your rule, but it associates this rule only with the variable x so that the replacement is not inadvertently done somewhere else in a big expression where it may not be allowed.

Here is a test:

1. x

(* ==> x *)
share|improve this answer

Thanks for your comments; 1. == 1 is True, but 1. === 1 (SameQ) is False. I will stay with my rule, because Rationalize will modify my whole equation system and I would like to keep it at machine precission.

share|improve this answer
1  
After Rationalize has handled all of the 1. and 0. coefficients, you can use N to return the remaining coefficients to machine precision. –  Bob Hanlon Jun 13 at 15:39
1  
This answer should be a comment below your own question. –  Jens Jun 13 at 18:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.