MathGroup Archive 2011

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

Search the Archive

Re: Need help integrating Wolfram Alpha data in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123460] Re: Need help integrating Wolfram Alpha data in Mathematica
  • From: Fred Klingener <jfklingener at gmail.com>
  • Date: Thu, 8 Dec 2011 05:23:33 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jbi6df$51h$1@smc.vnet.net>

On Dec 5, 5:29 am, Dave M <cvk1... at hotmail.com> wrote:
> I'm using Wolfram Mathematica 8, and I have a question about using Wolfram Alpha data in my calculations.
>
> What I would like to do is to pull data from the Wolfram Alpha search engine, and then store it in a Mathematica variable for future use. I cannot figure out how to do this easily.
>
> To illustrate what I mean, here's a simple example. Let's say I want the mass of a proton in kilograms. I can obtain this by simply typing "== mass of proton in kilograms", upon which Mathematica contacts the Wolfram Alpha server, and downloads a lot of data.
>
> This data doesn't only contain the actual number I wanted, it is actually a huge table with many cells containing information about how Wolfram Alpha parsed my query, the assumptions used, conversions of the answer to various other units, etc.
>
> However, all I wanted was simply the answer to my query in a computable numeric form, so I could store that in a variable in Mathematica.
>
> I can get somewhat usable results if I do something like this:
>
> WolframAlpha["mass of proton in kilograms", {{"Result", 1},
>   "ComputableData"}]
>
> However, this is a pretty complicated way of doing something really simple. Is there some simpler method that involves much less typing?

W|A seems to make everything complicated enough to prevent its use
except for cases where it's the only tool. My principal beef is that
they serve up disembodied numerical data and call it "Computable
Data." Try asking W|A "What is Computable Data?" It doesn't know.

For my purposes, I need at least the number AND the physical units.
Metadata like time/date stamp, repetition of the original query and
source would seem to be minimum appendage if it's ever going to be
taken seriously. I use the following approach.

WolframAlpha["proton mass in kilograms","PodIDs"]

{Input,Result,AdditionalConversion,ComparisonAsMass,CorrespondingQuantity}

WolframAlpha["proton mass in kilograms",{"Result"}]

{{{Result,0},Title},{{Result,0},Scanner},{{Result,0},ID},{{Result,
0},Position},{{Result,1},Plaintext},{{Result,1},Cell},{{Result,
1},Content},{{Result,1},Image},{{Result,1},DataFormats},{{Result,
1},ComputableData},{{Result,1},FormattedData},{{Result,1},NumberData},
{{Result,1},QuantityData}}

WolframAlpha["proton mass in kilogram",{{"Result",
1},"ComputableData"}]

1.6726217*10^-27

This is SlideRuleData, not ComputableData. To find at least the
physical units I ask for

qdk=WolframAlpha["proton mass in kilogram",{{"Result",
1},"QuantityData"}]

1.6726217*10^-27  kilograms

The harmless-looking structure can't be used for anything useful in
Mathematica, because the units are expressed as a String..

{Head[#],Length[#],Dimensions[#]}&@qdk

{WolframAlphaQuantity,2,{2}}

The element in position 1 is the number

qdk[[1]]

1.6726217*10^-27

and the second element is a String encoding the physical units.

qdk[[2]]

kilograms

For dimensional analysis, I'm a big fan of Jon McLoone's
AutomaticUnits` package (http://blog.wolfram.com/2010/12/09/automatic-
physical-units-in-mathematica/), but there are some contortions
required to get W|A to fit.

<<"AutomaticUnits`"

In early winter 2011, each Mathematica user with ambitions to use W|A
physical data in dimensional analysis has to build and maintain a
replacement rule table to read the unit strings W|A serves up and
replace them with computable unit expressions consistent with whatever
units package she's using. For this example, I need only one entry.

rule1="kilograms"->Kilogram

kilograms->Kilogram

So in this case I'd have

#[[1]] #[[2]]/.rule1&@WolframAlpha["proton mass in kilogram",
{{"Result",1},"QuantityData"}]

1.6726217*10^-27 Kilogram

a presentable form for computing the next step in Mathematica,
whatever that is.

Things can get involved in the general case. For example, if I had
started with

WolframAlpha["proton mass",{{"Result",1},"QuantityData"}]

938.27203  megaelectronvolts per speed of light squared

The AutomaticUnits` package doesn't define electron volts, so the user
would have to add it. Google serves up

DeclareUnit["ElectronVolt",1.60217646*10^-19 Joule]

ElectronVolt

or W|A could supply the conversion, using a procedure as contorted as
the one described.

The speed of light is available in Mathematica's PhysicalConstants`
package, but loading the PhysicalConstants` package evidently
overwrites some elements of the AutomaticUnits' package, so I add it
explicitly.

speedOfLight=299792458Meter/Second;

The rule to translate W|A's string representation of units to
Mathematica's AutomaticUnits` is

rule2="megaelectronvolts per speed of light squared"->Convert[Mega
ElectronVolt/speedOfLight^2,Kilogram]
megaelectronvolts per speed of light squared->1.78266*10^-30 Kilogram

#[[1]] #[[2]]/.rule2&@WolframAlpha["proton mass",{{"Result",
1},"QuantityData"}]

1.67262*10^-27 Kilogram

Pretty nifty as a stunt but too awkward, fussy and brittle for any
important work, the procedure exemplifies what I think is wrong with W|
A. To be a useful addition to Mathematica, W|A should be jacked up and
a consistent, usable foundation for physical units built under it. I'd
like to see AutomaticUnits eventually be the basis for both.

Hth,
Fred



  • Prev by Date: Re: Plot Legends For Side By Side Graphs
  • Next by Date: Re: Evaluate[expr]
  • Previous by thread: Re: Need help integrating Wolfram Alpha data in Mathematica
  • Next by thread: Passing function to custom plotting routine