|
5 | 5 | # and throw a `DomainError` for eigenvalues that are genuinely negative, since then the |
6 | 6 | # result cannot be expressed with the same (real) scalar type. |
7 | 7 | function _clamp_domain_eigenvalues!(λ::AbstractVector{<:Real}, atol::Real) |
8 | | - for i in eachindex(λ) |
9 | | - x = λ[i] |
10 | | - if x < -atol |
11 | | - throw( |
12 | | - DomainError( |
13 | | - x, |
14 | | - "The matrix has a negative real eigenvalue beyond `domain_atol = $atol` and the result of this matrix function is complex. " * |
15 | | - "Pass a complex matrix to obtain the principal value, or increase `domain_atol` if the eigenvalue is a rounding artifact." |
16 | | - ) |
| 8 | + λmin = minimum(λ; init = zero(eltype(λ))) |
| 9 | + if λmin < -atol |
| 10 | + throw( |
| 11 | + DomainError( |
| 12 | + λmin, |
| 13 | + "The matrix has a negative real eigenvalue beyond `domain_atol = $atol` and the result of this matrix function is complex. " * |
| 14 | + "Pass a complex matrix to obtain the principal value, or increase `domain_atol` if the eigenvalue is a rounding artifact." |
17 | 15 | ) |
18 | | - elseif x < 0 |
19 | | - λ[i] = zero(x) |
20 | | - end |
| 16 | + ) |
21 | 17 | end |
| 18 | + λ .= max.(λ, zero(eltype(λ))) |
22 | 19 | return λ |
23 | 20 | end |
24 | 21 |
|
|
33 | 30 | # Complex eigenvalues of a real matrix: only eigenvalues (numerically) on the negative |
34 | 31 | # real axis obstruct a real result; complex-conjugate pairs do not. |
35 | 32 | function _clamp_domain_eigenvalues!(λ::AbstractVector{<:Complex}, atol::Real) |
36 | | - for i in eachindex(λ) |
37 | | - x = λ[i] |
38 | | - if abs(imag(x)) <= atol && real(x) < 0 |
39 | | - if real(x) < -atol |
40 | | - throw( |
41 | | - DomainError( |
42 | | - x, |
43 | | - "The matrix has an eigenvalue on the negative real axis beyond `domain_atol = $atol` and the result of this matrix function is complex. " * |
44 | | - "Pass a complex matrix to obtain the principal value, or increase `domain_atol` if the eigenvalue is a rounding artifact." |
45 | | - ) |
46 | | - ) |
47 | | - else |
48 | | - λ[i] = zero(x) |
49 | | - end |
50 | | - end |
| 33 | + onaxis = x -> abs(imag(x)) <= atol && real(x) < 0 |
| 34 | + λmin = mapreduce(x -> onaxis(x) ? real(x) : zero(real(x)), min, λ; init = zero(real(eltype(λ)))) |
| 35 | + if λmin < -atol |
| 36 | + throw( |
| 37 | + DomainError( |
| 38 | + λmin, |
| 39 | + "The matrix has an eigenvalue on the negative real axis beyond `domain_atol = $atol` and the result of this matrix function is complex. " * |
| 40 | + "Pass a complex matrix to obtain the principal value, or increase `domain_atol` if the eigenvalue is a rounding artifact." |
| 41 | + ) |
| 42 | + ) |
51 | 43 | end |
| 44 | + λ .= ifelse.(onaxis.(λ), zero(eltype(λ)), λ) |
52 | 45 | return λ |
53 | 46 | end |
54 | 47 |
|
55 | 48 | # Reject (numerically) zero eigenvalues for functions that are undefined there, |
56 | 49 | # e.g. `logarithm` and `power` with a negative fractional power. |
57 | 50 | function _check_nonzero_eigenvalues(λ, atol::Real) |
58 | | - for x in λ |
59 | | - if abs(x) <= atol |
60 | | - throw( |
61 | | - DomainError( |
62 | | - x, |
63 | | - "The matrix has a (numerically) zero eigenvalue within `domain_atol = $atol`, for which this matrix function is not defined." |
64 | | - ) |
| 51 | + amin = minimum(abs, λ; init = typemax(real(eltype(λ)))) |
| 52 | + if amin <= atol |
| 53 | + throw( |
| 54 | + DomainError( |
| 55 | + amin, |
| 56 | + "The matrix has a (numerically) zero eigenvalue within `domain_atol = $atol`, for which this matrix function is not defined." |
65 | 57 | ) |
66 | | - end |
| 58 | + ) |
67 | 59 | end |
68 | 60 | return λ |
69 | 61 | end |
|
0 commit comments