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

Sum of two pq grids

Fixes error encountered when columns from a dimensioned matrix with
zeros are added. Included new test in grid-array-units to test for the
problem.
parent 9a02ccbd
Loading
Loading
Loading
Loading
+9 −8
Changes for physical-quantities/physical-quantities.lisp: 9 added lines, 8 removed lines.
Original line number Diff line number Diff line
;; Objects that represent physical measurements.                             
;; Liam Healy Wed Mar  6 2002 - 09:04
;; Time-stamp: <2017-10-15 23:09:10EDT physical-quantities.lisp>
;; Time-stamp: <2017-11-23 19:09:19EST physical-quantities.lisp>

;; Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
@@ -125,31 +125,32 @@
		(or (not (scalar-dimension y)) (not (scalar-dimension x))))
	   ;; All same units, possibly one has scalar dimension
	   ;; Note, dimension returned is for the last element in the grid.
	   (values
	    (equal-dimension-pqgrid x y errorp zeros-have-any-dimension match-dimensionless)
	    nil))
	   (equal-dimension-pqgrid x y errorp zeros-have-any-dimension match-dimensionless))
	  (t (err-return)))))

(defun equal-dimension-pqgrid
    "If each of the corresponding elements of the grid have equal dimensions, return the last dimel, otherwise NIL."
    (grid0 grid1
     &optional
       (errorp t)
       (zeros-have-any-dimension *zero-is-dimensionless*)
       match-dimensionless)
  "If the two pqgrids have equal dimension, return the dimel and nil (not scalar dim)."
  ;; Cribbed from grid:inner for scalar cumulation
  (let ((w0 (affi:make-walker (grid::affi grid0)))
	(w1 (affi:make-walker (grid::affi grid1))))
    (loop for e0 = (funcall w0)
	  for e1 = (funcall w1)
	  while (and e0 e1)
	  for answer
	  for eqdim
	    = (equal-dimension
	       (grid:aref* grid0 e0)
	       (grid:aref* grid1 e1)
	       errorp zeros-have-any-dimension match-dimensionless)
	  until (not answer)
	  finally (return answer))))
	  collect eqdim into result-dims
	  while eqdim
	  finally (if eqdim
		      (return (values (cl:coerce result-dims 'vector) nil))
		      (return (values nil nil))))))

(defun dimel-or-not (object)
  "Find the dimel for this object (a physical dimension quantity or a dimel), or
+15 −3
Changes for tests/physical-quantities-grid.lisp: 15 added lines, 3 removed lines.
Original line number Diff line number Diff line
;; Tests of physical quantities
;; Liam Healy 2011-01-09 17:38:41EST tests.lisp
;; Time-stamp: <2017-10-14 17:11:21EDT physical-quantities-grid.lisp>
;; Time-stamp: <2017-11-23 22:44:41EST physical-quantities-grid.lisp>

;; Copyright 2011, 2013, 2014, 2015, 2017 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
@@ -162,8 +162,20 @@
				      :dimensions '(4)
				      :initial-contents '(1.0d0 2.0d0 3.0d0 4.0d0))
	     #(meter meter second second)))
  
  )
  (lisp-unit:assert-equal
   ;; See [[id:39fe9321-7260-4335-bb3e-735ba1d00e43][Alternative approach to mixed/scalar pq-grid]]
   ;; I'd really like to have a better test than the overall type, but assert-equal won't match on the actual object.
   ;; Anyway the major problem was this would get an error, so this test at least checks for that.
   '(PHYSICAL-QUANTITY (GRID:FOREIGN-ARRAY 3) DOUBLE-FLOAT)
   (let* ((grid:*default-grid-type* 'grid:foreign-array)
	  (ex73
	    (make-pq
	     (grid:matrix-from-columns
	      (grid:grid 0.0 0.0 1000.0)
	      (grid:grid 0.0 -700.0 -800.0)
	      (grid:grid 0.0 900.0 500.0))
	     'm)))
     (grid:specification (+ (grid:column ex73 0) (grid:column ex73 1))))))

#|
;;; Things that should be made to work.