;;; Lisplab, level0-functions.lisp ;;; Contains ordinary functions and lisplab wrappers for common lisp functions. ;;; 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 matrix-p (x) nil) (defmethod vector-p (x) nil) (defun ^ (x n) "Synonym for expt" (expt x n)) (defun sqr (x) "Square function, (* x x)." (* x x)) (defun .+ (&rest args) "Generalized +. Reduces the arguments with .add." (if (and args (cdr args)) (reduce #'.add args) (car args))) (defun .* (&rest args) "Generalized *. Reduces the arguments with .mul." (if (and args (cdr args)) (reduce #'.mul args) (car args))) (defun ./ (&rest args) "Generalized /. Reduces the arguments with .div." (if (and args (cdr args)) (reduce #'.div args) (./ 1 (car args)))) (defun .- (&rest args) "Generalized -. Reduces the arguments with .sub." (if (and args (cdr args)) (reduce #'.sub args) (.- 0 (car args)))) (defun .^ (&rest args) "Generlized expt. Reduces the arguments with .expt." (reduce #'.expt args)) (defun .log (x &optional base) (if base (./ (.ln x) (.ln base)) (.ln x))) (defmethod .sgn ((a number)) (signum a)) (defmethod .abs ((a number)) (abs a)) (defmethod .re ((a number)) (realpart a)) (defmethod .im ((a number)) (imagpart a)) (defmethod .complex ((a number) (b number)) (complex a b)) (defmethod .max ((a number) (b number)) (max a b)) (defmethod .min ((a number) (b number)) (min a b)) (defmethod .= ((a number) (b number)) (if (= a b) 1 0)) (defmethod ./= ((a number) (b number)) (if (/= a b) 1 0)) (defmethod .< ((a number) (b number)) (if (< a b) 1 0)) (defmethod .<= ((a number) (b number)) (if (<= a b) 1 0)) (defmethod .> ((a number) (b number)) (if (> a b) 1 0)) (defmethod .>= ((a number) (b number)) (if (>= a b) 1 0)) ;;; The default operators on numbers (defmethod .add ((a number) (b number)) (+ a b)) (defmethod .mul ((a number) (b number)) (* a b)) (defmethod .div ((a number) (b number)) (/ a b)) (defmethod .sub ((a number) (b number)) (- a b)) (defmethod .expt ((a number) (b number)) (expt a b)) ;;;; logiclas operators (defmethod .and ((a integer) (b integer)) (logand a b)) (defmethod .nand ((a integer) (b integer)) (lognand a b)) (defmethod .or ((a integer) (b integer)) (logior a b)) (defmethod .nor ((a integer) (b integer)) (lognor a b)) (defmethod .xor ((a integer) (b integer)) (logxor a b)) ;;; The one input argument functions (define-constant +ordinary-functions-number-to-number-map+ '((.re . realpart)(.im . imagpart) (.abs . abs) (.conj . conjugate) (.not . lognot))) (defmacro expand-num-num () ;; TODO: optimize? why? (cons 'progn (mapcar (lambda (name) `(progn (defmethod ,(car name) ((a number)) (,(cdr name) a)))) +ordinary-functions-number-to-number-map+ ))) (expand-num-num) (define-constant +ordinary-functions-number-to-real-map+ '((.sin . sin) (.cos . cos) (.tan . tan) (.asin . asin) (.acos . acos) (.atan . atan) (.sinh . sinh) (.cosh . cosh) (.tanh . tanh) (.asinh . asinh) (.acosh . acosh) (.atanh . atanh) (.exp . exp) (.ln . log) (.sqrt . sqrt) (.sqr . sqr))) (defmacro expand-num-real () ;; TODO: optimize? why? (cons 'progn (mapcar (lambda (name) `(progn (defmethod ,(car name) ((a number)) (,(cdr name) (if (integerp a) ;; Coerce input to double float to prevent integer input ;; from becoming single-float (coerce a 'double-float) a))))) +ordinary-functions-number-to-real-map+ ))) (expand-num-real)