Skip to content

Commit 56ede2b

Browse files
authored
matrix-cos-angle (#118)
* matrix-cos-angle * prevent zero-matrix in test * better error on zero-matrix * update error message
1 parent 72dc858 commit 56ede2b

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

math-lib/math/private/matrix/matrix-basic.rkt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,10 @@
355355

356356
(inner A* B*)]
357357
[(or (nan? mxA) (nan? mxB) (= 0 mxA) (= 0 mxB))
358-
(/ (matrix-dot A B) (* mxA mxB))]
358+
(cond
359+
[(eqv? mxA 0) (error 'matrix-cos-angle "zero matrix ~a" A)]
360+
[(eqv? mxB 0) (error 'matrix-cos-angle "zero matrix ~a" B)]
361+
[else (/ (matrix-dot A B) (* mxA mxB))])]
359362
[else
360363
(define A* (if (rational? mxA)
361364
(inline-array-map (λ (x) (/ x mxA)) A)

math-test/math/tests/matrix-tests.rkt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,11 @@
686686
(for: ([i (in-range 10)])
687687
(define n (+ 1 (random 3)))
688688
(define m (+ 1 (random 4)))
689-
(define A (random-matrix n m))
690-
(define B (random-matrix n m))
689+
(define (non-zero-random-matrix [n : Integer] [m : Integer]) : (Array Integer)
690+
(define A (random-matrix n m))
691+
(if (matrix-zero? A) (non-zero-random-matrix n m) A))
692+
(define A (non-zero-random-matrix n m))
693+
(define B (non-zero-random-matrix n m))
691694
(define ca (matrix-cos-angle A B))
692695
(check-true (or (and (rational? ca) (<= ca 1.))
693696
(matrix-zero? A) (matrix-zero? B))))
@@ -714,6 +717,8 @@
714717
(matrix [[3 -8+inf.0i]]))
715718
(matrix-cos-angle (matrix [[2 9]])
716719
(matrix [[0. -0+1.0i]])))
720+
(check-exn #px"matrix-cos-angle: zero matrix " (λ () (matrix-cos-angle (matrix [[0 0][0 0]]) (matrix [[3 1][-1 2]]))))
721+
(check-exn #px"matrix-cos-angle: zero matrix " (λ () (matrix-angle (matrix [[1]]) (matrix [[0]]))))
717722

718723
;; TODO: matrix-normalize
719724

0 commit comments

Comments
 (0)