basiclu-sys 0.1.2

Rust FFI bindings for BasicLU
Documentation
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#
# Julia interface to BASICLU
#

module basiclu

using LinearAlgebra
using LinearAlgebra: checksquare
using Printf
using SparseArrays
using SparseArrays: getcolptr
using Libdl

if haskey(ENV, "JULIA_BASICLU_LIBRARY_PATH")
  const libbasiclu = joinpath(ENV["JULIA_BASICLU_LIBRARY_PATH"], "libbasiclu.$dlext")
else
  using basiclu_jll
end

# comment out if the MAT module is installed
# include("test.jl")

# status codes
const BASICLU_OK = 0
const BASICLU_WARNING_SINGULAR_MATRIX = 2
const BASICLU_ERROR_INVALID_STORE = -1
const BASICLU_ERROR_INVALID_CALL = -2
const BASICLU_ERROR_ARGUMENT_MISSING = -3
const BASICLU_ERROR_INVALID_ARGUMENT = -4
const BASICLU_ERROR_MAXIMUM_UPDATES = -5
const BASICLU_ERROR_SINGULAR_UPDATE = -6
const BASICLU_ERROR_INVALID_OBJECT = -8
const BASICLU_ERROR_OUT_OF_MEMORY = -9

# user parameters in xstore (zero based)
const BASICLU_DROP_TOLERANCE = 4
const BASICLU_ABS_PIVOT_TOLERANCE = 5
const BASICLU_REL_PIVOT_TOLERANCE = 6
const BASICLU_BIAS_NONZEROS = 7
const BASICLU_MAXN_SEARCH_PIVOT = 8
const BASICLU_PAD = 9
const BASICLU_STRETCH = 10
const BASICLU_COMPRESSION_THRESHOLD = 11
const BASICLU_SPARSE_THRESHOLD = 12
const BASICLU_REMOVE_COLUMNS = 13
const BASICLU_SEARCH_ROWS = 14

# user readable from xstore (zero based)
const BASICLU_DIM = 64
const BASICLU_STATUS = 65
const BASICLU_NUPDATE = 70
const BASICLU_NFORREST = 71
const BASICLU_NFACTORIZE = 72
const BASICLU_NUPDATE_TOTAL = 73
const BASICLU_NFORREST_TOTAL = 74
const BASICLU_NSYMPERM_TOTAL = 75
const BASICLU_LNZ = 76
const BASICLU_UNZ = 77
const BASICLU_RNZ = 78
const BASICLU_MIN_PIVOT = 79
const BASICLU_MAX_PIVOT = 80
const BASICLU_UPDATE_COST = 81
const BASICLU_TIME_FACTORIZE = 82
const BASICLU_TIME_SOLVE = 83
const BASICLU_TIME_UPDATE = 84
const BASICLU_TIME_FACTORIZE_TOTAL = 85
const BASICLU_TIME_SOLVE_TOTAL = 86
const BASICLU_TIME_UPDATE_TOTAL = 87
const BASICLU_LFLOPS = 88
const BASICLU_UFLOPS = 89
const BASICLU_RFLOPS = 90
const BASICLU_CONDEST_L = 91
const BASICLU_CONDEST_U = 92
const BASICLU_MAX_ETA = 93
const BASICLU_NORM_L = 94
const BASICLU_NORM_U = 95
const BASICLU_NORMEST_LINV = 96
const BASICLU_NORMEST_UINV = 97
const BASICLU_MATRIX_ONENORM = 98
const BASICLU_MATRIX_INFNORM = 99
const BASICLU_RESIDUAL_TEST = 111
const BASICLU_MATRIX_NZ = 100
const BASICLU_RANK = 101
const BASICLU_BUMP_SIZE = 102
const BASICLU_BUMP_NZ = 103
const BASICLU_NSEARCH_PIVOT = 104
const BASICLU_NEXPAND = 105
const BASICLU_NGARBAGE = 106
const BASICLU_FACTOR_FLOPS = 107
const BASICLU_TIME_SINGLETONS = 108
const BASICLU_TIME_SEARCH_PIVOT = 109
const BASICLU_TIME_ELIM_PIVOT = 110
const BASICLU_PIVOT_ERROR = 120

mutable struct Param
    # Factorization parameters
    absPivotTol::Float64
    relPivotTol::Float64
    dropTol::Float64
    biasNz::Int64
    maxSearch::Int64
    searchRows::Int64
    removeCols::Int64
    memPad::Int64
    memStretch::Float64
    memCompress::Float64
    # Solve parameters
    sparseThres::Float64
    function Param()
        param = new()
        for f in fieldnames(Param)
            setfield!(param, f, convert(fieldtype(Param, f), 0))
        end
        param
    end
end

mutable struct Info
    dim::Int64
    status::Int64
    # Info from last factorization
    nPivot::Int64
    nMatElem::Int64
    nBumpCol::Int64
    nBumpElem::Int64
    nSearchPivot::Int64
    nExpand::Int64
    nGarbage::Int64
    nFactorFlop::Int64
    timeFactorize::Float64
    timeSingletons::Float64
    timeSearchPivot::Float64
    timeElimPivot::Float64
    residualTest::Float64
    onenormMatrix::Float64
    infnormMatrix::Float64
    normL::Float64
    normU::Float64
    normestLinv::Float64
    normestUinv::Float64
    condestL::Float64
    condestU::Float64
    # Info from solves and updates since last factorization
    nUpdate::Int64
    nForrest::Int64
    nElemL::Int64
    nElemU::Int64
    nElemR::Int64
    nFlopL::Int64
    nFlopU::Int64
    nFlopR::Int64
    updateCost::Float64
    minPivot::Float64
    maxPivot::Float64
    maxEta::Float64
    pivotError::Float64
    timeSolve::Float64
    timeUpdate::Float64
    # Accumulated info since object was created
    nFactorize::Int64
    nUpdateTotal::Int64
    nForrestTotal::Int64
    nSympermTotal::Int64
    timeFactorizeTotal::Float64
    timeSolveTotal::Float64
    timeUpdateTotal::Float64
    function Info()
        info = new()
        for f in fieldnames(Info)
            setfield!(info, f, convert(fieldtype(Info, f), 0))
        end
        info
    end
end

function paramnumber(name::Symbol)
    if name == :absPivotTol
        return BASICLU_ABS_PIVOT_TOLERANCE
    elseif name == :relPivotTol
        return BASICLU_REL_PIVOT_TOLERANCE
    elseif name == :dropTol
        return BASICLU_DROP_TOLERANCE
    elseif name == :biasNz
        return BASICLU_BIAS_NONZEROS
    elseif name == :maxSearch
        return BASICLU_MAXN_SEARCH_PIVOT
    elseif name == :searchRows
        return BASICLU_SEARCH_ROWS
    elseif name == :removeCols
        return BASICLU_REMOVE_COLUMNS
    elseif name == :memPad
        return BASICLU_PAD
    elseif name == :memStretch
        return BASICLU_STRETCH
    elseif name == :memCompress
        return BASICLU_COMPRESSION_THRESHOLD
    elseif name == :sparseThres
        return BASICLU_SPARSE_THRESHOLD
    end
    throw(ErrorException("unknown parameter name"))
end

function infonumber(name::Symbol)
    if name == :dim
        return BASICLU_DIM
    elseif name == :status
        return BASICLU_STATUS
    elseif name == :nPivot
        return BASICLU_RANK
    elseif name == :nMatElem
        return BASICLU_MATRIX_NZ
    elseif name == :nBumpCol
        return BASICLU_BUMP_SIZE
    elseif name == :nBumpElem
        return BASICLU_BUMP_NZ
    elseif name == :nSearchPivot
        return BASICLU_NSEARCH_PIVOT
    elseif name == :nExpand
        return BASICLU_NEXPAND
    elseif name == :nGarbage
        return BASICLU_NGARBAGE
    elseif name == :nFactorFlop
        return BASICLU_FACTOR_FLOPS
    elseif name == :timeFactorize
        return BASICLU_TIME_FACTORIZE
    elseif name == :timeSingletons
        return BASICLU_TIME_SINGLETONS
    elseif name == :timeSearchPivot
        return BASICLU_TIME_SEARCH_PIVOT
    elseif name == :timeElimPivot
        return BASICLU_TIME_ELIM_PIVOT
    elseif name == :residualTest
        return BASICLU_RESIDUAL_TEST
    elseif name == :onenormMatrix
        return BASICLU_MATRIX_ONENORM
    elseif name == :infnormMatrix
        return BASICLU_MATRIX_INFNORM
    elseif name == :normL
        return BASICLU_NORM_L
    elseif name == :normU
        return BASICLU_NORM_U
    elseif name == :normestLinv
        return BASICLU_NORMEST_LINV
    elseif name == :normestUinv
        return BASICLU_NORMEST_UINV
    elseif name == :condestL
        return BASICLU_CONDEST_L
    elseif name == :condestU
        return BASICLU_CONDEST_U
    elseif name == :nUpdate
        return BASICLU_NUPDATE
    elseif name == :nForrest
        return BASICLU_NFORREST
    elseif name == :nElemL
        return BASICLU_LNZ
    elseif name == :nElemU
        return BASICLU_UNZ
    elseif name == :nElemR
        return BASICLU_RNZ
    elseif name == :nFlopL
        return BASICLU_LFLOPS
    elseif name == :nFlopU
        return BASICLU_UFLOPS
    elseif name == :nFlopR
        return BASICLU_RFLOPS
    elseif name == :updateCost
        return BASICLU_UPDATE_COST
    elseif name == :minPivot
        return BASICLU_MIN_PIVOT
    elseif name == :maxPivot
        return BASICLU_MAX_PIVOT
    elseif name == :maxEta
        return BASICLU_MAX_ETA
    elseif name == :pivotError
        return BASICLU_PIVOT_ERROR
    elseif name == :timeSolve
        return BASICLU_TIME_SOLVE
    elseif name == :timeUpdate
        return BASICLU_TIME_UPDATE
    elseif name == :nFactorize
        return BASICLU_NFACTORIZE
    elseif name == :nUpdateTotal
        return BASICLU_NUPDATE_TOTAL
    elseif name == :nForrestTotal
        return BASICLU_NFORREST_TOTAL
    elseif name == :nSympermTotal
        return BASICLU_NSYMPERM_TOTAL
    elseif name == :timeFactorizeTotal
        return BASICLU_TIME_FACTORIZE_TOTAL
    elseif name == :timeSolveTotal
        return BASICLU_TIME_SOLVE_TOTAL
    elseif name == :timeUpdateTotal
        return BASICLU_TIME_UPDATE_TOTAL
    end
    throw(ErrorException("unknown info name"))
end

mutable struct basiclu_object
    istore::Ptr{Int64}
    xstore::Ptr{Float64}
    Li::Ptr{Int64}
    Ui::Ptr{Int64}
    Wi::Ptr{Int64}
    Lx::Ptr{Float64}
    Ux::Ptr{Float64}
    Wx::Ptr{Float64}
    lhs::Ptr{Float64}
    ilhs::Ptr{Int64}
    nzlhs::Int64
    realloc_factor::Float64
    function basiclu_object(dim::Int64)
        obj = new()
        retcode = ccall((:basiclu_obj_initialize, libbasiclu), Int64,
                        (Ptr{basiclu_object}, Int64),
                        pointer_from_objref(obj), dim)
        checkretcode("basiclu_obj_initialize", retcode)
        finalizer(obj) do x
            ccall((:basiclu_obj_free, libbasiclu), Cvoid,
                  (Ptr{basiclu_object},), pointer_from_objref(x))
        end
    end
end

function getdim(obj::basiclu_object)
    ccall((:basiclu_obj_get_dim, libbasiclu), Int64,
          (Ptr{basiclu_object},), pointer_from_objref(obj))
end

function getparam(obj::basiclu_object, name::Symbol)
    val = 0.0
    if obj.xstore != C_NULL
        val = unsafe_load(obj.xstore, paramnumber(name) + 1)
    end
    convert(fieldtype(Param, name), val)
end

function getparam(obj::basiclu_object)
    param = Param()
    for f in fieldnames(Param)
        setfield!(param, f, getparam(obj, f))
    end
    param
end

function setparam!(obj::basiclu_object, name::Symbol, val)
    if obj.xstore != C_NULL
        unsafe_store!(obj.xstore, val, paramnumber(name) + 1)
    end
end

function setparam!(obj::basiclu_object, param::Param)
    for f in fieldnames(Param)
        setparam!(obj, f, getfield(param, f))
    end
end

function getinfo(obj::basiclu_object, name::Symbol)
    val = 0.0
    if obj.xstore != C_NULL
        val = unsafe_load(obj.xstore, infonumber(name) + 1)
    end
    convert(fieldtype(Info, name), val)
end

function getinfo(obj::basiclu_object)
    info = Info()
    for f in fieldnames(Info)
        setfield!(info, f, getinfo(obj, f))
    end
    info
end

"""
    factorize(obj::basiclu_object, B::SparseMatrixCSC{Float64, Int64}; check::Bool=true)

Factorize sparse matrix `B`, which must be square and have the dimension for
which `obj` was created.

The factorization method stops when all elements of the active submatrix are
below the absolute pivot tolerance. In this case the `L` and `U` matrix are
padded with columns of the identity matrix, yielding the factorization of a
matrix `B*` which equals `B` except that the dependent columns are replaced by
columns of the identity matrix. The number of actual pivot steps performed can
be obtained with `getinfo(obj, :nPivot)`.

When `check = true`, an error is thrown if the number of pivot steps is less
than the dimension of `B`.
"""
function factorize(obj::basiclu_object, B::SparseMatrixCSC{Float64, Int64}; check::Bool=true)
    dim = getdim(obj)
    m = checksquare(B)
    if dim != m
        throw(DimensionMismatch("matrix dimension does not match basiclu object"))
    end
    if dim != 0
        Bp = getcolptr(B) .- 1
        Bi = rowvals(B) .- 1
        Bx = nonzeros(B)        # don't need a copy
        retcode = ccall((:basiclu_obj_factorize, libbasiclu), Int64,
                        (Ptr{basiclu_object}, Ptr{Int64}, Ptr{Int64}, Ptr{Int64}, Ptr{Float64}),
                        pointer_from_objref(obj), Bp, pointer(Bp, 2), Bi, Bx)
        if retcode == BASICLU_WARNING_SINGULAR_MATRIX && !check
            retcode = BASICLU_OK
        end
        checkretcode("basiclu_obj_factorize", retcode)
    end
end

"""
    getfactors(obj::basiclu_object) -> L::SparseMatrixCSC{Float64, Int64},
                                       U::SparseMatrixCSC{Float64, Int64},
                                       p::Vector{Int64},
                                       q::Vector{Int64}

Extract LU factors after fresh factorization. `L` is unit lower triangular, `U`
is upper triangular and `p` and `q` are permutation vectors such that (ignoring
round-off errors) `B[p,q]=LU` when matrix `B` was factorizied.
"""
function getfactors(obj::basiclu_object)
    if getinfo(obj, :nUpdate) != 0
        throw(ErrorException("cannot extract LU factors after factorization has been updated"))
    end
    dim = getdim(obj)
    if dim == 0
        L = spzeros(0, 0)
        U = spzeros(0, 0)
        rowperm = zeros(Int64, 0)
        colperm = zeros(Int64, 0)
        return L, U, rowperm, colperm
    end
    info = getinfo(obj)
    rowperm = Vector{Int64}(undef, dim)
    colperm = Vector{Int64}(undef, dim)
    Lcolptr = Vector{Int64}(undef, dim + 1)
    Lrowidx = Vector{Int64}(undef, info.nElemL + dim)
    Lvalue = Vector{Float64}(undef, info.nElemL + dim)
    Ucolptr = Vector{Int64}(undef, dim + 1)
    Urowidx = Vector{Int64}(undef, info.nElemU + dim)
    Uvalue = Vector{Float64}(undef, info.nElemU + dim)
    retcode = ccall((:basiclu_obj_get_factors, libbasiclu), Int64,
                    (Ptr{basiclu_object}, Ptr{Int64}, Ptr{Int64},
                     Ptr{Int64}, Ptr{Int64}, Ptr{Float64},
                     Ptr{Int64}, Ptr{Int64}, Ptr{Float64}),
                    pointer_from_objref(obj), rowperm, colperm,
                    Lcolptr, Lrowidx, Lvalue,
                    Ucolptr, Urowidx, Uvalue)
    if retcode == BASICLU_ERROR_INVALID_CALL
        throw(ErrorException("no fresh factorization available"))
    end
    checkretcode("basiclu_get_factors", retcode)
    L = SparseMatrixCSC{Float64, Int64}(dim, dim, Lcolptr .+ 1, Lrowidx .+ 1, Lvalue)
    U = SparseMatrixCSC{Float64, Int64}(dim, dim, Ucolptr .+ 1, Urowidx .+ 1, Uvalue)
    return L, U, rowperm .+ 1, colperm .+ 1
end

"""
    solve(obj::basiclu_object, rhs::Vector{Float64}, trans::AbstractChar) -> Vector{Float64}

Solve linear system with dense right-hand side. `rhs` is not modified. `trans`
must be 'T' for transposed solve or 'N' for forward solve.
"""
function solve(obj::basiclu_object, rhs::Vector{Float64}, trans::AbstractChar)
    lhs = copy(rhs)
    solve!(obj, lhs, trans)
end

"""
    solve!(obj::basiclu_object, rhs::Vector{Float64}, trans::AbstractChar) -> Vector{Float64}

Solve linear system with dense right-hand side. Solution overwrites `rhs`.
`trans` must be 'T' for transposed solve or 'N' for forward solve.
"""
function solve!(obj::basiclu_object, rhs::Vector{Float64}, trans::AbstractChar)
    checktrans(trans)
    dim = getdim(obj)
    if length(rhs) != dim
        throw(DimensionMismatch("dimension of right-hand side does not match basiclu object"))
    end
    if dim != 0
        retcode = ccall((:basiclu_obj_solve_dense, libbasiclu), Int64,
                        (Ptr{basiclu_object},  Ptr{Float64}, Ptr{Float64}, Cchar),
                        pointer_from_objref(obj), rhs, rhs, trans)
        if retcode == BASICLU_ERROR_INVALID_CALL
            throw(ErrorException("no factorization available"))
        end
        checkretcode("basiclu_obj_solve_dense", retcode)
    end
    rhs
end

"""
    solve(obj::basiclu_object, rhs::SparseVector{Float64, Int64}, trans::AbstractChar) -> SparseVector{Float64, Int64}

Solve linear system with sparse right-hand side. `rhs` is not modified. `trans`
must be 'T' for transposed solve or 'N' for forward solve.
"""
function solve(obj::basiclu_object, rhs::SparseVector{Float64, Int64}, trans::AbstractChar)
    lhs = copy(rhs)
    solve!(obj, lhs, trans)
end

"""
    solve!(obj::basiclu_object, rhs::SparseVector{Float64, Int64}, trans::AbstractChar) -> SparseVector{Float64, Int64}

Solve linear system with sparse right-hand side. Solution overwrites `rhs`.
`trans` must be 'T' for transposed solve or 'N' for forward solve.
"""
function solve!(obj::basiclu_object, rhs::SparseVector{Float64, Int64}, trans::AbstractChar)
    checktrans(trans)
    dim = getdim(obj)
    if length(rhs) != dim
        throw(DimensionMismatch("dimension of right-hand side does not match basiclu object"))
    end
    if dim == 0
        return spzeros(dim)
    end
    retcode = ccall((:basiclu_obj_solve_sparse, libbasiclu), Int64,
                    (Ptr{basiclu_object},  Int64, Ptr{Int64}, Ptr{Float64}, Cchar),
                    pointer_from_objref(obj), nnz(rhs), rowvals(rhs) .- 1, nonzeros(rhs), trans)
    if retcode == BASICLU_ERROR_INVALID_CALL
        throw(ErrorException("no factorization available"))
    end
    checkretcode("basiclu_obj_solve_sparse", retcode)
    gathersol!(obj, rhs)
end

"""
    solve_for_update(obj::basiclu_object, rhs::SparseVector{Float64, Int64}; getsol::Bool=false) -> SparseVector{Float64, Int64}

Solve forward system in preparation to update the factorization. `rhs` holds the
column to be inserted into the factorized matrix in the next call to
[`update`](@ref). When `getsol = true`, then the solution from the forward
solve with right-hand side `rhs` is returned. Otherwise only the update is
prepared.
"""
function solve_for_update(obj::basiclu_object, rhs::SparseVector{Float64, Int64}; getsol::Bool=false)
    dim = getdim(obj)
    if length(rhs) != dim
        throw(DimensionMismatch("dimension of right-hand side does not match basiclu object"))
    end
    if dim == 0
        return getsol ? spzeros(dim) : nothing
    end
    retcode = ccall((:basiclu_obj_solve_for_update, libbasiclu), Int64,
                    (Ptr{basiclu_object}, Int64, Ptr{Int64}, Ptr{Float64}, Cchar, Int64),
                    pointer_from_objref(obj), nnz(rhs), rowvals(rhs) .- 1, nonzeros(rhs), 'N', getsol ? 1 : 0)
    if retcode == BASICLU_ERROR_INVALID_CALL
        throw(ErrorException("no factorization available"))
    end
    if retcode == BASICLU_ERROR_MAXIMUM_UPDATES
        throw(ErrorException("maximum number of updates reached"))
    end
    checkretcode("basiclu_obj_solve_for_update", retcode)
    return getsol ? gathersol(obj) : nothing
end

"""
    solve_for_update(obj::basiclu_object, pos::Int64; getsol::Bool=false) -> SparseVector{Float64, Int64}

Solve transposed system in preparation to update the factorization. `pos` holds
the column index of the factorized matrix to be replaced in the next call to
[`update`](@ref). When `getsol = true`, then the solution from the transposed
solve with a unit vector as right-hand side is returned. Otherwise only the
update is prepared.
"""
function solve_for_update(obj::basiclu_object, pos::Int64; getsol::Bool=false)
    dim = getdim(obj)
    if pos < 1 || pos > dim
        throw(DimensionMismatch("column index outside dimension of basiclu object"))
    end
    if dim == 0
        return getsol ? spzeros(dim) : nothing
    end
    retcode = ccall((:basiclu_obj_solve_for_update, libbasiclu), Int64,
                    (Ptr{basiclu_object}, Int64, Ptr{Int64}, Ptr{Float64}, Cchar, Int64),
                    pointer_from_objref(obj), 0, Ref{Int64}(pos-1), C_NULL, 'T', getsol ? 1 : 0)
    if retcode == BASICLU_ERROR_INVALID_CALL
        throw(ErrorException("no factorization available"))
    end
    if retcode == BASICLU_ERROR_MAXIMUM_UPDATES
        throw(ErrorException("maximum number of updates reached"))
    end
    checkretcode("basiclu_obj_solve_for_update", retcode)
    return getsol ? gathersol(obj) : nothing
end

"""
    update(obj::basiclu_object, pivot::Float64) -> Float64

Update the factorization after a column modification. The column position and
the new column must have been set in previous calls to
[`solve_for_update`](@ref).

`pivot` is the pivot element corresponding to the update operation; i.e. when
column `j` of `B` is to be replaced by vector `v`, then `pivot = (B\\v)[j]`. The
absolute difference between `pivot` and a recomputed version can be obtained
with `getinfo(obj, :pivotError)`; this is also the return value. A pivot error
larger than 1e-8, say, indicates numerical instability and suggests
refactorization.

An error is thrown when the recomputed pivot element is below the absolute pivot
tolerance. In this case no update is performed and the old factorization remains
valid.
"""
function update(obj::basiclu_object, pivot::Float64)
    dim = getdim(obj)
    if dim != 0
        retcode = ccall((:basiclu_obj_update, libbasiclu), Int64,
                        (Ptr{basiclu_object},  Float64),
                        pointer_from_objref(obj), pivot)
        if retcode == BASICLU_ERROR_INVALID_CALL
            throw(ErrorException("not prepared for update"))
        end
        checkretcode("basiclu_update", retcode)
    end
    getinfo(obj, :pivotError)
end

"""
    maxvolume(obj::basiclu_object, A::SparseMatrixCSC{Float64, Int64}, basis::Vector{Int64}, volumetol::Float64=2.0) -> Int64

Given an initial basis such that `A[:,basis]` is square and nonsingular, make
one pass over the nonbasic columns of `A` and pivot each column into the basis
when it increases the absolute value of the determinant of the basis matrix by
more than a factor `volumetol`. On return `basis` has been updated. Return the
number of basis updates performed.
"""
function maxvolume(obj::basiclu_object, A::SparseMatrixCSC{Float64, Int64}, basis::Vector{Int64},
                   volumetol::Float64=2.0)
    m, n = size(A)
    if m != getdim(obj)
        throw(DimensionMismatch("number of rows of A does not match dimension of basiclu object"))
    end
    if length(basis) != getdim(obj)
        throw(DimensionMismatch("basis has incorrect length"))
    end
    isbasic = zeros(Int64, n)
    isbasic[basis] .= 1
    if sum(isbasic) != m
        throw(ErrorException("duplicate index in basis"))
    end
    cbasis = basis .- 1
    Ap = getcolptr(A) .- 1
    Ai = rowvals(A) .- 1
    Ax = nonzeros(A)            # don't need a copy
    p_nupdate = Ref{Int64}(0)
    retcode = ccall((:basiclu_obj_maxvolume, libbasiclu), Int64,
                    (Ptr{basiclu_object}, Int64, Ptr{Int64}, Ptr{Int64}, Ptr{Float64},
                     Ptr{Int64}, Ptr{Int64}, Float64, Ptr{Int64}),
                    pointer_from_objref(obj), n, Ap, Ai, Ax, cbasis, isbasic, volumetol, p_nupdate)
    basis[:] = cbasis .+ 1
    checkretcode("basiclu_obj_maxvolume", retcode)
    return p_nupdate[]
end

"""
    maxvolbasis(A::SparseMatrixCSC{Float64, Int64}; lindeptol::Float64=1e-8,
                volumetol::Float64=2.0, maxpass::Int64=2, verbose::Bool=true) -> Vector{Int64}, basiclu_object 

Find a set of column indices for the matrix `AI = [A I]` such that `AI[:,basis]`
is square and nonsingular and the number of slack columns in the basis is
minimum (this is the row rank deficiency of `A`). Return the vector of column
indices of `AI` which form the basis matrix and a `basiclu_object` which holds a
factorization of the basis matrix.

Method: Scale the slack columns of `AI` by `lindeptol` and try to find a maximum
volume basis for this matrix by making at most `maxpass` calls to
[`maxvolume`](@ref). If `verbose` is true, then print the number of basis
updates after each call.
"""
function maxvolbasis(A::SparseMatrixCSC{Float64, Int64}; lindeptol::Float64=1e-8,
                     volumetol::Float64=2.0, maxpass::Int64=2,
                     verbose::Bool=true)
    m, n = size(A)
    rowmax = maximum(abs.(A), dims=2)[:]
    rowmax = max.(rowmax, 1.)
    AI = [A spdiagm(0 => lindeptol.*rowmax)]
    basis = collect(n+1:n+m)
    obj = basiclu_object(m)
    for pass = 1:maxpass
        nupdate = maxvolume(obj, AI, basis, volumetol)
        if verbose
            @printf("pass %d: %d updates\n", pass, nupdate)
        end
        if nupdate == 0 break; end
    end
    return basis, obj
end

function checktrans(trans::AbstractChar)
    if !(trans == 'N' || trans == 'T')
        throw(ArgumentError("trans argument must be 'N' (no transpose) or 'T' (transpose), got '$trans'"))
    end
    trans
end

function checkretcode(funcname::String, retcode::Int64)
    if retcode == BASICLU_ERROR_OUT_OF_MEMORY
        throw(OutOfMemoryError())
    elseif retcode == BASICLU_WARNING_SINGULAR_MATRIX
        msg = @sprintf("%s() encountered singular matrix", funcname)
        throw(ErrorException(msg))
    elseif retcode == BASICLU_ERROR_SINGULAR_UPDATE
        msg = @sprintf("%s() encountered singular update", funcname)
        throw(ErrorException(msg))
    elseif retcode != BASICLU_OK
        msg = @sprintf("%s() failed with return code %d", funcname, retcode)
        throw(ErrorException(msg))
    end
end

function gathersol(obj::basiclu_object)
    dim = getdim(obj)
    nzind = Vector{Int64}(undef, obj.nzlhs)
    nzval = Vector{Float64}(undef, obj.nzlhs)
    lhs = SparseVector{Float64, Int64}(dim, nzind, nzval)
    gathersol!(obj, lhs)
end

function gathersol!(obj::basiclu_object, lhs::SparseVector{Float64, Int64})
    dim = getdim(obj)
    @assert length(lhs) == dim
    if nnz(lhs) != obj.nzlhs
        resize!(lhs.nzind, obj.nzlhs)
        resize!(lhs.nzval, obj.nzlhs)
    end
    for i = 1:obj.nzlhs
        idx = unsafe_load(obj.ilhs, i) + 1
        @assert idx >= 1 && idx <= dim
        lhs.nzind[i] = idx
        lhs.nzval[i] = unsafe_load(obj.lhs, idx)
    end
    p = sortperm(lhs.nzind)
    lhs.nzind[:] = lhs.nzind[p]
    lhs.nzval[:] = lhs.nzval[p]
    lhs
end

end