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

Discourage use of copy-to

Added an argument to #'copy-to for the element type, defaulting to
*default-element-type*, with discouraging words for its use in files
(vs. interactively) unless all arguments are supplied, due to the
specials used that could be changed with unexpected results.  Change
uses of #'copy-to to #'copy because the element type needs to clearly
follow the latter function's rules.
parent 26220c37
;; Find the Jacobian matrix of a multivariate function
;; Liam Healy 2011-10-15 12:40:05EDT jacobian.lisp
;; Time-stamp: <2011-10-15 15:37:37EDT jacobian.lisp>
;; Time-stamp: <2013-12-25 19:09:57EST jacobian.lisp>
;; Copyright 2011 Liam M. Healy
;; Copyright 2011, 2013 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
......@@ -36,8 +36,8 @@ calculation. Step must be a scalar."
:dimensions (list (grid:dim0 point) matrix-or-nrows)
:grid-type (grid:gridp point)))))
(iter (for col from 0 below (grid:dim1 matrix))
(let ((pos (grid:copy-to point))
(neg (grid:copy-to point)))
(let ((pos (grid:copy point :grid-type *default-grid-type*))
(neg (grid:copy point :grid-type *default-grid-type*)))
(incf (grid:aref pos col) (/ step 2))
(decf (grid:aref neg col) (/ step 2))
(setf (grid:column matrix col)
......
;; Methods for lisp-unit functions
;; Liam Healy 2010-08-22 19:10:26EDT lisp-unit-extension.lisp
;; Time-stamp: <2011-01-09 22:15:51EST lisp-unit-extension.lisp>
;; Time-stamp: <2013-12-25 19:17:37EST lisp-unit-extension.lisp>
;; Copyright 2011 Liam M. Healy
;; Copyright 2011, 2013 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
......@@ -23,7 +23,8 @@
(defmethod norm-equal
((data1 grid:foreign-array) (data2 grid:foreign-array)
&optional (epsilon *epsilon*) (measure *measure*))
(norm-equal (grid:copy-to data1) (grid:copy-to data2)
(norm-equal (grid:copy data1 :grid-type 'grid:foreign-array :element-type 'double-float)
(grid:copy data2 :grid-type 'grid:foreign-array :element-type 'double-float)
epsilon measure))
(defmethod numerical-equal
......@@ -32,6 +33,7 @@
"Return true if the arrays are equal in dimension and each element
is equal according to :TEST."
(and (equal (grid:dimensions result1) (grid:dimensions result2))
(numerical-equal (grid:copy-to result1 'array)
(grid:copy-to result2 'array)
:test test)))
(numerical-equal
(grid:copy result1 :grid-type 'array :element-type 'double-float)
(grid:copy result2 :grid-type 'array :element-type 'double-float)
:test test)))
;; Copy objects
;; Liam Healy 2009-12-21 10:16:04EST copy.lisp
;; Time-stamp: <2010-08-19 15:37:52EDT copy.lisp>
;; Time-stamp: <2013-12-25 19:34:52EST copy.lisp>
;;
;; Copyright 2009, 2010 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
......@@ -64,9 +64,9 @@
destination))
(apply 'copy-grid object args)))
(defun copy-to (object &optional (type *default-grid-type*))
"Make a grid of the specified type from the object."
(copy object :grid-type type))
(defun copy-to (object &optional (type *default-grid-type*) (element-type *default-element-type*))
"Make a grid of the specified type from the object. This is different from #'copy in that it takes the default values for the destinatoin from special variables rather than the argument. This is provided only as a convenience for interactive use and should not be used in distributed software where the user may change the specials with unexpected results."
(copy object :grid-type type :element-type element-type))
(define-condition array-mismatch (error)
()
......
;; Read an indexed set of data
;; Liam Healy 2011-12-30 10:10:58EST indexed.lisp
;; Time-stamp: <2012-05-30 15:32:09EDT indexed.lisp>
;; Time-stamp: <2013-12-25 19:37:12EST indexed.lisp>
;;
;; Copyright 2011, 2012, 2013 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/>.
(in-package :grid)
......@@ -14,7 +30,7 @@
;;; - accept make-simple-grid keyword arguments
;;; - accept an existing grid, and fill it, rather than always creating a new one
;;; - do not require maximum-index-parameters (hard unless input is read twice or saved when read)
;;; - maybe make the input (file, stream, string) a kind of grid without random access, unless stream can be rewound; then you could just "copy-to" the grid you wanted.
;;; - maybe make the input (file, stream, string) a kind of grid without random access, unless stream can be rewound; then you could just copy the grid you wanted.
(defun read-indexed-data
(input maximum-index-parameters &rest args &key index-algorithm (default-value 0) (initial-element 0))
......
;; Define generic operators for GSL functions
;; Liam Healy 2011-01-01 09:51:47EST linear-algebra.lisp
;; Time-stamp: <2011-10-10 10:41:27EDT linear-algebra.lisp>
;; Time-stamp: <2013-12-25 19:51:29EST linear-algebra.lisp>
;; Copyright 2011 Liam M. Healy
;; Copyright 2011, 2013 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
......@@ -34,31 +34,31 @@
(gsl:matrix-product a b))
(defmethod *i ((a grid:foreign-array) (b number))
(gsl:elt* (grid:copy-to a 'grid:foreign-array) (coerce b (grid:element-type a))))
(gsl:elt* (grid:copy a) (coerce b (grid:element-type a))))
(defmethod *i ((a number) (b grid:foreign-array))
(gsl:elt* (grid:copy-to b 'grid:foreign-array) (coerce a (grid:element-type b))))
(gsl:elt* (grid:copy b) (coerce a (grid:element-type b))))
(defmethod /i ((a grid:foreign-array) (b number))
(gsl:elt* (grid:copy-to a 'grid:foreign-array)
(gsl:elt* (grid:copy a)
(cl:/ (coerce b (grid:element-type a)))))
(defmethod +i ((a grid:foreign-array) (b grid:foreign-array))
(gsl:elt+ (grid:copy-to a 'grid:foreign-array) b))
(gsl:elt+ (grid:copy a) b))
(defmethod +i ((a grid:foreign-array) (b number))
(gsl:elt+ (grid:copy-to a 'grid:foreign-array)
(gsl:elt+ (grid:copy a)
(coerce b (grid:element-type a))))
(defmethod +i ((a number) (b grid:foreign-array))
(gsl:elt+ (grid:copy-to b 'grid:foreign-array)
(gsl:elt+ (grid:copy b)
(coerce a (grid:element-type b))))
(defmethod -i ((a grid:foreign-array) (b grid:foreign-array))
(gsl:elt- (grid:copy-to a 'grid:foreign-array) b))
(gsl:elt- (grid:copy a) b))
(defmethod -i ((a grid:foreign-array) (b number))
(gsl:elt+ (grid:copy-to a 'grid:foreign-array) (cl:- b)))
(gsl:elt+ (grid:copy a) (cl:- b)))
(defmethod -i ((a number) (b grid:foreign-array))
(gsl:elt+ (- b) a))
......@@ -77,11 +77,12 @@
;;;;****************************************************************************
;;; Eventually it would be nice to be able to specify the method, instead of only using LU.
;;; These assume that the supplied arrays are of type grid:foreign-array.
(defun invert-matrix (matrix)
"Invert the matrix; return the inverse and determinant."
(multiple-value-bind (LU permutation signum)
(gsl:LU-decomposition (grid:copy-to matrix 'grid:foreign-array))
(gsl:LU-decomposition (grid:copy matrix))
(values
(gsl:LU-invert LU permutation)
(gsl:LU-determinant LU signum))))
......@@ -89,14 +90,14 @@
(defun determinant (matrix)
"Find the determinant of the matrix."
(multiple-value-bind (LU permutation signum)
(gsl:LU-decomposition (grid:copy-to matrix 'grid:foreign-array))
(gsl:LU-decomposition (grid:copy matrix))
(declare (ignore permutation))
(gsl:LU-determinant LU signum)))
(defun solve-linear (matrix vector)
"Solve the linear algebra equation."
(multiple-value-bind (upper permutation signum)
(gsl:LU-decomposition (grid:copy-to matrix))
(gsl:LU-decomposition (grid:copy matrix))
(declare (ignore signum))
(let ((initial-solution (gsl:LU-solve upper vector permutation T)))
(gsl:LU-refine matrix upper permutation vector initial-solution))))
......@@ -116,7 +117,7 @@
;;; These are direct tests of matrix inversion
(save-test
lu
(grid:copy-to
(grid:copy
(invert-matrix
(grid:make-foreign-array 'double-float
:dimensions '(2 2)
......@@ -127,7 +128,7 @@
#2A((-1.9999999999999998d0 1.0d0)
(1.4999999999999998d0 -0.49999999999999994d0)))
(MULTIPLE-VALUE-LIST
(GRID:COPY-TO
(GRID:COPY
(INVERT-MATRIX
(GRID:MAKE-FOREIGN-ARRAY 'DOUBLE-FLOAT :DIMENSIONS '(2 2)
:INITIAL-CONTENTS
......
;; Sample sets of values from a low-discrepancy (quasi-random) sequence
;; Liam Healy 2011-06-06 09:42:22EDT low-discrepancy-sequence.lisp
;; Time-stamp: <2013-09-25 12:26:46EDT low-discrepancy-sequence.lisp>
;; Time-stamp: <2013-12-25 19:38:25EST low-discrepancy-sequence.lisp>
;; Copyright 2011 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
......@@ -32,7 +32,7 @@
(vec (grid:make-foreign-array 'double-float :dimensions rank)))
(loop repeat count
do (gsl:qrng-get gen vec)
collect (grid:copy-to vec grid-type))))
collect (copy vec :grid-type grid-type :element-type 'double-float))))
(defun low-discrepancy-sample (count make-function &rest parameters)
"Call the make function to generate a set of objects with the
......
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