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

Grids cannot be scalars in operations with sequences

When performing a mathematical operation with a sequence and a
non-sequence, the sequence cannot be a grid.  Checking for this
requires relocating generic.lisp with the generic mathematics
definitions to a place in which it will be loaded after grid:gridp is
defined.
parent ffaf4ace
;; Antik system definition
;; Liam Healy 2010-12-24 09:43:28EST antik.asd
;; Time-stamp: <2011-02-15 21:32:04EST antik.asd>
;; Time-stamp: <2011-02-21 19:53:38EST antik.asd>
;; Copyright 2011 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
......@@ -50,12 +50,9 @@
((:file "package")
(:file "utility")
(:file "conditions")
(:file "generic" :depends-on ("utility"))
(:file "object")
(:file "iterate")
(:file "intermediate") ; temporary
(:file "trigonometry" :depends-on ("generic")) ; should be relocated
))
(:file "intermediate"))) ; temporary
(:module grid
:depends-on (init)
:components
......@@ -67,6 +64,7 @@
(:file "affi" :depends-on ("utility"))
(:file "transformers" :depends-on ("affi"))))
(:file "functions" :depends-on ("util" affi))
(:file "generic" :depends-on ("functions"))
(:file "specification" :depends-on ("util" "functions" affi))
(:file "array" :depends-on ("functions" affi))
(:file "map" :depends-on (affi "functions"))
......@@ -108,7 +106,7 @@
(:file "scalar" :depends-on ("units"))
(:file "grid" :depends-on ("scalar"))
(:file "funcall")
(:file "unit-definitions" :depends-on ("units" "grid"))
(:file "unit-definitions" :depends-on ("units" "grid" "funcall"))
(:file "sysunit-definitions"
:depends-on ("units" "unit-definitions"))
(:file "physical-quantities" :depends-on ("units" "scalar"))
......@@ -153,6 +151,10 @@
"sysunit-definitions" "time-interval" "timepoint" "read-time"))
(:file "dtmath"
:depends-on ("dtspec" "read-time" "timepoint" "convert-timescale"))))
(:module mathematics
:serial t
:components
((:file "trigonometry")))
(:module cartesian
:serial t
:components
......
;; Generic functions for generalized numbers
;; Liam Healy Tue Feb 9 1999 - 21:38
;; Time-stamp: <2011-02-15 21:31:47EST generic.lisp>
;; Time-stamp: <2011-02-21 19:22:34EST generic.lisp>
;; Copyright 2011 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
......@@ -73,14 +73,18 @@
(defgeneric +i (a b)
(:documentation "Addition of generalized or regular numbers.")
(:method ((a number) (b number))
(cl:+ a b))
(cl:+ a b))
(:method (a b) (funcall-dyadic '+i a b 'zerop))
(:method ((a sequence) (b sequence))
(map (type-of a) #'+i a b))
(map (type-of a) #'+i a b))
(:method ((a sequence) b) ; b is a scalar
(map (type-of a) (lambda (x) (+i x b)) a))
(if (grid:gridp b)
(call-next-method)
(map (type-of a) (lambda (x) (+i x b)) a)))
(:method (a (b sequence)) ; a is a scalar
(map (type-of b) (lambda (x) (+i a x)) b)))
(if (grid:gridp a)
(call-next-method)
(map (type-of b) (lambda (x) (+i a x)) b))))
(defun + (&rest args)
"Addition of generalized or regular numbers with an
......@@ -101,11 +105,15 @@
(*i -1 a)
(map (type-of a) #'-i a b)))
(:method ((a sequence) b) ; b is a scalar
(map (type-of a) (lambda (x) (-i x b)) a))
(if (grid:gridp b)
(call-next-method)
(map (type-of a) (lambda (x) (-i x b)) a)))
(:method (a (b sequence)) ; a is a scalar
(if (null b)
(*i -1 a)
(map (type-of b) (lambda (x) (-i a x)) b))))
(if (grid:gridp a)
(call-next-method)
(map (type-of b) (lambda (x) (-i a x)) b)))))
(defun - (&rest args)
"Subtraction of generalized or regular numbers."
......@@ -115,23 +123,27 @@
(:documentation "Multiplication of generalized or regular numbers.")
;; Ordinary number multiplication
(:method ((a number) (b number))
(cl:* a b))
(cl:* a b))
(:method (a b) (funcall-dyadic '*i a b))
;; Two sequences
(:method ((a sequence) (b sequence))
(if (null b)
a ; null second arg - return first
(progn
(assert (eq (length a) (length b)) (a b)
"Sequence arguments are not of the same length.")
(map (type-of a) #'*i a b))))
(if (null b)
a ; null second arg - return first
(progn
(assert (eq (length a) (length b)) (a b)
"Sequence arguments are not of the same length.")
(map (type-of a) #'*i a b))))
;; Scalar multiplication of sequences
(:method ((a sequence) b) ; b is a scalar
(map (type-of a) (lambda (x) (*i x b)) a))
(if (grid:gridp b)
(call-next-method)
(map (type-of a) (lambda (x) (*i x b)) a)))
(:method (a (b sequence)) ; a is a scalar
(if (null b)
a ; null second arg - return first
(map (type-of b) (lambda (x) (*i a x)) b))))
(if (null b)
a ; null second arg - return first
(if (grid:gridp a)
(call-next-method)
(map (type-of b) (lambda (x) (*i a x)) b)))))
(defun * (&rest args)
"Multiplication of generalized or regular numbers with an
......@@ -158,10 +170,14 @@
;; Scalar division of sequences
(defmethod /i ((a sequence) b)
(map (type-of a) (lambda (x) (/i x b)) a))
(if (grid:gridp b)
(call-next-method)
(map (type-of a) (lambda (x) (/i x b)) a)))
(defmethod /i (a (b sequence))
(map (type-of b) (lambda (x) (/i a x)) b))
(if (grid:gridp a)
(call-next-method)
(map (type-of b) (lambda (x) (/i a x)) b)))
(defun / (&rest args)
"Division of generalized or regular numbers for an
......
;; Generic dyadic calls
;; Liam Healy 2011-02-15 21:30:46EST funcall.lisp
;; Time-stamp: <2011-02-15 21:31:44EST funcall.lisp>
;; Time-stamp: <2011-02-21 19:16:24EST funcall.lisp>
;;; This must be defined here because grid type must be loaded.
......@@ -29,10 +29,13 @@
return-other-monadic on the other argument if one passes that
test."
;; This should not be used if both a and b are numbers. Check that
;; both are arguments are not non-pq grids, otherwise it will
;; recurse indefinitely.
;; both arguments are not non-pq grids, otherwise it will recurse
;; indefinitely.
(assert (not (and (grid:gridp a) (not (typep a 'physical-quantity))
(grid:gridp b) (not (typep b 'physical-quantity)))))
(grid:gridp b) (not (typep b 'physical-quantity))))
(op a b)
"There is no known way to apply ~a to arguments of type ~a and ~a."
op (type-of a) (type-of b))
(cond
((and return-other (funcall return-other a))
(funcall (or return-other-monadic 'identity) b))
......@@ -54,5 +57,5 @@
;; This is redundant, but I'm leaving it here so that it
;; could be replaced with something more meaningful if
;; necessary. Can't use call-next-method here.
(error "Don't know how to coerce."))))))
(error "Don't know how to coerce ~a and ~a." a b))))))
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