# Common LAPACK/Blas utility functions

LAPACK and Blas have all these handy little routines like

dlascl - scale an array between two normalized quantities handling overflow/underflow/etc.

``````# _dlascl(from, to, x)
# This will compute x .*= x (to/from) and handle special cases
julia> _dlascl(1e-300, 1e300, [1e-300, 1e-301, 1e-299])
3-element Vector{Float64}:
1.0000000000000002e300
1.0000000000000002e299
1.0000000000000002e301
``````

dlapy2 - computer sqrt(x^2+y^2) handling underflow/overflow

``````julia> _dlapy2(1e300,1e200)
1.0e300

julia> sqrt(1e300^2+1e200^2)
Inf
``````

Am I missing some package/scenario in Julia where these exist? (I realize I can call BLAS/LAPACK directly, but I want to know if there’s another place I should be looking for these little micro-utilities.)

Each routine isn’t very complicated, but they are often somewhat subtle, e.g.

``````@inline function _dlapy2(x::T, y::T) where T
!isnan(x) || return x
!isnan(y) || return y
w = max(abs(x),abs(y))
z = min(abs(x),abs(y))
if z == 0
return w
else
return w*sqrt(1+(z/w)^2)
end
end
``````

Apologies if I haven’t found just the right google search or my list of what I would search for wasn’t quite expansive enough.

PS - Edited for code blocks

This is the `hypot` function.

`dlascl` would be easy enough to implement, but there doesn’t seem to have been any demand for it.

PS. Technically, these are LAPACK auxiliary routines. The generally useful ones (e.g. `dlamch`, `dsecnd`, `dlisnan`, `dlapy2`, `dlapy3`, `dlarnv`, etc.) seem to mostly already have Julia equivalents.

3 Likes