From 9d1f81faa68320c208eaa2df97bba761ce468d46 Mon Sep 17 00:00:00 2001 From: Pavel Panchekha Date: Sun, 4 Oct 2026 16:33:50 -0700 Subject: [PATCH 1/2] Use flhypot in matrix-2norm to fix the failing test --- math-lib/math/private/matrix/matrix-basic.rkt | 14 +++++++++----- math-test/math/tests/matrix-tests.rkt | 4 ++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/math-lib/math/private/matrix/matrix-basic.rkt b/math-lib/math/private/matrix/matrix-basic.rkt index cc024b9..8dde873 100644 --- a/math-lib/math/private/matrix/matrix-basic.rkt +++ b/math-lib/math/private/matrix/matrix-basic.rkt @@ -222,6 +222,10 @@ (parameterize ([array-strictness #f]) (array-all-sum (inline-array-map nonstupid-magnitude M)))) +(: flonum-hypot (Nonnegative-Flonum Nonnegative-Flonum -> Nonnegative-Flonum)) +(define (flonum-hypot x y) + (assert (flhypot x y) nonnegative?)) + (: matrix-2norm (case-> ((Matrix Flonum) -> Nonnegative-Flonum) ((Matrix Real) -> Nonnegative-Real) ((Matrix Float-Complex) -> Nonnegative-Flonum) @@ -229,11 +233,11 @@ (define (matrix-2norm M) (parameterize ([array-strictness #f]) (let ([M (array-strict (inline-array-map nonstupid-magnitude M))]) - ;; Compute this divided by the maximum to avoid underflow and overflow - (define mx (array-all-max M)) - (cond [(and (rational? mx) (positive? mx)) - (* mx (sqrt (array-all-sum (inline-array-map (λ (x) (sqr (/ x mx))) M))))] - [else mx])))) + (cond + [(array-all-and (inline-array-map exact? M)) + (sqrt (array-all-sum (inline-array-map sqr M)))] + [else + (array-all-fold (inline-array-map fl M) flonum-hypot 0.0)])))) (: matrix-inf-norm (case-> ((Matrix Flonum) -> Nonnegative-Flonum) ((Matrix Real) -> Nonnegative-Real) diff --git a/math-test/math/tests/matrix-tests.rkt b/math-test/math/tests/matrix-tests.rkt index bbb92aa..1cb3843 100644 --- a/math-test/math/tests/matrix-tests.rkt +++ b/math-test/math/tests/matrix-tests.rkt @@ -641,6 +641,10 @@ (check-equal? (matrix-norm (matrix [[1e200 1e199]])) (flhypot 1e200 1e199)) +;; Mixed exact and inexact entries use the flonum hypot path. +(check-equal? (matrix-norm (matrix [[1 2.0]])) + (flhypot 1.0 2.0)) + ;; Taxicab (Manhattan) norm (check-equal? (matrix-norm (matrix [[1 2 3] [4 5 6]]) 1) (+ 1 2 3 4 5 6)) From 6324c72c7ae463d8d34b87c36262b5c2f5d3eaeb Mon Sep 17 00:00:00 2001 From: Pavel Panchekha Date: Thu, 8 Oct 2026 14:49:25 -0700 Subject: [PATCH 2/2] Use flhypot directly in matrix-2norm --- math-lib/math/private/matrix/matrix-basic.rkt | 6 +----- math-test/math/tests/matrix-tests.rkt | 3 +++ 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/math-lib/math/private/matrix/matrix-basic.rkt b/math-lib/math/private/matrix/matrix-basic.rkt index 8dde873..3ead762 100644 --- a/math-lib/math/private/matrix/matrix-basic.rkt +++ b/math-lib/math/private/matrix/matrix-basic.rkt @@ -222,10 +222,6 @@ (parameterize ([array-strictness #f]) (array-all-sum (inline-array-map nonstupid-magnitude M)))) -(: flonum-hypot (Nonnegative-Flonum Nonnegative-Flonum -> Nonnegative-Flonum)) -(define (flonum-hypot x y) - (assert (flhypot x y) nonnegative?)) - (: matrix-2norm (case-> ((Matrix Flonum) -> Nonnegative-Flonum) ((Matrix Real) -> Nonnegative-Real) ((Matrix Float-Complex) -> Nonnegative-Flonum) @@ -237,7 +233,7 @@ [(array-all-and (inline-array-map exact? M)) (sqrt (array-all-sum (inline-array-map sqr M)))] [else - (array-all-fold (inline-array-map fl M) flonum-hypot 0.0)])))) + (array-all-fold (inline-array-map fl M) flhypot 0.0)])))) (: matrix-inf-norm (case-> ((Matrix Flonum) -> Nonnegative-Flonum) ((Matrix Real) -> Nonnegative-Real) diff --git a/math-test/math/tests/matrix-tests.rkt b/math-test/math/tests/matrix-tests.rkt index 1cb3843..b32e67b 100644 --- a/math-test/math/tests/matrix-tests.rkt +++ b/math-test/math/tests/matrix-tests.rkt @@ -645,6 +645,9 @@ (check-equal? (matrix-norm (matrix [[1 2.0]])) (flhypot 1.0 2.0)) +;; A NaN entry should propagate through the flonum hypot path. +(check-true (nan? (matrix-norm (matrix [[+nan.0]])))) + ;; Taxicab (Manhattan) norm (check-equal? (matrix-norm (matrix [[1 2 3] [4 5 6]]) 1) (+ 1 2 3 4 5 6))