Merge pull request #38 from lukas-linhart/fix-readme-typos
[lisp-unit.git] / internal-test / floating-point.lisp
blobff7f13d1d40cc1593e29669907a3e9e1eb5a6671
1 #|
3 LISP-UNIT Floating Point Tests
5 Copyright (c) 2010-2016, Thomas M. Hermann
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
27 (in-package :lisp-unit)
29 ;;; List norms
31 (define-test %norm-list
32 "Internal test of %norm on lists."
33 (:tag :norm)
34 ;; Taxicab norm
35 (assert-rational-equal
36 36 (%norm '(-6 -5 -4 -3 -2 -1 0 1 2 3 4 5) 1))
37 (assert-float-equal
38 19.535658
39 (%norm
40 '(#C(1 0) #C(3 1) #C(2 3) #C(0 4)
41 #C(-2 3) #C(-3 1) #C(-1 0))
42 1))
43 ;; Euclidean norm
44 (assert-float-equal
45 12.083046
46 (%norm '(-6 -5 -4 -3 -2 -1 0 1 2 3 4 5) 2))
47 (assert-float-equal
48 8.0
49 (%norm
50 '(#C(1 0) #C(3 1) #C(2 3) #C(0 4)
51 #C(-2 3) #C(-3 1) #C(-1 0)) 2))
52 ;; P-norm
53 (let ((data '(-6 -5 -4 -3 -2 -1 0 1 2 3 4 5))
54 (zdata '(#C(1 0) #C(3 1) #C(2 3) #C(0 4)
55 #C(-2 3) #C(-3 1) #C(-1 0))))
56 (assert-float-equal 8.732892 (%norm data 3))
57 (assert-float-equal 6.064035 (%norm zdata 3)))
58 ;; Infinity norm
59 (assert-rational-equal
60 6 (%norm
61 '(-6 -5 -4 -3 -2 -1 0 1 2 3 4 5)
62 :infinity))
63 (assert-float-equal
64 4.0 (%norm
65 '(#C(1 0) #C(3 1) #C(2 3) #C(0 4)
66 #C(-2 3) #C(-3 1) #C(-1 0))
67 :infinity)))
69 ;;; Vector norms
71 (define-test %norm-vector
72 "Internal test of %norm on vectors"
73 (:tag :norm)
74 ;; Taxicab norm
75 (assert-rational-equal
76 36 (%norm #(-6 -5 -4 -3 -2 -1 0 1 2 3 4 5) 1))
77 (assert-float-equal
78 19.535658
79 (%norm
80 #(#C(1 0) #C(3 1) #C(2 3) #C(0 4)
81 #C(-2 3) #C(-3 1) #C(-1 0))
82 1))
83 ;; Euclidean norm
84 (assert-float-equal
85 12.083046
86 (%norm #(-6 -5 -4 -3 -2 -1 0 1 2 3 4 5) 2))
87 (assert-float-equal
88 8.0
89 (%norm
90 #(#C(1 0) #C(3 1) #C(2 3) #C(0 4)
91 #C(-2 3) #C(-3 1) #C(-1 0))
92 2))
93 ;; P-norm
94 (let ((data #(-6 -5 -4 -3 -2 -1 0 1 2 3 4 5))
95 (zdata #(#C(1 0) #C(3 1) #C(2 3) #C(0 4)
96 #C(-2 3) #C(-3 1) #C(-1 0))))
97 (assert-float-equal 8.732892 (%norm data 3))
98 (assert-float-equal 6.064035 (%norm zdata 3)))
99 ;; Infinity norm
100 (assert-rational-equal
101 6 (%norm #(-6 -5 -4 -3 -2 -1 0 1 2 3 4 5) :infinity))
102 (assert-float-equal
103 4.0 (%norm
104 #(#C(1 0) #C(3 1) #C(2 3) #C(0 4)
105 #C(-2 3) #C(-3 1) #C(-1 0))
106 :infinity)))