3
$\begingroup$

An undesired shape of a matrix has the following form:

enter image description here

which is created by

mymatrix = {{(1.0000000000000002` + 0.` I) E^(-0.683013`/
   t) + (0.9999999999999994` + 0.` I) E^(-0.18301299999999998`/
   t), (2.0816681711721685`*^-16 + 0.5773502691896256` I) E^(
 0.18301299999999998`/
  t)}, {(0.5773502691896257` + 
   9.817628307322482`*^-16 I) E^(-0.18301299999999998`/
  t), (3.95516952522712`*^-16 + 
   0.5773502691896254` I) E^(-0.18301299999999998`/t)}};

From my last post sametest I used Round in the construction of the matrix but there are some unimportant digits such as ...99999999998 in powers or .....**^-16 (real or imaginary parts).

How can I get rid of these unimportant terms and obtain a matrix as the following?

enter image description here

I know I can use Chop but it doesn't work correctly! As a matter of fact, the result after chopping is:

enter image description here

As it is obvious there are other unimportant terms such as 0.; also in powers there are unimportant digits which are clear in being copied.

$\endgroup$
2
  • 3
    $\begingroup$ This phenomenon is mentioned in the documentation for Chop under "Possible issues". It says that machine complex numbers always have machine real values for both the real part and the imaginary part, therefore the zero cannot be removed. $\endgroup$
    – C. E.
    Commented Jan 8, 2016 at 15:37
  • 2
    $\begingroup$ Why do you want to remove them? For display purposes, or for further processing? If the latter, it is definitely better not to do it since after that you will be working against Mathematica all the way. The same issue was raised in this question. $\endgroup$ Commented Jan 8, 2016 at 23:11

2 Answers 2

4
$\begingroup$

You can eliminate the two 1. but, as Pickett already commented, you cannot easily change the complex number 0. + 0.57735 I to 0.57735 I

(res = Chop[mymatrix] /. Times[a_, b_] /; Round[a, 10^-12] == 1. :> b) // MatrixForm

enter image description here

What you could do now (for display purposes) is

res /. Complex[a_, b_] /; a < 10^-12 :> HoldForm[b I] // MatrixForm

enter image description here

and return to "normal" form with

% // ReleaseHold;
$\endgroup$
6
$\begingroup$

Assuming that this is required only for formatting purposes, as it was in this seemingly identical question (I cannot decide which of these should be closed as the duplicate of the other, and would be grateful if someone could help me do so), then I would like to provide another approach in addition to what has been proposed so far. It seems to me that the other answers suffer variously from the following problems:

  • Modifying core system symbols (Complex)

  • Changing the structure of the expression and not only its presentation

  • Adding wrappers that make the formatted expression unusable in further computations without their prior removal

  • Requiring application to individual expressions rather than consistently changing how these numbers are displayed within a session

  • Retaining the parentheses that appear around e.g. (0. + 1. I)*2, but which are superfluous when this is changed to (1. I)*2

  • Not correctly observing the Orderless property of e.g. Plus and Times

This is not meant to denigrate the other answers, but I think we can do much better in just a few lines:

(* format rule for Complex[0., im_] as im I *)
MakeBoxes[Complex[0., im_], form_] := MakeBoxes[im I, form];

(* prevent now-unnecessary parentheses when this is part of another expression*)
MakeBoxes[h_[pre___, z : Complex[0., _], post___], form_] :=
 Module[{placeholder},
  MakeBoxes[h[pre, placeholder, post], form] /.
   MakeBoxes[placeholder, form] -> MakeBoxes[z, form]
 ];

Tests:

0. + I    (* -> 1. I *)
(0. + I) x    (* -> 1. I x *)
(1. + 1. I) x (* -> (1. + 1. I) x *)
(0. + I) x Exp[(0. + 2. I) y-(0. + 3. I) z]    (* -> 1. I E^(2. I y-3. I z) x *)
a = (0. + I) x; 7 a    (* -> 7. I x *)
((0. + I) + (1. + 0. I ) x) Exp[y] (* -> E^y (1. I + (1. + 0. I) x) *)
mymatrix (*from the question*) // Chop // MatrixForm

picture of MatrixForm output

mymatrix // Chop // Det
(* -> -0.333333 I + 0.57735 I E^(-0.866026/t) + 0.57735 I E^(-0.366026/t) *)

It seems to work reasonably well. I can't think of any possible issues with it, but would be happy for someone to point them out, if they exist.

$\endgroup$

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.