; Copyright (C) 2006 Will M. Farr ; ; 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 2 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, write to the Free Software Foundation, Inc., ; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. (module derivatives-test "generics.ss" (require "tuples.ss" "derivatives.ss" "test.ss") (provide derivatives-test-suite) (define derivatives-test-suite (test-suite "derivatives.ss tests" (test-case "D on scalars" (let ((f (lambda (x) (+ (log x) (exp x))))) (check-close? 1e-6 ((D f) 10) (+ 1/10 (exp 10))))) (test-case "D^2 on scalars" (let ((f (lambda (x) (+ (log x) (exp x))))) (check-close? 1e-6 ((D (D f)) 10) (+ -1/100 (exp 10))))) (test-case "D^3 on scalars" (let ((f (lambda (x) (+ (log x) (exp x))))) (check-close? 1e-6 ((D (D (D f))) 10) (+ 1/500 (exp 10))))) (test-case "D^2 on sin and cos" (check-close? 1e-6 ((D (D sin)) 1.2340987) (- (sin 1.2340987))) (check-close? 1e-6 ((D (D cos)) 1.45987) (- (cos 1.45987)))) (test-case "partials" (check= (((partial 0) (lambda (x y) (* x y))) 10 3) 3) (check= (((partial 1) (lambda (x y) (* x y))) 10 3) 10) (check= (((partial 1) ((partial 0) (lambda (x y) (* x y)))) 10 3) (((partial 0) ((partial 1) (lambda (x y) (* x y)))) 10 3))) (test-case "D on tuple-receiving function" (let ((f (lambda (x) (* (ref x 0) (ref x 1))))) (check-tuple= ((D f) (up 2 3)) (down 3 2)))) (test-case "D^2 on tuple-receiving function" (let ((f (lambda (x) (* (ref x 0) (ref x 1))))) (check-tuple= ((D (D f)) (up 2 3)) (down (down 0 1) (down 1 0))))) (test-case "D^2 on really nasty tuple-receiving function" (let* ((f (lambda (t) (let ((x (ref t 0)) (y (ref t 1))) (+ (* (expt x 3) (expt y 2)) (/ (cos (* x y)) (+ (square x) (square y))) (- (* (exp (+ (square x) (* x (expt y 3)))) (log (/ y (expt x 4))))))))) (f-2-arg (lambda (x y) (f (down x y)))) (x 0.16651) (y 0.85471) (D^2f (up (up -138.4640716417226 -8.8793366349873) (up -8.8793366349873 2.6298383068791)))) (check-tuple-close? 1e-6 ((D (D f)) (down x y)) D^2f) (check-tuple-close? 1e-6 (up (up (((partial 0) ((partial 0) f-2-arg)) x y) (((partial 0) ((partial 1) f-2-arg)) x y)) (up (((partial 1) ((partial 0) f-2-arg)) x y) (((partial 1) ((partial 1) f-2-arg)) x y))) D^2f))))))