Message Boards Message Boards

0
|
9866 Views
|
8 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Simple Unit Conversions

Posted 9 years ago

I continue to find functional programming somewhat confusing.

I have lists of depths with unit, with differing units drawn from columns of a database. I wish to simply convert these lists of depths so that all elements of the list are, with each element given in meters. Ignoring non-essential aspects, I simplify the problem below.

As an example input, I use the following list:

result[All,13]] = {"50 ft", "8 ft", "16 m", "35 ft", "70 ft", "6 m", "60 ft", "90 ft", "8 ft"}

Although I naively thought that ft and m are interpretable by Quantity[result[[All,13]]], I get an "Unable to interpret unit specification ..."

Consequently, I substitute the explicit values for units and then have tried to execute a slightly overloaded function to do this:

result[[All, 13]] = StringReplace[result[[All, 13]], {"fm" -> "Fathoms", "ft" -> "Feet", "m" -> "Meters"}]

depthfun[n_ /; n == ""] := Module[{y}, y = n;];
depthfun[n_ /; n != ""] := 
  Module[{x, y1, y2, z}, x = StringSplit[n]; y1 = x[[1]]; 
   y2 = x[[2]] // InputForm;
   If[y2 == "Feet", z = UnitConvert[Quantity[y1, y2], "Meters"], 
    If[y2 == "Fathoms", z = UnitConvert[Quantity[y1, y2], "Meters"], 
     z = Quantity[y1, y2]]]];

which I then attempt to Map across the list:

Map[Apply[depthfun[#] &], result[[All, 13]]]

Unfortunately, this doesn't work and simply leaves the original list unchanged. I don't understand why. Can anyone help me figure out why my function fails to perform the unit conversions? No doubt, there is a more appropriate/succinct way to do this, so alternative approaches would be instructive.

Thank you.

POSTED BY: Stuart Poss
8 Replies
Posted 9 years ago

Thank you for taking the time to provide further explanation.

Your remarks clarify some of my confusion.

For example the List

s = {"a", "b", c, 5}

which only has one level, level 1, with the Head itself at level 0.

has the generalized Head of List so that Head[s] returns "List".

If what you say "replacing of all the heads with a different head", one might expect that the following

ToString @@@ s

would make the Heads Head[s[[1]]], Head[s[[2]]], Head[s[[3]]], Head[s[[4]]] all return "String".

However, in fact this seems only true for Head[s[[1]] and Head[s[[2]], which are already strings, whereas Head[s[3]]] returns Symbol and Head[s[[4]]] returns Integer. It would seem that although all elements of List s taken together have a Head of String after the Apply at Level 1 function syntax is executed, each element within the list still retains its "atomic" [correct word?] character.

Thus, it seems that list s of the fundamental types is not, subsequent to the application of ToString @@@ s, a list of all elements with a Head of String, but still a list of mixed types [atoms?]. However, this behavior seems confined to when the expressions making up the elements of a list are atoms rather than functions.

However, in going through your example of Apply[f, {g[a], g[b], g[c]}, 1], I observe that as you and the documentation indicate if we assign this to a list of functions, then Apply works exactly as you say. I can see I must think more carefully about what elements are in my list, expressions that may be atoms or expressions that may be functions.

Consequently, I think much of my confusion is that often the individual elements of my lists are of mixed type and hence iterating or mapping over them can often produce unexpected results when the functions I create are not properly overloaded and where I fail to appreciate the difference between a list of "atoms" and a list of functions serving as input.

As for your comment with respect to Timing, your approach seems about 4 times more efficient, if viewed this way:

In[92]:=  Do[f @@@ {g[a], g[b], g[c]}, 100000] // Timing

{0.109375, Null}

Do[Map[Apply[f[#] &], {g[a],g[b], g[c]}], 100000] // Timing 

{0.46875, Null}

and 11 times more efficient, if set up this way:

u = Table[g[a], {a 1, 100000}]; v = f @@@ {u}; // Timing {0.015625, Null}

or this way,

y = Map[Apply[f[#]&, u] //Timing {0.171875, Null}

presumably because of the cost of rebuilding the pure function each time in the list or processing them sequentially. This is something worth keeping in mind for long lists or for lists that are repeatedly iterated in some way.

So your suggestion seems not only to be cleaner but also more efficient.

POSTED BY: Stuart Poss

I'll try to explain the point about syntax. It is rather subtle, and I myself took some time before I understood it, back when I was learning the language. Your syntax

Map[Apply[depthfun[#] &], Take[depthsReal3, 10]]

contains Take[depthsReal3, 10], which expands to a list of this structure:

{Quantity[n1,u], Quantity[n2,u],...}

What you want is to turn this list into this other form:

{depthfun[n1,u], depthfun[n2,u],...}

that is, you want to replace the Quantity heads of the elements of the list with the symbol depthfun (and then evaluate). This "replacing of all the heads with a different head" is precisely what Apply is designed to do. In your case you want to replace all the heads that are at level 1:

Apply[depthfun, {Quantity[n1,u], Quantity[n2,u],...}, 1]

a construct which is needed often enough to deserve a cryptic shorthand

depthfun@@@{Quantity[n1,u], Quantity[n2,u],...}

Your method with Map achieves the same result, but with a more convoluted syntax. I don't know if there is a speed penalty involved, probably negligible.

POSTED BY: Gianluca Gorni
Posted 9 years ago
POSTED BY: Stuart Poss

This is probably the most straightforward solution for converting things into units:

Interpreter["Quantity"][{"50 ft", "8 ft", "16 m", "35 ft", "70 ft",  "6 m", "60 ft", "90 ft", "8 ft"}]
POSTED BY: Sean Clarke
Posted 9 years ago

This worked for me using version 10.2:

lst = {"50 ft", "8 ft", "16 m", "35 ft", "70 ft", "6 m", "60 ft", "90 ft", "8 ft"};

(UnitConvert[#1, "Meter"] & ) /@ lst

{381/25m,1524/625m,16m,2667/250m,2667/125m,6m,2286/125m,3429/125m,1524/625m}
POSTED BY: Dana DeLouis

You can convert a Quantity to a string simply with ToString[Quantity[2,"Meters"]]. Your construct Map[Apply[depthfun[#] &], result[[All, 13]]] is better done using the syntax

Apply[f, {g[a], g[b], g[c]}, 1]

(Apply at level 1) or with the shorthand

f @@@ {g[a], g[b], g[c]}

instead of mapping the Apply

Map[Apply[f[#] &], {g[a], g[b], g[c]}]
POSTED BY: Gianluca Gorni
Posted 9 years ago

This answers my question quite nicely.

Thank you for introducing me to the Switch function Gianluca. Both work and are about equally fast. However, for my purposes it seems the first is more desirable as it returns a string, rather than Quantity objects. Also, it seems easier to control the precision, This can be done simply in the first by a slight modification:

y1 = SetPrecision[N@ToExpression[y1], 3];

whereas I'm still trying to figure out how to do so in the latter.

POSTED BY: Stuart Poss

You must convert your variable y1 from a string to an expression, and you should not introduce InputForm into calculations, but only for final display

depthfun[""] := Null;
depthfun[n_ /; n != ""] := Module[{y1, y2},
   {y1, y2} = StringSplit[n];
   y1 = N@ToExpression[y1];
   UnitConvert[Quantity[y1, y2], "Meters"]];

Another way to do the same task, with StringCases:

dpths = {"50 ft", "8 ft", "16 m", "35 ft", "70 ft", "6 m", "60 ft", 
   "90 ft", "8 ft"};
StringCases[
  dpths, (n : NumberString) ~~ (Whitespace ..) ~~ unit__ :> 
   UnitConvert[
    Quantity[N@ToExpression[n], 
     Evaluate@
      Switch[unit, "m", "Meters", "ft", "Feet", "fm", "Fathoms"]],
    "Meters"]] // InputForm
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract