Showing posts with label TableHeadings. Show all posts
Showing posts with label TableHeadings. Show all posts

Wednesday, December 2, 2015

The Basics of Slot (#) in Pure Functions

Pure Function, as it's called, such as Function[x, x^2] that yields x2 or the beautiful shorthand, #^2&, is extremely useful in Mathematica:
  1. For one-time functions
  2. As an argument in many functional-style commands such as Map, Nest, Fold, Select, Count, etc
  3. In the extended Postfix style of programming I developed to eliminate hard-to-read nested functions
  4. In predicates including those used for argument type-checking: _?(# <= 1 &)
  5. In the general pattern-matching predicate MatchQ
  6. And the new use in Associations in version 10

Slot must be used with the pure Function operator &. They are designed to be the Head of a function:

#1&[a,b,c]

a

When you use the Postfix style just remember that Postfix causes the ampersand abbreviation for Function to wrap around an entire expression preceding it. Thus it has a low precedence of 90; here are its neighbors:

Precedence/@{Set,Postfix,Colon,Function,TimesBy,Rule}

{40.,70.,80.,90.,100.,120.}

It can be confusing if there are nested used of Function, but using that principle, you can figure it out.

Important: What trips up beginners is using Slot with Mathematica's universal container, List. This doesn't work:

#1&{a,b,c}

{a (#1&),b (#1&),c (#1&)}

There are several solutions. Using Apply (@@) will return a Sequence:

args=##&@@{a,b,c,d}

Sequence[a,b,c,d]

A Sequence is a list designed to be fed to any function:

func@args

func[a,b,c,d]

You can wrap SlotSequence in List so it returns a List:

{##}&[a,b,c,d]

{a,b,c,d}

Here are typical uses with Function as the Head or using Apply. Note that you cannot use negative position numbers with Slot or SlotSequence since that would conflict with using Subtract in pure Functions (like #-2&).

"First" (the default position argument for Slot is 1 so you don't need to specify it with "#1")

#&@@{a,b,c}

a

Return the first two arguments:

{#,#2}&@@{1,2,3,4}

{1,2}

Return all arguments:  ## is SlotSequence, which returns a Sequence, which is designed to be given to any function as its argument list.

##&[a,b,c,d]

Sequence[a, b, c, d]

{##}&[a,b,c,d]

{a, b, c, d}

##&@@@[a,b,c,d]

{a, b, c, d}

Return the first and fourth arguments:

{#,#4}&@@{1,2,3,4}

{1,4}

"Rest": Return the 2nd argument to the last:

{##2}&[a,b,c,d]


Sequence[b,c,d]

As of version 10, there is an interesting new usage for Slot with Associations. Slot suffixed with any Key (name) of an Association will automatically return the Value associated with the Key.

cityPopulationAssociation=Association["Tokyo"->"37 million","Jakarta"->"26 million","Seoul"->"17 million","Delhi"->"22 million","Shanghai"->"21 million"];

#Delhi&@cityPopulationAssociation

22 million

Wednesday, June 3, 2015

Two Ways to Add Line Numbers to a Table

Often we'd like to add line numbers to a table of data. Here's a simple method using the formula for distance traveled in meters by a falling body near the earth's surface (0.5 x 9.8 m/s2).

Using Table with Iterator

It is easy to see that the lone iterator "i" simply supplies the line numbers, indicating 1 to 20 seconds, for TableForm.

Table[{i,4.9 i^2},{i,20}]//TableForm




















Here's a fancier version with headings. The first "None" in TableHeadings is for row labels, which we don't need since the Table iterator provided them, while the second set of values are the column headings. TableForm doesn't provide for a title, but we can add one with Labeled. A "\n" creates a line break and we need Style to center the title.


Table[{i,4.9 i^2},{i,20}]//TableForm[#,TableHeadings->{None,{"Seconds","Meters"}}]&//Labeled[#,"Distance Traveled in 20 Seconds\nof Free Fall Near the Earth's Surface\n",Top]&//Style[#,TextAlignment->Center]&





















Using MapIndexed

Here the formula for distance in feet is expressed in pure Function shorthand, which is then mapped onto the integers from 1 to 20 generated by Range, representing 20 seconds of free fall from v0 = 0. A pure Function is the fast way to write a one-off function that you don't intend to re-use.

MapIndexed adds the index to the expression as a suffix, so we need to Map Reverse on to each data item that will be a row in the table.

MapIndexed[{16#^2,#2}&,Range@2]

Out[53]= {{16,{1}},{64,{2}}}

Last we apply TableForm.

MapIndexed[{4.9 #^2, #2} &, Range@20] // Reverse /@ # & // TableForm