Commit 35688357 authored by Liam Healy's avatar Liam Healy

Define defmobject for permutation

Permutation object is now defined by defmobject. This should be more
portable to various word sizes.
parent cb9c6bfd
;; Permutations ;; Permutations
;; Liam Healy, Sun Mar 26 2006 - 11:51 ;; Liam Healy, Sun Mar 26 2006 - 11:51
;; Time-stamp: <2016-06-14 23:31:07EDT permutation.lisp> ;; Time-stamp: <2016-12-03 21:03:56CST permutation.lisp>
;; ;;
;; Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2014, 2016 Liam M. Healy ;; Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2014, 2016 Liam M. Healy
;; Distributed under the terms of the GNU General Public License ;; Distributed under the terms of the GNU General Public License
...@@ -27,57 +27,41 @@ ...@@ -27,57 +27,41 @@
;;;; Permutation structure and CL object ;;;; Permutation structure and CL object
;;;;**************************************************************************** ;;;;****************************************************************************
(defclass permutation (defmobject permutation
(#+int64 grid:vector-unsigned-byte-64 #+int32 grid:vector-unsigned-byte-32) "gsl_permutation"
() ((size :sizet))
(:documentation "GSL permutations.")) "permutation"
:initialize-suffix "init")
(defmethod initialize-instance :after
((object permutation) &key dimensions &allow-other-keys) ;;; Need this because default the method defined by defmobject has c-return = :int.
(let ((mptr (cffi:foreign-alloc '(:struct gsl-permutation-c)))) (defmfun reinitialize-instance ((object permutation) &key)
(setf (grid:metadata-slot object 'mpointer) "gsl_permutation_init"
mptr (((mpointer object) :pointer))
(cffi:foreign-slot-value mptr '(:struct gsl-permutation-c) 'data) :definition :method
(grid:foreign-pointer object) :qualifier :after
(cffi:foreign-slot-value mptr '(:struct gsl-permutation-c) 'size) :return (object)
(first dimensions)) :c-return :void
(tg:finalize object (lambda () (cffi:foreign-free mptr))))) :export nil
:index (reinitialize-instance permutation))
(export 'make-permutation)
(defun make-permutation (n &optional (initialize t)) (defun make-permutation (size &optional (initialize t))
"Make the object representing a permutation of n objects. ;; Define this outside of defmobject in order to add the optional argument.
If n is a permutation, make a new permutation of the same size. If "Create the GSL object representing a permutation (class PERMUTATION)."
initialize is T (default), set to the identity permutation if n is (let ((object (make-instance 'permutation :size size)))
an integer, or copy the permutation if it's a permutation." (when initialize (reinitialize-instance object))
(let ((perm object))
(make-instance
'permutation
:element-type '(unsigned-byte #+int64 64 #+int32 32)
:dimensions (if (typep n 'permutation) (grid:dimensions n) (list n)))))
(when initialize
(if (typep n 'permutation)
(error "not available yet") ; (copy perm n)
(set-identity perm)))
perm))
(defmethod print-object ((object permutation) stream) (defmethod print-object ((object permutation) stream)
(print-unreadable-object (object stream :type t) (print-unreadable-object (object stream :type t)
(princ (grid:contents object) stream))) (format stream "size ~d contents ~a" (size object) (grid:contents object))))
;;;;**************************************************************************** ;;;;****************************************************************************
;;;; Setting values ;;;; Setting/getting values
;;;;**************************************************************************** ;;;;****************************************************************************
(defmfun set-identity ((permutation permutation)) (defmethod set-identity ((p permutation))
"gsl_permutation_init" "Initialize the permutation p to the identity, i.e. (0,1,2,...,n-1)."
(((mpointer permutation) :pointer)) (reinitialize-instance p))
:definition :method
:c-return :void
:outputs (permutation)
:return (permutation)
:documentation ; FDL
"Initialize the permutation p to the identity, i.e.
(0,1,2,...,n-1).")
(defmfun perm-copy (source destination) (defmfun perm-copy (source destination)
"gsl_permutation_memcpy" "gsl_permutation_memcpy"
...@@ -109,6 +93,12 @@ ...@@ -109,6 +93,12 @@
:documentation ; FDL :documentation ; FDL
"Exchanges the ith and jth elements of the permutation p.") "Exchanges the ith and jth elements of the permutation p.")
(defmfun grid:aref ((p permutation) &rest indices)
"gsl_permutation_get"
(((mpointer p) :pointer) ((first indices) :sizet))
:definition :method
:c-return :sizet)
;;;;**************************************************************************** ;;;;****************************************************************************
;;;; Permutation properties ;;;; Permutation properties
;;;;**************************************************************************** ;;;;****************************************************************************
...@@ -122,6 +112,9 @@ ...@@ -122,6 +112,9 @@
:documentation ; FDL :documentation ; FDL
"The size of the permutation p.") "The size of the permutation p.")
(defmethod grid:dimensions ((p permutation))
(list (size p)))
(defmfun permutation-data (p) (defmfun permutation-data (p)
"gsl_permutation_data" "gsl_permutation_data"
(((mpointer p) :pointer)) (((mpointer p) :pointer))
...@@ -226,8 +219,7 @@ ...@@ -226,8 +219,7 @@
:outputs (data) :outputs (data)
:return (data) :return (data)
:documentation ; FDL :documentation ; FDL
"Apply the permutation p to the array data of "Apply the permutation p to the array data of size n with stride stride.")
size n with stride stride.")
(defmfun permute-inverse (defmfun permute-inverse
((p permutation) (v vector) &optional size stride) ((p permutation) (v vector) &optional size stride)
...@@ -238,12 +230,7 @@ ...@@ -238,12 +230,7 @@
:outputs (v) :outputs (v)
:return (v) :return (v)
:documentation ; FDL :documentation ; FDL
"Apply the permutation p to the elements of the "Apply the permutation p to the elements of the vector v considered as a row-vector acted on by a permutation matrix from the right, v' = v P. The jth column of the permutation matrix P is given by the p_j-th column of the identity matrix. The permutation p and the vector v must have the same length.")
vector v considered as a row-vector acted on by a permutation
matrix from the right, v' = v P. The jth column of the
permutation matrix P is given by the p_j-th column of the
identity matrix. The permutation p and the vector v must
have the same length.")
(defmfun permute-inverse (defmfun permute-inverse
(p (data #.grid:+foreign-pointer-class+) &optional (size 1) (stride 1)) (p (data #.grid:+foreign-pointer-class+) &optional (size 1) (stride 1))
...@@ -266,9 +253,7 @@ ...@@ -266,9 +253,7 @@
:outputs (p) :outputs (p)
:return (p) :return (p)
:documentation ; FDL :documentation ; FDL
"Combine the two permutations pa and pb into a "Combine the two permutations pa and pb into a single permutation p where p = pa . pb. The permutation p is equivalent to applying pb first and then pa.")
single permutation p where p = pa . pb. The permutation
p is equivalent to applying pb first and then pa.")
;;;;**************************************************************************** ;;;;****************************************************************************
;;;; Permutations in cyclic form ;;;; Permutations in cyclic form
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment