Commit f022b844 authored by Liam M. Healy's avatar Liam M. Healy
Browse files

Mathematical functions on grids

Define monadic and dyadic with scalar functions on grids.  In some
cases, the same result is available for foreign-arrays using GSLL, but
these definitions apply to all grids.  Improved formatting in "plain"
style for 2D arrays; does not define ends of rows and columns but also
does not inject TeXisms.
parent 043a16fd
;; Antik system definition
;; Liam Healy 2010-12-24 09:43:28EST antik.asd
;; Time-stamp: <2011-01-02 18:52:11EST antik.asd>
;; Time-stamp: <2011-01-03 22:06:24EST antik.asd>
;; Copyright 2011 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
......@@ -66,6 +66,7 @@
(:file "higher" :depends-on (affi))
(:file "copy")
(:file "iterate" :depends-on ("compose"))
(:file "mathematics" :depends-on ("map"))
(:module tests
:components
((:file "grids")))))
......
;; Basic elementwise mathematics on grids.
;; Liam Healy 2011-01-03 21:04:20EST mathematics.lisp
;; Time-stamp: <2011-01-03 22:19:43EST mathematics.lisp>
;; Copyright 2011 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; 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 3 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, see <http://www.gnu.org/licenses/>.
;;; These are elementary matematical operations. At present, the
;;; scalars must be numbers and the components of arrays must also be
;;; numbers. This might be broadened later.
(in-package :antik)
(defmacro def-monadic (operation)
"Define a method of the operation that operates elementwise on a grid."
`(defmethod ,operation ((x t))
(if (grid:gridp x)
(grid:map-grid :source x :element-function ',operation)
(call-next-method))))
(def-monadic sin)
(def-monadic cos)
(def-monadic tan)
(def-monadic asin)
(def-monadic acos)
(def-monadic exp)
(def-monadic abs)
;;; Note that GSLL duplicates some of this functionality for
;;; foreign-array in antik/linear-algebra.lisp.
(defmacro def-dyadic-scalar (method operation &optional (left t) (right t))
"Define a method of the operation that operates elementwise on a grid."
`(progn
,@(when right
`((defmethod ,method ((a t) (b number))
(if (grid:gridp a)
(grid:map-grid
:source a
:element-function (alexandria:rcurry ',operation b))
(call-next-method)))))
,@(when left
`((defmethod ,method ((a number) (b t))
(if (grid:gridp b)
(grid:map-grid
:source b
:element-function (alexandria:curry ',operation a))
(call-next-method)))))))
(def-dyadic-scalar *i cl:*)
(def-dyadic-scalar /i cl:/)
(def-dyadic-scalar +i cl:+)
(def-dyadic-scalar -i cl:-)
(def-dyadic-scalar expt cl:expt nil)
(defmethod log ((a t) &optional base)
(if (grid:gridp a)
(if base
(grid:map-grid
:source a
:element-function
(alexandria:rcurry 'cl:log base))
(grid:map-grid
:source a
:element-function 'cl:log))
(call-next-method)))
(defmethod atan ((num t) &optional (den 1))
(if (grid:gridp num)
(grid:map-grid
:source num
:element-function
(alexandria:rcurry 'atan den))
(call-next-method)))
;; Generic functions for generalized numbers
;; Liam Healy Tue Feb 9 1999 - 21:38
;; Time-stamp: <2011-01-02 18:49:57EST generic.lisp>
;; Time-stamp: <2011-01-03 21:52:33EST generic.lisp>
;; Copyright 2011 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
......@@ -139,7 +139,7 @@
;; Ordinary number multiplication
(:method ((a number) (b number))
(cl:* a b))
(:method (a b) (funcall-dyadic '*i a b 'null))
(:method (a b) (funcall-dyadic '*i a b))
;; Two sequences
(:method ((a sequence) (b sequence))
(if (null b)
......@@ -306,14 +306,16 @@
(:method ((x sequence))
(map (type-of x) #'exp x)))
(defgeneric log (num)
(defgeneric log (num &optional base)
(:documentation "The natural logarithm of the generalized or regular number.")
(:method ((num rational))
(cl:log (coerce num *read-default-float-format*)))
(:method ((num number))
(cl:log num))
(:method ((x sequence))
(map (type-of x) #'log x)))
(:method ((num number) &optional base)
(if base
(cl:log num base)
(cl:log num)))
(:method ((x sequence) &optional base)
(if base
(map (type-of x) (alexandria:rcurry 'log base) x)
(map (type-of x) #'log x))))
(defgeneric abs (num)
(:documentation "The absolute value.")
......
;; Format numerical output
;; Liam Healy Wed Dec 11 2002 - 16:37
;; Time-stamp: <2011-01-02 18:20:33EST format-output.lisp>
;; Time-stamp: <2011-01-03 22:53:15EST format-output.lisp>
;; Copyright 2011 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
......@@ -188,6 +188,7 @@
;;; This is still under development, but works for TeX output.
;;; Should use tex-format-separator?
;;; And/or tex-format-separator should be better when used above.
;;; Does not distinguish rows and columns for plain output.
(defmethod nf ((object array) &optional (stream *standard-output*))
(if (= (array-rank object) 2)
(progn
......@@ -198,15 +199,16 @@
(array-dimension object 1)
:initial-element #\r)))
(loop for i from 0 below (array-dimension object 0)
do
(when (plusp i)
(format stream " \\\\~&~8t"))
(loop for j from 0 below (array-dimension object 1)
do
(when (plusp j) (format stream "~&~8t& "))
(nf (aref object i j) stream)))
do
(when (plusp i)
(format stream (if texstyle " \\\\~&~8t" "~&~8t")))
(loop for j from 0 below (array-dimension object 1)
do
(when (plusp j)
(format stream (if texstyle "~&~8t& " "~&~8t")))
(nf (aref object i j) stream)))
(when texstyle (format stream "~&\\end{array}\\right]")))
(call-next-method)))
(call-next-method)))
(defun object-as-nf (object)
"Define an object exactly as the current nf options
......
;; Physical quantity with a magnitude that is a grid.
;; Liam Healy 2011-01-01 12:05:37EST grid.lisp
;; Time-stamp: <2011-01-02 19:08:19EST class.lisp>
;; Time-stamp: <2011-01-03 22:23:53EST class.lisp>
;; Copyright 2011 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
......@@ -22,7 +22,7 @@
(in-package :antik)
(export '(make-pq *zero-is-dimensionless*))
(export '(physical-quantity *zero-is-dimensionless* make-pq))
;;;;****************************************************************************
;;;; The class and functions to make instances
......
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