nlsic package¶
Submodules¶
nlsic.lapack_ssg module¶
- class nlsic.lapack_ssg.QR(a, rcond=np.float64(2.220446049250313e-14))[source]¶
Bases:
object
QR class for storing and dealing with QR decomposition by lapack. Input matrix a and right hand side b are modified by QR() and solve() respectivly if they are in fortran storage else a copy is made. If you need just to solve a full column rank, overdetermined linear system a*x=b and don’t need Q, R, rank and so on, use ls_solve(a, b) instead of this class. ls_solve() based on dgels lapack routine is slightly quiker for this task
- nlsic.lapack_ssg.ls_solve(a, b)[source]¶
Wraper for lapack dgels routine. Solve over- or under-determined linear system a*x=b by QR or LR decomposition return x which has dimensions nrow(x)==ncol(a) and ncol(x)==ncol(b). Thus, multiple right hand sides in b are allowed. If a has been already QR factorized by QR(a), you can use qra.solve(b) where qra is a QR instance. If the system is under-determined, the least norm solution is returned. If a and b are F-contiguous they are modified “in place”. You may want to protect them by copying before call. If a and b are _not_ F-contiguous they are copied inside the call to dgels (a warning is printed on stderr if -DF2PY_REPORT_ON_ARRAY_COPY was set during f2py compiling) so the original a and b are _not_ modified
- nlsic.lapack_ssg.tri_solve(a, b, uplo='U', trans='N')[source]¶
Solve triangular system a*x=b or at*x=b (trans=’N’ or ‘T’) The matrix a can represent upper or lower triangular system (uplo=’U’ or ‘L’) Other part of rectangular matrix a is not referenced. b is modified “in place” if it is F-contiguous.
nlsic.nlsic module¶
A set of functions for solving Non-linear Least Squares with Inequality constraints.
Main function is nlsic(...)
- class nlsic.nlsic.Obj(**kwargs)[source]¶
Bases:
object
tarnsform a dictionnary into object to allow access like o.field
- nlsic.nlsic.nlsic(par, r, u=None, co=None, control={}, e=None, eco=None, flsi=<function lsi>, *kargs, **kwargs)[source]¶
solve non linear least square problem min of ||r(par,*kargs, **kwargs).res||_2 with optional inequality constraints u*par>=co and optional equality constraints e*par=eco
Method sequential lsi globalized by backtracking technique. If e, eco are not None, reduce jacobian before lsi() call.
Notes If function r() return an object having attribute “jacobian” when cjac==True it is supposed to be equal to a matrix jacobian dr_dpar. Else numerical derivation is used for its construction.
- Parameters:
par (numpy array (float64)) – initial values for parameter vector (can be in non feasible domain) At return it contains the result vector (i.e. modified in-place).
r (function) –
function calculating residual vector by a call to r(par, cjac=True|False, *kargs, **kwargs) where
- par:
is a current parameter vector,
- cjac:
is logical indicating if we need or not a Jacobean together with residuals.
- *kargs:
positional parameters passed through to r()
- **kwargs:
keyword=value parameters passed through to r()
The call to r() must return an oject having an attribute “res” containing the residual vector and optionnaly an attribute “jacobian” when cjac is set to TRUE.
u (numpy matrix (float64), optional) – linear inequality constraint matrix in u*par>=co. Defaults to None.
co (numpy vector (float64), optional) – inequality constraints vector
controls (dict, optional) –
control parameters (=default values which is assumed if an entry is missing):
- tolx=1.e-7:
error on L2 norm of the iteration step sqrt(pt*p).
- maxit=100:
maximum of newton iterations
- btstart=1.:
(0;1) starting value for backtracking
- btfrac=0.5:
(0;1) by this value we diminish the step till tight up to the quadratic model of norm reduction in backtrack (bt) iterations
- btdesc=0.5:
(0;1) how good we have to tight up to the quadratic model. 0-we are very relax, 1 we are very close (may need many bt iterations)
- btmaxit=15:
maximum of backtrack iterations
- trace=0:
print tracing information on stdout
Defaults to empty dict {} which is equivalent to the above default values.
e (numpy matrix (float64), optional) – linear equality constraint matrix in e*par=eco. Defaults to None.
eco (numpy vector (float64), optional) – equality constraints vector
flsi (function) – solution of least squares with inequality constraints. Defaults to nlsic.lsi. Can be nlsic.lsi_ln if least norm increment is required.
kargs (list, optional) – optional positional arguments to be passed through to r function
kwargs (dict, optional) – optional keyword=value parameters to pass through to r function
- Returns:
result Object with the following fields:
- par:
resulting parameter vector
- laststep:
last increment after a possible back-tracking contraction
- normp:
L2 norm of the last increment before back-tracking
- res:
last residual vector
- prevres:
previous residual vector
- indx:
vector of p and z sets. indx[:nsetp] is a p-set indexes, while indx[nsetp:] is z-set indexes
- nsetp:
size of p-set.
- it:
number of non-linear iterations
- btit:
number of back-tracking iterations at the last non linear iteration
- error:
execution error code: 0 means no error occurred
- a:
last Jacobean calculated
- nte:
null-space basis matrix if e is provided
- mes:
if error != 0, then str with informative message.
- Return type:
Object
nlsic.nnls module¶
This module provides solvers for linear system with inequality constraints. Theay are based on nnls algorithm and software originaly designed and (re)written in Fortran77 by [Lawson & al, 1974].
- nnls:
solve Non Negative Least Square problem a*x~b, subject to x_i >= 0 for all indexes i
- ldp:
solve Least Distance Problem x~0 subject to u*x>=c. LDP is solved by reducing this problem to nnls problem.
- lsi:
solve Least Square problem with Inequality constraints a*x~b subject to u*x>=c. LSI problem is solved by reducing it to ldp problem. If matrices a and u are F-contiguous, they can be modified “in place”.
- lsi_ln:
the same as lsi but find least norm solution for rank-deficient matrix a.
- class nlsic.nnls.Obj(**kwargs)[source]¶
Bases:
object
tarnsform a dictionnary into object to allow access like obj.field
- nlsic.nnls.ldp(u, co, indx=None, nsetp=None)[source]¶
solve Least Distance Problem: min(x**2) subject to u*x>=co by reducing it to nnls problem. Params: u - m x n real matrix, m <= n, u is of full rank co - m real vector indx - m int vector nsetp - 1 int vector Return a real vector of lenght n. indx and nsetp are modified in place. Raise an exception if constraints are unfeasible u and co are not modified.
- nlsic.nnls.lsi(a, b, u=None, co=None, indx=None, nsetp=None)[source]¶
solve linear Least Square problem (min ||a*x-b||) subject to Inequalities u*x>=co by reducing it to LDP problem Return x indx, nsetp are modified in place. a and b are modified in place too. Raise an exception if a is not of full rank
- nlsic.nnls.lsi_ln(a, b, u=None, co=None, indx=None, nsetp=None)[source]¶
solve linear Least Square problem (min ||a*x-b||) subject to Inequalities u*x>=co by reducing it to LDP problem If matrix a is not of full rank, a least norm solution is provided. Return x indx, nsetp are modified in place. a and b are modified in place too.
- nlsic.nnls.nnls(a, b, indx=None, nsetp=None)[source]¶
wrapper for nnlsf call
- Parameters:
a – m x n real matrix, m >= n
b – m x 1
- Returns:
an object with fields:
- x:
solution vector of length n
- rnorm:
norm of residual ax-b
- w:
the dual solution vector of length n. w will satisfy w[i] = 0. for all i in set p and w[i] <= 0. for all i in set z ,
- indx:
p and z set. indx[:nsetp] is a p-set indexes, while indx[nsetp:] is z-set indexes
- nsetp:
p and z set separator in indx