;;; Lisplab, level2-operator.lisp ;;; Level2, non-specialized methods. ;;; Copyright (C) 2009 Joern Inge Vestgaarden ;;; ;;; This program is free software; you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 2 of the License, or ;;; (at your option) any later version. ;;; ;;; This program is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License along ;;; with this program; if not, write to the Free Software Foundation, Inc., ;;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. (in-package :lisplab) #| (defmethod .complex ((a vector-base) (b vector-base)) (.+ a (.* %i b))) (defmethod .complex ((a vector-base) b) (.+ a (.* %i b))) (defmethod .complex (a (b vector-base)) (.+ a (.* %i b))) |# (defmethod .some (pred (a vector-base) &rest args) (dotimes (i (size a)) (when (apply pred (mapcar (lambda (x) (vref x i)) (cons a args))) (return-from .some t))) nil) (defmethod .every (pred (a vector-base) &rest args) (dotimes (i (size a)) (unless (apply pred (mapcar (lambda (x) (vref x i)) (cons a args))) (return-from .every nil))) t) #| ;;;; Basic boolean operators (defmethod .= ((a vector-base) (b vector-base) &optional acc) (if acc (.every (lambda (a b) (.= a b acc)) a b) (.every #'.= a b))) (defmethod .= ((a vector-base) (b number) &optional acc) (if acc (.every (lambda (a) (.= a b acc)) a) (.every (lambda (a) (.= a b)) a))) (defmethod .= ((a number) (b vector-base) &optional acc) (if acc (.every (lambda (b) (.= a b acc)) b) (.every (lambda (b) (.= a b)) b))) (defmethod ./= ((a vector-base) (b vector-base) &optional acc) (not (.= a b acc))) (defmethod ./= ((a vector-base) (b number) &optional acc) (not (.= a b acc))) (defmethod ./= ((a number) (b vector-base) &optional acc) (not (.= a b acc))) (defmacro def-vector-base-boolean-operator (op) (let ((a (gensym)) (b (gensym))) `(progn (defmethod ,op ((,a vector-base) (,b vector-base)) (.every #',op ,a ,b)) (defmethod ,op ((,a vector-base) (,b number)) (.every (lambda (,a) (,op ,a ,b)) ,a)) (defmethod ,op ((,a number) (,b vector-base)) (.every (lambda (,b) (,op ,a ,b)) ,b))))) (def-vector-base-boolean-operator .<) (def-vector-base-boolean-operator .<=) (def-vector-base-boolean-operator .>) (def-vector-base-boolean-operator .>=) |# ;;; Element-wise operators (defmethod mmap-operator (op (a vector-base) b output) (mmap-into output (lambda (x) (funcall op x b)) a)) (defmethod mmap-operator (op a (b vector-base) output) (mmap-into output (lambda (x) (funcall op a x)) b)) (defmethod mmap-operator (op (a vector-base) (b vector-base) output) (mmap-into output op a b)) (defmacro defmethod-operator-vector-vector (name) (let ((a (gensym)) (b (gensym))) `(defmethod ,name ((,a vector-base) (,b vector-base)) (mmap-operator #',name ,a ,b (mcreate ,a))))) (defmacro defmethod-operator-vector-any (name) (let ((a (gensym)) (b (gensym)) (out (gensym))) `(defmethod ,name ((,a vector-base) ,b) (let ((,out (mcreate ,a))) (mmap-operator #',name ,a ,b ,out))))) (defmacro defmethod-operator-any-vector (name) (let ((a (gensym)) (b (gensym)) (out (gensym))) `(defmethod ,name (,a (,b vector-base)) (let ((,out (mcreate ,b))) (mmap-operator #',name ,a ,b ,out))))) (defmacro def-each-element-operator (name) "Makes so that the binary operator can map element-wice." `(progn (defmethod-operator-vector-vector ,name) (defmethod-operator-vector-any ,name) (defmethod-operator-any-vector ,name) 'thats-it)) (def-each-element-operator .complex) (def-each-element-operator .add) (def-each-element-operator .mul) (def-each-element-operator .div) (def-each-element-operator .sub) (def-each-element-operator .expt) (def-each-element-operator .max) (def-each-element-operator .min) (def-each-element-operator .and) (def-each-element-operator .nand) (def-each-element-operator .or) (def-each-element-operator .nor) (def-each-element-operator .xor) (def-each-element-operator .=) (def-each-element-operator ./=) (def-each-element-operator .<) (def-each-element-operator .<=) (def-each-element-operator .>) (def-each-element-operator .>=)