Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14958 > unrolled thread
| Started by | Sseziwa Mukasa <mukasa@gmail.com> |
|---|---|
| First post | 2013-06-21 09:45 +0000 |
| Last post | 2013-06-23 00:41 +0000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: How to remove the "0." from "0. + 1.41774i" Sseziwa Mukasa <mukasa@gmail.com> - 2013-06-21 09:45 +0000
Re: How to remove the "0." from "0. + 1.41774i" Richard Fateman <fateman@cs.berkeley.edu> - 2013-06-22 07:29 +0000
Re: How to remove the "0." from "0. + 1.41774i" Christoph Lhotka <christoph.lhotka@univie.ac.at> - 2013-06-23 00:40 +0000
Re: How to remove the "0." from "0. + 1.41774i" Richard Fateman <fateman@eecs.berkeley.edu> - 2013-06-23 00:40 +0000
Re: How to remove the "0." from "0. + 1.41774i" Christoph Lhotka <christoph.lhotka@univie.ac.at> - 2013-06-23 00:41 +0000
| From | Sseziwa Mukasa <mukasa@gmail.com> |
|---|---|
| Date | 2013-06-21 09:45 +0000 |
| Subject | Re: How to remove the "0." from "0. + 1.41774i" |
| Message-ID | <kq17bv$goc$1@smc.vnet.net> |
It's a precision issue, the documentation on rationalize says:
Rationalize[x] yields x unchanged if there is no rational number close enough to x to satisfy the condition =E2=88=AA p/q - x =E2=88=AA < c/q^2, with c chosen to be 10^-4.
Rationalize[x, 0] converts any x to rational form.
So:
(Debug) In[17]:= nuChop[t_] := Rationalize[Chop[t], 0];
A = {{-1.2, 1.5}, {-2.3, 1.2}};
eigval1 = Eigenvalues[A][[1]]
nuChop[eigval1]
(Debug) Out[19]= 2.77556*10^-17 + 1.41774 I
(Debug) Out[20]= (116833629 I)/82408088
But I don't find (116833629 I)/82408088 more visually useful than 0. + 1.41774 I.
However here's another approach:
(Debug) In[32]:= suppressMachinePrecisionZeros[Complex[a_, b_]] :=
SetPrecision[Chop[a], $MachinePrecision] +
SetPrecision[Chop[b], $MachinePrecision] I
(Debug) In[33]:= suppressMachinePrecisionZeros /@ Eigenvalues[A]
(Debug) Out[33]= {1.417744687875782 I, -1.417744687875782 I}
On Jun 20, 2013, at 8:15 AM, Mike Bayville <mike.bayville@gmail.com> wrote:
> Thanks, but Rationalize works in the manner you executed it; however, it does not work like this...
>
> In[216]:= nuChop[t_] = Rationalize[t];
> A = {{-1.2, 1.5}, {-2.3, 1.2}};
> eigval1 = Eigenvalues[A][[1]]
> nuChop[eigval1]
>
> Out[218]= 2.7755576*10^-17 + 1.4177447 I
>
> Out[219]= 2.7755576*10^-17 + 1.4177447 I
>
>
> On Thu, Jun 20, 2013 at 7:54 AM, Sseziwa Mukasa <mukasa@gmail.com> wrote:
> (Debug) In[3]:= Rationalize[0. + 1.41774 I]
> (Debug) Out[3]= (70887 I)/50000
>
> That has it's own problems with respect to large values in the numerator or denominator.
>
> On Jun 20, 2013, at 4:44 AM, Mike Bayville <mike.bayville@gmail.com> wrote:
>
> > Can anyone suggest a built-in function for removing the "0." from "0. + 1.41774i"?
> >
> > Thanks!
> >
> > Mike
> >
> > INPUT
> > A={{-1.2, 1.5}, {-2.3, 1.2}};
> > Clear[eigenvalue];
> > {eigenvalue[1], eigenvalue[2]} = Chop[Eigenvalues[A]]
> > Chop[eigenvalue[1]]
> > Chop[eigenvalue[2]]
> >
> > OUTPUT
> > {0. + 1.41774i, 0. - 1.41774i}
> > 0. + 1.41774i
> > 0. - 1.41774i
> >
>
>
[toc] | [next] | [standalone]
| From | Richard Fateman <fateman@cs.berkeley.edu> |
|---|---|
| Date | 2013-06-22 07:29 +0000 |
| Message-ID | <kq3joh$ner$1@smc.vnet.net> |
| In reply to | #14958 |
1. Mathematica 7.0 performs differently. 4.0*I looks like 4.I Mathematica 9. does this.. 4.0*I looks like 0.+4.I 2. If you want to do something different, consider something like this: f[x_Complex]: If [VerySmall[Re[x]], Im[x]*i, Re[x]+Im[x]*i] where VerySmall is defined as you wish. Could be VerySmall(x_]:=x==0.0 for example. Note that the answers are no longer complex numbers because they involve the symbol i not I. You can do %/. i->I but then you will see 0.+4.I again. RJF
[toc] | [prev] | [next] | [standalone]
| From | Christoph Lhotka <christoph.lhotka@univie.ac.at> |
|---|---|
| Date | 2013-06-23 00:40 +0000 |
| Message-ID | <kq5g5k$skp$1@smc.vnet.net> |
| In reply to | #14958 |
Dear mathgroup,
there is indeed a very elegant solution to these kind of problems. It took me some while (looking up the documentation center) but I think this works fine:
Step 1: define your notation (here on the output format of complex numbers):
In[1]:= format[c_Complex] :=
Interpretation[
Which[
Re[c] == 0 && Im[c] == 0, "0",
Re[c] == 0 && Im[c] != 0, ToString[Im[c] // N] <> "\[ImaginaryI]",
Re[c] != 0 && Im[c] == 0, ToString[Re[c] // N],
True, ToString[c // N, StandardForm]], c]
Step 2: Tell Mathematica to apply this functions to any output expression
In[2]:= $Post = # /. c_Complex :> format[c] &
Out[2]= #1 /. c_Complex :> format[c] &
Step 3: Work as usual, you will always see the format you defined in
Step 1....
In[4]:= x = {0. + I, 0 + 1. I, 1 + 0. I, 1. + 2. I}
Out[4]= {1.I,1.I,1.,1. +2. I}
...but you will always be able to continue your calculations independent
of the format you
may define:
In[5]:= Total[x]
Out[5]= Complex[2., 4.]
Step 4: clear $Post to return to normal Mathematica behaviour:
In[6]:= Clear[$Post]
Hope, that helps,
Christoph
PS: Here is a nice joke you can implement in your friends Mathematica
version:
In[1]:= format[c_Complex]:=
Interpretation[
Im[c]+Re[c]I,c]
In[2]:= $Post=#/.c_Complex:>format[c]&
Out[2]= #1/. c_Complex:>format[c]&
In[3]:= x={0.+I,0+1.I,1+0.I,1.+2.I}
Out[3]= {1. +0. I,1. +0. I,0. +1. I,2. +1. I}
In[4]:= y=Total[x]
Out[4]= 4. +2. I
Do not forget to release him! With
In[5]:= Clear[$Post]
In[6]:= x
Out[6]= {0. +1. I,0. +1. I,1. +0. I,1. +2. I}
In[7]:= y
Out[7]= 2. +4. I
...everything turns out to be fine ;o)
On 06/22/2013 09:33 AM, Richard Fateman wrote:
> 1. Mathematica 7.0 performs differently.
>
> 4.0*I looks like 4.I
>
> Mathematica 9. does this..
> 4.0*I looks like
> 0.+4.I
>
> 2. If you want to do something different, consider something
> like this:
>
> f[x_Complex]: If [VerySmall[Re[x]], Im[x]*i, Re[x]+Im[x]*i]
>
> where VerySmall is defined as you wish. Could be VerySmall(x_]:=x==0.0
> for example.
>
> Note that the answers are no longer complex numbers because they involve
> the symbol i not I.
>
> You can do %/. i->I
> but then you will see 0.+4.I again.
>
> RJF
>
>
>
>
>
[toc] | [prev] | [next] | [standalone]
| From | Richard Fateman <fateman@eecs.berkeley.edu> |
|---|---|
| Date | 2013-06-23 00:40 +0000 |
| Message-ID | <kq5g69$skr$1@smc.vnet.net> |
| In reply to | #14958 |
On 6/22/2013 8:40 AM, Christoph Lhotka wrote: > Dear mathgroup, > > there is indeed a very elegant solution to these kind of problems. > It took > me some while (looking up the documentation center) but I think this > works fine: Well, there are lots of little problems with this solution, some of which have to do with the semantics of N, so that 1/2+2I looks like floats, and 2^5000+2I looks like 1.4124...x10^1505 + 0. I. with a red box around the 0. but also there is an issue that FullForm[%] is peculiar. So the idea of changing the display format in $Post processing is a possibility, it rapidly becomes less elegant if you expect it to cover every eventuality, and probably not possible if you want FullForm to work. RJF
[toc] | [prev] | [next] | [standalone]
| From | Christoph Lhotka <christoph.lhotka@univie.ac.at> |
|---|---|
| Date | 2013-06-23 00:41 +0000 |
| Message-ID | <kq5g85$sld$1@smc.vnet.net> |
| In reply to | #14958 |
Hello, On 06/22/2013 10:21 PM, Richard Fateman wrote: > On 6/22/2013 8:40 AM, Christoph Lhotka wrote: >> Dear mathgroup, >> >> there is indeed a very elegant solution to these kind of problems. >> It took >> me some while (looking up the documentation center) but I think this >> works fine: > Well, there are lots of little problems with this solution, some of > which have to do > with the semantics of N, so that 1/2+2I looks like floats, and > 2^5000+2I looks like > 1.4124...x10^1505 + 0. I. with a red box around the 0. > of course my definition of format did not take your cases into account (it was meant to work on realistic approximate complex numbers), but for sure you can modify the definition to satisfy your needs. > but also there is an issue that FullForm[%] is peculiar. > FullForm works as expected, also pattern matching works (in terms of the interpreted expression). > So the idea of changing the display format in $Post processing is a > possibility, > it rapidly becomes less elegant if you expect it to cover every > eventuality, and > probably not possible if you want FullForm to work. > RJF > I think, that you did not see the main point. It is not the definition of format it is the mechanism that allows to implement any output format in terms of interpreted expressions. Here I merge your approach of a previous post with mine to work in a more elegant way: In[1]:= format[c_Complex]:= Interpretation[ Re[c]+i Im[c],c] In[2]:= $Post=#/.c_Complex:>format[c]& Out[2]= #1/. c_Complex:>format[c]& In[3]:= c1=1/2+2I Out[3]= 1/2+2 i In[4]:= c1^2 Out[4]= -(15/4)+2 i In[5]:= c1=2^5000+2I; In[6]:= Im[c1] Out[6]= 2 In[7]:= FullForm[0.+1.i] Out[7]//FullForm= Plus[0.`,Times[1.`,i]] In[8]:= c1/.c_Complex:>Im[c] Out[8]= 2 In[9]:= Clear[$Post] I like this!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web