aboutsummaryrefslogtreecommitdiff
path: root/src/algebra/herm.as.pamphlet
blob: 81915bae71cde9afaa19aaae844e69740263af84 (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
\documentclass{article}
\usepackage{axiom}
\begin{document}
\title{\$SPAD/src/algebra herm.as}
\author{Michael Richardson}
\maketitle
\begin{abstract}
\end{abstract}
\eject
\tableofcontents
\eject
\begin{verbatim}
-- N.B. ndftip.as inlines this, must be recompiled if this is.

-- To test:
-- sed -ne '1,/^#if NeverAssertThis/d;/#endif/d;p' < herm.as > herm.input    
-- axiom
-- )set nag host <some machine running nagd>
-- )r herm.input
\end{verbatim}
\section{PackedHermitianSequence}
<<PackedHermitianSequence>>=
#include "axiom.as"

INT ==> Integer ;
NNI ==> NonNegativeInteger ;
PHS ==> PackedHermitianSequence ;
 
+++ Author: M.G. Richardson
+++ Date Created: 1995 Nov. 24
+++ Date Last Updated:
+++ Basic Functions:
+++ Related Constructors: Vector
+++ Also See:
+++ AMS Classifications:
+++ Keywords:
+++ References:
+++ Description:
+++ This type represents packed Hermitian sequences - that is, complex
+++ sequences s, whose tails ("rest s", in Axiom terms) are conjugate to
+++ themselves reversed - by real sequences, in the "standard" manner;
+++ in this, the real parts of the elements of the first "half" of the
+++ tail are stored there and the imaginary parts are stored in reverse
+++ order in the second "half" of the tail.
+++ (If the tail has an odd number of elements, its middle element is
+++ real and is stored unchanged.  The "halves" mentioned above then
+++ refer to the elements before and after the middle, respectively.)

PackedHermitianSequence(R : CommutativeRing) : LinearAggregate(R) with{

  pHS : List R -> % ;
++ pHS(l) converts the list l to a packedHermitianSequence.

  expand : % -> Vector Complex R ;
++ expand(h) converts the packedHermitianSequence h to a Hermitian
++ sequence (a complex vector).

  packHS : Vector Complex R -> % ;
++ packHS(v) checks that the complex vector v represents a Hermitian
++ sequences and, if so, converts it to a packedHermitianSequence;
++ otherwise an error message is printed.

  conjHerm : % -> % ;
++ conjHerm(h) returns the packedHermitianSequence which represents the
++ Hermitian sequence conjugate to that represented by h.

 coerce : % -> OutputForm -- shouldn't need this, should be inherited
			  -- from Vector.

} == Vector(R) add {
  Rep ==> Vector R;
  import from Rep;
  import from INT ;
  import from R ;
  import from Vector R ;
  import from Complex R ;
  import from Vector Complex R ;
  import from ErrorFunctions ;
  import from String ;
  import from List String ;

  local (..)(a:INT,b:INT):Generator INT == {
    generate {
      t := a ;
      while (t <= b) repeat {
        yield t ;
        t := t + 1 ;
        }
      }
    }

  pHS(l : List R) : % == (vector l) pretend PHS R ;

  expand(h : %) : Vector Complex R == {
 
    local len : NNI ;
    local nvals, npairs, n1, n2 : INT ;
    local fullh : Vector Complex R ;

    {
    len := # h ;
    nvals := len pretend INT ; -- pretend since :: fails
    npairs := (nvals - 1) quo 2 ;
    fullh := new(len, 0) ;
    (nvals = 0) => () ;
    fullh.1 := complex(h.1,0) ;
    (nvals = 1) => () ;
    fullh.(npairs+2) := complex(h.(npairs+2),0) ; -- need if even length
                                                  -- (not worth testing)
    for j in 1 .. npairs repeat {
      n1 := j + 1 ;
      n2 := nvals - j + 1 ;
      fullh.n1 := complex(h.n1, h.n2) ;
      fullh.n2 := complex(h.n1, -(h.n2)) ;
      }

    }
      
    fullh

  }

  packHS(v : Vector Complex R) : % == {

    local len : NNI ;
    local nonhs : String ==  "The argument of packHS is not Hermitian" ;
    local nvals, testprs, n1, n2 : INT ;
    local hpacked : Vector R ;
    local v1, v2 : Complex R ;
    local r1, i1, r2, i2 : R ;

    {
    len := # v ;
    nvals := len pretend INT ; -- pretend since :: fails
    testprs := nvals quo 2 ;
    hpacked := new(len, 0) ;
    (nvals = 0) => () ;
    if imag(v.1) ~= 0 
    then error [nonhs, " - the first element must be real."]
    else {
      hpacked.1 := real(v.1) ;
      (nvals = 1) => () ;
      for j in 1 .. testprs repeat {
        n1 := j + 1 ;
        n2 := nvals - j + 1 ;
        v1 := v.n1 ;
        v2 := v.n2 ;
        r1 := real v1 ;
        i1 := imag v1 ;
        r2 := real v2 ;
        i2 := imag v2 ;
        if r1 ~= r2 or i1 ~= -i2
        then if n1 = n2
	     then error [nonhs,
			 " - element ",
			 string(n1),
			 " must be real to be self-conjugate."]
             else error [nonhs,
                         " - elements ",
			 string(n1),
			 " and ",
			 string(n2),
			 " are not conjugate."]
        else {
	  hpacked.n2 := i1 ; -- This order means that when the tail of v
	  hpacked.n1 := r1 ; -- has odd length, the (real part) of its
			     -- middle element ends up in that position.
	  }
	}
      }
    }

    hpacked pretend %

  }

  local set!(x: %, i: INT, v: R): () == {
	(rep x).i := v;
  }
  conjHerm(h : %) : %  == {

    local len : NNI ;
    local nvals, npairs : INT ;
    local ch : % ;

    ch := copy h ;
    len := # h ;
    (len < 3) => ch ; -- these Hermitian sequences are self-conjugate.
    nvals := len pretend INT ; -- pretend since :: fails
    npairs := (nvals - 1) quo 2 ;
    for j in (nvals - npairs + 1) .. nvals repeat ch.j := - h.j ;
    ch

  }
   
  import from List OutputForm ;
  
  coerce(h : %) : OutputForm ==
    bracket commaSeparate [
       qelt(h, k) :: OutputForm for k in minIndex h .. maxIndex h]

}

#if NeverAssertThis

)lib herm

h0 := pHS([] :: List INT)

--       []

h1 := pHS [1]

--       [1]

h2 := pHS [1,2]

--       [1,2]

h3 := pHS [1,2,3]

--       [1,2,3]

h4 := pHS [1,2,3,4]

--       [1,2,3,4]

h5 := pHS [1,2,3,4,5]

--       [1,2,3,4,5]


f0 := expand h0

--       []

f1 := expand h1

--       [1]

f2 := expand h2

--       [1,2]

f3 := expand h3

--       [1,2 + 3%i,2 - 3%i]

f4 := expand h4

--       [1,2 + 4%i,3,2 - 4%i]

f5 := expand h5

--       [1,2 + 5%i,3 + 4%i,3 - 4%i,2 - 5%i]
      
packHS f0

--       []

packHS f1

--       [1]

packHS f2

--       [1,2]

packHS f3

--       [1,2,3]

packHS f4

--       [1,2,3,4]

packHS f5

--       [1,2,3,4,5]

packHS vector[%i,3,3,3]

-- Error signalled from user code:
--    The argument of packHS is not Hermitian - the first element must
--    be real.

packHS vector [1, 3, 5, 7]

-- Error signalled from user code:
--    The argument of packHS is not Hermitian - elements 2 and 4 are 
--    not conjugate.

packHS [1, 3, %i, 3]

-- Error signalled from user code:
--    The argument of packHS is not Hermitian - element 3 must be real
--    to be self-conjugate.

conjHerm h0

--       []

conjHerm h1

--       [1]

conjHerm h2

--       [1,2]

conjHerm h3

--       [1,2,- 3]

conjHerm h4

--       [1,2,3,- 4]

conjHerm h5

--       [1,2,3,- 4,- 5]

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>>

<<PackedHermitianSequence>>
@
\eject
\begin{thebibliography}{99}
\bibitem{1} nothing
\end{thebibliography}
\end{document}