Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overflow/underflow in norm computations #1861

Closed
stevengj opened this issue Jan 1, 2013 · 0 comments
Closed

overflow/underflow in norm computations #1861

stevengj opened this issue Jan 1, 2013 · 0 comments
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@stevengj
Copy link
Member

stevengj commented Jan 1, 2013

Several of the norm functions implemented in linalg.jl suffer from spurious overflow/underflow problems.

For example,

julia> x = [1e300]; norm(x,3)
Inf

julia> x = [1e-300]; norm(x,3)
0.0

A simple fix would be for the norm(x::AbstractVector, p::Number) and norm(x::AbstractVector) to first scale x by 1/max(abs(x)) before computing the norm, and then scale the result by max(abs(x)) (assuming it is nonzero).

Similar problems occur for the Frobenius norm in norm(A::AbstractMatrix, p).

Also, computing the Frobenius norm by sqrt(sum(diag(A'*A))) is insanely inefficient, since it is O(n^3) for an n-by-n matrix instead of O(n^2). You should really just do something like norm(reshape(A, numel(A))), or whatever the equivalent of reshape is that does not create a temporary vector. (This has the added advantage of using the BLAS nrm2 operation where possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants