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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
\documentclass{article}
\usepackage{axiom}
\begin{document}
\title{\$SPAD/src/algebra nepip.as}
\author{Michael Richardson}
\maketitle
\begin{abstract}
\end{abstract}
\eject
\tableofcontents
\eject
\section{NagEigenInterfacePackage}
<<NagEigenInterfacePackage>>=
+++ Author: M.G. Richardson
+++ Date Created: 1996 January 12
+++ Date Last Updated:
+++ Basic Functions:
+++ Related Constructors:
+++ Also See:
+++ AMS Classifications:
+++ Keywords:
+++ References:
+++ Description:
+++ This package provides Axiom-like interfaces to the NAG generalised
+++ eigenvalue and eigenvector routines in the NAGlink.
DF ==> DoubleFloat ;
CDF ==> Complex DoubleFloat ;
FFCDF ==> FormalFraction Complex DoubleFloat ;
LFFCDF ==> List FormalFraction Complex DoubleFloat ;
LDF ==> List DoubleFloat ;
LCDF ==> List Complex DoubleFloat ;
LLDF ==> List LDF ;
VDF ==> Vector DoubleFloat ;
LVDF ==> List VDF ;
VCDF ==> Vector Complex DoubleFloat ;
LVCDF ==> List VCDF ;
MDF ==> Matrix DoubleFloat ;
MCDF ==> Matrix Complex DoubleFloat ;
INT ==> Integer ;
NNI ==> NonNegativeInteger ;
RCD ==> Record ;
RSLT ==> Result ;
STRG ==> String ;
UNNRES ==> Union(a:LDF,b:LFFCDF) ; -- a & b are dummy tags
RURSLV ==> RCD(eigenvalues : UNNRES, eigenvectors : LVCDF) ;
NagEigenInterfacePackage: with {
nagEigenvalues : (MDF,MDF,DF) -> UNNRES ;
++ nagEigenvalues(A,B,eps) returns a list of the eigenvalues
#if saturn
++ $ \lambda $
#else
++ \spad{l}
#endif
++ of the system
#if saturn
++ $ A x = \lambda B x $
#else
++ \spad{A*x = l*B*x}
#endif
++
++ The numerical calculation is performed by one of the NAG routines
++ F02ADF and F02BJF, depending on the the form of \spad{A} and B.
++ The result is of type Union(List DoubleFloat, List FormalFraction
++ Complex DoubleFloat), the first branch resulting from F02ADF and
++ the second from F02BJF. Note that in the latter case values should
++ be inspected for numerically small numerators and denominators,
++ ratios of which may be in effect indeterminate, before the result is
++ converted to List Complex DoubleFloat.
++
++ The parameter eps, if positive, defines a tolerance to be used in
++ recognising negligable matrix elements when F02BJF is called; setting
++ this may result in faster execution with less accuracy.
++
++ For more detailed information, please consult the NAG manual
++ via the Browser pages for the operations f02adf and f02bjf.
nagEigenvalues : (MDF,MDF) -> UNNRES ;
++ nagEigenvalues(A,B) returns a list of the eigenvalues
#if saturn
++ $ \lambda $
#else
++ \spad{l}
#endif
++ of the system
#if saturn
++ $ A x = \lambda B x $
#else
++ \spad{A*x = l*B*x}
#endif
++
++ The numerical calculation is performed by one of the NAG routines
++ F02ADF and F02BJF, depending on the the form of \spad{A} and B.
++ The result is of type Union(List DoubleFloat, List FormalFraction
++ Complex DoubleFloat), the first branch resulting from F02ADF and
++ the second from F02BJF. Note that in the latter case values should
++ be inspected for numerically small numerators and denominators,
++ ratios of which may be in effect indeterminate, before the result is
++ converted to List Complex DoubleFloat.
++
++ For more detailed information, please consult the NAG manual
++ via the Browser pages for the operations f02adf and f02bjf.
nagEigenvectors : (MDF,MDF,DF) -> RURSLV ;
++ nagEigenvectors(A,B,eps) returns a record consisting of a list of the
++ eigenvalues
#if saturn
++ $ \lambda $
#else
++ \spad{l}
#endif
++ and a list of the corresponding eigenvectors of the system
#if saturn
++ $ A x = \lambda B x $
#else
++ \spad{A*x = l*B*x}
#endif
++ where
#if saturn
++ $A$ and $B$
#else
++ \spad{A} and B
#endif
#if saturn
++ $B$
#else
++ B
#endif
++ is positive-definite.
++
++ The numerical calculation is performed by one of the NAG routines
++ F02AEF and F02BJF, depending on the the form of \spad{A} and B.
++ The first component of the result, \spad{eigenvalues},
++ is of type Union(List DoubleFloat, List FormalFraction
++ Complex DoubleFloat), the first branch resulting from F02AEF and
++ the second from F02BJF. Note that in the latter case values should
++ be inspected for numerically small numerators and denominators,
++ ratios of which may be in effect indeterminate, before the result is
++ converted to List Complex DoubleFloat.
++
++ The parameter eps, if positive, defines a tolerance to be used in
++ recognising negligable matrix elements when F02BJF is called; setting
++ this may result in faster execution with less accuracy.
++
++ For more detailed information, please consult the NAG manual
++ via the Browser pages for the operations f02aef and f02bjf.
nagEigenvectors : (MDF,MDF) -> RURSLV ;
++ nagEigenvectors(A,B) returns a record consisting of a list of the
++ eigenvalues
#if saturn
++ $ \lambda $
#else
++ \spad{l}
#endif
++ and a list of the corresponding eigenvectors of the system
#if saturn
++ $ A x = \lambda B x $
#else
++ \spad{A*x = l*B*x}
#endif
++ where
#if saturn
++ $A$ and $B$
#else
++ \spad{A} and B
#endif
#if saturn
++ $B$
#else
++ B
#endif
++ is positive-definite.
++
++ The numerical calculation is performed by one of the NAG routines
++ F02AEF and F02BJF, depending on the the form of \spad{A} and B.
++ The first component of the result, \spad{eigenvalues},
++ is of type Union(List DoubleFloat, List FormalFraction
++ Complex DoubleFloat), the first branch resulting from F02AEF and
++ the second from F02BJF. Note that in the latter case values should
++ be inspected for numerically small numerators and denominators,
++ ratios of which may be in effect indeterminate, before the result is
++ converted to List Complex DoubleFloat.
++
++ For more detailed information, please consult the NAG manual
++ via the Browser pages for the operations f02aef and f02bjf.
} == add {
import from AnyFunctions1 INT ;
import from AnyFunctions1 MDF ;
import from CDF;
import from ErrorFunctions ;
import from MDF ;
import from NagResultChecks ;
import from NagEigenPackage ;
import from List STRG ;
import from Symbol ;
import from VDF ;
import from Boolean ;
import from Result ;
local (..)(a:INT,b:INT):Generator INT == {
generate {
t := a ;
while (t <= b) repeat {
yield t ;
t := t + 1 ;
}
}
}
local ipIfail : INT := -1 ;
-- First, some local functions:
f02bjfEigVals(A : MDF, B : MDF, orderAB : INT, eps : DF) : LFFCDF == {
-- orderAB is the common order of the square matrices A and B.
local f02bjfResult : RSLT ;
local numR, numI, den : LDF ;
f02bjfResult := f02bjf(orderAB,orderAB,orderAB,eps,
false,orderAB,A,B,ipIfail) ;
den := entries(row(checkMxDF(f02bjfResult, "beta", "F02BJF"), 1)) ;
numR := entries(row(retract(f02bjfResult."alfr") @ MDF, 1)) ;
numI := entries(row(retract(f02bjfResult."alfi") @ MDF, 1)) ;
[ (complex(r,i)/complex(d,0@DF))$FFCDF for r in numR
for i in numI
for d in den ]
}
f02bjfEigVecs(A : MDF, B : MDF, orderAB : INT, eps : DF) : RURSLV == {
-- orderAB is the common order of the square matrices A and B.
local size : NNI ;
local f02bjfResult : RSLT ;
local numR, numI, den : LDF ;
local eVals : UNNRES ;
local vecMat : MDF ;
local eVecs : LVCDF ;
local j : INT ;
local thisVec, leftVec : VCDF ;
size := orderAB pretend NNI ;
f02bjfResult := f02bjf(orderAB,orderAB,orderAB,eps,
true,orderAB,A,B,ipIfail) ;
den := entries(row(checkMxDF(f02bjfResult, "beta", "F02BJF"), 1)) ;
numR := entries(row(retract(f02bjfResult."alfr") @ MDF, 1)) ;
numI := entries(row(retract(f02bjfResult."alfi") @ MDF, 1)) ;
vecMat := retract(f02bjfResult."v") @ MDF ;
-- outer [] for union type:
eVals := [[(complex(r,i)/complex(d,0@DF))$FFCDF for r in numR
for i in numI
for d in den]] ;
eVecs := [] ;
j := orderAB ;
while j > 0 repeat {
if numI.j ~= 0$DF then {
if j = 1 or numI.(j-1) = 0$DF then
error("nagEigenvectors",
"Inconsistent results returned from NAG routine F02BJF") ;
thisVec := new(size,0) ;
leftVec := new(size,0) ;
for i in 1 .. orderAB repeat {
thisVec.i := complex(vecMat(i,j-1),-vecMat(i,j)) ;
leftVec.i := complex(vecMat(i,j-1),vecMat(i,j)) ;
}
eVecs := cons(leftVec,cons(thisVec,eVecs)) ;
j := j - 2;
}
else {
thisVec := new(size,0) ;
for i in 1 .. orderAB repeat
thisVec.i := complex(vecMat(i,j),0@DF) ;
eVecs := cons(thisVec,eVecs) ;
j := j - 1 ;
}
}
[eVals,eVecs]
}
nagError(routine : STRG, opIfail : INT) : Exit ==
error ["An error was detected when calling the NAG Library routine ",
routine,
". The error number (IFAIL value) is ",
string(opIfail),
", please consult the NAG manual via the Browser for",
" diagnostic information."] ;
-- Then the exported functions:
nagEigenvalues(A : MDF, B : MDF, eps : DF) : UNNRES == {
-- Strategy: if either matrix is asymmetric, use F02BJF, otherwise
-- try F02ADF in case B is positive-definite.
-- If F02ADF gives IFAIL=1 (should happen quickly if at all),
-- not positive-definite, use less efficient F02BJF.
local rA, rB, cA, cB : NNI ;
local orderAB, opIfail: INT ;
local vals : UNNRES ;
rA := nrows A ;
rB := nrows B ;
if rA ~= rB
then error("nagEigenvalues",
"the two matrices supplied are of different sizes.") ;
orderAB := rA pretend INT ;
if symmetric?(A) and symmetric?(B) then {
f02adfResult := f02adf(orderAB,orderAB,orderAB,A,B,ipIfail) ;
opIfail := retract(f02adfResult."ifail") @ INT ;
if zero? opIfail then -- using [] to give union type:
vals := [entries(row(retract(f02adfResult."r") @ MDF,1))] ;
else if opIfail = 1 then
vals := [f02bjfEigVals(A,B,orderAB,eps)]
else
nagError("F02BJF",opIfail) ;
}
else {
cA := ncols A ;
if cA ~= rA then
error("nagEigenvalues",
"the first matrix supplied is not square") ;
cB := ncols B ;
if cB ~= rB then
error("nagEigenvalues",
"the second matrix supplied is not square") ;
vals := [f02bjfEigVals(A,B,orderAB,eps)] ;
}
vals
}
nagEigenvalues(A : MDF, B : MDF) : UNNRES
== nagEigenvalues(A,B,0@DF) ;
nagEigenvectors(A : MDF, B : MDF, eps : DF) : RURSLV == {
-- Strategy: if either matrix is asymmetric, use F02BJF, otherwise
-- try F02AEF in case B is positive-definite.
-- If F02AEF gives IFAIL=1 (should happen quickly if at all),
-- not positive-definite, use less efficient F02BJF.
local rA, rB, cA, cB : NNI ;
local orderAB, opIfail, j : INT ;
local eVals : UNNRES ;
local eVecs : LVCDF ;
local vecMat : MDF ;
local thisVec : VCDF ;
local f02aefResult : RSLT ;
local result : RURSLV ;
rA := nrows A ;
rB := nrows B ;
if rA ~= rB
then error("nagEigenvectors",
"the two matrices supplied are of different sizes.") ;
orderAB := rA pretend INT ;
if symmetric?(A) and symmetric?(B) then {
f02aefResult := f02aef(orderAB,orderAB,orderAB,
orderAB,A,B,ipIfail) ;
opIfail := retract(f02aefResult."ifail") @ INT ;
if zero? opIfail then {
-- using [] to give union type:
eVals := [entries(row(retract(f02aefResult."r") @ MDF,1))] ;
vecMat := retract(f02aefResult."v") @ MDF ;
eVecs := [] ;
j := orderAB ;
while j > 0 repeat {
thisVec := new(rA,0) ;
for i in 1 .. orderAB repeat
thisVec.i := complex(vecMat(i,j),0@DF) ;
eVecs := cons(thisVec,eVecs) ;
j := j - 1 ;
}
result := [eVals,eVecs]
}
else if opIfail = 1 then
result := f02bjfEigVecs(A,B,orderAB,eps)
else
nagError("F02BJF",opIfail) ;
}
else {
cA := ncols A ;
if cA ~= rA then
error("nagEigenvectors",
"the first matrix supplied is not square") ;
cB := ncols B ;
if cB ~= rB then
error("nagEigenvectors",
"the second matrix supplied is not square") ;
result := f02bjfEigVecs(A,B,orderAB,eps) ;
}
result
}
nagEigenvectors(A : MDF, B : MDF) : RURSLV
== nagEigenvectors(A,B,0@DF) ;
}
#if NeverAssertThis
-- Note that the conversions of results from DoubleFloat to Float
-- will become unnecessary if outputGeneral is extended to apply to
-- DoubleFloat quantities.
)lib nrc
)lib ffrac
)lib nepip
outputGeneral 5
mA1 := matrix [[ 0.5 , 1.5 , 6.6 , 4.8], _
[ 1.5 , 6.5 , 16.2 , 8.6], _
[ 6.6 , 16.2 , 37.6 , 9.8], _
[ 4.8 , 8.6 , 9.8 , -17.1]];
mB1 := matrix[[ 1 , 3 , 4 , 1], _
[ 3 , 13 , 16 , 11], _
[ 4 , 16 , 24 , 18], _
[ 1 , 11 , 18 , 27]];
mA2 := matrix [[ 3.9 , 12.5 , -34.5 , -0.5], _
[ 4.3 , 21.5 , -47.5 , 7.5], _
[ 4.3 , 21.5 , -43.5 , 3.5], _
[ 4.4 , 26.0 , -46.0 , 6.0]];
mB2 := matrix[[ 1 , 2 , -3 , 1], _
[ 1 , 3 , -5 , 4], _
[ 1 , 3 , -4 , 3], _
[ 1 , 3 , -4 , 4]];
nagEigenvalues(mA1,mB1) :: List Float
-- [- 3.0,- 1.0,2.0,4.0]
vv1 := nagEigenvectors(mA1,mB1);
(vv1.eigenvalues) :: List Float
-- [- 3.0,- 1.0,2.0,4.0]
(vv1.eigenvectors) :: List Vector Complex Float
-- [[- 4.35,0.05,1.0,- 0.5], [- 2.05,0.15,0.5,- 0.5], [- 3.95,0.85,0.5,- 0.5],
-- [2.65,0.05,- 1.0,0.5]]
nagEigenvalues(mA2,mB2)
-- all components are O(1) or more so:
% :: List Complex Float
-- [2.0,3.0 + 4.0 %i,3.0 - 4.0 %i,4.0]
vv2 := nagEigenvectors(mA2,mB2);
vv2.eigenvalues
-- all components are O(1) or more so:
% :: List Complex Float
-- [2.0,3.0 + 4.0 %i,3.0 - 4.0 %i,4.0]
vv2.eigenvectors :: List Vector Complex Float
-- [[0.99606,0.0056917,0.062609,0.062609],
--
-- [0.94491, 0.18898 + 0.26077 E -14 %i, 0.11339 - 0.15119 %i,
-- 0.11339 - 0.15119 %i]
-- ,
--
-- [0.94491, 0.18898 - 0.26077 E -14 %i, 0.11339 + 0.15119 %i,
-- 0.11339 + 0.15119 %i]
-- ,
-- [0.98752,0.010972,- 0.032917,0.15361]]
-- The same call with eps=0.0001:
vv2a := nagEigenvectors(mA2,mB2,0.0001);
vv2a.eigenvalues :: List Complex Float
-- [1.9989,3.0003 + 3.9994 %i,3.0003 - 3.9994 %i,4.0]
vv2a.eigenvectors :: List Vector Complex Float
-- [[0.99605,0.0057355,0.062656,0.062656],
--
-- [0.94491, 0.18899 - 0.000048882 %i, 0.11336 - 0.15119 %i,
-- 0.11336 - 0.15119 %i]
-- ,
--
-- [0.94491, 0.18899 + 0.000048882 %i, 0.11336 + 0.15119 %i,
-- 0.11336 + 0.15119 %i]
-- ,
-- [0.98751,0.011031,- 0.032912,0.15367]]
mB1(1,1) := -1;
-- The next test should fail on F02ADF then call F02BJF:
nagEigenvalues(mA1,mB1)
-- all components are O(1) or more so:
% :: List Complex Float
-- [3.5016,- 1.5471,0.041212 + 0.21738 %i,0.041212 - 0.21738 %i]
-- Similarly, this should fail on F02AEF then call F02BJF:
vv3 := nagEigenvectors(mA1,mB1);
vv3.eigenvalues
-- all components are O(1) or more so:
% :: List Complex Float
-- [3.5016,- 1.5471,0.041212 + 0.21738 %i,0.041212 - 0.21738 %i]
vv3.eigenvectors :: List Vector Complex Float
-- [[- 0.034577,0.63045,- 0.75202,0.1892],
-- [0.17876,- 0.73845,0.047413,0.64845],
--
-- [0.80838, - 0.00095133 + 0.47557 %i, - 0.20354 - 0.21737 %i,
-- 0.15404 + 0.089179 %i]
-- ,
--
-- [0.80838, - 0.00095133 - 0.47557 %i, - 0.20354 + 0.21737 %i,
-- 0.15404 - 0.089179 %i]
-- ]
outputGeneral()
output "End of tests"
#endif
@
\section{License}
<<license>>=
--Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
--All rights reserved.
--
--Redistribution and use in source and binary forms, with or without
--modification, are permitted provided that the following conditions are
--met:
--
-- - Redistributions of source code must retain the above copyright
-- notice, this list of conditions and the following disclaimer.
--
-- - Redistributions in binary form must reproduce the above copyright
-- notice, this list of conditions and the following disclaimer in
-- the documentation and/or other materials provided with the
-- distribution.
--
-- - Neither the name of The Numerical ALgorithms Group Ltd. nor the
-- names of its contributors may be used to endorse or promote products
-- derived from this software without specific prior written permission.
--
--THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
--IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
--TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
--PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
--OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
--EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
--PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
--PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
--LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
--NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@
<<*>>=
<<license>>
-- NagEigenProblemInterfacePackage
-- To test:
-- sed '1,/^#if NeverAssertThis/d;/#endif/d' < nepip.as > nepip.input
-- axiom
-- )set nag host <some machine running nagd>
-- )r nepip.input
#unassert saturn
#include "axiom.as"
<<NagEigenInterfacePackage>>
@
\eject
\begin{thebibliography}{99}
\bibitem{1} nothing
\end{thebibliography}
\end{document}
|