File Lesson_1.red artifact ab530e162b on branch master


COMMENT

                  REDUCE INTERACTIVE LESSON NUMBER 1

                         David R. Stoutemyer
                         University of Hawaii


COMMENT This is lesson 1 of 7 interactive lessons about the REDUCE
system for computer symbolic mathematics.  These lessons presume an
acquaintance with elementary calculus, together with a previous
exposure to some computer programming language.

In REDUCE, any sequence of characters from the word "COMMENT" through
the next semicolon or dollar-sign statement separator is an
explanatory remark ignored by the system.  In general, either
separator signals the end of a statement, with the dollar sign
suppressing any output that might otherwise automatically be produced
by the statement.  The typing of a carriage return initiates the
immediate sequential execution of all statements which have been
terminated on that line.  When REDUCE is ready for more input, it will
prompt you with a prompt number followed by a colon and a space at the
left margin.

Expressions can be formed using "^", "*", "/", "+", and "-" to
indicate exponentiation, multiplication, division, addition, and
subtraction or negation respectively.  Assignments to variables can be
done using the operator ":=".  For example:;

r2d2 := (987654321/15)^3;

COMMENT The immediately preceding line, without a semicolon, is the
computed output generated by the line with a semicolon which precedes
it.  Note that exact indefinite-precision rational arithmetic was
used, in contrast to the limited-precision arithmetic of traditional
programming languages.

We can use the name R2D2 to represent its value in subsequent
expressions such as;

r2d2 := -r2d2/25 + 3*(13-5);

COMMENT We could equally well write the name of this variable as R2D2,
r2D2 or R2d2 since REDUCE is case-insensitive.  Using this
flexibility, in these lessons code discussed within the text will be
written in upper case to make it stand out, but code to be actually
executed by REDUCE will be written in lower case.

Now I will give you an opportunity to try some analogous computations.
To do so, type the letter n or N followed by a carriage return in
response to the question "Cont?".  (You could type y or Y if you wish
to relinquish this opportunity, but I strongly recommend reinforced
learning through active participation.)  After trying an example or
two, type the command "cont" (with any capitalization) terminated by a
semicolon and carriage return when you wish to proceed with the rest
of the lesson.  To avoid interference with our examples, please don't
assign anything to any variable names beginning with the letters E
through I.  To avoid lengthy delays, I recommend keeping all of your
examples approximately as trivial as ours, saving your more ambitious
experiments until after the lesson.  If you happen to initiate a
calculation requiring an undue amount of time to evaluate or display,
you can abort that computation with an interrupt (Ctrl-C) to terminate
REDUCE.  Restart REDUCE, followed by the statement "in less1.red",
followed by a semicolon and return, to restart the lesson at the
beginning.;

pause;

COMMENT Now watch this example illustrating some more dramatic
differences from traditional scientific programming systems:;

e1 := 2*g + 3*g + h^3/h;

COMMENT Note how we are allowed to use variables to which we have
assigned no values!  Note too how similar terms and similar factors
are combined automatically.  REDUCE also automatically expands
products and powers of sums, together with placing expressions over
common denominators, as illustrated by the examples:;

e2 := e1*(f+g);
e2 := e1^2;
e1+1/e1;

COMMENT Our last example also illustrates that there is no need to
assign an expression if we do not plan to use its value later.  Try
some similar examples.;

pause;

COMMENT It is not always desirable to expand expressions over a common
denominator, and we can use the OFF statement to turn off either or
both computational switches which control these transformations.  The
switch named EXP controls EXPansion, and the switch named MCD controls
the Making of Common Denominators;

off exp, mcd;
e2 := e1^2$
e2 := e2*(f+g) + 1/e1;

COMMENT To turn these switches back on, we type:;

on exp, mcd;

COMMENT Try a few relevant examples with these switches turned off
individually and jointly;

pause;

on exp;  % Just in case you turned it off!

COMMENT The "%" character is another way to introduce a comment that
extends as far as the end of the line.

Now consider the example:;

e2 := (2*(f*h)^2 - f^2*g*h - (f*g)^2 - f*h^3 + f*h*g^2 - h^4
       + g*h^3)/(f^2*h - f^2*g - f*h^2 + 2*f*g*h - f*g^2
       - g*h^2 + g^2*h);

COMMENT It is not obvious, but the numerator and denominator of this
expression share a nontrivial common divisor which can be canceled.
To make REDUCE automatically cancel greatest common divisors, we turn
on the computational switch named GCD:;

on gcd;
e2;

COMMENT The switch is not on by default because

    1.  It can consume a lot of time.
    2.  Often we know in advance the few places where a nontrivial
        GCD can occur in our problem.
    3.  Even without GCD cancellation, expansion and common
        denominators guarantee that any rational expression which is
        equivalent to zero simplifies to zero.
    4.  When the denominator is the greatest common divisor, such
        as for  (X^2 - 2*X + 1)/(X - 1),  REDUCE cancels the
        greatest common divisor even when GCD is OFF.
    5.  GCD cancellation sometimes makes expressions more
        complicated, such as with  (F^10 - G^10)/(F^2 + F*G - 2*G^2).

Try the examples mentioned in this comment, together with one or two
other relevant ones.;

pause;

COMMENT Exact rational arithmetic can consume an alarming amount of
computer time when the constituent integers have quite large
magnitudes, and the results become awkward to interpret qualitatively.
When this is the case and somewhat inexact numerical coefficients are
acceptable, we can have the arithmetic done using floating point by
turning on the computational switch ROUNDED.  With this switch on, any
non-integer rational numbers are approximated by floating-point
numbers, and the result of any arithmetic operation is floating-point
when any of its operands is floating point.  For example:;

on rounded;
e1 := (12.3456789e3*f + 3*g)^2 + 1/2;

COMMENT With ROUNDED off, any floating-point constants are
automatically represented as explicit rational numbers:;

off rounded;
e1 := 12.35*g;
pause;

COMMENT A number of elementary functions, such as SIN, COS and LOG,
are built into REDUCE.  Moreover, the letter E represents the base of
the natural logarithms, so the exponentiation operator enables us to
represent the exponential function as well as fractional powers.  For
example:;

e1:= sin(-f*g) + log(e) + (3*g^2*cos(-1))^(1/2);

COMMENT What automatic simplifications can you identify in this
example?

Note that REDUCE never approximates the values of these functions,
even for numerical arguments, and exact computations are generally
impossible for non-trivial numerical arguments.

Experimentally determine some other built-in simplifications for these
functions.;

pause;

COMMENT Later you will learn how to introduce additional
simplifications and additional functions.  To compute numerical
approximations for examples such as COS(1) simply turn on ROUNDED.

Differentiation is also built-into REDUCE.  For example, to
differentiate E1 with respect to F:;

e2 := df(e1,f);

COMMENT To compute the second derivative of E2 with respect to G, we
can type either DF(E2,G,2) or DF(E1,F,1,G,2) or DF(E1,F,G,2) or
DF(E1,G,2,F,1) or;

df(e1,g,2,f);

COMMENT Surely you can't resist trying a few derivatives of your own!
(Careful, high-order derivatives can be alarmingly complicated.);

pause;

COMMENT REDUCE uses the name I to represent (-1)^(1/2), incorporating
some simplification rules such as replacing I^2 by -1.  Here is an
opportunity to experimentally determine other simplifications such as
for I^3, 1/I^23, and (I^2-1)/(I-1).;

pause;

COMMENT Clearly it is inadvisable to use E or I as a variable.  T is
also inadvisable for reasons that will become clear later.

The value of a variable is said to be "bound" to the variable.  Any
variable to which we have assigned a value is called a bound variable,
and any variable to which we have not assigned a value is called an
indeterminate.  Occasionally it is desirable to make a bound variable
into an indeterminate, and this can be done using the CLEAR command.
For example:;

clear r2d2, e1, e2;
e2;

COMMENT If you suspect that a degenerate assignment, such as E1:=E1,
would suffice to clear a bound variable, try it on one of your own
bound variables.;

pause;

COMMENT REDUCE also supports matrix algebra, as illustrated by the
following:;

matrix e1(4,1), f;
e1;  f;

COMMENT This declaration establishes E1 as a matrix with 4 rows and 1
column, while establishing F as a matrix of unspecified size.  To
establish element values (and sizes if not already established in the
MATRIX declaration), we can use the MAT function, as illustrated by
the following example:;

h := mat((log(g), g+3), (g, 5/7));

COMMENT Only after establishing the size of a matrix by declaring it
or executing a matrix assignment can we refer to an individual element
or to the matrix as a whole.  For example to increase the last element
of H by 1 then form twice the transpose of H, we can type:;

h(2,2) := h(2,2) + 1;
2*tp(h);

COMMENT To compute the determinant of H:;

det(h);

COMMENT To compute the trace of H:;

trace(h);

COMMENT To compute the inverse of H, we can use H^(-1) or 1/H.  To
compute the solution to the equation H*F = MAT((G),(2)), we can
left-multiply the right-hand side by the inverse of H:;

f := 1/h*mat((g),(2));

COMMENT Notes:

   1.  MAT((G),(2))/H would denote right-multiplication by the
       inverse, which is not what we want.
   2.  Solutions for a set of right-hand-side vectors are most
       efficiently computed simultaneously by collecting the right-
       hand sides together as the columns of a single multiple-column
       matrix.
   3.  Sub-expressions of the form 1/H*... or H^(-1)*... are computed
       more efficiently than if the inverse is computed separately in
       a previous statement, so separate computation of the inverse
       is advisable only if several solutions are desired and if
       they cannot be computed simultaneously.
   4.  MAT must have parentheses around each row of elements even if
       there is only one row or only one element per row.
   5.  References to individual matrix elements must have exactly two
       subscripts, even if the matrix has only one row or one column.

Congratulations on completing lesson 1!  I urge you to try a sequence
of more ambitious examples for the various features that have been
introduced, in order to gain some familiarity with the relationship
between problem size and computing time for various operations.  (The
command "ON TIME" causes computing time to be output.)  I also urge
you to bring to the next lesson appropriate examples from textbooks,
articles, or elsewhere, in order to experience the decisive learning
reinforcement afforded by meaningful personal examples that are not
arbitrarily contrived.

To avoid the possibility of interference from assignments and
declarations in lesson 1, it is wise to execute lesson 2 in a fresh
REDUCE session, when you are ready.

;end;


iCAS Bundled REDUCE Scripts
Homepage | GitHub Mirror | SourceHut Mirror | NotABug Mirror | Chisel Mirror | Chisel RSS ]