This file is indexed.

/usr/share/acl2-7.2dfsg/books/misc/sin-cos.lisp is in acl2-books-source 7.2dfsg-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
; sin-cos.lisp  --  series approximations to SIN and COS
; Copyright (C) 1997  Computational Logic, Inc.
; License: A 3-clause BSD license.  See the LICENSE file distributed with ACL2.

;;;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;;;
;;;    sin-cos.lisp
;;;
;;;    Unlimited series approximations to SIN and COS, plus functions for
;;;    creating SIN/COS tables.
;;;
;;;    Bishop Brock and Calvin Harrison
;;;    Computational Logic, Inc.
;;;    1717 West 6th Street, Suite 290
;;;    Austin, Texas 78703
;;;    brock@cli.com
;;;
;;;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

; Modified by Jared, 2014-10, to port documentation to xdoc.

;;;****************************************************************************
;;;
;;;  Environment
;;;
;;;****************************************************************************

(in-package "ACL2")
(include-book "xdoc/top" :dir :system)

(defxdoc sin-cos
  :parents (miscellaneous)
  :short "SIN/COS approximations.")

;;;****************************************************************************
;;;
;;;  Series approximations of sin/cos.
;;;
;;;****************************************************************************

(defsection compute-series
  :parents (sin-cos)
  :short "Series approximation to SIN/COS."
  :long #{"""<p>This function is used to calculate the following Maclaurin series.
To compute SIN:</p>

@([
    x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + ...
])

<p>To compute COS:</p>

@([
    1- \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + ...
])

<p>Arguments:</p>

<ul>
<li> x      -- x</li>
<li> parity -- T to add the new term, NIL to subtract the new term.</li>
<li> ex     -- @($x^{num}$)</li>
<li> fact   -- @($num!$)</li>
<li> itr    -- Number of iterations</li>
<li> ans    -- Accumulated answer.</li>
</ul>"""}

  (defun compute-series (x parity ex fact num itr ans)
    (declare (xargs :guard (and (rationalp x)
                                (booleanp parity)
                                (rationalp ex)
                                (integerp fact)
                                (> fact 0)
                                (integerp num)
                                (>= num 0)
                                (integerp itr)
                                (>= itr 0)
                                (rationalp ans))))
    (if (zp itr)
        ans
      (compute-series x (not parity) (* x x ex) (* (+ 2 num) (+ 1 num) fact)
                      (+ 2 num) (1- itr) (if parity
                                             (+ ans (/ ex fact))
                                           (- ans (/ ex fact)))))))

(local
 (defthm type-of-compute-series
  (implies
   (and (rationalp x)
	(rationalp ex)
	(integerp fact)
	(integerp num)
	(rationalp ans))
   (rationalp (compute-series x parity ex fact num itr ans)))
  :rule-classes :type-prescription))

(defsection fast-compute-series
  :parents (sin-cos)
  :short "Efficient series approximation to SIN/COS."
  :long #{"""<p>This function is used to calculate the following Maclaurin series.
To compute SIN:</p>

@([
    x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + ...
])

<p>To compute COS:</p>

@([
    1- \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + ...
])

<p>Rather than accumulating each term as shown, we instead compute the
numerator and denominator of the sum, and return these two values.  This avoids
the necessity of reducing each rational as it is accumulated.  On one set of
examples this procudure was almost an order of magnitude faster than the simple
summation given by @(see COMPUTE-SERIES).</p>

<p>Given @($x^2$) as @($\frac{num-x^2}{denom-x^2}$), and a partial sum
@($\frac{num-sum}{denom-sum}$), we compute the next partial sum as:</p>

@([
\frac{num-sum}{denom-sum}
   *
\frac{
   denom-x^2 (n + 1) (n + 2)
}{ denom-x^2 (n + 1) (n + 2) }
  *
\frac{
    num-x^n num-x^2
}{  denom-x^2 (n + 1) (n + 2)  }

])

<p>Again, the rationals are not actually computed, and instead we simply return
the numerator and denominator of the answer.</p>

<p>Arguments:</p>

<ul>
<li>num-x^2   -- (Numerator of x)^2.</li>
<li>denom-x^2 -- (Denominator of x)^2.</li>
<li>num-x^n   -- @($(num-x)^n$)</li>
<li>num-sum   -- Numerator of partial sum.</li>
<li>denom-sum -- Denominator of partial sum.</li>
<li>n         -- n</li>
<li>parity    -- T to add next term, NIL to subtract next term.</li>
<li>itr       -- Number of iterations to perform.</li>
</ul>"""}

  (defun fast-compute-series
    (num-x^2 denom-x^2 num-x^n num-sum denom-sum n parity itr)
    (declare (xargs :guard (and (integerp num-x^2)
                                (integerp denom-x^2)
                                (not (= denom-x^2 0))
                                (integerp num-x^n)
                                (integerp num-sum)
                                (integerp denom-sum)
                                (not (= denom-sum 0))
                                (integerp n)
                                (<= 0 n)
                                (booleanp parity)
                                (integerp itr)
                                (<= 0 itr))
                    :guard-hints (("Goal" :in-theory (disable DISTRIBUTIVITY)))))

    (if (zp itr)
        (mv num-sum denom-sum)
      (let*
          ((n+1*n+2          (* (+ n 1) (+ n 2)))
           (multiplier       (* denom-x^2 n+1*n+2))
           (new-denom-sum    (* denom-sum multiplier))
           (adjusted-num-sum (* num-sum multiplier))
           (new-num-x^n      (* num-x^n num-x^2))
           (new-num-sum      (if parity
                                 (+ adjusted-num-sum new-num-x^n)
                               (- adjusted-num-sum new-num-x^n))))

        (fast-compute-series num-x^2 denom-x^2 new-num-x^n
                             new-num-sum new-denom-sum
                             (+ 2 n) (not parity) (1- itr))))))

(local
 (defthm type-of-fast-compute-series
  (implies
   (and (integerp num-x^2)
	(integerp denom-x^2)
	(not (= denom-x^2 0))
	(integerp num-x^n)
	(integerp num-sum)
	(integerp denom-sum)
	(not (= denom-sum 0))
	(integerp n)
	(<= 0 n)
	(booleanp parity)
	(integerp itr)
	(<= 0 itr))
   (and
    (integerp
     (mv-nth 0 (fast-compute-series num-x^2 denom-x^2 num-x^n num-sum denom-sum
				    n parity itr)))
    (integerp
     (mv-nth 1 (fast-compute-series num-x^2 denom-x^2 num-x^n num-sum denom-sum
				    n parity itr)))
    (not
     (equal
      (mv-nth 1
	      (fast-compute-series num-x^2 denom-x^2 num-x^n num-sum denom-sum
				   n parity itr))
      0))))
  :rule-classes
  ((:type-prescription
    :corollary
    (implies
     (and (integerp num-x^2)
	  (integerp denom-x^2)
	  (not (= denom-x^2 0))
	  (integerp num-x^n)
	  (integerp num-sum)
	  (integerp denom-sum)
	  (not (= denom-sum 0))
	  (integerp n)
	  (<= 0 n)
	  (booleanp parity)
	  (integerp itr)
	  (<= 0 itr))
     (integerp
      (mv-nth 0
	      (fast-compute-series num-x^2 denom-x^2 num-x^n num-sum denom-sum
				   n parity itr)))))
   (:type-prescription
    :corollary
    (implies
     (and (integerp num-x^2)
	  (integerp denom-x^2)
	  (not (= denom-x^2 0))
	  (integerp num-x^n)
	  (integerp num-sum)
	  (integerp denom-sum)
	  (not (= denom-sum 0))
	  (integerp n)
	  (<= 0 n)
	  (booleanp parity)
	  (integerp itr)
	  (<= 0 itr))
     (and
      (integerp
       (mv-nth 1
	       (fast-compute-series num-x^2 denom-x^2 num-x^n num-sum denom-sum
				    n parity itr)))
      (not
       (equal
	(mv-nth 1
		(fast-compute-series num-x^2 denom-x^2 num-x^n
				     num-sum denom-sum n parity itr))
	0))))))

  :hints
  (("Goal"
    :in-theory (disable distributivity))))) ;Too slow

(defsection fast-compute-cos
  :parents (sin-cos)
  :short "This function returns the numerator and denominator of a rational
approximation to cos(x) (in radians) by itr iterations of @(see
FAST-COMPUTE-SERIES)."

  (defun fast-compute-cos (x itr)
    (declare (xargs :guard (and (rationalp x)
                                (integerp itr)
                                (>= itr 0))))
    (fast-compute-series (* (numerator x) (numerator x))
                         (* (denominator x) (denominator x))
                         1 1 1 0 nil itr)))

(defsection fast-compute-sin
  :parents (sin-cos)
  :short "This function returns the numerator and denominator of a rational
approximation to sin(x) (in radians) by itr iterations of @(see
FAST-COMPUTE-SERIES)."

  (defun fast-compute-sin (x itr)
    (declare (xargs :guard (and (rationalp x)
                                (integerp itr)
                                (>= itr 0))))
    (fast-compute-series (* (numerator x) (numerator x))
                         (* (denominator x) (denominator x))
                         (numerator x) (numerator x) (denominator x) 1 nil itr)))

(defsection truncated-integer-cos
  :parents (sin-cos)
  :short "Integer approximation to @($cos(x) * scale$)."
  :long "<p>A rational approximation to @($cos(x)$), scaled up by scale, and
then @(see truncate)d to an integer.</p>"

  (defun truncated-integer-cos (x itr scale)
    (declare (xargs :guard (and (rationalp x)
                                (integerp itr)
                                (<= 0 itr)
                                (rationalp scale))
                    :guard-hints (("Goal" :in-theory (disable mv-nth)))))
    (mv-let (num denom) (fast-compute-cos x itr)
      (truncate (* num scale) denom))))

(defsection truncated-integer-sin
  :parents (sin-cos)
  :short "Integer approximation to @($sin(x) * scale$)."
  :long "<p>A rational approximation to @($cos(x)$), scaled up by scale, and
then @(see truncate)d to an integer.</p>"

  (defun truncated-integer-sin (x itr scale)
    (declare (xargs :guard (and (rationalp x)
                                (integerp itr)
                                (<= 0 itr)
                                (rationalp scale))
                    :guard-hints (("Goal" :in-theory (disable mv-nth)))))
    (mv-let (num denom) (fast-compute-sin x itr)
      (truncate (* num scale) denom))))

(defsection truncated-integer-sin/cos-table-fn
  :parents (sin-cos)
  :short "Helper for @(see SIN/COS-TABLE-FN)."
  :long #{"""<p>Note that this function has special code for @($0$),
@($\pi/2$), @($\pi$), and @($\frac{3}{2}\pi$).  The convergence of the series
at these points is problematic in the context of
truncation (vs. rounding).</p>"""}

  (defun truncated-integer-sin/cos-table-fn (sin/cos i n pie itr scale)
    (declare (xargs :guard (and (or (eq sin/cos :SIN) (eq sin/cos :COS))
                                (integerp i)
                                (<= 0 i)
                                (integerp n)
                                (<= 0 n)
                                (<= i n)
                                (rationalp pie)
                                (integerp itr)
                                (<= 0 itr)
                                (rationalp scale))
                    :measure (ifix (if (<= n i) 0 (- n i)))))
    (cond
     ((zp (- n i)) nil)
     (t (let ((i/n (/ i n)))
          (cons
           (cons i (case sin/cos
                     (:sin
                      (case i/n
                        ((0 1/2) 0)	        ;0, pi
                        (1/4 (truncate scale 1)) ;pi/2
                        (3/4 (truncate scale -1)) ;(3/2)pi
                        (t
                         (truncated-integer-sin (* 2 pie i/n) itr scale))))
                     (t
                      (case i/n
                        (0 (truncate scale 1))  ;0
                        ((1/4 3/4) 0)	        ;pi/2, (3/2)pi
                        (1/2 (truncate scale -1)) ;pi
                        (t
                         (truncated-integer-cos (* 2 pie i/n) itr scale))))))
           (truncated-integer-sin/cos-table-fn
            sin/cos (1+ i) n pie itr scale)))))))

(defsection truncated-integer-sin/cos-table
  :parents (sin-cos)
  :short "Create a scaled, truncated integer sin/cos table from @($0$) to @($2*\\pi$)."
  :long #{"""<p>This function creates a table of approximations to</p>

@([
    sin[cos]( (2 \pi i)/n ) * scale
])

<p>For @($i = 0,...,n-1$).  The result is an alist binding @($i$) to @($sin[cos](i)$).</p>

<p>Arguments:</p>

<ul>
<li>sin/cos -- :SIN or :COS.</li>
<li>n       -- Total number of table entries</li>
<li>pie     -- An approximation to @($\pi$) sufficiently accurate for the user's
               purposes.</li>
<li>itr     -- Required number of iterations of FAST-COMPUTE-SIN[COS]
               sufficient for user's accuracy.</li>
<li>scale   -- Scale factor.</li>
</ul>"""}

  (defun truncated-integer-sin/cos-table (sin/cos n pie itr scale)
    (declare (xargs :guard (and (or (eq sin/cos :SIN) (eq sin/cos :COS))
                                (integerp n)
                                (<= 0 n)
                                (rationalp pie)
                                (integerp itr)
                                (<= 0 itr)
                                (rationalp scale))))
    (truncated-integer-sin/cos-table-fn sin/cos 0 n pie itr scale)))