/usr/share/julia/base/sparse/umfpack.jl is in julia-common 0.4.7-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | # This file is a part of Julia. License is MIT: http://julialang.org/license
module UMFPACK
export UmfpackLU
import Base: (\), Ac_ldiv_B, At_ldiv_B, findnz, getindex, show, size
import Base.LinAlg: A_ldiv_B!, Ac_ldiv_B!, At_ldiv_B!, Factorization, det, lufact
importall Base.SparseMatrix
import Base.SparseMatrix: increment, increment!, decrement, decrement!, nnz
include("umfpack_h.jl")
type MatrixIllConditionedException <: Exception
message::AbstractString
end
function umferror(status::Integer)
if status==UMFPACK_OK
return
elseif status==UMFPACK_WARNING_singular_matrix
throw(LinAlg.SingularException(0))
elseif status==UMFPACK_WARNING_determinant_underflow
throw(MatrixIllConditionedException("the determinant is nonzero but underflowed"))
elseif status==UMFPACK_WARNING_determinant_overflow
throw(MatrixIllConditionedException("the determinant overflowed"))
elseif status==UMFPACK_ERROR_out_of_memory
throw(OutOfMemoryError())
elseif status==UMFPACK_ERROR_invalid_Numeric_object
throw(ArgumentError("invalid UMFPack numeric object"))
elseif status==UMFPACK_ERROR_invalid_Symbolic_object
throw(ArgumentError("invalid UMFPack symbolic object"))
elseif status==UMFPACK_ERROR_argument_missing
throw(ArgumentError("a required argument to UMFPack is missing"))
elseif status==UMFPACK_ERROR_n_nonpositive
throw(ArgumentError("the number of rows or columns of the matrix must be greater than zero"))
elseif status==UMFPACK_ERROR_invalid_matrix
throw(ArgumentError("invalid matrix"))
elseif status==UMFPACK_ERROR_different_pattern
throw(ArgumentError("pattern of the matrix changed"))
elseif status==UMFPACK_ERROR_invalid_system
throw(ArgumentError("invalid sys argument provided to UMFPack solver"))
elseif status==UMFPACK_ERROR_invalid_permutation
throw(ArgumentError("invalid permutation"))
elseif status==UMFPACK_ERROR_file_IO
throw(ErrorException("error saving / loading UMFPack decomposition"))
elseif status==UMFPACK_ERROR_ordering_failed
throw(ErrorException("the ordering method failed"))
elseif status==UMFPACK_ERROR_internal_error
throw(ErrorException("an internal error has occurred, of unknown cause"))
else
throw(ErrorException("unknown UMFPack error code: $status"))
end
end
macro isok(A)
:(umferror($A))
end
# check the size of SuiteSparse_long
if Int(ccall((:jl_cholmod_sizeof_long,:libsuitesparse_wrapper),Csize_t,())) == 4
const UmfpackIndexTypes = (:Int32, )
typealias UMFITypes Union{Int32}
else
const UmfpackIndexTypes = (:Int32, :Int64)
typealias UMFITypes Union{Int32, Int64}
end
typealias UMFVTypes Union{Float64,Complex128}
## UMFPACK
# the control and info arrays
const umf_ctrl = Array(Float64, UMFPACK_CONTROL)
ccall((:umfpack_dl_defaults,:libumfpack), Void, (Ptr{Float64},), umf_ctrl)
const umf_info = Array(Float64, UMFPACK_INFO)
function show_umf_ctrl(level::Real)
old_prt::Float64 = umf_ctrl[1]
umf_ctrl[1] = Float64(level)
ccall((:umfpack_dl_report_control, :libumfpack), Void, (Ptr{Float64},), umf_ctrl)
umf_ctrl[1] = old_prt
end
show_umf_ctrl() = show_umf_ctrl(2.)
function show_umf_info(level::Real)
old_prt::Float64 = umf_ctrl[1]
umf_ctrl[1] = Float64(level)
ccall((:umfpack_dl_report_info, :libumfpack), Void,
(Ptr{Float64}, Ptr{Float64}), umf_ctrl, umf_info)
umf_ctrl[1] = old_prt
end
show_umf_info() = show_umf_info(2.)
## Should this type be immutable?
type UmfpackLU{Tv<:UMFVTypes,Ti<:UMFITypes} <: Factorization{Tv}
symbolic::Ptr{Void}
numeric::Ptr{Void}
m::Int
n::Int
colptr::Vector{Ti} # 0-based column pointers
rowval::Vector{Ti} # 0-based row indices
nzval::Vector{Tv}
end
function lufact{Tv<:UMFVTypes,Ti<:UMFITypes}(S::SparseMatrixCSC{Tv,Ti})
zerobased = S.colptr[1] == 0
res = UmfpackLU(C_NULL, C_NULL, S.m, S.n,
zerobased ? copy(S.colptr) : decrement(S.colptr),
zerobased ? copy(S.rowval) : decrement(S.rowval),
copy(S.nzval))
finalizer(res, umfpack_free_symbolic)
umfpack_numeric!(res)
end
lufact(A::SparseMatrixCSC) = lufact(float(A))
function show(io::IO, f::UmfpackLU)
println(io, "UMFPACK LU Factorization of a $(f.m)-by-$(f.n) sparse matrix")
f.numeric != C_NULL && println(io, f.numeric)
end
## Wrappers for UMFPACK functions
# generate the name of the C function according to the value and integer types
umf_nm(nm,Tv,Ti) = "umfpack_" * (Tv == :Float64 ? "d" : "z") * (Ti == :Int64 ? "l_" : "i_") * nm
for itype in UmfpackIndexTypes
sym_r = umf_nm("symbolic", :Float64, itype)
sym_c = umf_nm("symbolic", :Complex128, itype)
num_r = umf_nm("numeric", :Float64, itype)
num_c = umf_nm("numeric", :Complex128, itype)
sol_r = umf_nm("solve", :Float64, itype)
sol_c = umf_nm("solve", :Complex128, itype)
det_r = umf_nm("get_determinant", :Float64, itype)
det_z = umf_nm("get_determinant", :Complex128, itype)
lunz_r = umf_nm("get_lunz", :Float64, itype)
lunz_z = umf_nm("get_lunz", :Complex128, itype)
get_num_r = umf_nm("get_numeric", :Float64, itype)
get_num_z = umf_nm("get_numeric", :Complex128, itype)
@eval begin
function umfpack_symbolic!(U::UmfpackLU{Float64,$itype})
if U.symbolic != C_NULL return U end
tmp = Array(Ptr{Void},1)
@isok ccall(($sym_r, :libumfpack), $itype,
($itype, $itype, Ptr{$itype}, Ptr{$itype}, Ptr{Float64}, Ptr{Void},
Ptr{Float64}, Ptr{Float64}),
U.m, U.n, U.colptr, U.rowval, U.nzval, tmp,
umf_ctrl, umf_info)
U.symbolic = tmp[1]
return U
end
function umfpack_symbolic!(U::UmfpackLU{Complex128,$itype})
if U.symbolic != C_NULL return U end
tmp = Array(Ptr{Void},1)
@isok ccall(($sym_c, :libumfpack), $itype,
($itype, $itype, Ptr{$itype}, Ptr{$itype}, Ptr{Float64}, Ptr{Float64}, Ptr{Void},
Ptr{Float64}, Ptr{Float64}),
U.m, U.n, U.colptr, U.rowval, real(U.nzval), imag(U.nzval), tmp,
umf_ctrl, umf_info)
U.symbolic = tmp[1]
return U
end
function umfpack_numeric!(U::UmfpackLU{Float64,$itype})
if U.numeric != C_NULL return U end
if U.symbolic == C_NULL umfpack_symbolic!(U) end
tmp = Array(Ptr{Void}, 1)
status = ccall(($num_r, :libumfpack), $itype,
(Ptr{$itype}, Ptr{$itype}, Ptr{Float64}, Ptr{Void}, Ptr{Void},
Ptr{Float64}, Ptr{Float64}),
U.colptr, U.rowval, U.nzval, U.symbolic, tmp,
umf_ctrl, umf_info)
if status != UMFPACK_WARNING_singular_matrix
umferror(status)
end
U.numeric = tmp[1]
return U
end
function umfpack_numeric!(U::UmfpackLU{Complex128,$itype})
if U.numeric != C_NULL return U end
if U.symbolic == C_NULL umfpack_symbolic!(U) end
tmp = Array(Ptr{Void}, 1)
status = ccall(($num_c, :libumfpack), $itype,
(Ptr{$itype}, Ptr{$itype}, Ptr{Float64}, Ptr{Float64}, Ptr{Void}, Ptr{Void},
Ptr{Float64}, Ptr{Float64}),
U.colptr, U.rowval, real(U.nzval), imag(U.nzval), U.symbolic, tmp,
umf_ctrl, umf_info)
if status != UMFPACK_WARNING_singular_matrix
umferror(status)
end
U.numeric = tmp[1]
return U
end
function solve(lu::UmfpackLU{Float64,$itype}, b::VecOrMat{Float64}, typ::Integer)
umfpack_numeric!(lu)
size(b,1)==lu.m || throw(DimensionMismatch())
x = similar(b)
joff = 1
for k = 1:size(b,2)
@isok ccall(($sol_r, :libumfpack), $itype,
($itype, Ptr{$itype}, Ptr{$itype}, Ptr{Float64}, Ptr{Float64},
Ptr{Float64}, Ptr{Void}, Ptr{Float64}, Ptr{Float64}),
typ, lu.colptr, lu.rowval, lu.nzval, pointer(x,joff), pointer(b,joff), lu.numeric, umf_ctrl, umf_info)
joff += size(b,1)
end
x
end
function solve(lu::UmfpackLU{Complex128,$itype}, b::VecOrMat{Complex128}, typ::Integer)
umfpack_numeric!(lu)
size(b,1)==lu.m || throw(DimensionMismatch())
x = similar(b)
n = size(b,1)
br = Array(Float64, n)
bi = Array(Float64, n)
xr = Array(Float64, n)
xi = Array(Float64, n)
joff = 0
for k = 1:size(b,2)
for j = 1:n
bj = b[joff+j]
br[j] = real(bj)
bi[j] = imag(bj)
end
@isok ccall(($sol_c, :libumfpack), $itype,
($itype, Ptr{$itype}, Ptr{$itype}, Ptr{Float64}, Ptr{Float64},
Ptr{Float64}, Ptr{Float64}, Ptr{Float64}, Ptr{Float64},
Ptr{Void}, Ptr{Float64}, Ptr{Float64}),
typ, lu.colptr, lu.rowval, real(lu.nzval), imag(lu.nzval),
xr, xi, br, bi,
lu.numeric, umf_ctrl, umf_info)
for j = 1:n
x[joff+j] = complex(xr[j],xi[j])
end
joff += n
end
x
end
function det(lu::UmfpackLU{Float64,$itype})
mx = Array(Float64,1)
@isok ccall(($det_r,:libumfpack), $itype,
(Ptr{Float64},Ptr{Float64},Ptr{Void},Ptr{Float64}),
mx, C_NULL, lu.numeric, umf_info)
mx[1]
end
function det(lu::UmfpackLU{Complex128,$itype})
mx = Array(Float64,1)
mz = Array(Float64,1)
@isok ccall(($det_z,:libumfpack), $itype,
(Ptr{Float64},Ptr{Float64},Ptr{Float64},Ptr{Void},Ptr{Float64}),
mx, mz, C_NULL, lu.numeric, umf_info)
complex(mx[1], mz[1])
end
function umf_lunz(lu::UmfpackLU{Float64,$itype})
lnz = Array($itype, 1)
unz = Array($itype, 1)
n_row = Array($itype, 1)
n_col = Array($itype, 1)
nz_diag = Array($itype, 1)
@isok ccall(($lunz_r,:libumfpack), $itype,
(Ptr{$itype},Ptr{$itype},Ptr{$itype},Ptr{$itype},Ptr{$itype},Ptr{Void}),
lnz, unz, n_row, n_col, nz_diag, lu.numeric)
(lnz[1], unz[1], n_row[1], n_col[1], nz_diag[1])
end
function umf_lunz(lu::UmfpackLU{Complex128,$itype})
lnz = Array($itype, 1)
unz = Array($itype, 1)
n_row = Array($itype, 1)
n_col = Array($itype, 1)
nz_diag = Array($itype, 1)
@isok ccall(($lunz_z,:libumfpack), $itype,
(Ptr{$itype},Ptr{$itype},Ptr{$itype},Ptr{$itype},Ptr{$itype},Ptr{Void}),
lnz, unz, n_row, n_col, nz_diag, lu.numeric)
(lnz[1], unz[1], n_row[1], n_col[1], nz_diag[1])
end
function umf_extract(lu::UmfpackLU{Float64,$itype})
umfpack_numeric!(lu) # ensure the numeric decomposition exists
(lnz, unz, n_row, n_col, nz_diag) = umf_lunz(lu)
Lp = Array($itype, n_row + 1)
Lj = Array($itype, lnz) # L is returned in CSR (compressed sparse row) format
Lx = Array(Float64, lnz)
Up = Array($itype, n_col + 1)
Ui = Array($itype, unz)
Ux = Array(Float64, unz)
P = Array($itype, n_row)
Q = Array($itype, n_col)
Rs = Array(Float64, n_row)
@isok ccall(($get_num_r,:libumfpack), $itype,
(Ptr{$itype},Ptr{$itype},Ptr{Float64},
Ptr{$itype},Ptr{$itype},Ptr{Float64},
Ptr{$itype},Ptr{$itype},Ptr{Void},
Ptr{$itype},Ptr{Float64},Ptr{Void}),
Lp,Lj,Lx,
Up,Ui,Ux,
P, Q, C_NULL,
&0, Rs, lu.numeric)
(transpose(SparseMatrixCSC(min(n_row, n_col), n_row, increment!(Lp), increment!(Lj), Lx)),
SparseMatrixCSC(min(n_row, n_col), n_col, increment!(Up), increment!(Ui), Ux),
increment!(P), increment!(Q), Rs)
end
function umf_extract(lu::UmfpackLU{Complex128,$itype})
umfpack_numeric!(lu) # ensure the numeric decomposition exists
(lnz, unz, n_row, n_col, nz_diag) = umf_lunz(lu)
Lp = Array($itype, n_row + 1)
Lj = Array($itype, lnz) # L is returned in CSR (compressed sparse row) format
Lx = Array(Float64, lnz)
Lz = Array(Float64, lnz)
Up = Array($itype, n_col + 1)
Ui = Array($itype, unz)
Ux = Array(Float64, unz)
Uz = Array(Float64, unz)
P = Array($itype, n_row)
Q = Array($itype, n_col)
Rs = Array(Float64, n_row)
@isok ccall(($get_num_z,:libumfpack), $itype,
(Ptr{$itype},Ptr{$itype},Ptr{Float64},Ptr{Float64},
Ptr{$itype},Ptr{$itype},Ptr{Float64},Ptr{Float64},
Ptr{$itype},Ptr{$itype},Ptr{Void}, Ptr{Void},
Ptr{$itype},Ptr{Float64},Ptr{Void}),
Lp,Lj,Lx,Lz,
Up,Ui,Ux,Uz,
P, Q, C_NULL, C_NULL,
&0, Rs, lu.numeric)
(transpose(SparseMatrixCSC(min(n_row, n_col), n_row, increment!(Lp), increment!(Lj), complex(Lx, Lz))),
SparseMatrixCSC(min(n_row, n_col), n_col, increment!(Up), increment!(Ui), complex(Ux, Uz)),
increment!(P), increment!(Q), Rs)
end
end
end
function nnz(lu::UmfpackLU)
lnz, unz, = umf_lunz(lu)
return Int(lnz + unz)
end
### Solve with Factorization
A_ldiv_B!{T<:UMFVTypes}(lu::UmfpackLU{T}, b::Vector{T}) = solve(lu, b, UMFPACK_A)
A_ldiv_B!{T<:UMFVTypes}(lu::UmfpackLU{T}, b::Matrix{T}) = solve(lu, b, UMFPACK_A)
function A_ldiv_B!{Tb<:Complex}(lu::UmfpackLU{Float64}, b::Vector{Tb})
r = solve(lu, [convert(Tlu,real(be)) for be in b], UMFPACK_A)
i = solve(lu, [convert(Tlu,imag(be)) for be in b], UMFPACK_A)
Tb[r[k]+im*i[k] for k = 1:length(r)]
end
Ac_ldiv_B!{T<:UMFVTypes}(lu::UmfpackLU{T}, b::VecOrMat{T}) = solve(lu, b, UMFPACK_At)
function Ac_ldiv_B!{Tb<:Complex}(lu::UmfpackLU{Float64}, b::Vector{Tb})
r = solve(lu, [convert(Float64,real(be)) for be in b], UMFPACK_At)
i = solve(lu, [convert(Float64,imag(be)) for be in b], UMFPACK_At)
Tb[r[k]+im*i[k] for k = 1:length(r)]
end
At_ldiv_B!{T<:UMFVTypes}(lu::UmfpackLU{T}, b::VecOrMat{T}) = solve(lu, b, UMFPACK_Aat)
function At_ldiv_B!{Tb<:Complex}(lu::UmfpackLU{Float64}, b::Vector{Tb})
r = solve(lu, [convert(Float64,real(be)) for be in b], UMFPACK_Aat)
i = solve(lu, [convert(Float64,imag(be)) for be in b], UMFPACK_Aat)
Tb[r[k]+im*i[k] for k = 1:length(r)]
end
function getindex(lu::UmfpackLU, d::Symbol)
L,U,p,q,Rs = umf_extract(lu)
d == :L ? L :
(d == :U ? U :
(d == :p ? p :
(d == :q ? q :
(d == :Rs ? Rs :
(d == :(:) ? (L,U,p,q,Rs) :
throw(KeyError(d)))))))
end
for Tv in (:Float64, :Complex128), Ti in UmfpackIndexTypes
f = symbol(umf_nm("free_symbolic", Tv, Ti))
@eval begin
function ($f)(symb::Ptr{Void})
tmp = [symb]
ccall(($(string(f)), :libumfpack), Void, (Ptr{Void},), tmp)
end
function umfpack_free_symbolic(lu::UmfpackLU{$Tv,$Ti})
if lu.symbolic == C_NULL return lu end
umfpack_free_numeric(lu)
($f)(lu.symbolic)
lu.symbolic = C_NULL
return lu
end
end
f = symbol(umf_nm("free_numeric", Tv, Ti))
@eval begin
function ($f)(num::Ptr{Void})
tmp = [num]
ccall(($(string(f)), :libumfpack), Void, (Ptr{Void},), tmp)
end
function umfpack_free_numeric(lu::UmfpackLU{$Tv,$Ti})
if lu.numeric == C_NULL return lu end
($f)(lu.numeric)
lu.numeric = C_NULL
return lu
end
end
end
show_umf_info() = show_umf_info(2.)
function umfpack_report_symbolic(symb::Ptr{Void}, level::Real)
old_prl::Float64 = umf_ctrl[UMFPACK_PRL]
umf_ctrl[UMFPACK_PRL] = Float64(level)
@isok ccall((:umfpack_dl_report_symbolic, :libumfpack), Int,
(Ptr{Void}, Ptr{Float64}), symb, umf_ctrl)
umf_ctrl[UMFPACK_PRL] = old_prl
end
umfpack_report_symbolic(symb::Ptr{Void}) = umfpack_report_symbolic(symb, 4.)
function umfpack_report_symbolic(lu::UmfpackLU, level::Real)
umfpack_report_symbolic(umfpack_symbolic!(lu).symbolic, level)
end
umfpack_report_symbolic(lu::UmfpackLU) = umfpack_report_symbolic(lu.symbolic,4.)
function umfpack_report_numeric(num::Ptr{Void}, level::Real)
old_prl::Float64 = umf_ctrl[UMFPACK_PRL]
umf_ctrl[UMFPACK_PRL] = Float64(level)
@isok ccall((:umfpack_dl_report_numeric, :libumfpack), Int,
(Ptr{Void}, Ptr{Float64}), num, umf_ctrl)
umf_ctrl[UMFPACK_PRL] = old_prl
end
umfpack_report_numeric(num::Ptr{Void}) = umfpack_report_numeric(num, 4.)
function umfpack_report_numeric(lu::UmfpackLU, level::Real)
umfpack_report_numeric(umfpack_numeric!(lu).numeric, level)
end
umfpack_report_numeric(lu::UmfpackLU) = umfpack_report_numeric(lu,4.)
end # UMFPACK module
|