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
//! Python wrapper for the solver operation in [`crate::ops::solver`].
//!
//! Free function solving a linear system into a [`PyNodeField`]. Kept here —
//! mirroring `src/ops/solver/` — per the `py/ops/` convention (its identity
//! is the *operation*, not its `NodeField` result).
use crate;
use cratePyMatrix;
use cratePyNodeField;
use cratePySignals;
use *;
use py_op;
/// Solve the linear system `A·x = b` for `x` (sparse direct, faer).
///
/// The matrix carries everything the solve needs: its relation rows, their
/// coefficients and their sense (`=`, `>=`, `<=`), declared by the constraints
/// that built them — no model is passed alongside. As soon as it carries an
/// inequality, an active-set loop decides which relations are saturated: an
/// active relation holds as an equation (its `lambda` is `<= 0` for `>=`, `>= 0`
/// for `<=`), an inactive one reports `lambda = 0`.
///
/// # Parameters
///
/// - `matrix`: the assembled system `A`.
/// - `rhs`: the right-hand side `b`, read at every row of `matrix`; a row no zone
/// of `rhs` defines reads zero. The imposed value of a constraint relation
/// lives at its multiplier node, component `imposed`.
/// - `method`: the factorization. `"lu"` works on any square matrix;
/// `"cholesky"` asks that the system be symmetric **and** positive definite —
/// never a system with multipliers, usually one reduced by elimination. The
/// active-set loop factorizes by LU whatever is asked.
/// - `relations`: how the constraint relations are enforced. `"lagrange"`: by
/// their multipliers, the system solved as assembled. `"eliminate"`: each
/// equality relation eliminates one unknown and the smaller system left,
/// without multipliers, is solved; an inequality is not eliminated, it stays
/// on the reduced system and is resolved by the same active-set loop. Scope of
/// `"eliminate"`: each relation eliminates an unknown of its own, which
/// appears in no other equality relation.
/// - `active_set`: how each status of the active-set loop is factorized, read
/// only when `matrix` carries an inequality. `"schur"` factorizes the
/// inequality-free base once and updates it per status, falling back on
/// `"refactorize"` when that base is singular; `"refactorize"` factorizes the
/// full system at each status change.
/// - `cache`: reuse what a previous solve stored on `matrix` — the
/// factorization, the condensation, the active-set state and its last status —
/// and store what this one computes. The store is cleared when the matrix
/// changes. A matrix keeps one of them at a time, and a factorization serves
/// the `method` it was made with only: another `relations` or `method`
/// computes afresh and replaces it. `False` neither reads nor fills it.
/// - `max_iter`: the bound of the active-set loop, in status changes.
/// - `tol`: the sign tolerance of the active-set loop, on the multiplier and on
/// the gap.
/// - `verbosity`: how much the solve reports on stdout: `"silent"`, `"brief"` or
/// `"detailed"`.
///
/// # Returns
///
/// The solution, read at the columns of `matrix`: one zone per column support of
/// its blocks, each on the support of the block itself. Two solves of the same
/// model therefore share their supports, and their fields combine zone by zone.
/// The multiplier of every relation comes back at its node, component `lambda`,
/// whether the relations were kept or eliminated.
///
/// # Raises
///
/// `RuntimeError` if `matrix` has no assembled values, if the LU meets an
/// exactly zero pivot (an unknown nothing holds at all), if `"cholesky"` meets a
/// matrix that is not positive definite, if a relation is out of the scope of
/// `"eliminate"`, or if the active-set loop has not converged after `max_iter`
/// status changes. `ValueError` for an unknown `method`, `relations`,
/// `active_set` or `verbosity`. `KeyboardInterrupt` on `Ctrl+C`.
///
/// # Notes
///
/// `Ctrl+C` is honoured between the phases of the solve and at each status
/// change; a factorization is a single library call and is not interrupted
/// mid-way.
///
/// A matrix that is singular only up to rounding is not always reported: a
/// structure left free to move as a rigid body, its boundary conditions
/// forgotten, can come back as a finite but absurdly large solution. Check the
/// boundary conditions when the magnitudes are implausible.