Programming

Mathematica contains a set of program paradigms, which allows writing any program in the best way it can be written in.

Example 1. Procedural programming. Operators are similar to C++, Pascal, ... Appropriation and conditional operator If:

z = 3 ; If[0≤z≤2 && z>5, z, -z ]

-3

Cycle operators are: For, Do, While, Throw, NestWhile. Calculating the sum of the first four natural numbers with intermediate prints:

For[sum1 = 0 ; i = 1,   i<5,   i ++,   sum1 += i ; Print[" i= ", i, "      sum1 to this moment is = ", sum1]]

 i= 1      sum1 to this moment is = 1

 i= 2      sum1 to this moment is = 3

 i= 3      sum1 to this moment is = 6

 i= 4      sum1 to this moment is = 10

z = 2 Do[Print[z *= z + i], {i, 3}]

2

6

48

2448

Example 2. Programing the list base. A lot of operations are treated easily as lists. They can be created in different ways, i.e. with the Table operator, by adding to the beginning, end or at a random location etc. Examples of  list operations and manipulations are shown below.

Clear[d] d = {}        (*  empty list, where this ... n the calculation cell *) For[i = 1, i<6, i ++, d = Append[d, i]] d Prepend[d, a] Append[%, b]

{}

{1, 2, 3, 4, 5}

{a, 1, 2, 3, 4, 5}

{a, 1, 2, 3, 4, 5, b}

Cycle for three dimensional table 'k':

k = Table[i^j, {i, 4}, {j, i}] TableForm[k]

{{1}, {2, 4}, {3, 9, 27}, {4, 16, 64, 256}}

1      
2 4    
3 9 27  
4 16 64 256

The next function produces the upper table in a single dimensional list:

Flatten[%]

{1, 2, 4, 3, 9, 27, 4, 16, 64, 256}

And this one combines it in pairs:

Partition[%, 2]

{{1, 2}, {4, 3}, {9, 27}, {4, 16}, {64, 256}}

Example 3. Functional programming.

NestList[f, x, 4]

{x, f[x], f[f[x]], f[f[f[x]]], f[f[f[f[x]]]]}

If  (1 + #)^2 & is a "lear function", its argument places it at the location of  #.

NestList[(1 + #)^2 &, x, 3]

{x, (1 + x)^2, (1 + (1 + x)^2)^2, (1 + (1 + (1 + x)^2)^2)^2}

Example 4. Programming based on rules. The symbol ':=' here means defining the function 'p[]' and  'x_'  and 'y_' are formal parameters.

p[x_ + y_] := p[x] + p[y]

p[a + b + c]

p[a] + p[b] + p[c]

The symbol _ can be changed with a single expression, whereas the symbol __ is a random number of expressions.

s[{x__, a_, y__}, a_] := {a, x, x, y, y}

s[{1, 2, 3, 4, 5, 6}, 4]

{4, 1, 2, 3, 1, 2, 3, 5, 6, 5, 6}

Clear[p, s]

Example 5. Object-oriented programming. Three definitons of functions associated with the object 'h' are shown below.

h/:h[x_] + h[y_] := hplus[x, y] h/:p[h[x_], x_] := hp[x] h/:f_[h[x_]] := fh[f, x]

Here is how the three definitons can be isolated.

h[a] + h[b] + f[h[r]] + h[h[x]]

fh[f, r] + fh[h, x] + hplus[a, b]

Clear[h]

Example 6. Programming the string base.

StringReplace["aababbaabaabababa", {"aa"->"", "ba"->""}]

baa

Example 7. Mixed porgramming paradigms.

Position[{1, 2, 3, 4, 5}/2, _Integer]

{{2}, {4}}

MapIndexed[Power, {a, b, c, d}]

{{a}, {b^2}, {c^3}, {d^4}}

FixedPointList[If[EvenQ[#1], #1/2, #1] &, 10^5]

{100000, 50000, 25000, 12500, 6250, 3125, 3125}

ReplaceList[{a, b, c, d, e}, {x__, y__} -> {{x}, {y}}]

{{{a}, {b, c, d, e}}, {{a, b}, {c, d, e}}, {{a, b, c}, {d, e}}, {{a, b, c, d}, {e}}}

Exampleр 8. Programs can be written in different styles. This is illustrated below with small programs (functions), each of which calulates n!

f[n_] := n !

f[n_] := Gamma[n - 1]

f[n_] := n  f[n - 1] ; f[1] = 1

f[n_] := Product[i, {i, n}]

f[n_] := Product[i, {i, n}]

f[n_] := Module[{t = 1}, Do[t = t * i, {i, n}] ; t]

f[n_] := Module[{t = 1, i}, For[i = 1, i <=n, i ++, t *= i] ; t]

f[n_] := Module[{t = 1, i}, For[i = 1, i <=n, i ++, t *= i] ; t]

f[n_] := Apply[Times, Range[n]]

f[n_] := Fold[Times, 1, Range[n]]

f[n_] := If[n == 1, 1, n f[n - 1]]

f = If[#1 == 1, 1, #1 #0[#1 - 1]] &

f[n_] := Fold[#2[#1] &, 1, Array[Function[t, #t] &, n]] Clear[f]


Created by Mathematica  (October 6, 2007)