2

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Eden Harder
  • 1,145
  • 6
  • 22
  • 2
    Please do not add the [tag: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. – Szabolcs Nov 24 '16 at 12:13

1 Answers1

5

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

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Thanks! Is there any function similar to NumberForm which does change the number to a string (not displays the expression only)? – Eden Harder Nov 24 '16 at 12:33
  • @EdenHarder ToString. But you must apply it to numbers individually, not to a list of numbers. You can also use ToString@NumberForm[number]. – Szabolcs Nov 24 '16 at 12:42
  • Isn't StandardForm an exception? – QuantumDot Nov 25 '16 at 01:52
  • @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. – Szabolcs Nov 25 '16 at 07:47