1

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?

32u-nd
  • 963
  • 4
  • 10
  • 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 '14 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 '14 at 14:08

3 Answers3

4

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 *)
Jens
  • 97,245
  • 7
  • 213
  • 499
1

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 )

george2079
  • 38,913
  • 1
  • 43
  • 110
0

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.

32u-nd
  • 963
  • 4
  • 10
  • 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 '14 at 15:39
  • 1
    This answer should be a comment below your own question. – Jens Jun 13 '14 at 18:34