2
$\begingroup$

If I calculate the dimension of the matrix after NumberForm directly, then there will be something wrong

test = NumberForm[IdentityMatrix[2], 2];
test // Dimensions
(*output {2}, but {2,2} is expected*)

If I show the result of NumberForm and then calculate the dimension, the result is right

{{"1","0"},{"0","1"}} //Dimensions
(*ouput {2,2}*)

What causes this? Is it a bug?

$\endgroup$
1
  • 2
    $\begingroup$ Please do not add the bugs tag to your own questions. This is a special tag that is meant to be added by someone else than the original poster, after the bug has been verified by the community. $\endgroup$
    – Szabolcs
    Commented Nov 24, 2016 at 12:13

1 Answer 1

5
$\begingroup$

This is not a bug:

Head[test]
(* NumberForm *)

Generally all *Form function, including NumberForm, MatrixForm, etc. are wrappers that are meant to display the expression only. In fact the output indicates the presence of the wrapper:

enter image description here

Once the expression is wrapped, it is not suitable for calculations. Or you need to extract it to do calculations.

MatrixQ[test]
(* False *)

MatrixQ[First[test]]
(* True *)

Dimensions[First[test]]
(* {2, 2} *)

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks! Is there any function similar to NumberForm which does change the number to a string (not displays the expression only)? $\endgroup$ Commented Nov 24, 2016 at 12:33
  • $\begingroup$ @EdenHarder ToString. But you must apply it to numbers individually, not to a list of numbers. You can also use ToString@NumberForm[number]. $\endgroup$
    – Szabolcs
    Commented Nov 24, 2016 at 12:42
  • $\begingroup$ Isn't StandardForm an exception? $\endgroup$
    – QuantumDot
    Commented Nov 25, 2016 at 1:52
  • $\begingroup$ @QuantumDot No. The confusing bit is that SomeForm[x] then % gives x and not SomeForm[x]. Out treats these forms specially and doesn't record them. See $OutputForms. $\endgroup$
    – Szabolcs
    Commented Nov 25, 2016 at 7:47

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