5
$\begingroup$

For more info about the difference between 0. and 0

I am a self-taught Mathematica user and I have some elementary questions-- perhaps too elementary for this site, but here they are.

Matrix

  1. If you look at the identity matrix, you'll see a "dot" in each entry in the upper triangular portion of the matrix. Why are they there? Is it because those entries are not really zeros?

  2. How do I get the entries in MatrixForm[Inverse[M]] to appear as fractions?

Thanks everyone!

So I just found this link: Expressing a decimal as a fraction in lowest terms

and tried the following:

Matrix2

Isn't there a way to tell Mathematica to not show entries like -9.71445146547012`*^-17 (up to 17th decimal place)?

$\endgroup$
8
  • 2
    $\begingroup$ Do not use decimals like 0.5 in your initial Matrix. Do integers or fractions only - like 1/2. $\endgroup$ Commented Jun 26, 2012 at 7:05
  • 1
    $\begingroup$ Thank you! I just tried that, and it works perfectly! $\endgroup$ Commented Jun 26, 2012 at 7:06
  • 1
    $\begingroup$ It's better to put your code as text, so that others (like me) can easily copy it and play around. $\endgroup$
    – Ajasja
    Commented Jun 26, 2012 at 7:30
  • 2
    $\begingroup$ @math-visitor If you copy from mma then make sure you put 4 spaces of indentation (so it will be syntax highlighted). Also there is a much more convinient way to copy code to this site: meta.mathematica.stackexchange.com/questions/151/… $\endgroup$
    – Ajasja
    Commented Jun 26, 2012 at 8:09
  • 2
    $\begingroup$ @Mr.Wizard Definitely VERY related:) Although this one is just concerned with the display, while the other is in essence the question about why 0.===0 is False. So they approach the same problem from different directions. $\endgroup$
    – Ajasja
    Commented Jun 26, 2012 at 8:12

2 Answers 2

6
$\begingroup$

The dot after the number means it is a floating point number and not an integer. Compare for example

Head[0]
(*Integer*)

and

Head[0.]
(*Real*)

See also: The difference between 0. and 0

If you want to get rid of small values use Chop:

Chop[{.5, -1.8*10^-17}]
(* {0.5, 0} *)
$\endgroup$
7
$\begingroup$

Although there is an internal difference between 0 and 0., that doesn't mean that you have to display the floating point zero as 0. every time. You also may not always want to use Rationalize because in a matrix it can create an unbalanced, "non-uniform" appearance.

Instead, you can customize the format in which floating point numbers are displayed by using a definition like this:

trimPoint = 
 Sequence[NumberFormat -> (DisplayForm@
      RowBox[Join[{StringTrim[#1, RegularExpression["\\.$"]]}, 
        If[#3 != "", {"\[Times]", SuperscriptBox[#2, #3]}, {}]]] &)]

This is meant to be used as an optional argument to the formatting functions NumberForm, ScientificForm, EngineeringForm and AccountingForm.

I posted something like this as part of another answer but not in a way that's easily used on matrices.

Example

Define a random matrix m with one floating-point zero element:

m = RandomReal[{0, 1}, {5, 5}]; m[[1, 1]] = 0.;
m // MatrixForm

$ \left( \begin{array}{ccccc} 0. & 0.0712901 & 0.00252376 & 0.930725 & 0.0120859 \\ 0.504716 & 0.537549 & 0.715322 & 0.351001 & 0.363718 \\ 0.905092 & 0.0874893 & 0.624776 & 0.546527 & 0.542502 \\ 0.0776307 & 0.360377 & 0.388662 & 0.406045 & 0.441652 \\ 0.947527 & 0.270716 & 0.0121501 & 0.744597 & 0.830463 \\ \end{array} \right) $

Here we have the 0. appearing in the top left entry, but maybe that's not desired. To remove the decimal point where it isn't followed by any nonzero digits, you can now do this:

NumberForm[m // MatrixForm, trimPoint]

$ \left( \begin{array}{ccccc} 0 & 0.0712901 & 0.00252376 & 0.930725 & 0.0120859 \\ 0.504716 & 0.537549 & 0.715322 & 0.351001 & 0.363718 \\ 0.905092 & 0.0874893 & 0.624776 & 0.546527 & 0.542502 \\ 0.0776307 & 0.360377 & 0.388662 & 0.406045 & 0.441652 \\ 0.947527 & 0.270716 & 0.0121501 & 0.744597 & 0.830463 \\ \end{array} \right) $

Notice that the decimal point following the zero is gone, but only in the displayed form (it's still a floating point number internally).

The trimPoint option can also be used with the other output formats:

EngineeringForm[m // MatrixForm, trimPoint]

etc.

Of course you can also use a second argument to these function to restrict the total number of digits. For example, try

NumberForm[m // MatrixForm, 3, trimPoint]

Finally, if you have numbers like -9.71445146547012*^-17 and want to use Chop to get rid of them, you can combine that with the above display format by doing something like

NumberForm[m // Chop // MatrixForm, 3, trimPoint]
$\endgroup$
1
  • $\begingroup$ Thank you Jens! This is a lot of information but I will definitely give all of them a try. =o) $\endgroup$ Commented Jun 27, 2012 at 2:45

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.