2
$\begingroup$

This question already has an answer here:

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$

marked as duplicate by m_goldberg, MarcoB, Feyre, happy fish, corey979 Nov 24 '16 at 21:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 Nov 24 '16 at 12:13
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$
  • $\begingroup$ Thanks! Is there any function similar to NumberForm which does change the number to a string (not displays the expression only)? $\endgroup$ – Eden Harder Nov 24 '16 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 Nov 24 '16 at 12:42
  • $\begingroup$ Isn't StandardForm an exception? $\endgroup$ – QuantumDot Nov 25 '16 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 Nov 25 '16 at 7:47

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