MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to navigate the documentation?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110151] Re: How to navigate the documentation?
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Fri, 4 Jun 2010 08:02:28 -0400 (EDT)

Let me try the second "challenge" -- which is not too challenging after 
all!  (Of course it depends on your prior experience with Mathematica. 
One should hardly attempt to accomplish such a task upon the first or 
second moment's engagement with the system.)

The challenge begins, "In Mathematica, how do I convert the string...." 
  So in the Documentation Center I searched for:

   convert string

At the bottom of the first page of results, I saw the entry:

   Converting between Expressions & Stings

Now I know, as should any user, that in Mathematica, "everything is an 
expression". So I selected that entry.

The second function cited on the target page, 
guide/ConvertingBetweenExpressionsAndStrings, is ToExpression. I 
selected that. And the documentation suggests that it's what to use. So 
I was going to try:

   ToExpression["1.23E-4"]

But everybody knows that won't work, since E in Mathematica is the base 
of natural logarithms, so that the value is going to be that of 
1.23*Exp[1] - 4; and indeed you get:

-0.656513

Now we have to deal with this unusual number format 1.23E-4. If you have 
some prior knowledge (and why else would you have numbers in this format 
at all?), then you know that it's Fortran notation. The simplist thing 
is to look up what numerical formats Mathematica does understand

But here's another path. You know the notation means that you need to 
multiply the mantissa 1.23 by 10^(-4), and that's usually called 
scientific form. So look up "scientific form". You'll get the page 
ref/ScientificForm.  OK, I see that's a format Mathematica understands. 
But how to enter it?  Well, obviously you use the usual (1-dimensional) 
input format 10^-4 instead of the 2-dimensional form.

So this now reduces the whole enterprise to the question of how to 
replace E-4 in the string to 10^-4.  So look up "replace string" and in 
a couple of steps you'll get, for example, to ref/StringReplace. So use 
that:

   StringReplace["1.23E-4","E"->" 10^"]
1.23 10^-4

OK. [Notice the necessary inserted space. Of course you could instead 
use the rule "E"->"*10^" .]  So finally:

   ToExpression[StringReplace["1.23E-4", "E" -> " 10^"]]
0.000123

Now if you're going to do this a lot, evidently you want a function to 
apply to such strings:

   toNumber[s_String] := ToExpression@StringReplace[s, "E" -> " 10^"]

Actually, if you know about, or look up and learn about, a shorthand 
notation for exponential form in Mathematica, then you could convert the 
string "1.23E-4" instead to "1.23*^-4", with no explicit base 10 
indicated.  So now our function becomes:

   toNumber[s_String] := ToExpression@StringReplace[s, "E" -> "*^"]

(Saving two letters in the string replacement.)

Does the solution above to solving your documentation challenge satisfy 
you?  Or were you expecting to be able to pose such a question and 
immediately get an answer in one shot?

Perhaps there is such a one-shot way, but I didn't see it.  So what?




On 6/3/2010 5:39 AM, kj wrote:
> This question is about the Mathematica documentation, but it would
> more helpful to pose it in terms of an example question:
>
> "I have a file containing newline-separated records.  The fields
> in each record are separated by tabs.  The number of fields varies
> from one record to the next, but it always has the form 2*(n+1),
> where n>= 3.  The first two fields are always alphabetic strings;
> the remaining 2*n fields are real numbers (expressed using the "E"
> notation: e.g. 1.23E-4 == 0.000123), and they represent n points
> in R^2.  In summary records have the following form:
>
> <string>  <TAB>  <string>  <TAB>  <real>  <TAB>  ...<TAB>  <real>  <NL>
>
> I want to read this file into a Mathematica structure having the form
>
> {String, String, {{Real, Real}, {Real, Real}, {Real, Real}, ...}}
> "
>
>
> OK, now: The *challenge* is to find the answer to the question
> above in the Mathematica documentation.
>
>
> I'm sorry to say that, after *hours* of effort, I failed completely.
>
>
> OK, here's a much simpler question:
>
> "In Mathematica, how do I convert the string "1.23E-4" the real
> number 0.000123?"
>
> The *challenge* now is like before: find the answer to this (much
> simpler) question in the Mathematica documentation.
>
> Again, after much effort, I was not able to answer even this simple
> question using the Mathematica documentation.  Can it be done at
> all???
>
>
> Someone please show me how to navigate the Mathematica documentation
> to answer basic questions like these.
>
> TIA!
>
> ~K
>

-- 
Murray Eisenberg                     murrayeisenberg at gmail.com
80 Fearing Street                    phone 413 549-1020 (H)
Amherst, MA 01002-1912


  • Prev by Date: Re: How to navigate the documentation?
  • Next by Date: Re: DateListPlot (Margins? or something?)
  • Previous by thread: Re: How to navigate the documentation?
  • Next by thread: Re: How to navigate the documentation?