Showing posts with label idempotent. Show all posts
Showing posts with label idempotent. Show all posts

Thursday, February 4, 2016

Wolfram Language Attribute: OneIdentity

Attributes modify the behavior of entire classes of functions. Some Attributes cause functions to have well-known mathematical properties such as commutativity, associativity, or idempotency, notably for valid pattern-matching. Others control the way the Wolfram Language evaluates or does not evaluate a function's arguments, which if you study that you will see is at the very core of Wolfram Language operation.

Listable is a valuable functional language Attribute since it incorporates Thread into a function so it automatically maps over a List and instead of taking a List apart, we apply a function to it as a gestalt.

NumericFunction is an Attribute of all Wolfram Language mathematical functions and is True when all of their arguments are numerical. I suspect it is integral to the interaction of many functions with the underlying knowledge base of Mathematica and therefore to many functions' valid behavior (e.g. Piecewise – always define piecewise mathematical functions with Piecewise).

Attribute: OneIdentity


OneIdentity is the mathematical property of idempotency, which dictates that applying a function any number of times to its argument (i.e. Nest) returns the argument unchanged. Examples are adding zero to any number, or multiplying any number by one, or raising any number to 1st or 0th power (except 0 - someone explain that inconsistency to me!).

0^0

During evaluation of In[549]:= Power::indet: Indeterminate expression 0^0 encountered. >>

Out[549]= Indeterminate

The Doc Center says that OneIdentity is necessary for pattern-matching to work with the default of Times, Plus, and Power, which is a period. I.e., x_+ y_. => x + 0, x_ * y_. => x * 1, and x_^y_. => x^1.

MatchQ[x, Times[n_. , x_]]

True

You can see that OneIdentity removes nested Heads. It says to the evaluator, 'If the given pattern x_ matches any of these patterns, return True.'

a+b+c/.x_:>u/;Print@FullForm[Plus[x]//HoldForm]

HoldForm[Plus[Plus[a,b,c]]]
HoldForm[Plus[Plus]]
HoldForm[Plus[a]]
HoldForm[Plus[b]]
HoldForm[Plus[c]]

a+b+c

Here are related posts on Flat and Orderless. This post shows how to reveal all built-in functions with a given Attribute, categorized by the Attribute.

This is an excellent out-of-print book but there is plenty to learn from it. I'd compare it to David Wagner's Power Programming in Mathematica in its insight to how the Wolfram Language works.

Bahder, Thomas B. Mathematica for Scientists and Engineers. Reading, MA: Addison-Wesley. 1994.




Get new posts by email:

Wolfram Language Attribute: Orderless

Attributes modify the behavior of entire classes of functions. Some Attributes cause functions to have well-known mathematical properties such as commutativity, associativity, or idempotency, notably for valid pattern-matching. Others control the way the Wolfram Language evaluates or does not evaluate a function's arguments, which if you study that you will see is at the very core of Wolfram Language operation.

Listable is a valuable functional language Attribute since it incorporates Thread into a function so it automatically maps over a List and instead of taking a List apart, we apply a function to it as a gestalt.

NumericFunction is an Attribute of all WL mathematical functions and is True when all of their arguments are numerical. I suspect it is integral to the interaction of many functions with the underlying knowledge base of Mathematica and therefore to many functions' valid behavior (e.g. Piecewise  – always define piecewise mathematical functions with Piecewise).

Attribute: Orderless


Thomas Bahder uses a neat trick to show how Attributes work and how some affect pattern-matching. Here is the trick revealing the mechanism of Orderless, which is equivalent to the mathematical commutative property, that is, x + y = y + x.



Bahder uses a Condition that will never be True, which causes WL to try all permutations of the function's arguments that are valid under Orderless. In the false predicate he inserts a Print statement to reveal how the WL evaluator works. HoldForm is necessary to stop the evaluator from putting the arguments into standard order before printing them.

Orderless says to the Wolfram Language evaluator, 'If given pattern matches any of these patterns, return True.'

a+b+c/.x_+y_+z_:>u/;Print@FullForm[Plus[x,y,z]//HoldForm]

HoldForm[Plus[a,b,c]]
HoldForm[Plus[a,c,b]]
HoldForm[Plus[b,a,c]]
HoldForm[Plus[b,c,a]]
HoldForm[Plus[c,a,b]]
HoldForm[Plus[c,b,a]]

a+b+c

Here are related posts on Flat and OneIdentity. This post lists all Wolfram Language functions that have any Attribute except Protected.

This is an excellent out-of-print book but there is plenty to learn from it. I'd compare it to David Wagner's Power Programming in Mathematica in its insight to how the Wolfram Language works. 


Bahder, Thomas B. Mathematica for Scientists and Engineers. Reading, MA: Addison-Wesley. 1994.