4
$\begingroup$

I'm getting a huge output, but it appears that almost every term should actually be zero, unless I'm misunderstanding the expression.

For example, part of the output reads

a (0.25 + 0. b) + b^2 (0. + 0./c) + b (-1. + 0. c) - 0.5 c + (b (-1. b - 0.5 c) c)/a^2

Simplify and FullSimplify won't get rid of these zero terms even with assumptions that a,b and c are real and non-zero. Suggestions ?

$\endgroup$
5
  • 1
    $\begingroup$ expr /. {x_Real /; x==0 -> 0} $\endgroup$
    – ssch
    Commented Oct 28, 2013 at 17:32
  • 6
    $\begingroup$ Examine the FullForm of the result. You may be surprised. Chop is useful too. $\endgroup$ Commented Oct 28, 2013 at 17:33
  • $\begingroup$ SetOptions[Simplify, TransformationFunctions :> Function[x, x //. {0. -> 0, 1. y_ :> y, -1. y_ :> y}]] and then Simplify simplifies as you want. $\endgroup$ Commented Oct 28, 2013 at 18:29
  • $\begingroup$ @RolfMertig. I think you meant to write -1. y_ :> -y $\endgroup$
    – RunnyKine
    Commented Oct 28, 2013 at 20:15
  • $\begingroup$ @RunnyKine : sure. thanx. $\endgroup$ Commented Oct 28, 2013 at 22:27

1 Answer 1

4
$\begingroup$

To get what was said in the comments on record as an answer.

Chop is probably what you are looking for.

expr = 
  a (0.25 + 0. b) + b^2 (0. + 0./c) + b (-1. + 0. c) - 0.5 c + 
   (b (-1. b - 0.5 c) c)/a^2 // Chop
0.25 a - 1. b - 0.5 c + (b (-1. b - 0.5 c) c)/a^2

You might want to go further

expr = expr /. {1. x_ :> x, -1. x_ :> -x}
0.25 a - b - 0.5 c + (b (- b - 0.5 c) c)/a^2

Update

In general expressions with inexact coefficients can contain the forms 0.x, 1.x, and -1.x. None of these cause computation difficulties, but they are unattractive, and often we want to eliminate them.

With a help from Mr.Wizard, I offer the following pattern to do this task.

fixerPattern = u_ /; Sign[u] == u :> Sign[u] 

With this we can clean up

expr = 
  a (0.25 + 0. b) + b^2 (0. + 0./c) + 1.b (-1. + 0. c) - 0.5 c + 
    (b (-1. b - 0.5 c) c)/a^2

without using Chop and it takes care of the instances of both 1.b and -1.b.

expr /. fixerPattern

fixup

$\endgroup$
9
  • 1
    $\begingroup$ I think you'll want something like /. {c_ x_ /; c == 1 :> x, c_ x_ /; c == -1 :> -x} for generality? (As you used in a more recent answer...) $\endgroup$
    – Mr.Wizard
    Commented Jun 8, 2017 at 2:17
  • $\begingroup$ @Mr.Wizard. What do you think of /. c_ x_ /; Abs[c ] == 1 :> Sign[c] x as a more concise alternative? $\endgroup$
    – m_goldberg
    Commented Jun 8, 2017 at 6:16
  • 1
    $\begingroup$ Might not that affect complex values in a strange way? A contrived example: (0.5` + 0.8660254037844386` I) /. c_ /; Abs[c] == 1 :> foo $\endgroup$
    – Mr.Wizard
    Commented Jun 8, 2017 at 6:47
  • $\begingroup$ @Mr.Wizard. Good point, What about this? /. c_Real x_ /; Abs[c] == 1 :> Sign[c] x $\endgroup$
    – m_goldberg
    Commented Jun 8, 2017 at 6:51
  • 2
    $\begingroup$ I believe we could handle all three cases (including zero) with one rule: x_ /; Sign[x] == x :> Sign[x] $\endgroup$
    – Mr.Wizard
    Commented Jun 8, 2017 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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