Principia Matematicae

Dr. C. Chryssomalakos, ICN - UNAM

This is a set of notes on some general aspects of Mathematica. Some of the material is scattered in the references cited in the bibliography, mostly Refs. [2], [3], [5] ---the rest are things unearthed while struggling with the subject.

SetDirectory["E:\chryss\math\workshop"];

Expressions

Everything is an expression

Everything in Mathematica has a type. 

There are three fundamental types: numbers, symbols or strings, each of which is an atom --- everything else is just an expression (atoms are also expressions).

Numbers can be real or integers. Rationals, Gaussian integers, Gaussian rationals and complex numbers are not atoms, they are expressions. Numbers can have any number of digits, any exponent, and can be expressed in any base from 2 to 36.

Symbols are sequences of letters, integers, and $, not starting with integers (e.g., a2, xx, $Path, AstronomyDomine).

Strings are sequences of ASCII characters between quotes (e.g., "I am not a string"). 
  
An expression is of the form f[(Subscript[a, 1]),  (Subscript[a, 2]),  ...  ,  (Subscript[a, n])], where f, (Subscript[a, 1]), (Subscript[a, 2]), ... , (Subscript[a, n]) are expressions. f must exist, n can be zero ( f[] is ok, [a] is not).

Consider the assignment

exp = f[x,  g[u, v],  h[a[r, s], t]]

To get rid of the message "General::spell1..."

Off[General::spell, General::spell1]

Head and FullForm

f is the head of exp

Head[exp]

x is the first argument of exp, g[u,v] the second and h[a[r,s],t] the third. The length of exp is three. You can access, e.g., the second argument with

exp[[2]]

Another expression

expr2 = g[s, r][a, b, g[u, v]]

Access its head through

Head[expr2]

Look at the third argument

expr2[[3]]

More expressions

Sin[x]

Head[%]

FullForm[Sin[x]]

b = Expand[(((x + a))^10)]

Head[b]

FullForm[b]

A list is an expression

alist = {s, t, u}

Head[alist]

FullForm[alist]

Comment

FullForm of atoms does not include the head

{Head[5], FullForm[5]}

More examples

{27.35, Head[27.35], FullForm[27.35]}

{a + b + c, Head[a + b + c], FullForm[a + b + c]}

Get rid of the definition of b

Clear[b]

{a + b + c, Head[a + b + c], FullForm[a + b + c]}

Subtraction is not an independent operation

FullForm[x - y]

How about

FullForm[3 + 7]

To prevent evaluation

FullForm[Hold[3 + 7]]

Raising to a power is an independent operation, division is not

{(x^n), FullForm[(x^n)], (x/y), FullForm[(x/y)]}

Prefix, infix, postfix, circumfix

Operators can have in general a prefix, an infix and a postfix form.

Prefix

Plus[a, b]

Infix

a + b

Prefix

Simplify[(x + 5) (x - 5)]

Postfix

(x + 5) (x - 5) // Simplify

There is also a circumfix form.
Prefix

List[a, b, c]

Circumfix

{a, b, c}

Equal signs in assignments are infix forms of Set or SetDelayed.

FullForm[Hold[x = y]]

FullForm[Hold[x := y]]

Function application

{f[a], f@a, a // f}

Double square brackets for part extraction is postfix for Part

FullForm[Hold[x[[2]]]]

More FullForms

{FullForm[a ≥ b], FullForm[2 - I 25], 
      FullForm[a + I b], FullForm[3/4]}

FullForm /@ {Infinity,  x_, g :> h, # &}

FullForm /@ {
Plot[
          Sin[x], {x, 0, 2  Pi}], 
Play[
          10  Cos[(300 + 
                    100  Cos[4  Pi t]^2) ( .4 +  .8 
                       E^(- .1 (t - 4)^2)) 2  Pi t], {t, 0, 
            8}]
}

Probing expressions

Parts of expressions

e3 = g[x, y, z]

{e3[[0]], e3[[1]], e3[[2]], e3[[3]]}

{e3[[-3]], e3[[-2]], e3[[-1]], 
      e3[[0]]}

Parts of expressions and partspecs

Define an expression

e4 = g[a][Sin[a + f], a^(g + u + f)]

Look at its TreeForm

TreeForm[e4]

Use branch indices to access various parts

{
e4[[1]], 
e4[[1]][[1]], 
e4[[1]][[1]][[2]]
}

There is an abbreviated form for branch indices, called partspec

{
e4[[1, 1]], 
e4[[1, 1, 
          2]]
}

Find the partspec of f in e4

Position[e4, f]

Notice that Mathematica has rearranged g, u, f in a standard (alphabetical, in this case) order.

Levels of expressions and levelspecs

The level of a part of an expression is the level of the head of the part in the TreeForm of the expression (the root of the TreeForm is at level 0).

e4

Find all subtrees of e4 with root at level 1

Level[e4, {1}]

Same for level 2

Level[e4, {2}]

Same for level 3

Level[e4, {3}]

Omiting the brackets returns the indicated level or higher

Level[e4, 2]

Level[e4, 3]

Define e5

e5 = Sin[Tan[a + Log[v] + (j^k)]]

Two numbers in {} specify a range of levels

Level[e5, {2, 3}]

Negative levels refer to depth of subexpressions. The following returns all subtrees of depth 1, i.e. the leaves of the expression.

Level[e5, {-1}]

Look for all subtrees of depth 2.

Level[e5, {-2}]

There is only one subtree of depth 3.

Level[e5, {-3}]

The second argument of Level is the levelspec.

Encryption

Text

Off[N::meprec];

Start with a reserve of primes

primes = Select[Range[1000], PrimeQ]

Take the first n of them

primesupto[n_] := Take[primes, n]

Text to be encrypted

text = "adios";

ToCharacterCode returns ASCII codes

Gcc[abc_] := ToCharacterCode[abc] - 96;
textCC = Gcc[text]

Encrypt via prime decomposition

Times @@ (primesupto[Length[textCC]]^(textCC))

FactorInteger[%]

Squeeze into [0,1]

Goedeltext[
        text_] := 
N[(Sin[
                Times @@ (primesupto[
                        Length[Gcc[text]]]^(Gcc[text]))] + 1)/2, 
        20]

Everything ever written is mapped to a point in [0,1]

Goedeltext["Goedel"]

Is there any text with easy to remember Goedeltext?

Goedeltext["In the middle of all texts"]

Stereo sound

ListPlay[{Random[] & /@ Range[10000], Random[] & /@ 
          Range[10000]}]

Import stereo sound

Lc = 
        Flatten[Import["Lchannel.dat", "Table"]];
Rc = Flatten[Import["Rchannel.dat", "Table"]];

What is it?

ListPlay[{Lc, Rc}]

FullForm[Lc]

Put the two channels together

LRc = Lc  +  I Rc;

Length[LRc]

Fold the list into a rectangular array

ZdataA = Partition[LRc, 100];

Length[ZdataA]

ListPlot3D[
Abs[
        Fourier[ZdataA]], 
Lighting -> 
        False, 
ColorFunction -> 
        Automatic, 
Mesh -> 
        False, 
ViewPoint -> {40, 30, 
          40}
]

<< Graphics`Animation`

Import a sequence of graphics

<< Ztablefile

ShowAnimation[Ztable]

Bibliography

[1] S. Wolfram, "The Mathematica Book", Cambridge University Press (1999), fourth edition.

[2] J. W. Gray, "Mastering Mathematica", Academic Press (1997).

[3] H. Ruskeepaa, "Mathematica Navigator", Academic Press (1998).

[4] R. Maeder, "Programming in Mathematica", Addison Wesley (1997), third edition.

[5] T. W. Gray, J. Glynn, "Exploring Mathematics with Mathematica", Addison Wesley (1991)

[6] A. Gray, "Modern Differential Geometry of Curves and Surfaces with Mathematica", CRC Press (1998).
[7] M. Trott, "The Mathematica GuideBook",  Vols. I-IV, Springer