Robert Smith  committed 73479a2

Add PERM= and PERM=*

  • Participants
  • Parent commits 8bfa442

Comments (0)

Files changed (3)

 perm-eval* : one-based (standard) reference with out-of-bounds handling
 perm-inverse-eval : one-based (standard) reference of inverse
 perm-inverse-eval* : one-based (standard) reference of inverse with out-of-bounds handling
+perm= : check for equality
+perm=* : check for equality of different sized perms
 perm-size : the size of the permutation (number of mapped elements)
 perm-length : number of inversions
 perm-even-p |

File package.lisp

+   #:perm=
+   #:perm=*

File permutation.lisp

       (position n (perm.spec perm))))
+(defun perm= (perm other-perm)
+  "Are PERM and OTHER-PERM mathematically equal? (Note: Different sized perms are considered unequal. See PERM=* for extended equality.)"
+  (let ((size (perm-size perm)))
+    (and (= size (perm-size other-perm))
+         (loop :for i :from 1 :to size
+               :always (= (perm-eval perm i)
+                          (perm-eval other-perm i))))))
+(defun perm=* (perm other-perm)
+  "Are PERM and OTHER-PERM mathematically equal when viewed as functions on naturals? (Note: For inequality on different sized perms, see PERM=.)"
+  (loop :for i :from 1 :to (max (perm-size perm)
+                                (perm-size other-perm))
+        :always (= (perm-eval* perm i)
+                   (perm-eval* other-perm i))))
 (defun perm-size (perm)
   "The size of a permutation PERM."
   (1- (length (perm.spec perm))))
 (defun cycles-to-one-line (cycles)
   "Convert CYCLES to one-line notation. This is not the same as
-  (%make-perm :spec (coerce (cons 0 (mapcan 'identity cycles)) 'vector)))
+  (%make-perm :spec (coerce (cons 0 (mapcan 'identity cycles)) 'vector)))