• Aucun résultat trouvé

The algebraic operations addition, subtraction, multiplication, and division are denoted as usual by +, -, *, and /. The

* for multiplication can be omitted by using a blank space instead. Parentheses () are used exclusively for grouping, and brackets [] are used for enclosing arguments in functions. Braces {} are used to enclose components of vectors and elements of sets (here, any number of elements of arbitrary type are allowed, which can be nested to any level).

Mathematical Expression Mathematica Form

Addition c+b ö c + b

Subtraction d-e ö d − e

Multiplication 3 x ö 3 xor3∗x

Division 4êr ö 4êy H4êx yisH4êxL∗yL

Exponentiation hl ö h^l

Grouping H2+3L 4 ö H2 + 3L 4

Function with an argument fHxL ö f@xD

Discrete iterator i=1, 2, 3, …, 9, 10 ö 8i, 1, 10, 1<or8i, 10<

Continuous range x=0 … 1 ö 8x, 0, 1<

Vector 9ax,ay,az= ö 8ax, ay, az<

Decimal number 3.567 ö 3.567

Assignment x=3 ö x = 3

Mathematical equality sinHpê2L=1 ö Sin@Piê2D == 1 Function definition fHxL=sinHxL ö f@x_D := Sin@xD String “hello world” ö "hello world"

“Collection” of items 8apple, apple, < ö 8apple, apple, <

The following list describes the syntax used in Mathematica:

† The ith element of Iax,ay,azM: {ax, ay, az}[[i]] (i is a concrete positive integer number)

† Prevent the display of (long) results by using a semicolon at the end of input: expression;

† The last expression given by Mathematica: %

† The next-to-last (penultimate) expression given by Mathematica: %%

† The ith output of Mathematica: %i or Out[i]

† When an expression is too long to fit on one line, the symbol \ (or Ö) is displayed, indicating that the expression is continued on the next line (if an expression is incomplete when the end of the line is reached, the expression is automati-cally considered to be continued on the next line)

† Comments can be written in the form, (* material to be ignored when sent to the Mathemat-ica kernel *) (comments can be inserted anywhere in MathematMathemat-ica source code)

† Information on the command command: ?command

† More information on the command command: ??command

† Metacharacter inside a string (standing for an arbitrary symbol): *

† Options of functions are set in the form option -> value, for instance: PlotPoints -> 50.

† “Ordinary”, Greek, Gothic, Script, and doublestruck letters represent different letters (B∫B∫∫)∫), and symbol names made from them are considered different. But plain, bold, italic, bold-italic, and underlined versions of a letter are considered equal (B=B=B=B=B). (The Mathematica inputs of the GuideBooks will make use of “ordinary”, Greek, Gothic, Script, and doublestruck letters, but all inputs will be in bold-nonitalic.) As the default output format, we will use StandardForm. In StandardForm, some symbols appear in a slightly “doubled” version. Most frequently, we will encounter  for e, the base of the natural logarithm, for -1, and for the differential d in integrals.

† Independent inputs can either be placed on separate lines or they can be separated by semicolons: inputStatement1; inputStatement2; …; inputStatementn.

The use of parentheses (someExpressions) for grouping and brackets [argumentsOfAFunction] for arguments of functions is essential for correct syntax; braces {} and double square brackets [[sequenceOfPositiveIntegersOr0]]

are short forms for the commands List and Part.

Using a functional programming style, it is often possible to write Mathematica code without using auxiliary variables.

As a consequence, a large number of brackets [] is often needed. In order to make such parts of a program easier to understand, the convention used (if space allows) in this book series is to align corresponding pairs of brackets […] and often pairs of () and {} vertically or horizontally (but this is a matter of the user’s personal taste). This process usually means indenting the code appropriately. Thus, Mathematica source code for programs should be printed using families of monospaced fonts with equally sized letters, such as Courier. It is common to include blank spaces around relatively weak operators, such as +, _, or ->. This convention does not apply inside short forms of commands.

Sixty-five commands in Mathematica have short forms; around 50 of these commands consist of two or three ASCII characters (e.g., -> [Rule] for replacement, != [Unequal] for inequality). No blank spaces are allowed between the symbols in these short forms. Relatively short Mathematica inputs representing mathematical expressions often look better in StandardForm notation (in StandardForm no additional spaces should be added). Because this book contains a lot of code and to maintain uniformity, we will use InputForm throughout this book. In some rare cases, we will use StandardForm, mainly for demonstration purposes.

In procedural programs, we will typically use one line per procedural statement. If possible and appropriate, we will carry out multiple assignments at once (for instance {one, two} = {1, 2} instead of one = 1; two = 2).

Below is an example of the general rules for Mathematica source code. In addition to the formatting, note that named temporary auxiliary variables can be largely dispensed with using Mathematica’s functional programming capabilities.

In the following code only, the variables armed, numberOfPoints, and rotation in the function definition appear; no further user-defined variables exist. Starting from now, we will display user-changeable arguments in italic.

For the function RotatedBlackWhiteStrips below the three arguments armed, numberOfPoints, and rotation are user-changeable arguments. The frequent appearance of # and & are parts of so-called pure functions; we discuss them in detail in Chapter 3.

It is a common convention in Mathematica that, whenever possible, a “typical” mathematical symbol (character sequence) for a quantity should be used. If not, a notation should be chosen to reflect the effect of the corresponding command or the contents of the corresponding list.

Readers will probably not understand the following code initially. However, after reading this book and looking at this code again, they will have no problem understanding how it works.

RotatedBlackWhiteStrips[

We now look at three short examples of RotatedBlackWhiteStrips [762÷], [489÷].

Show[GraphicsArray[{RotatedBlackWhiteStrips[ 4, 24, 1/4], RotatedBlackWhiteStrips[12, 36, -1/8], RotatedBlackWhiteStrips[72, 36, 1/4]}]]

In the programming code, we will try adhere to the aforementioned formatting conventions. But because of both horizontal and vertical space limitations on the pages of the book, it will not always be possible to follow the conven-tions exactly in every piece of code. Closing parentheses, brackets, and braces will not often be aligned vertically with the corresponding opening ones. Successive arguments of functions will either be written in one line or sometimes aligned vertically. This is in particular the case when a program uses many nested (pure) functions such as following.

Here we partition a regular n-gon (n even) into rhombuses (once again, we make no use of temporary auxiliary

MapIndexed[{GrayLevel[1 - (#2[[1]] - 1)/(n/2 - 1)], #1}&, Line /@ #]}}&[

(* the points calculated by iteration *)

Drop[Flatten[Transpose[{#1, Join[#2, {{}}]}], 1], -1]& @@ #& /@

NestList[{Last[#],

Show[GraphicsArray[

{GrayRhombusPartition[ 8, Background -> Hue[0.12]], GrayRhombusPartition[28, Background -> Hue[0.12]]}]]

Obeying strictly the above-formulated guidelines, this routine is quite big and nearly “ununderstandable” if formatted

“properly” on paper.

GrayRhombusPartition[n_?(EvenQ[#] && # > 4&), opts___] :=

Graphics[

Function[ (* º 100 lines deleted for brevity *) ][ (* º another 120 lines deleted for brevity *) ], Rule[AspectRatio, Automatic]

]

Σ (* session summary *) TMGBs`PrintSessionSummary[]

1.2 Introductory Examples

à 1.2.0 Remarks

In this section, we will give a short overview of the mathematical, graphical, and numerical possibilities built into Mathematica. The examples are largely unrelated to each other. We discuss all graphics-related commands in the Graphics volume of the GuideBooks [1283÷] and mathematics-related Mathematica commands in detail in the Numer-ics [1284÷] and SymbolNumer-ics [1285÷] volumes. Mathematica also contains a fully developed programming language. We will discuss programming-related features in detail in the next five chapters. The meaning of some of the inputs will be clear to readers without prior Mathematica experience. Some of the inputs will use commands that are not immediately recognizable; others will use “cryptic” shortcuts. In the following chapters, we will discuss the meaning of all the commands, as well as their aliases, in detail.

The division into programming, graphics, numerics, and symbolics does not reflect the structure of Mathematica. Just the opposite: The harmonic and fluent connection between all functions makes Mathematica an integrated environment where all parts can be used together in a smooth way. Also, the division into numerics and symbolics is not a strict one:

To derive efficient numerical methods, one needs symbolic techniques, and for carrying out complicated symbolic calculations, one frequently needs validated numeric decision procedures.

The examples of this chapter form a “random” collection. By no means are they intended to give up a complete, coher-ent, and logically built overview of Mathematica. Its capabilities are much too many and too diverse to even try to give such an overview inside one chapter.