From 0298e19cfc8de5d73fcf195c52418d36cf4763c7 Mon Sep 17 00:00:00 2001
From: Mirko Vukovic <mirko.vukovic@gmail.com>
Date: Sun, 22 Nov 2015 16:16:06 -0500
Subject: [PATCH 3/3] GSL2.0 syntax change in
 solve-minimize-fit/nonlinear-least-squares

GSLL would not load the two files in GSL2.0.  Added fixes that
are conditional on the feature :GSL2.x

In solver-struct.lisp the Jacbian field was removed from a structure

In nonlinear-least-squares a new function returns the Jacovian from
the solver

In that same file, the non-linear-fit example was slightly reformatted
for easier reading.
---
 solve-minimize-fit/nonlinear-least-squares.lisp | 42 ++++++++++++++++++++++---
 solve-minimize-fit/solver-struct.lisp           |  4 +--
 2 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/solve-minimize-fit/nonlinear-least-squares.lisp b/solve-minimize-fit/nonlinear-least-squares.lisp
index 2f39923..888f81d 100644
--- a/solve-minimize-fit/nonlinear-least-squares.lisp
+++ b/solve-minimize-fit/nonlinear-least-squares.lisp
@@ -1,6 +1,6 @@
 ;; Nonlinear least squares fitting.
 ;; Liam Healy, 2008-02-09 12:59:16EST nonlinear-least-squares.lisp
-;; Time-stamp: <2012-01-13 12:01:17EST nonlinear-least-squares.lisp>
+;; Time-stamp: <2015-11-22 13:43:30Eastern Standard Time nonlinear-least-squares.lisp>
 ;;
 ;; Copyright 2008, 2009, 2011 Liam M. Healy
 ;; Distributed under the terms of the GNU General Public License
@@ -141,7 +141,36 @@
   ;; Raw pointer, because we presume we're passing it on to another GSL function. 
   (cffi:foreign-slot-value (mpointer solver) 'gsl-fdffit-solver 'dx))
 
-(defun jacobian (solver)
+
+#+gsl2.x
+(defmfun jacobian
+    (solver
+     &optional (jacobian (grid:make-foreign-array
+			  'double-float
+			  :dimensions (list (dim0 solver) (dim1 solver)))))
+  "gsl_multifit_fdfsolver_jac"
+  (((mpointer solver) :pointer) ((mpointer jacobian) :pointer))
+  :definition :function
+  :return (jacobian)
+  :documentation "Return SOLVER's JACOBIAN.
+
+Required argument: SOLVER
+Optional argument: JACOBIAN
+
+If the Jacobian is not provided, it is initialized as a double-float matrix of
+dimensions (num-observations num-variables)")
+
+#|
+;; a preferable function would be
+(defmfun jacobian (solver)
+  ;; initialize jacobian in body
+  ...)
+;; However, I was unable to figure out the defmfun syntax to
+initialize the jacobian within the body.
+|#
+
+
+#+gsl1.6(defun jacobian (solver)
   ;; Raw pointer, because we presume we're passing it on to another GSL function. 
   (cffi:foreign-slot-value (mpointer solver) 'gsl-fdffit-solver 'jacobian))
 
@@ -249,7 +278,7 @@
 				       :dimensions
 				       (list (dim1 solver) (dim1 solver))))))
   "gsl_multifit_covar"
-  (((jacobian solver) :pointer) (relative-error :double) ((mpointer cov) :pointer))
+  (((mpointer (jacobian solver)) :pointer) (relative-error :double) ((mpointer cov) :pointer))
   :return (cov)
   :documentation 			; FDL
   "Compute the covariance matrix of the best-fit parameters
@@ -302,8 +331,11 @@
 	 (rng (make-random-number-generator +mt19937+ 0)))
      (dotimes (i number-of-observations arr)
        (setf (grid:aref arr i)
-	     (+ 1 (* 5 (exp (* -1/10 i)))
-		(sample rng :gaussian :sigma 0.1d0)))))
+	     (let ((A 5)
+		   (lambda 1/10)
+		   (b 2))
+	     (+ b (* a (exp (* (- lambda) i)))
+		(sample rng :gaussian :sigma 0.1d0))))))
    :sigma
    (grid:make-foreign-array
     'double-float :dimensions number-of-observations :initial-element 0.1d0)))
diff --git a/solve-minimize-fit/solver-struct.lisp b/solve-minimize-fit/solver-struct.lisp
index a7cb5ae..1fcfdcc 100644
--- a/solve-minimize-fit/solver-struct.lisp
+++ b/solve-minimize-fit/solver-struct.lisp
@@ -1,6 +1,6 @@
 ;; Define the structures for solvers
 ;; Liam Healy 2009-06-06 16:46:38EDT solver-struct.lisp
-;; Time-stamp: <2010-05-23 11:37:49EDT solver-struct.lisp>
+;; Time-stamp: <2015-11-13 14:49:44Eastern Standard Time solver-struct.lisp>
 ;;
 ;; Copyright 2009, 2010 Liam M. Healy
 ;; Distributed under the terms of the GNU General Public License
@@ -35,7 +35,7 @@
 
 (cstruct gsl-fdffit-solver "gsl_multifit_fdfsolver"
   (f "f" :type :pointer)
-  (jacobian "J" :type :pointer)
+  #+gsl1.6(jacobian "J" :type :pointer)
   (dx "dx" :type :pointer))
 
 (include "gsl/gsl_multiroots.h")
-- 
2.6.2

