\documentclass[a4paper,11pt]{article}
\RequirePackage[margin=2cm]{geometry}
%\RequirePackage[pdftex]{graphicx}
\RequirePackage{mathtools} % loads amsmath
\RequirePackage{amsfonts}
\RequirePackage{breqn} % for d[group|math]
\RequirePackage[utf8]{inputenc}
\RequirePackage[unicode,pdftex,bookmarks,colorlinks,linkcolor=blue]{hyperref}
\RequirePackage{listings}
\RequirePackage{framed}

\def\xcolorcmd{\colorlet{black}{blue}}
\usepackage{xcolor}

\newcommand{\reduce}{REDUCE}

%% formatting
% use a san-serif font for online display
\renewcommand\familydefault{cmss}
\usepackage{parskip}

\pagestyle{plain}
\author{Martin Gregory}

\title{A Brief Overview of \reduce \\
  Highlighting Differences to R}
%\VignetteIndexEntry{A Brief Overview of REDUCE Highlighting Differences to R}

\begin{document}
\maketitle
\tableofcontents

\section{Introduction}
\label{sec:Introduction}
The aim of this document is to provide a brief introduction to \reduce{} for R users
unfamiliar with \reduce, highlighting differences between the two
environments. This vignette is neither a reference to nor a tutorial for using \reduce, for
these purposes see the online \reduce{} User's Manual~\cite{manual} or the online
Tutorials~\cite{Tutorials}.

\reduce{} is an open source, portable, general-purpose, computer algebra system. The first
versions date from the early 1960's as a class of formal computations for problems in high
energy physics which are difficult to do and verify manually. Today the system has
expanded its scope significantly, is actively developed and has many packages contributed
by users.

\reduce{} is written in Standard LISP of which there are several implementations. It
consist of two distinct but inter-related modes: algebraic and symbolic. The algebraic
mode is written in symbolic mode and both modes support LISP features using a simpler,
ALGOL-based syntax. In general users who are not developing \reduce{} packages have no
need for symbolic mode, but it can be used to improve performance of complicated
calculations. Futher details are beyond the scope of this document but interested readers
can find details in Chapter 21 of the \reduce{} User Manual~\cite{symprimer} and more in
the \reduce{} Symbolic Mode Primer~\cite{symprimer}.

Conventions used in this vignette: 
\begin{itemize}
\item differences are highlighted by a {\color{red}$\star$} in the margin 
\item \reduce{} keywords are rendered in italic upper case in the text and roman upper case in
examples
\item when examples show both commands and output, the commands are numbered. If only commands
are shown, no numbers are used.
\end{itemize}

\section{Terminology}
\label{sec:terminology}
	Before going into details it is helpful to clarify some \reduce{} terminology.

\begin{description}
\item [command] refers to a certain type of statement which affect the environment, for
  example {\it LOAD\_PACKAGE}, {\it QUIT}, {\it CLEAR}.
\item [log] is the \reduce{} equivalent of the R transcript. In contrast to R, input is
  preceded by an input line number.
\item [operator] is used in four different contexts:
  \begin{itemize}
  \item a {\it prefix operator} is a function and takes one or more arguments and is
    written \texttt{ f(a1, ... aN)}. In the case of functions with a single argument, the
    parenthesis may be omitted,\marginpar{\color{red}$\star$} e.g. {\texttt sin(x)} and {\texttt sin
      x} are identical. Most commands and declarations may be written as prefix operators,
    i.e. with parentheses;
  \item a {\it boolean operator} is a unary logical operator, e.g. {\it evenp} is true if
    its argument is even. Boolean operators may only\marginpar{\color{red}$\star$} be used
    in {\it IF}, {\it FOR}, {\it WHILE} or {\it UNTIL} statements;
  \item an {\it infix operator} is an operator which takes two arguments with the operator
    between the arguments. Arithmetic and comparison operators are examples;
  \item a variable may be declared to be an operator in which case it may take
    arguments. For an example of using this in combination with let to define mathematical
    functions with specific properties, see section~\ref{sec:oper-subst}.

  \end{itemize}
\item [procedure] is equivalent to the R function {\it function}. Procedures are
  automatically declared as prefix operators.
\item [switch] is an  option with only logical values.
\item [kernel] is, naively, an expression which cannot be further simplified. See
  \href{https://reduce-algebra.sourceforge.io/manual/manualse55.html#x66-1200008.1}{Section~8.1}
  of the \reduce{} User's Manual for details.
  
\end{description}

\section{Summary of Important Differences}
\label{sec:summ-import-diff}
If using {\it redcas} without any programming in \reduce, there are some differences to be
aware of:
\begin{enumerate}
\item names consist of a letter followed by any combination of letters, decimal digits and
  underscore;
\item the variable names $e$, $i$ and $pi$ are reserved for the numbers $e$, $\sqrt{-1}$
  and $\pi$, respectively;
\item names in \reduce{} are case-insensitive by default;
\item the operator $:=$ is used for assignment, $=$ is the comparison operator;
\item statements require a terminator, either semi-colon or dollar sign.
\end{enumerate}

%% NOTE: select == lapply

\section{Language Elements}
\label{sec:data-types}

\subsection{Names, symbols and variables}
\label{sec:variables}
\begin{enumerate}
\item the terms identifier and symbol are the equivalent of name and symbol in R;
\item variable names consist of a letter followed by any combination of
  letters, \marginpar{\color{red}$\star$}decimal digits and underscore. Other symbols may be used in
  variable names if they are escaped using the exclamation mark. Since \reduce{} does not
  have a namespace concept, internally this feature is used extensively;
\item variables do not need to be declared but also do not need\marginpar{\color{red}$\star$} to
  have a value. A variable to which no value has been assigned is known as unbound and can
  represent a variable in the mathematical sense;
\item apart from literal strings and characters preceded by an exclamation mark, \reduce{} is
  case-insensitive. In order to maintain mixed case in identifiers it is necessary to
  precede upper case letters with the exclamation mark;
\item \reduce{} has a large number of reserved names covering commands, operators,
  variables and switches. The most relevant reserved names, in alphabetical order, are:
  \begin{description}
  \item [e] the base of natural logarithms. If the {\it ROUNDED} switch is on it has the
    value of $e$ to the current degree of floating point precision;
  \item [Golden\_Ratio] the number $(1+\sqrt{5})/2$;
  \item [i] the square root of $-1$. In $i^n$, $i^2$ is replaced by $-1$. As a formal
    parameter to a procedure, as a local variable or as the index variable in a {\it for}
    statement $i$ may be used as a variable although this prevents its use in complex
    numbers in those contexts;
  \item [infinity] represents $\infty$ in limit, power series and definite integration
    calculations. \reduce{} does not handle arithmetic with {\it infinity}, treating it as
    any unbound variable, e.g. {\it infinity - infinity} returns 0;
  \item [negative] used in the {\it roots} package;
  \item [nil] in symbolic mode, logical false, in algebraic mode, a synonym for zero which
    is also logical false;
  \item [pi] represents $\pi$. If the {\it ROUNDED} switch is on it has the
    value of $\pi$ to the current degree of floating point precision;
  \item [positive] used in the {\it roots} package;
  \item [t] logical true.
  \end{description}
\end{enumerate}

\subsection{Numbers}
\begin{description}
\item [integers] are of arbitrary size;\marginpar{\color{red}$\star$}
\item [rational numbers] are stored as the quotient of two integers and are therefore also
  of arbitrary size;\marginpar{\color{red}$\star$}
\item [floats] are known as rounded numbers.\marginpar{\color{red}$\star$} The precision of the
  mantissa defaults to 50 but may be set to any value using the {\it precision}
  declaration. A value of 0 displays the current precision. The exponent, being an
  integer, may be of arbitary size. By default, \reduce{} does not use floats. Setting the
  switch {\it ROUNDED} to on enables floats;
\item [complex numbers] have the form $a + b*i$ \marginpar{\color{red}$\star$}where $i$ is a
  reserved identifier with the property $i^2=-1$. The real and imaginary parts may be
  numbers or variables.
\item [scientific notation] is supported both for input and output. Output is controlled via
  the declaration {\it SCIENTIFIC\_NOTATION};
\item [modular arithmetic] can be turned on using the declaration {\it SETMOD} to define
  the modulus and the {\it MODULAR} switch. When turned on, modular arithmetic also
  applies to the coefficients of polynomials. Note that a {\it SETMOD} declaration is
  required before turning the {\it MODULAR} switch on.
\end{description}

\subsection{Strings}
\label{sec:strings}
Strings are very sparingly used in \reduce{} and so the features for manipulating them are
limited to the {\it WRITE} command. Symbolic mode provides standard Lisp features for
string handling. Details can be found in Chapter 21 of the \reduce{} User
Manual\cite{manual} or in the \reduce{} Symbolic Mode Primer\cite{symprimer} 
\begin{enumerate}
\item Literal strings are identified using double quotes;
\item double quotes are escaped within a string by doubling the double quote;\marginpar{\color{red}$\star$}
\item single quotes are generally only used in symbolic mode \marginpar{\color{red}$\star$} in the
  same way as in LISP, for example, {\texttt 'a} is the equivalent of {\texttt (quote a)}.
\end{enumerate}


\subsection{Expressions}
\label{sec:expressions}
Mathematical expressions are similar to R, combining numbers, variables and functions using
the operators {\bf +, -, *, /} and {\bf \textasciicircum or ** } and parentheses for
precedence.

\subsection{Aggregate data types}
\label{sec:aggregate-data-types}
There are several aggregate data types:
\begin{description}
\item [array] declares one or more arrays \marginpar{\color{red}$\star$} including
  their dimensions using the syntax
  \begin{quote}
    ARRAY a1(d1[,d2[,...[,dN]]]), a2(d1[,d2[,...[,dM]]]), ... ;
  \end{quote}
  Unlike R, array indexing is {\bf zero-based}, so a declared\marginpar{\color{red}$\star$}
  dimension size of $n$ means an actual dimension size of $n+1$. Furthermore, declaring an
  array causes all its element to be set to zero.  Elements may be numbers, identifiers,
  expressions or strings and elements of different types are allowed in the same array.
  Arrays are global even when they are declared within a function. Each element of an
  array must be assigned separately, possibly via nested loops. Assigning an array to a
  variable or to the name of another declared array has no effect: the assigned variable
  is unbound. Unlike R\marginpar{\color{red}$\star$}, \reduce{} arrays have no print method;
\item [equation] consists of two expressions separated by $=$. Functions {\it LHS} and
  {\it RHS} return the left and right hand side of the equations, respectively. Equations
  may be added, subtracted, simplified, solved, differentiated and integrated.
\item [list] as in R may contain any data type.\marginpar{\color{red}$\star$} Lists are displayed
  using braces and assigned using the {\it LIST} function or braces. The package {\it
    ASSIST}\footnote{\textit{ASSIST} must be explicitly loaded. {\it redcas} does this during
  session start} provides some essential functions for working with lists. List elements
  cannot easily be named;
\item [matrix] elements may be numbers, identifiers or expressions but may not be
  strings. Similar to the {\it ARRAY} declaration, the {\it MATRIX} declaration declares
  one or more matrices with the dimensions and sets all elements to zero. Matrix indexing,
  in contrast to arrays, is {\bf 1-based}. In addition the {\it MAT} function can be used
  to assign all values to a matrix in a single statement and, if necessary, declare
  it. Elements may also be individually assigned. Functions to calculate the determinant,
  eigenvalue equation and eigenvectors, transpose, trace, cofactors, rank and linear basis
  of a matrix are available. Unlike arrays, matrices have a print method;
\item [tensor] the user-contributed packages {\it ATENSOR}, {\it CANTENS}, {\it DUMMY},
  {\it EXCALC}, {\it FIDE} and {\it PHYSOP} provide support for tensor calculations.
\item [avector] the user-contributed packages {\it ASSIST}, {\it AVECTOR}, {\it EXCALC},
  {\it ORTHOVEC}, {\it PHYSOP} and {\it SPARSE} provide support for vector calculations.
\end{description}

\subsection{Comments}
\label{sec:comments}
\reduce{} provides three different ways to comment code:
\begin{itemize}
\item the \textit{COMMENT} statement, terminated by semi-colon or dollar sign allows multi-line
  comments;
\item anything following a \% sign until the end of the line is ignored;
\item The C-style multi-line comment (\verb+/* ... */+) is supported.
\end{itemize}

\section{Statements}
\label{sec:statements}

Statements require a terminator which may be the semicolon or dollar sign. These differ in
that a dollar terminator suppresses displaying the input even if the \textit{ECHO} switch is
on. 

\subsection{Assignment statements}
\label{sec:assignm-stat}
Assignment of variables is done using the operator $:=$. The symbol $=$ is either a
comparison operator or part of an equation and is not used for assignment.  As in R,
assignment statements may be used as targets of assignments.  For details see
\href{https://reduce-algebra.sourceforge.io/manual/manualse14.html#x22-340005.1}{Section~5.1} of
the \reduce{} User's Manual.

\subsection{Expression as statement}
\label{sec:exprn-stmt}
As in R, an expression can serve as a statement causing the result of evaluating the
expression to be displayed. If the expression is an array only the array name is printed
as there is no print method for arrays.

\subsection{Group and Compound Statements}
\label{sec:group-comp-stat}
\reduce{} has two different ways of using multiple statements where syntax requires a single
statement:
\begin{description}
  
\item [{\it GROUP}] statement uses the syntax \verb+<<stmt1;+ \ldots \verb+; stmtN>>+ and
  is equivalent to braces in R. For example:
\begin{verbatim}
  IF x>1 THEN <<x:=x-1; WRITE "x is ", x>> ELSE WRITE x, " is not greater than 1";
\end{verbatim}
The group statement returns the value of the \marginpar{\color{red}$\star$} last expression unless
this expression is followed by a terminator prior to the closing \verb+>>+ in which case
the value is zero.
\item [compound] statement,\marginpar{\color{red}$\star$} also know as block statement,
  has the form \texttt{BEGIN} \ldots \texttt{END} and allows for declaration of local
  variables and can use a {\it RETURN} statement to explicitly exit the block. As an
  example consider
\begin{verbatim}
a:=1 ; b:=2 ;
BEGIN SCALAR a ; % a is local to block, initialized to 0
   b:=a+b+25 ;
   IF a=1 THEN RETURN ;
   WRITE "In block a is ", a, ", b is ", b 
END ;
WRITE "After block a is ", a, ", b is ", b ;
\end{verbatim}
Because of the  {\it SCALAR} declaration $a$ is local to the block and is initialized to
zero. $b$ is accessible from the block. The result of execution is:
\begin{verbatim}
In block a is 0, b is 27
After block a is 1, b is 27
\end{verbatim}
In addition to {\it SCALAR}, {\it INTEGER} and {\it REAL} may also be used.
\end{description}
Both types require a terminator after the block, but not on the last statement of the
block.  For group statement see
\href{https://reduce-algebra.sourceforge.io/manual/manualse15.html#x23-360005.2}{Section~5.2}, for
compound statements
\href{https://reduce-algebra.sourceforge.io/manual/manualse20.html#x28-410005.7}{Section~5.7} of
the \reduce{} User's Manual.

\subsection{Conditional Statement}
\label{sec:if}
Syntax is similar R with two exceptions: a \textit{THEN} keyword is required and using
parentheses around the condition is optional:
\begin{quote}
  IF {\it condition} THEN {\it statement} [ELSE {\it statement}] ;\marginpar{\color{red}$\star$}
\end{quote}
The condition can be a boolean, numeric or scalar expression. In the non-boolean case, any
value other than zero is true.  Note that there is no terminator after the \textit{THEN}
statement if there is an \textit{ELSE} statement. Group statements may be used. The \textit{IF}
statement returns the value of the last statement executed and may be part of an
expression in the target of an assignment. For example:
\begin{verbatim}
a:=IF x<5 THEN 123 ELSE 456;
b:=u + v^(IF NUMBERP z THEN 10*z ELSE 1) + w;
\end{verbatim}
If no action is to be performed when the condition is true, the \textit{THEN} statement may be
omitted.\marginpar{\color{red}$\star$} For example
\begin{verbatim}
  IF x=1 THEN ELSE WRITE "not 1" ;
\end{verbatim}

If the \textit{THEN} keyword is omitted and an \textit{ELSE} keyword is present, \reduce{}
responds\marginpar{\color{red}$\star$} with the following rather confusing error message
\begin{verbatim}
  ***** else invalid in if statement
\end{verbatim}
If the \textit{THEN} keyword is omitted and no \textit{ELSE} keyword is present the
message is:
\begin{verbatim}
  ***** ; invalid in if statement
\end{verbatim}

For details see \href{https://reduce-algebra.sourceforge.io/manual/manualse16.html}{Section~5.3}
of the \reduce{} User's Manual.


\subsection{\textit{FOR} statement}
\label{sec:for}
The syntax differs significantly from that of R and is as
follows:\marginpar{\color{red}$\star$}
\begin{quote}
  FOR {\it interation-expression} {\it action} {\it expression}
\end{quote}
In the case of indexed iteration {\it interation-expression} has two forms
\begin{quote}
  {\it var} := {\it number} STEP {\it number} UNTIL {\it number} \\
  {\it var} := {\it number} : {\it number}
\end{quote}
with the latter case assuming the step is 1. {\it number} may also be an expression which
resolves to a number. In the case of interating down a list the {\it
  interation-expression} has the form
\begin{quote}
  EACH {\it var} IN|ON {\it list}
\end{quote}

There are five different actions:
\begin{description}
\item [{\it DO}] executes the expression for each iteration. This action does not return a value;
\item [{\it PRODUCT}] returns the product of all expressions;
\item [{\it SUM}] returns the sum of all expressions;
\item [{\it COLLECT}] returns a list of the expressions;
\item [{\it JOIN}] expects the result of each expression to be a list and returns a list of
  these lists.
\end{description}
For details see 
\href{https://reduce-algebra.sourceforge.io/manual/manualse17.html#x25-380005.4}{Section~5.4} of
the \reduce{} User's Manual.

\subsection{\textit{WHILE} \ldots \textit{DO}}
\label{sec:while-do}
The {\it WHILE} statement differs from R\marginpar{\color{red}$\star$} in that it requires a {\it
  DO} keyword before the statement:
\begin{verbatim}
  WHILE condition DO statement ;
\end{verbatim}
The statement can be a group or compound statement. The condition is tested at the start
of each loop. {\it WHILE} does not return a value and the only explicit
statement\marginpar{\color{red}$\star$} for prematurely ending the loop is {\it GO TO} or
its alias {\it GOTO}. For details on {\it WHILE} see
\href{https://reduce-algebra.sourceforge.io/manual/manualse18.html#x26-390005.5}{Section~5.5} of
the \reduce{} User's Manual and for {\it GO TO} see
\href{https://reduce-algebra.sourceforge.io/manual/manualse20.html#x28-410005.7}{Section~5.7.2}.

\subsection{\textit{REPEAT} \ldots \textit{UNTIL}}
\label{sec:repeat-until}
The {\it REPEAT} statement differs from  R\marginpar{\color{red}$\star$} in that it uses a
condition. Essentially it is the same as {\it WHILE} with the exception that the condition
is tested after each loop execution. 
\begin{verbatim}
  REPEAT statement UNTIL condition ;
\end{verbatim}
The statement can be a group or compound statement. Like {\it WHILE} no value is returned
and prematurely ending the loop can only be done using {\it GO TO}.  For details see
\href{https://reduce-algebra.sourceforge.io/manual/manualse19.html#x27-400005.6}{Section~5.6} of
the \reduce{} User's Manual.


\subsection{\textit{RETURN}}
\label{sec:return}
By default the value returned from a compound statement is zero. \marginpar{\color{red}$\star$} The
\textit{RETURN} statement can be used to return the result of an expression from a compound
statement and control when the compound statement exits. It can be used
\begin{itemize}
\item directly within a compound statement,
\item in a group statement within a compound statement, or
\item in a conditional statement within a compound statement.
\end{itemize}
In all three cases it exits the compound statement. The syntax is
\begin{verbatim}
  RETURN [expression] ;
\end{verbatim}
Omitting the expression returns zero.  For details see
\href{https://reduce-algebra.sourceforge.io/manual/manualse20.html#x28-410005.7}{Section~5.7.3} of
the \reduce{} User's Manual.

\section{Commands}
\label{sec:commands}
Commands are statements which influence the \reduce{} environment, for example, loading
packages or determining what simplification methods to use.
\begin{description}
\item [{\it CLEAR}] clears the value and any attributes of a variable, a component of an array,
  list or matrix or user-defined procedure.
\item [{\it RESETREDUCE}] removes all declarations, clears all variables and user-defined
  procedures, restores all switches to their default setting and resets the line number to
  one. Does not unload packages.
\item [{\it LOAD\_PACKAGE}] loads one or more packages.\marginpar{\color{red}$\star$} If
  multiple packages are to be loaded package names are separated by commas.  Unlike R,
  this command is only needed for user-contributed packages\marginpar{\color{red}$\star$}
  -- all system packages are either loaded at session start or automatically when one of
  their functions are called.
\item [{\it ON / OFF}] turn a switch on or off. See section~\ref{sec:options} below.
\item [{\it BYE / QUIT}] is used to terminate a session. For CSL, at least on UNIX-like
  operating systems, {\it control D} also terminates a session.
\item [{\it file handling commands}] There are three file handling commands:
  \begin{description}
  \item [{\it IN}] is the equivalent of the R {\it source} function. It loads a file containing
    \reduce{} commands. The file must end with the statement {\it ;END;}. The only
    parameter is a string naming the file. If the file name is in a variable F, the syntax
    \verb+<<F>>+ may be used.
  \item [{\it OUT}] is the equivalent of the R {\it sink} function. It takes a single parameter
    which is either the name of a file or \verb+<<F>>+ where F is a variable containing
    the name of the file. Further \textit{OUT} commands change the file to be written to
    but\marginpar{\color{red}$\star$} do not close previously opened files. Note that output
    redirected by the \textit{OUT} command is not available when using {\it redcas}.
  \item [{\it SHUT}] closes an output file specified by the name of a file or \verb+<<F>>+
    where F is a variable containing the name of the file.
  \end{description}
  File handling commands are documented in
  \href{https://reduce-algebra.sourceforge.io/manual/manualch12.html#manualse87.html}{Section
    12} of the \reduce User's 
  Manual.
\item [{\it SHOWTIME}] writes the CPU time used since the last call of this command to the
  \reduce{} log. On the first call it writes the CPU time used since the start of the
  session. The time is displayed to the nearest millisecond. See
  \href{https://reduce-algebra.sourceforge.io/manual/manualse25.html#x34-500006.5}{Section
    6.5} of the \reduce{} User's Manual for a description of using the Lisp {\it time}
  function which returns the number of milliseconds since the session started and may be
  assigned to a variable.
\item [{\it SHOW}] provides some limited information similar to R's {\it ls} function and is
  part of the \textit{ASSIST} package. \textit{SHOW} displays a list of objects specified by a (required)
  parameter which can take the values {\it ALL, SCALARS, ARRAYS, LISTS, MATRICES, VECTORS}
  or {\it FORMS}.
\end{description}


\section{Declarations}
\label{sec:declare}

Declarative statements assign a property or relationship to one or more
variables. Important declarations are:
\begin{description}
\item [{\it ARRAY}] described in section~\ref{sec:aggregate-data-types} above.
\item [{\it DEPEND}] declares a variable to be dependent on one or more other variables. This is
  relevant for functions such as differentiation: if an unbound variable is not declared
  dependent on the differentiating variable, it is treat as constant in this context and
  the result is zero. The declaration {\it nodepend} is used to remove dependencies. The
  operator \textit{DEPATOM} provided by \textit{ASSIST} returns a lists of the dependencies of its argument;
\item [{\it LET}] defines global substitution rules allowing one to influence how expressions
  are simplified. See section~\ref{sec:oper-subst} below;
\item [{\it MATRIX}] declares one or more variables to be matrices. Unlike R a two dimensional
  array is not a matrix;\marginpar{\color{red}$\star$}
\item [{\it OPERATOR}] declares a variable to be a function or operator in the mathematical
  sense, i.e. it may take arguments; \marginpar{\color{red}$\star$}
\end{description}
Other declarative statements may be defined by user-contributed packages.

\section{Options}
\label{sec:options}
While R provides a single feature\marginpar{\color{red}$\star$} (options) \reduce{} uses
three different ways to achieve the same effects.

The first feature is a {\it SWITCH} which, as the name suggests, is boolean. There are
over two hundred switches affecting the behaviour of the system, some of which are defined
in user-defined packages and only available if these are loaded. The commands \textit{ON}
and \textit{OFF} are used to set switches\footnote{The {\it swset} procedure supplied with
{\it redcas} can be used to toggle switches while recording the current state which can be
restored using {\it swreset}}. See ``Switch Summary''~\cite{switches} or Appendix A.7 of
the User's Manual~\cite{manual} for a full list. For a list by context, see the vignette
{\it Switches by Context}.

The second feature is to use declarations such as {\it PRECISION}, {\it KORDER} or {\it
  ORDER} with allow one to specify precision, the internal order of variables or the
displayed order of variables in expressions, respectively.

The third feature uses global variables. For example, the {\it DF} operator
(differentiation) uses the variable {\it FANCY\_PRINT\_DF} to determine how to display
derivatives as in the following
\begin{eqnarray*}
  Setting & df(f,x,2,y)  \\
  PARTIAL & \frac{\partial ^3 f}{\partial x^2 \partial y} \\
  TOTAL & \frac{d^3 f}{dx^2 dy} \\
  INDEXED & f_{x,x,y}
\end{eqnarray*}

\section{Operators and Substitution}
\label{sec:oper-subst}
The operator declaration allows a variable to take arguments. For example
\begin{verbatim}
1: OPERATOR f, g ;
2: DF(f(x), x) ;
\end{verbatim}
returns $df(x)/dx$ because $f(x)$ implies that $f$ depends on $x$. \reduce's substitution
features allow one to define attributes of operators. There are two features providing
substitution. The \textit{SUB} operator provides once-off substitution while the
\textit{LET} declaration defines rules which apply whenever an expression in a rule is
encountered. The syntax for \textit{SUB} is
\begin{verse}
  \texttt{SUB(} substitution\_list, expression\texttt{)}
\end{verse}
and the elements of the substitution list are of the form {\it kernel = expression}. If we
want to define $df(x)/dx=g(x)$ in $d^2f(x)/dx^2 + df(x)/dx$ we
could use
\begin{verbatim}
3: SUB(DF(f(x),x)=g(x), DF(f(x),x,2) + DF(f(x),x));

df(f(x),x,2) + g(x)
\end{verbatim}
i.e. only the specific term is substituted. To have the substitution made in the second
derivative we use a \textit{LET} rule, the syntax of which is
\begin{verse}
  LET substitution\_list ;
\end{verse}
where {\it substitution\_list} is one or more rules of the following forms
\begin{verse}
  var $=>$ expression \\
  operator(arg,...,arg)  $=>$ expression \\
  arg infix-operator arg $=>$ expression
\end{verse}
Using this we see
\begin{verbatim}
4: LET DF(f(x),x) => g(x) ;
5: DF(f(x),x,2) + DF(f(x),x));

df(g(x),x) + g(x)
\end{verbatim}
If we used a variable other than $x$, no substitution will be made. If we define the rule
as follows it applies to any variable:
\begin{verbatim}
6: LET DF(f(~x),x) => g(x) ;
7: DF(f(y),y,2) + DF(f(y),y));

df(g(y),y) + g(y)
\end{verbatim}

\section{Procedures}
\label{sec:functions}
The \textit{PROCEDURE} statement is the equivalent of {\it function()} in
R.\marginpar{\color{red}$\star$} The syntax is
\begin{verse}
  [type] \textit{PROCEDURE} ([{\it arg, ..., arg}]);  {\it statement} ;
\end{verse}
where the procedure type is one of
\begin{description}
\item [{\it ALGEBRAIC}] is the default type and may be omitted,
\item [{\it INTEGER}] an algebraic procedure which returns an integer,
\item [{\it REAL}] an algebraic procedure which returns a real number,
\item [{\it LISP}] creates a symbolic mode procedure, i.e. one which uses lisp code.
\end{description}
Some points to note:
\begin{itemize}
\item procedures are automatically declared as prefix operators;
\item a procedure with exactly two argument can be declared an infix operator using the
{\it   INFIX} declaration;
\item for a multi-statement procedure the {\it BEGIN \ldots END} construct is used;
\item although in some languages procedures don't return a value, in \reduce{} they do. By default
the value is zero (nil in symbolic mode). The \textit{RETURN} statement allows the procedure
to return a specified value and terminate.
\item in the case of integer and real procedures\marginpar{\color{red}$\star$}, returning a value
  which does not match the type is an error;
\item algebraic procedures can only return scalars\marginpar{\color{red}$\star$}. To return a list or
  matrix, instead of \textit{PROCEDURE} use \textit{LISTPROC} or \textit{MATPROC}, respectively. No
  type is required. There is no corresponding procedure for returning arrays. These may be
  declared in the body of a procedure but will be global. Declaring an existing array will
  replace it;
\item procedure  arguments cannot be named\marginpar{\color{red}$\star$};
\item all arguments must be supplied\marginpar{\color{red}$\star$} even if they are empty. It is
  possible to bypass this restriction by using a list as the only argument;
\item creating variables which have not been declared local are
  global. \marginpar{\color{red}$\star$} In this case \reduce{} warns when the procedure is defined.
\end{itemize}
For further details see
\href{https://reduce-algebra.sourceforge.io/manual/manualch15.html#x117-23900015}{Section~15} of
the \reduce{} User's Manual.

%%
%% Bibliography
%%
\begin{thebibliography}{99}

\bibitem{manual}
  \reduce{} Developers.
  \newblock {\href{https://reduce-algebra.sourceforge.io/manual/manual.html}{\em \reduce{} User's Manual}
    [Online; accessed 2026-10-02]}. Also part of the \reduce{} installation.

\bibitem{Tutorials}
  \reduce{} Developers.
  \newblock{\href{https://reduce-algebra.sourceforge.io/tutorials.html}{\em \reduce
      Tutorials} [Online; accessed 2026-10-02]}. Also partially available with the \reduce
  installation. 

\bibitem{switches}
  \reduce{} Developers
  \newblock{\href{https://reduce-algebra.sourceforge.io/switches.html}{Switch Summary} [Online;
      accessed 2026-10-02]}.
  
\bibitem{symprimer} Melenk, H.
  \href{https://reduce-algebra.sourceforge.io/lisp-docs/primer.html}{Symbolic Mode Primer [Online;
      accessed 2026-10-02]}, 2016. Also provided as part of the \reduce{} installation.
    
\end{thebibliography}

\end{document}
