Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix arithmetic in GF(2) #75

Open
wants to merge 1 commit into
base: enhancement/misc-changes
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions magicl.asd
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
(:file "types/complex-single-float")
(:file "types/complex-double-float")
(:file "types/int32")
(:file "types/bit")
(:file "lapack-templates")
(:file "lapack-bindings")
(:file "constructors")
Expand Down
6 changes: 6 additions & 0 deletions src/high-level/specialize-constructor.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
((subtypep type 'double-float) 'vector/double-float)
((subtypep type '(complex single-float)) 'vector/complex-single-float)
((subtypep type '(complex double-float)) 'vector/complex-double-float)
((subtypep type 'bit) 'vector/bit)
((subtypep type '(signed-byte 32)) 'vector/int32)
(t (error "no compatible tensor constructor for type ~a" type))))
((cl:= 2 (length shape))
Expand All @@ -22,6 +23,7 @@
((subtypep type 'double-float) 'matrix/double-float)
((subtypep type '(complex single-float)) 'matrix/complex-single-float)
((subtypep type '(complex double-float)) 'matrix/complex-double-float)
((subtypep type 'bit) 'matrix/bit)
((subtypep type '(signed-byte 32)) 'matrix/int32)
(t (error "no compatible tensor constructor for type ~a" type))))
(t
Expand All @@ -30,6 +32,7 @@
((subtypep type 'double-float) 'tensor/double-float)
((subtypep type '(complex single-float)) 'tensor/complex-single-float)
((subtypep type '(complex double-float)) 'tensor/complex-double-float)
((subtypep type 'bit) 'tensor/bit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The amount of repetition is making be think there must be a better way. Probably in a different PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is one of the reasons I paused this.

((subtypep type '(signed-byte 32)) 'tensor/int32)
(t (error "no compatible tensor constructor for type ~a" type)))))
(cond
Expand All @@ -39,18 +42,21 @@
(double-float 'vector/double-float)
((complex single-float) 'vector/complex-single-float)
((complex double-float) 'vector/complex-double-float)
(bit 'vector/bit)
((signed-byte 32) 'vector/int32)))
((cl:= 2 (length shape))
(etypecase val
(single-float 'matrix/single-float)
(double-float 'matrix/double-float)
((complex single-float) 'matrix/complex-single-float)
((complex double-float) 'matrix/complex-double-float)
(bit 'matrix/bit)
((signed-byte 32) 'matrix/int32)))
(t (etypecase val
(single-float 'tensor/single-float)
(double-float 'tensor/double-float)
((complex single-float) 'tensor/complex-single-float)
((complex double-float) 'tensor/complex-double-float)
(bit 'tensor/bit)
((signed-byte 32) 'tensor/int32))))))

34 changes: 34 additions & 0 deletions src/high-level/types/bit.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;;;; bit.lisp
;;;;
;;;; Author: Juan M. Bello-Rivas

(in-package #:magicl)

(deftensor tensor/bit bit)

(defmatrix matrix/bit bit tensor/bit)

(defvector vector/bit bit tensor/bit)

(defcompatible
(lambda (tensor)
(case (order tensor)
(1 '(vector/bit tensor/bit))
(2 '(matrix/bit tensor/bit))
(t '(tensor/bit))))
tensor/bit
matrix/bit
vector/bit)

(defun mod2+ (source1 source2 &optional target)
(binary-operator (lambda (x y) (mod (+ x y) 2)) source1 source2 target))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are x and y guaranteed to be bits? If so, maybe consider logxor.


;;; TODO improve this.
(defmethod .+ ((source1 vector/bit) (source2 vector/bit) &optional target)
(mod2+ source1 source2 target))

(defmethod .+ ((source1 matrix/bit) (source2 matrix/bit) &optional target)
(mod2+ source1 source2 target))

(defmethod .+ ((source1 tensor/bit) (source2 tensor/bit) &optional target)
(mod2+ source1 source2 target))