MATHEMATICA TUTORIAL

# Basic Objects

## Expressions

Expressions are the main type of data in Mathematica.

Expressions can be written in the form . The object h is known generically as the head of the expression. The are termed the elements of the expression. Both the head and the elements may themselves be expressions.

The parts of an expression can be referred to by numerical indices. The head has index 0; element has index i. Part[expr, i] or gives the part of expr with index i. Negative indices count from the end.

Part[expr, i1, i2, ...], , or Extract[expr, {i1, i2, ...}] gives the piece of expr found by successively extracting parts of subexpressions with indices . If you think of expressions as trees, the indices specify which branch to take at each node as you descend from the root.

The pieces of an expression that are specified by giving a sequence of exactly n indices are defined to be at level n in the expression. You can use levels to determine the domain of application of functions like Map. Level 0 corresponds to the whole expression.

The depth of an expression is defined to be the maximum number of indices needed to specify any part of the expression, plus one. A negative level number -n refers to all parts of an expression that have depth n.

## Symbols

Symbols are the basic named objects in Mathematica.

The name of a symbol must be a sequence of letters, letter-like forms, and digits, not starting with a digit. Uppercase and lowercase letters are always distinguished in Mathematica.

 aaaaa user-defined symbol Aaaaa system-defined symbol \$Aaaa global or internal system-defined symbol aaaa\$ symbol renamed in a scoping construct aa\$nn unique local symbol generated in a module

Conventions for symbol names.

Essentially all system-defined symbols have names that contain only ordinary English letters, together with numbers and . The exceptions are , , , and .

System-defined symbols conventionally have names that consist of one or more complete English words. The first letter of each word is capitalized, and the words are run together.

Once created, an ordinary symbol in Mathematica continues to exist unless it is explicitly removed using Remove. However, symbols created automatically in scoping constructs such as Module carry the attribute Temporary, which specifies that they should automatically be removed as soon as they no longer appear in any expression.

When a new symbol is to be created, Mathematica first applies any value that has been assigned to \$NewSymbol to strings giving the name of the symbol and the context in which the symbol would be created.

If the message General::newsym is switched on, then Mathematica reports new symbols that are created. This message is switched off by default. Symbols created automatically in scoping constructs are not reported.

## Contexts

The full name of any symbol in Mathematica consists of two parts: a context and a short name. The full name is written in the form . The context can contain the same characters as the short name. It may also contain any number of context mark characters , and must end with a context mark.

At any point in a Mathematica session, there is a current context \$Context and a context search path \$ContextPath consisting of a list of contexts. Symbols in the current context, or in contexts on the context search path, can be specified by giving only their short names, provided they are not shadowed by another symbol with the same short name.

 name search \$ContextPath, then \$Context; create in \$Context if necessary `name search \$Context only; create there if necessary context`name search context only; create there if necessary `context`name search only; create there if necessary

Contexts used for various specifications of symbols.

With Mathematica packages, it is conventional to associate contexts whose names correspond to the names of the packages. Packages typically use BeginPackage and EndPackage to define objects in the appropriate context, and to add the context to the global \$ContextPath. EndPackage prints a warning about any symbols that were created in a package but which are "shadowed" by existing symbols on the context search path.

The context is included in the printed form of a symbol only if it would be needed to specify the symbol at the time of printing.

## Atomic Objects

All expressions in Mathematica are ultimately made up from a small number of basic or atomic types of objects.

These objects have heads that are symbols that can be thought of as "tagging" their types. The objects contain "raw data", which can usually be accessed only by functions specific to the particular type of object. You can extract the head of the object using Head, but you cannot directly extract any of its other parts.

 Symbol symbol (extract name using SymbolName) String character string (extract characters using Characters) Integer integer (extract digits using IntegerDigits) Real approximate real number (extract digits using RealDigits) Rational rational number (extract parts using Numerator and Denominator) Complex complex number (extract parts using Re and Im)

Atomic objects.

Atomic objects in Mathematica are considered to have depth 0 and yield True when tested with AtomQ.

## Numbers

 Integer integer nnnn Real approximate real number Rational rational number Complex complex number nnn+nnn I

Basic types of numbers.

All numbers in Mathematica can contain any number of digits. Mathematica does exact computations when possible with integers and rational numbers, and with complex numbers whose real and imaginary parts are integers or rational numbers.

There are two types of approximate real numbers in Mathematica: arbitrary precision and machine precision. In manipulating arbitrary-precision numbers, Mathematica tries to modify the precision so as to ensure that all digits actually given are correct.

With machine-precision numbers, all computations are done to the same fixed precision, so some digits given may not be correct.

Unless otherwise specified, Mathematica treats as machine-precision numbers all approximate real numbers that lie between \$MinMachineNumber and \$MaxMachineNumber and that are input with less than \$MachinePrecision digits.

In InputForm, Mathematica prints machine-precision numbers with \$MachinePrecision digits, except when trailing digits are zero.

In any implementation of Mathematica, the magnitudes of numbers (except 0) must lie between \$MinNumber and \$MaxNumber. Numbers with magnitudes outside this range are represented by and Overflow[].

## Character Strings

Character strings in Mathematica can contain any sequence of characters. They are input in the form .

The individual characters can be printable ASCII (with character codes between 32 and 126), or in general any 8- or 16-bit characters. Mathematica uses the Unicode character encoding for 16-bit characters.

In input form, 16-bit characters are represented when possible in the form , and otherwise as .

Null bytes can appear at any point within Mathematica strings.

## Tutorial CollectionTutorial Collection

New to Mathematica? Find your learning path »
Have a question? Ask support »