2.5.3 Attributes - Wolfram Research

2. Principles of Mathematica
318
2.5.3 Attributes
Definitions such as f x_] = x^2 specify values for functions. Sometimes, however, you need to specify general properties of functions, without necessarily giving explicit values.
Mathematica provides a selection of attributes that you can use to specify various properties of functions. For example, you can use the attribute Flat to specify that a particular function is “flat”, so that
nested invocations are automatically flattened, and it behaves as if it were associative.
This assigns the attribute Flat to the
function f.
In 1]:= SetAttributes f, Flat]
Now f behaves as a flat, or associative,
function, so that nested invocations are
automatically flattened.
In 2]:= f f a, b], c]
Out 2]= f a, b, c]
Attributes like Flat can affect not only evaluation, but also operations such as pattern matching. If
you give definitions or transformation rules for a function, you must be sure to have specified the attributes of the function first.
Here is a definition for the flat function
f.
In 3]:= f x_, x_] := f x]
Because f is flat, the definition is
automatically applied to every
subsequence of arguments.
In 4]:= f a, a, a, b, b, b, c, c]
Attributes f]
Out 4]= f a, b, c]
give the attributes of f
Attributes f] = {attr1 , attr2 , ... }
set the attributes of f
Attributes f] = {}
SetAttributes f, attr]
ClearAttributes f, attr]
set f to have no attributes
add attr to the attributes of f
remove attr from the attributes of f
Manipulating attributes of symbols.
This shows the attributes assigned to f.
In 5]:= Attributes f]
Out 5]= {Flat}
This removes the attributes assigned to
f.
In 6]:= Attributes f] = { }
Out 6]= {}
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2.5 Evaluation of Expressions
319
Orderless
Flat
OneIdentity
orderless, commutative, function (arguments are sorted into
standard order)
flat, associative, function (arguments are “flattened out”)
f f a]], etc. are equivalent to a for pattern matching
Listable
f is automatically “threaded” over lists that appear as
arguments (e.g., f {a,b}] becomes {f a], f b]})
Constant
all derivatives of f are zero
Protected
Locked
ReadProtected
HoldFirst
HoldRest
HoldAll
Temporary
Stub
values of f cannot be changed
attributes of f cannot be changed
values of f cannot be read
the first argument of f is not evaluated
all but the first argument of f is not evaluated
all the arguments of f are not evaluated
f is a local variable, removed when no longer used
Needs is automatically called if f is ever explicitly input
The complete list of attributes for symbols in Mathematica.
Here are the attributes for the built-in
function Plus.
In 7]:= Attributes Plus]
Out 7]= {Flat, Listable, OneIdentity, Orderless, Protected}
An important attribute assigned to all built-in mathematical functions in Mathematica is the attribute
Listable. This attribute specifies that a function should automatically be distributed or “threaded”
over lists that appear as its arguments. This means that the function effectively gets applied separately
to each element in any lists that appear as its arguments.
The built-in Sqrt function is Listable.
In 8]:= Sqrt {5, 8, 11}]
Out 8]= {Sqrt 5], 2 Sqrt 2], Sqrt 11]}
This defines the function p to be listable.
In 9]:= SetAttributes p, Listable]
Now p is automatically threaded over
lists that appear as its arguments.
In 10]:= p {a, b, c}, d]
Out 10]= {p a, d], p b, d], p c, d]}
Many of the attributes you can assign to functions in Mathematica directly affect the evaluation of
those functions. Some attributes, however, affect only other aspects of the treatment of functions. For example, the attribute OneIdentity affects only pattern matching, as discussed in Section 2.3.7. Similarly,
the attribute Constant is only relevant in differentiation, and operations that rely on differentiation.
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2. Principles of Mathematica
320
The Protected attribute affects assignments. Mathematica does not allow you to make any definition
associated with a symbol that carries this attribute. The functions Protect and Unprotect discussed
in Section 2.4.12 can be used as alternatives to SetAttributes and ClearAttributes to set and clear
this attribute. As discussed in Section 2.4.12 most built-in Mathematica objects are initially protected so
that you do not make definitions for them by mistake.
Here is a definition for the function g.
In 11]:= g x_] = x + 1
Out 11]= 1 + x
This sets the Protected attribute for g.
In 12]:= Protect g]
Out 12]= {g}
Now you cannot modify the definition
of g.
In 13]:= g x_] = x
Set::write: Tag g in g x_] is Protected.
Out 13]= x
You can usually see the definitions you have made for a particular symbol by typing ?f, or by using
a variety of built-in Mathematica functions. However, if you set the attribute ReadProtected , Mathematica will not allow you to look at the definition of a particular symbol. It will nevertheless continue
to use the definitions in performing evaluation.
Although you cannot modify it, you can
still look at the definition of g.
In 14]:= ?g
Global`g
Attributes g] = {Protected}
g x_] = 1 + x
This sets the ReadProtected attribute
for g.
In 14]:= SetAttributes g, ReadProtected]
Now you can no longer read the
definition of g.
In 15]:= ?g
Global`g
Attributes g] = {Protected, ReadProtected}
Functions like SetAttributes and ClearAttributes usually allow you to modify the attributes of
a symbol in any way. However, if you once set the Locked attribute on a symbol, then Mathematica will
not allow you to modify the attributes of that symbol for the remainder of your Mathematica session.
Using the Locked attribute in addition to Protected or ReadProtected, you can arrange for it to be
impossible for users to modify or read definitions.
Clear f]
remove values for f, but not attributes
ClearAll f]
remove both values and attributes of f
Clearing values and attributes.
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2.5 Evaluation of Expressions
321
This clears values and attributes of p
which was given attribute Listable
above.
In 15]:= ClearAll p]
Now p is no longer listable.
In 16]:= p {a, b, c}, d]
Out 16]= p {a, b, c}, d]
By defining attributes for a function you specify properties that Mathematica should assume whenever that function appears. Often, however, you want to assume the properties only in a particular instance. In such cases, you will be better off not to use attributes, but instead to call a particular function to implement the transformation associated with the attributes.
By explicitly calling Thread, you can implement the transformation that would
be done automatically if p were listable.
Orderless
Flat
In 17]:= Thread p {a, b, c}, d]]
Out 17]= {p a, d], p b, d], p c, d]}
Sort f args]]
Flatten f args]]
Listable
Thread f args]]
Constant
Dt expr, Constants->f]
Functions that perform transformations associated with some attributes.
Attributes in Mathematica can only be permanently defined for single symbols. However, Mathematica also allows you to set up pure functions which behave as if they carry attributes.
Function vars, body, {attr1 , ... }]
a pure function with attributes attr1 , ...
Pure functions with attributes.
This pure function applies p to the
whole list.
In 18]:= Function {x}, p x]] {a, b, c}]
By adding the attribute Listable, the
function gets distributed over the
elements of the list before applying p.
In 19]:= Function {x}, p x], {Listable}] {a, b, c}]
Out 18]= p {a, b, c}]
Out 19]= {p a], p b], p c]}
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.