Odds and Ends

The "Print" Command

Usually, Mathematica only returns output, after a command has been executed. Use the "Print" command to see output, while a command is being executed.

k = 5; Print["This number is ", k, "."]

Strings are enclosed by "...", items are separated by commata.
"\t" produces tabbing, "\n" a new line.

k = 2.8; 
    Print["The Sine and Cosine of\t", k, "\tare given by\n", Sin[k], 
      "\t", Cos[k], "."]

Procedural Programming

Mathematica also features the familiar procedural programming commands you might know from Fortran or Pascal: "Do", "If" and "While".

\(k = 1; 

    Do[
	Print["This number is ", k, "."]; k = k + 1, 

      	{k, 1, 5}
]\)

The syntax of "If":  If[condition, do if true, do if false].

\(\(
Do[
	
      If[k < 10, 
		Print["This number is ", k, "."], 
	\ \ 
        Print["This number is too big."]
], 
	{k, 1, 15, 2}]\)\)

\(\(
k = 1; 

    While[k < 10, Print["This number is ", k, "."]; k = k + 1]\)\)

Example: Prime Factorization

\(pf[n_Integer] := 
      \((k = n; m = 1; 
		
        While[And[k != 1, k >= Sqrt[Prime[m]]], 
			
          If[Mod[k, Prime[m]] == 0, 
				k = k/Prime[m]; 
            Print[Prime[m]], 
				m = m + 1]]; )\)\)

pf[257931]

The "Module" Command

If others use your notebook, you want to hide auxiliary variables from your users; this can be done via the "Module" command. 
The syntax is a s follows: "Module[{local variable1, local variable2}, commands]"

a = 5

Module[{a}, a = Sqrt[2]; Print[N[a]]]

a

Let's try this for the Prime Factorization Program:

\(pf[n_Integer] := 
      \((k = n; m = 1; 
		
        While[And[k != 1, k >= Sqrt[Prime[m]]], 
			
          If[Mod[k, Prime[m]] == 0, 
				k = k/Prime[m]; 
            Print[Prime[m]], 
				m = m + 1]]; )\)\)

Clear[k, m]pf[1234]

k

m

\(pfm[n_Integer] := 
	
      Module[{k = n, m = 1}, 
		
        While[And[k != 1, k >= Sqrt[Prime[m]]], 
			
          If[Mod[k, Prime[m]] == 0, 
				k = k/Prime[m]; 
            Print[Prime[m]], 
				m = m + 1]]]\)

Clear[k, m]pfm[1234]

k\(m
\)

Replacement Rules

Using a replacement rule is a convenient way to extract results of a "Solve" command:

sol = Solve[2  x^2 - 3  x - 7 == 0, x]

sol[[1]]

x = x /. sol[[1]]

Replacing x by "sol" gives you a list of the solutions:

x = x /. sol

A replacement rule can also be used for algebraic substitutions.
The next example shows how to "depress" a cubic equation:

\(Clear[a, x]; 
eqn = a\ x^3 + b\ x^2\  + c\ x + d\)

eqn = eqn /. {x -> y - b/(3  a)}

Collect[eqn, y]

Another example :

Show[
      Graphics[Table[{AbsolutePointSize[10], Black, Point[{i, 0}]}, {i, 1, 
            10}]], AspectRatio -> 1/10];

k = Table[Random[Integer], {10}]

pic = 
      Show[Graphics[
          Table[{AbsolutePointSize[10], k[[i]] /. {1 -> Red, 0 -> Gray}, 
              Point[{i, 0}]}, {i, 1, 10}]], AspectRatio -> 1/10, 
        PlotRange -> {-1, 2}];

Etc

\(Needs["Graphics`Master`"]; 
Needs["Calculus`Master`"]; \)

Helmut Knaust, email: helmut@math.utep.edu. Created 3-28-1999.