1\section{SOLVE Operator}\ttindextype{SOLVE}{operator}
2\hypertarget{operator:SOLVE}{}
3SOLVE is an operator for solving one or more simultaneous algebraic
4equations. It is used with the syntax:
5\begin{syntax}
6  \texttt{SOLVE}(\meta{EXPRN:algebraic}[,\,\meta{VAR:kernel}|,\,\meta{VARLIST:list
7    of kernels}])\,:\,\textit{list}.
8\end{syntax}
9\texttt{EXPRN} is of the form \meta{expression} or
10\texttt{\{} \meta{expression1},\meta{expression2}, \dots \texttt{\}}.
11Each expression is an
12algebraic equation, or is the difference of the two sides of the equation.
13The second argument is either a kernel or a list of kernels representing
14the unknowns in the system.  This argument may be omitted if the number of
15distinct, non-constant, top-level kernels equals the number of unknowns,
16in which case these kernels are presumed to be the unknowns.
17
18For one equation, \texttt{SOLVE}\ttindextype{SOLVE}{operator} recursively uses
19factorization and decomposition, together with the known inverses of
20\texttt{LOG}, \texttt{SIN}, \texttt{COS}, \texttt{\textasciicircum},
21\texttt{ACOS}, \texttt{ASIN}, and
22linear, quadratic, cubic, quartic, or binomial factors. Solutions
23of equations built with exponentials or logarithms are often
24expressed in terms of Lambert's \texttt{W} function.\index{Lambert's W function}
25This function is (partially) implemented in the special functions package.
26
27\hypertarget{switch:CRAMER}{}
28Linear equations are solved by the multi-step elimination method due to
29Bareiss, unless the switch \sw{CRAMER}\ttindexswitch{CRAMER} is on, in which
30case Cramer's method is used.  The Bareiss method is usually more
31efficient unless the system is large and dense.
32
33Non-linear equations are solved using the Groebner basis
34package\indexpackage{Groebner}\extendedmanual{ (chapter~\ref{GROEBNER})}.
35Users should note that this
36can be quite a time consuming process.
37
38\textit{Examples:}
39\begin{verbatim}
40            solve(log(sin(x+3))^5 = 8,x);
41            solve(a*log(sin(x+3))^5 - b, sin(x+3));
42            solve({a*x+y=3,y=-2},{x,y});
43\end{verbatim}
44
45\hypertarget{operator:ROOT_OF}{}
46\texttt{SOLVE} returns a list of solutions.  If there is one unknown, each
47solution is an equation for the unknown.  If a complete solution was
48found, the unknown will appear by itself on the left-hand side of the
49equation.  On the other hand, if the solve package could not find a
50solution, the ``solution'' will be an equation for the unknown in terms
51of the operator \texttt{ROOT\_OF}\ttindextype{ROOT\_OF}{operator}. If there
52are several unknowns, each solution will be a list of equations for the
53unknowns.  For example,
54\begin{verbatim}
55     solve(x^2=1,x);             -> {X=-1,X=1}
56
57     solve(x^7-x^6+x^2=1,x)
58                            6
59            -> {X=ROOT_OF(X_  + X_ + 1,X_,TAG_1),X=1}
60
61     solve({x+3y=7,y-x=1},{x,y}) -> {{X=1,Y=2}}.
62\end{verbatim}
63\hypertarget{reserved:ROOT_MULTIPLICITIES}{}
64\ttindextype{ROOT\_MULTIPLICITIES}{global variable}
65\ttindextype{SOLVE}{operator!ROOT\_MULTIPLICITIES global variable@\texttt{ROOT\_MULTIPLICITIES} global variable}
66The TAG argument is used to uniquely identify those particular solutions.
67Solution multiplicities are stored in the global variable
68\var{ROOT\_MULTIPLICITIES} rather than the solution list.  The value of this
69variable is a list of the multiplicities of the solutions for the last
70call of \texttt{SOLVE}. \ttindextype{SOLVE}{operator} For example,
71\begin{verbatim}
72       solve(x^2=2x-1,x); root_multiplicities;
73\end{verbatim}
74gives the results
75\begin{verbatim}
76        {X=1}
77
78        {2}
79\end{verbatim}
80
81\hypertarget{switch:MULTIPLICITIES}{}
82If you want the multiplicities explicitly displayed, the switch
83\sw{MULTIPLICITIES}\ttindexswitch{MULTIPLICITIES} can be turned on. For example
84\begin{verbatim}
85        on multiplicities; solve(x^2=2x-1,x);
86\end{verbatim}
87yields the result
88\begin{verbatim}
89        {X=1,X=1}
90\end{verbatim}
91
92\subsection{Handling of Undetermined Solutions}
93When \texttt{SOLVE} cannot find a solution to an equation, it normally
94returns an equation for the relevant indeterminates in terms of the
95operator \texttt{ROOT\_OF}.\ttindextype{ROOT\_OF}{operator}  For example, the expression
96\begin{verbatim}
97        solve(cos(x) + log(x),x);
98\end{verbatim}
99returns the result
100\begin{verbatim}
101       {X=ROOT_OF(COS(X_) + LOG(X_),X_,TAG_1)} .
102\end{verbatim}
103
104\hypertarget{operator:ONE_OF}{}
105\hypertarget{operator:EXPAND_CASES}{}
106An expression with a top-level \texttt{ROOT\_OF} operator is implicitly a
107list with an unknown number of elements (since we don't always know how
108many solutions an equation has).  If a substitution is made into such an
109expression, closed form solutions can emerge.  If this occurs, the {\tt
110ROOT\_OF} construct is replaced by an operator \texttt{ONE\_OF}.\ttindextype{ONE\_OF}{operator}
111At this point it is of course possible to transform the result of the
112original \texttt{SOLVE} operator expression into a standard \texttt{SOLVE}
113solution.  To effect this, the operator \texttt{EXPAND\_CASES}
114\ttindextype{EXPAND\_CASES}{operator} can be used.
115
116The following example shows the use of these facilities:
117\begin{verbatim}
118solve(-a*x^3+a*x^2+x^4-x^3-4*x^2+4,x);
119              2     3
120{X=ROOT_OF(A*X_  - X_  + 4*X_ + 4,X_,TAG_2),X=1}
121
122sub(a=-1,ws);
123
124{X=ONE_OF({2,-1,-2},TAG_2),X=1}
125
126expand_cases ws;
127
128{X=2,X=-1,X=-2,X=1}
129\end{verbatim}
130
131\subsection{Solutions of Equations Involving Cubics and Quartics}
132\hypertarget{switch:FULLROOTS}{}
133\hypertarget{switch:TRIGFORM}{}
134
135Since roots of cubics and quartics can often be very messy, a switch
136\sw{FULLROOTS}\ttindexswitch{FULLROOTS} is available, that, when off (the
137default), will prevent the production of a result in closed form.  The
138\texttt{ROOT\_OF} construct will be used in this case instead.
139
140In constructing the solutions of cubics and quartics, trigonometrical
141forms are used where appropriate.  This option is under the control of a
142switch \sw{TRIGFORM},\ttindexswitch{TRIGFORM} which is normally on.
143
144The following example illustrates the use of these facilities:
145\begin{verbatim}
146let xx = solve(x^3+x+1,x);
147
148xx;
149             3
150{X=ROOT_OF(X_  + X_ + 1,X_)}
151
152on fullroots;
153
154xx;
155
156                           - SQRT(31)*I
157                    ATAN(---------------)
158                            3*SQRT(3)
159{X=(I*(SQRT(3)*SIN(-----------------------)
160                              3
161\end{verbatim}
162\newpage
163\begin{verbatim}
164                      - SQRT(31)*I
165               ATAN(---------------)
166                       3*SQRT(3)
167        - COS(-----------------------)))/SQRT(3),
168                         3
169
170                              - SQRT(31)*I
171                       ATAN(---------------)
172                               3*SQRT(3)
173 X=( - I*(SQRT(3)*SIN(-----------------------)
174                                 3
175
176                         - SQRT(31)*I
177                  ATAN(---------------)
178                          3*SQRT(3)
179           + COS(-----------------------)))/SQRT(
180                            3
181
182      3),
183
184                  - SQRT(31)*I
185           ATAN(---------------)
186                   3*SQRT(3)
187    2*COS(-----------------------)*I
188                     3
189 X=----------------------------------}
190                SQRT(3)
191
192off trigform;
193
194xx;
195                             2/3
196{X=( - (SQRT(31) - 3*SQRT(3))   *SQRT(3)*I
197
198                             2/3    2/3
199     - (SQRT(31) - 3*SQRT(3))    - 2   *SQRT(3)*I
200
201        2/3                           1/3  1/3
202     + 2   )/(2*(SQRT(31) - 3*SQRT(3))   *6
203
204                1/6
205              *3   ),
206
207                          2/3
208 X=((SQRT(31) - 3*SQRT(3))   *SQRT(3)*I
209
210                             2/3    2/3
211     - (SQRT(31) - 3*SQRT(3))    + 2   *SQRT(3)*I
212
213        2/3                           1/3  1/3
214     + 2   )/(2*(SQRT(31) - 3*SQRT(3))   *6
215
216                1/6
217              *3   ),
218
219                           2/3    2/3
220     (SQRT(31) - 3*SQRT(3))    - 2
221 X=-------------------------------------}
222                          1/3  1/3  1/6
223    (SQRT(31) - 3*SQRT(3))   *6   *3
224\end{verbatim}
225
226\subsection{Other Options}
227\hypertarget{switch:SOLVESINGULAR}{}
228\hypertarget{switch:ALLBRANCH}{}
229\hypertarget{switch:ARBVARS}{}
230\hypertarget{operator:ARBCOMPLEX}{}
231\hypertarget{operator:ARBINT}{}
232%\hypertarget{operator:ARBREAL}{}
233If \sw{SOLVESINGULAR}\ttindexswitch{SOLVESINGULAR} is on (the default setting),
234degenerate systems such as \texttt{x+y=0}, \texttt{2x+2y=0} will be solved by
235introducing appropriate arbitrary constants.
236The consistent singular equation 0=0 or equations involving functions with
237multiple inverses may introduce unique new indeterminant kernels
238\texttt{ARBCOMPLEX(j)}, or \texttt{ARBINT(j)}, ($j$=1,2,...),  % \texttt{ARBREAL(j)},
239\ttindex{ARBCOMPLEX}\ttindex{ARBINT}
240representing arbitrary complex or integer numbers respectively.  To
241automatically select the principal branches, do \texttt{OFF ALLBRANCH}.
242\ttindexswitch{ALLBRANCH} To avoid the introduction of new indeterminant kernels
243do \texttt{OFF ARBVARS}\ttindexswitch{ARBVARS} -- then no equations are generated for the free
244variables and their original names are used to express the solution forms.
245To suppress solutions of consistent singular equations do
246\texttt{OFF SOLVESINGULAR}.
247
248To incorporate additional inverse functions do, for example:
249\begin{verbatim}
250     put('sinh,'inverse,'asinh);
251     put('asinh,'inverse,'sinh);
252\end{verbatim}
253together with any desired simplification rules such as
254\begin{verbatim}
255     for all x let sinh(asinh(x))=x, asinh(sinh(x))=x;
256\end{verbatim}
257For completeness, functions with non-unique inverses should be treated as
258\texttt{\textasciicircum}, \texttt{SIN}, and \texttt{COS} are in the \texttt{SOLVE}
259module source.
260
261Arguments of \texttt{ASIN} and \texttt{ACOS} are not checked to ensure that the
262absolute value of the real part does not exceed 1; and arguments of
263\texttt{LOG} are not checked to  ensure that the absolute value of the imaginary
264part does not exceed $\pi$; but checks (perhaps involving user response
265for non-numerical arguments) could be introduced using
266\texttt{LET}\ttindex{LET} statements for these operators.
267
268\subsection{Parameters and Variable Dependency}
269
270The proper design of a variable sequence
271supplied as a second argument to \texttt{SOLVE} is important
272for the structure of the solution of an equation system.
273Any unknown in the system
274not in this list is considered totally free. E.g.\  the call
275\begin{verbatim}
276    solve({x=2*z,z=2*y},{z});
277\end{verbatim}
278produces an empty list as a result because there is no function
279$z=z(x,y)$ which fulfills both equations for arbitrary $x$ and $y$ values.
280\hypertarget{reserved:requirements}{}
281In such a case the share variable \var{REQUIREMENTS}\ttindextype{REQUIREMENTS}{shared variable}
282\ttindextype{SOLVE}{operator!REQUIREMENTS variable@\texttt{REQUIREMENTS} shared variable}
283displays a set of restrictions for the parameters of the system:
284\begin{verbatim}
285    requirements;
286
287    {x - 4*y}
288\end{verbatim}
289The non-existence of a formal solution is caused by a
290contradiction which disappears only if the parameters
291of the initial system are set such that all members
292of the requirements list take the value zero.
293For a linear system the set is complete: a solution
294of the requirements list makes the initial
295system solvable. E.g.\  in the above case a substitution
296$x=4y$ makes the equation set consistent. For a non-linear
297system only one inconsistency is detected. If such a system
298has more than one inconsistency, you must reduce them
299one after the other.
300\footnote{
301The difference between linear and non--linear
302inconsistent systems is based on the algorithms which
303produce this information as a side effect when attempting
304to find a formal solution; example:
305$solve(\{x=a,x=b,y=c,y=d\},\{x,y\}$ gives a set $\{a-b,c-d\}$
306while $solve(\{x^2=a,x^2=b,y^2=c,y^2=d\},\{x,y\}$ leads to $\{a-b\}$.
307}
308The  set shows you also the dependency among the parameters: here
309one of $x$ and $y$ is free and a formal solution of the system can be
310computed by adding it to the variable list of \texttt{solve}.
311The requirement set is not unique -- there may be other such sets.
312
313
314A system  with parameters may have a formal solution, e.g.\
315\begin{verbatim}
316     solve({x=a*z+1,0=b*z-y},{z,x});
317
318        y     a*y + b
319   {{z=---,x=---------}}
320        b        b
321\end{verbatim}
322which is not valid for all possible values of the parameters.
323\hypertarget{reserved:assumptions}{}
324The variable \var{ASSUMPTIONS}\ttindextype{ASSUMPTIONS}{variable}
325\ttindextype{SOLVE}{operator!ASSUMPTIONS variable@\texttt{ASSUMPTIONS} variable}
326contains then a list of
327restrictions: the solutions are valid only as long
328as none of these expressions vanishes. Any zero of one of them
329represents a special case that is not covered by the
330formal solution. In the above case the value is
331\begin{verbatim}
332    assumptions;
333
334    {b}
335\end{verbatim}
336which excludes formally the case $b=0$; obviously this special
337parameter value makes the system singular. The set of assumptions
338is complete for both, linear and non--linear systems.
339
340\hypertarget{switch:VAROPT}{}
341\texttt{SOLVE} rearranges the variable sequence
342to reduce the (expected) computing time. This behavior is controlled
343by the switch \sw{VAROPT}\ttindexswitch{VAROPT}, which is on by default.
344If it is turned off, the supplied variable sequence is used
345or the system kernel ordering is taken if the variable
346list is omitted. The effect is demonstrated by an example:
347\begin{verbatim}
348   s:= {y^3+3x=0,x^2+y^2=1};
349
350   solve(s,{y,x});
351
352                  6       2
353   {{y=root_of(y_  + 9*y_  - 9,y_),
354
355            3
356         - y
357     x=-------}}
358          3
359
360   off varopt; solve(s,{y,x});
361
362                  6       4        2
363   {{x=root_of(x_  - 3*x_  + 12*x_  - 1,x_),
364
365               4      2
366        x*( - x  + 2*x  - 10)
367     y=-----------------------}}
368                  3
369
370\end{verbatim}
371In the first case, \texttt{solve} forms the solution as a set of
372pairs $(y_i,x(y_i))$ because the degree of $x$ is higher --
373such a rearrangement makes the internal computation of the Gr\"obner basis
374generally faster. For the second case the explicitly given variable sequence
375is used such that the solution has now the form $(x_i,y(x_i))$.
376Controlling the variable sequence is especially important if
377the system has one or more free variables.
378As an alternative to turning off \sw{varopt}, a partial dependency among
379the variables can be declared using the \texttt{depend}\ttindex{DEPEND}
380statement: \texttt{solve} then rearranges the variable sequence but keeps any
381variable ahead of those on which it depends.
382\begin{verbatim}
383   on varopt;
384   s:={a^3+b,b^2+c}$
385   solve(s,{a,b,c});
386
387                           3       6
388   {{a=arbcomplex(1),b= - a ,c= - a }}
389
390   depend a,c; depend b,c; solve(s,{a,b,c});
391
392   {{c=arbcomplex(2),
393
394                 6
395     a=root_of(a_  + c,a_),
396
397           3
398     b= - a }}
399\end{verbatim}
400Here \texttt{solve} is forced to put $c$ after $a$ and after $b$, but
401there is no obstacle to interchanging $a$ and $b$.
402
403