aboutsummaryrefslogtreecommitdiff
path: root/src/input/wester.input.pamphlet
blob: 0576c2f69f20cd96194d8314dfb265cb4bd21b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
\documentclass{article}
\usepackage{axiom}
\begin{document}
\title{\$SPAD/src/input wester.input}
\author{Michael Wester}
\maketitle
\begin{abstract}
\end{abstract}
\eject
\tableofcontents
\eject
\section{License}
<<license>>=
--Copyright The Numerical Algorithms Group Limited 1996.
@
<<*>>=
<<license>>


-- ----------[ A x i o m ]----------
-- ---------- Initialization ----------
)set messages autoload off
)set messages time on
)set quit unprotected
)set streams calculate 7
-- ---------- Numbers ----------
-- Let's begin by playing with numbers: infinite precision integers
factorial(50)
factor(%)
-- Infinite precision rational numbers
1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10
-- Arbitrary precision floating point numbers
digits(50)
-- This number is nearly an integer
exp(sqrt(163.)*%pi)
digits(20)
-- Special functions
besselJ(2, 1 + %i)
-- Complete decimal expansion of a rational number
decimal(1/7)
-- Continued fractions
continuedFraction(%pi)
-- Simplify an expression with nested square roots
s1:=sqrt(2*sqrt(3) + 4)
p:POLY FRAC INT:= (ratPoly(s1::Expression Integer)::SUP FRAC INT).'z
solp:=radicalSolve p
rhs select (z+-> real abs (complexNumeric rhs z - complexNumeric s1) < 1.E-19,solp).1
simplify(s1)
-- Try a more complicated example (from the Putnam exam)
s1:=sqrt(14 + 3*sqrt(3 + 2*sqrt(5 - 12*sqrt(3 - 2*sqrt(2)))))
p:POLY FRAC INT:= (ratPoly(s1::Expression Integer)::SUP FRAC INT).'z
solp:=radicalSolve p
rhs select (z+-> real abs (complexNumeric rhs z - complexNumeric s1) < 1.E-19,solp).1
simplify(s1)
-- Cardinal numbers
2*Aleph(0) - 3
-- ---------- Statistics ----------
-- ---------- Algebra ----------
-- Numbers are nice, but symbols allow for variability---try some high school
-- algebra: rational simplification
(x**2 - 4)/(x**2 + 4*x + 4)
-- This example requires more sophistication
(%e**x - 1)/(%e**(x/2) + 1)
normalize(%)
-- Expand and factor polynomials
(x + 1)**20
D(%, x)
factor(%)
x**100 - 1
factor(%)
-- Factor polynomials over finite fields and field extensions
p:= x**4 - 3*x**2 + 1
factor(p)
phi:= rootOf(phi**2 - phi - 1);
factor(p, [phi])
factor(p :: Polynomial(PrimeField(5)))
expand(%)
-- Partial fraction decomposition
(x**2 + 2*x + 3)/(x**3 + 4*x**2 + 5*x + 2)
padicFraction(
   partialFraction(numerator(%) :: UnivariatePolynomial(x, Fraction Integer),
                   factor(denominator(%) :: Polynomial Integer) ::
                      Factored UnivariatePolynomial(x, Fraction Integer)))
-- ---------- Inequalities ----------
-- ---------- Trigonometry ----------
-- Trigonometric manipulations---these are typically difficult for students
r:= cos(3*x)/cos(x)
real(complexNormalize(%))
real(normalize(simplify(complexNormalize(r))))
-- Use rewrite rules
sincosAngles:= rule _
  (cos((n | integer?(n)) * x) == _
      cos((n - 1)*x) * cos(x) - sin((n - 1)*x) * sin(x); _
   sin((n | integer?(n)) * x) == _
      sin((n - 1)*x) * cos(x) + cos((n - 1)*x) * sin(x) )
sincosAngles r
-- ---------- Determining Zero Equivalence ----------
-- The following expressions are all equal to zero
sqrt(997) - (997**3)**(1/6)
sqrt(999983) - (999983**3)**(1/6)
s1:=(2**(1/3) + 4**(1/3))**3 - 6*(2**(1/3) + 4**(1/3)) - 6
simplify(%)
p:POLY FRAC INT:= (ratPoly(s1::Expression Integer)::SUP FRAC INT).'z
solp:=radicalSolve p
rhs select (z+-> real abs (complexNumeric rhs z - complexNumeric s1) < 1.E-19,solp).1
-- This expression is zero for x, y > 0 and n not equal to zero
x**(1/n)*y**(1/n) - (x*y)**(1/n)
normalize(%)
-- See Joel Moses, ``Algebraic Simplification: A Guide for the Perplexed'',
-- CACM, Volume 14, Number 8, August 1971
expr:= log(tan(1/2*x + %pi/4)) - asinh(tan(x))
complexNormalize(%)
-- Use a roundabout method---show that expr is a constant equal to zero
D(expr, x)
normalize(rootSimp(expand(simplify(%))))
normalize(eval(expr, x = 0))
expr:=log((2*sqrt(r) + 1)/sqrt(4*r + 4*sqrt(r) + 1))
D(expr, x)
eval(expr, x = 0)
(4*r + 4*sqrt(r) + 1)**(sqrt(r)/(2*sqrt(r) + 1)) _
   * (2*sqrt(r) + 1)**(1/(2*sqrt(r) + 1)) - 2*sqrt(r) - 1
normalize(%)
-- ---------- The Complex Domain ----------
-- Complex functions---separate into their real and imaginary parts
rectform(z) == real(z) + %i*imag(z)
rectform(log(3 + 4*%i))
simplify(rectform(tan(x + %i*y)))
-- Check for branch abuse.  See David R. Stoutemyer, ``Crimes and Misdemeanors
-- in the Computer Algebra Trade'', Notices of the AMS, Volume 38, Number 7,
-- September 1991.  This first expression can simplify to sqrt(x y)/sqrt(x),
-- but no further in general (consider what happens when x, y = -1).
sqrt(x*y*abs(z)**2) / (sqrt(x)*abs(z))
rootSimp %
-- If z = -1, sqrt(1/z) is not equal to 1/sqrt(z)
sqrt(1/z) - 1/sqrt(z)
-- If z = 3 pi i, log(exp(z)) is not equal to z
log(%e**z)
normalize(%)
-- The principal value of this expression is (10 - 4 pi) i
log(%e**(10*%i))
normalize(%)
-- If z = pi, arctan(tan(z)) is not equal to z
atan(tan(z))
-- If z = 2 pi i, sqrt(exp(z)) is not equal to exp(z/2)
sqrt(%e**z) - %e**(z/2)
-- ---------- Equations ----------
-- Manipulate an equation using a natural syntax
(x = 0)/2 + 1
-- Solve various nonlinear equations---this cubic polynomial has all real roots
radicalSolve(3*x**3 - 18*x**2 + 33*x - 19 = 0, x)
map(e +-> lhs(e) = rectform(rhs(e)), %)
-- Some simple seeming problems can have messy answers
eqn:= x**4 + x**3 + x**2 + x + 1 = 0
radicalSolve(eqn, x)
-- Check one of the answers
eval(eqn, %.1)
%e**(2*x) + 2*%e**x + 1 = z
solve(%, x)
-- This equation is already factored and so *should* be easy to solve
(x + 1) * (sin(x)**2 + 1)**2 * cos(3*x)**3 = 0
solve(%, x)
-- The following equations have an infinite number of solutions (let n be an
-- arbitrary integer): z = 0 [+ n 2 pi i]
solve(%e**z = 1, z)
-- x = pi/4 [+ n pi]
solve(sin(x) = cos(x), x)
solve(tan(x) = 1, x)
-- x = 0, 0 [+ n pi, + n 2 pi]
solve(sin(x) = tan(x), x)
-- This equation has no solutions
solve(sqrt(x**2 + 1) = x - 2, x)
-- Solve a system of linear equations
eq1:=   x +   y +   z =  6
eq2:= 2*x +   y + 2*z = 10
eq3:=   x + 3*y +   z = 10
-- Note that the solution is parametric
solve([eq1, eq2, eq3], [x, y, z])
-- Solve a system of nonlinear equations
eq1:= x**2*y + 3*y*z - 4 = 0
eq2:= -3*x**2*z + 2*y**2 + 1 = 0
eq3:= 2*y*z**2 - z**2 - 1 = 0
-- Solving this by hand would be a nightmare
solve([eq1, eq2, eq3], [x, y, z])
-- ---------- Matrix Algebra ----------
m:= matrix([[a, b], [1, a*b]])
-- Invert the matrix
minv:= inverse(m)
m * minv
-- Define a Vandermonde matrix (useful for doing polynomial interpolations)
matrix([[1,    1,    1,    1   ], _
        [w,    x,    y,    z   ], _
        [w**2, x**2, y**2, z**2], _
        [w**3, x**3, y**3, z**3]])
determinant(%)
-- The following formula implies a general result
factor(%)
-- Compute the eigenvalues of a matrix from its characteristic polynomial
m:= matrix([[ 5, -3, -7], _
            [-2,  1,  2], _
            [ 2, -3, -4]])
characteristicPolynomial(m, lambda)
solve(% = 0, lambda)
-- ---------- Tensors ----------
-- ---------- Sums and Products ----------
-- Sums: finite and infinite
summation(k**3, k = 1..n)
sum(k**3, k = 1..n)
limit(sum(1/k**2 + 1/k**3, k = 1..n), n = %plusInfinity)
-- Products
product(k, k = 1..n)
-- ---------- Calculus ----------
-- Limits---start with a famous example
limit((1 + 1/n)**n, n = %plusInfinity)
limit((1 - cos(x))/x**2, x = 0)
-- Apply the chain rule---this is important for PDEs and many other
-- applications
y:= operator('y);
x:= operator('x);
D(y(x(t)), t, 2)
)clear properties x y
-- ---------- Indefinite Integrals ----------
1/(x**3 + 2)
-- This would be very difficult to do by hand
integrate(%, x)
D(%, x)
-- This example involves several symbolic parameters
integrate(1/(a + b*cos(x)), x)
map(simplify, map(f +-> D(f, x), %))
-- Calculus on a non-smooth (but well defined) function
D(abs(x), x)
integrate(abs(x), x)
-- Calculus on a piecewise defined function
a(x) == if x < 0 then -x else x
D(a(x), x)
integrate(a(x), x)
)clear properties a
-- The following two integrals should be equivalent.  The correct solution is
-- [(1 + x)^(3/2) + (1 - x)^(3/2)] / 3
integrate(x/(sqrt(1 + x) + sqrt(1 - x)), x)
integrate((sqrt(1 + x) - sqrt(1 - x))/2, x)
-- ---------- Definite Integrals ----------
-- The following two functions have a pole at zero
integrate(1/x, x = -1..1)
integrate(1/x**2, x = -1..1)
-- Different branches of the square root need to be chosen in the intervals
-- [0, 1] and [1, 2].  The correct results are 4/3, [4 - sqrt(8)]/3,
-- [8 - sqrt(8)]/3, respectively.
integrate(sqrt(x + 1/x - 2), x = 0..1)
integrate(sqrt(x + 1/x - 2), x = 0..1, "noPole")
integrate(sqrt(x + 1/x - 2), x = 1..2)
integrate(sqrt(x + 1/x - 2), x = 1..2, "noPole")
integrate(sqrt(x + 1/x - 2), x = 0..2)
integrate(sqrt(x + 1/x - 2), x = 0..2, "noPole")
-- Contour integrals
integrate(cos(x)/(x**2 + a**2), x = %minusInfinity..%plusInfinity)
integrate(cos(x)/(x**2 + a**2), x = %minusInfinity..%plusInfinity, "noPole")
-- Integrand with a branch point
integrate(t**(a - 1)/(1 + t), t = 0..%plusInfinity)
integrate(t**(a - 1)/(1 + t), t = 0..%plusInfinity, "noPole")
-- Multiple integrals: volume of a tetrahedron
integrate(integrate(integrate(1, z = 0..c*(1 - x/a - y/b)), _
                    y = 0..b*(1 - x/a)), _
          x = 0..a)
-- ---------- Series ----------
-- Taylor series---this first example comes from special relativity
1/sqrt(1 - (v/c)**2)
series(%, v = 0)
1/%**2
tsin:= series(sin(x), x = 0)
tcos:= series(cos(x), x = 0)
-- Note that additional terms will be computed as needed
tsin/tcos
series(tan(x), x = 0)
-- Look at the Taylor series around x = 1
)set streams calculate 1
log(x)**a*exp(-b*x)
series(%, x = 1)
)set streams calculate 7
-- Compare the Taylor series of two different formulations of a function
taylor(log(sinh(z)) + log(cosh(z + w)), z = 0)
% - taylor(log(sinh(z) * cosh(z + w)), z = 0)
series(log(sinh(z)) + log(cosh(z + w)), z = 0)
% - series(log(sinh(z) * cosh(z + w)), z = 0)
-- Power series (compute the general formula)
log(sin(x)/x)
series(%, x = 0)
exp(-x)*sin(x)
series(%, x = 0)
-- Derive an explicit Taylor series solution of y as a function of x from the
-- following implicit relation
y:= operator('y);
x = sin(y(x)) + cos(y(x))
seriesSolve(%, y, x = 1, 0)
)clear properties y
-- Pade (rational function) approximation
pade(1, 1, taylor(exp(-x), x = 0))
-- ---------- Transforms ----------
-- Laplace and inverse Laplace transforms
laplace(cos((w - 1)*t), t, s)
inverseLaplace(%, s, t)
-- ---------- Difference and Differential Equations ----------
-- Second order linear recurrence equation
r:= operator('r);
r(n + 2) - 2 * r(n + 1) + r(n) = 2
[%, r(0) = 1, r(1) = m]
)clear properties r
-- Second order ODE with initial conditions---solve first using Laplace
-- transforms
f:= operator('f);
ode:= D(f(t), t, 2) + 4*f(t) = sin(2*t)
map(e +-> laplace(e, t, s), %)
-- Now, solve the ODE directly
solve(ode, f, t = 0, [0, 0])
-- First order linear ODE
y:= operator('y);
x**2 * D(y(x), x) + 3*x*y(x) = sin(x)/x
solve(%, y, x)
-- Nonlinear ODE
D(y(x), x, 2) + y(x)*D(y(x), x)**3 = 0
solve(%, y, x)
-- A simple parametric ODE
D(y(x, a), x) = a*y(x, a)
solve(%, y, x)
D(y(x), x) = a*y(x)
solve(%, y, x)
-- ODE with boundary conditions.  This problem has nontrivial solutions
-- y(x) = A sin([pi/2 + n pi] x) for n an arbitrary integer.
solve(D(y(x), x, 2) + k**2*y(x) = 0, y, x)
-- bc(%, x = 0, y = 0, x = 1, D(y(x), x) = 0)
-- System of two linear, constant coefficient ODEs
x:= operator('x);
system:= [D(x(t), t) = x(t) - y(t), D(y(t), t) = x(t) + y(t)]
solve(system,[x,y],t)
-- Check the answer
-- Triangular system of two ODEs
system:= [D(x(t), t) = x(t) * (1 + cos(t)/(2 + sin(t))), _
          D(y(t), t) = x(t) - y(t)]
-- Try solving this system one equation at a time
solve(system.1, x, t)
genericx:=C1*%.basis.1
eval(lhs rightZero system.2,x,genericx,t)
solve(%,y,t)
genericy:=simplify (%.particular)+K1*(%.basis.1)
eval(lhs rightZero system.1,x,genericx,t)
eval(lhs rightZero system.2,[x,y],[genericx,genericy],t)
)clear properties x y
-- ---------- Operators ----------
-- Linear differential operator
DD:= operator("D") :: Operator(Expression Integer)
evaluate(DD, e +-> D(e, x))$Operator(Expression Integer)
L:= (DD - 1) * (DD + 2)
g:= operator('g)
L(f(x))
subst(L(subst(g(y), y = x)), x = y)
subst(L(subst(A * sin(z**2), z = x)), x = z)
-- Truncated Taylor series operator
T:= (f, xx, a) +-> subst((DD**0)(f(x)), x = a)/factorial(0) * (xx - a)**0 + _
                   subst((DD**1)(f(x)), x = a)/factorial(1) * (xx - a)**1 + _
                   subst((DD**2)(f(x)), x = a)/factorial(2) * (xx - a)**2
T(f, x, a)
T(g, y, b)
Sin:= operator("sin") :: Operator(Expression Integer)
evaluate(Sin, x +-> sin(x))$Operator(Expression Integer)
T(Sin, z, c)
-- ---------- Programming ----------
-- Write a simple program to compute Legendre polynomials
)clear properties p
p(n, x) == 1/(2**n*factorial(n)) * D((x**2 - 1)**n, x, n)
for i in 0..4 repeat {  output("");    output(concat(["p(", string(i), ", x) = "]));    output(p(i, x))}
eval(p(4, x), x = 1)
-- Now, perform the same computation using a recursive definition
pp(0, x) == 1
pp(1, x) == x
pp(n, x) == ((2*n - 1)*x*pp(n - 1, x) - (n - 1)*pp(n - 2, x))/n
for i in 0..4 repeat {   output("");   output(concat(["pp(", string(i), ", x) = "]));   output(pp(i, x))}
)clear properties p pp
-- ---------- Translation ----------
-- Horner's rule---this is important for numerical algorithms
a:= operator('a)
sum(a(i)*x**i, i = 1..5)
p:= factor(%)
-- Convert the result into FORTRAN syntax
)set fortran ints2floats off
outputAsFortran('p = p)
-- ---------- Boolean Logic ----------
-- Simplify logical expressions
true and false
x or (not x)
--x or y or (x and y)
@
\eject
\begin{thebibliography}{99}
\bibitem{1} nothing
\end{thebibliography}
\end{document}