-
Notifications
You must be signed in to change notification settings - Fork 20
/
zeta-transform.lisp
82 lines (77 loc) · 2.82 KB
/
zeta-transform.lisp
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
(defpackage :cp/zeta-transform
(:use :cl)
(:export #:zeta-subtransform! #:zeta-supertransform!
#:moebius-subtransform! #:moebius-supertransform!)
(:documentation "Provides Zeta/Moebius transforms w.r.t. subsets and
supersets."))
(in-package :cp/zeta-transform)
;; TODO: Should we integrate zeta- and moebius- into a function?
(declaim (inline zeta-subtransform!))
(defun zeta-subtransform! (vector &optional (plus #'+))
"Does the fast zeta transform w.r.t. subsets. The length of VECTOR must be a
power of two."
(declare (vector vector))
(let* ((n (length vector))
;; cardinality of the underlying set
(card (- (integer-length n) 1)))
(assert (= 1 (logcount n)))
(dotimes (i card)
(let ((mask (ash 1 i)))
(dotimes (j n)
(unless (zerop (logand j mask))
(setf (aref vector j)
(funcall plus
(aref vector j)
(aref vector (logxor j mask))))))))
vector))
(declaim (inline zeta-supertransform!))
(defun zeta-supertransform! (vector &optional (plus #'+))
"Does the fast zeta transform w.r.t. supersets. The length of VECTOR must be a
power of two."
(declare (vector vector))
(let* ((n (length vector))
(card (- (integer-length n) 1)))
(assert (= 1 (logcount n)))
(dotimes (i card)
(let ((mask (ash 1 i)))
(dotimes (j n)
(when (zerop (logand j mask))
(setf (aref vector j)
(funcall plus
(aref vector j)
(aref vector (logior j mask))))))))
vector))
(declaim (inline moebius-subtransform!))
(defun moebius-subtransform! (vector &optional (minus #'-))
"Does the inverse of ZETA-SUBTRANSFORM! The length of VECTOR must be a power
of two."
(declare (vector vector))
(let* ((n (length vector))
(card (- (integer-length n) 1)))
(assert (= 1 (logcount n)))
(dotimes (i card)
(let ((mask (ash 1 i)))
(dotimes (j n)
(unless (zerop (logand j mask))
(setf (aref vector j)
(funcall minus
(aref vector j)
(aref vector (logxor j mask))))))))
vector))
(declaim (inline moebius-supertransform!))
(defun moebius-supertransform! (vector &optional (minus #'-))
"Does the inverse of ZETA-SUPERTRANSFORM!. The length of VECTOR must be a
power of two."
(declare (vector vector))
(let* ((n (length vector))
(card (- (integer-length n) 1)))
(assert (= 1 (logcount n)))
(dotimes (i card)
(let ((mask (ash 1 i)))
(dotimes (j n)
(when (zerop (logand j mask))
(setf (aref vector j)
(funcall minus
(aref vector j)
(aref vector (logior j mask))))))))
vector))