📄 dpr1fact.c
字号:
/*
% [Lden,L.d] = dpr1fact(x, d, Lsym, smult, maxu)
% DPR1FACT Factor d[iag] p[lus] r[ank] 1:
% [Lden,L.d] = dpr1fact(x, d, Lsym, smult, maxu)
% Computes fi and d such that
% diag(d_IN) + x*diag(smult)*x' =
%(PI_{i=1}^n L(p_OUT^i,beta_i)) * diag(d_OUT) * (PI_{i=1}^n L(p_OUT^i,beta_i))'
% where L(p,beta) = eye(n) + tril(p*beta',-1).
%
% Lden.dopiv(k) = 1 if p(:,k) has been reordered, with permutation in
% Lden.pivperm.
% We reorder if otherwise |p(i,k)*beta(j,k)| > maxu.
%
% SEE ALSO fwdpr1,bwdpr1,sedumi
% ******************** INTERNAL FUNCTION OF SEDUMI ********************
function [Lden,L.d] = dpr1fact(x, d, Lsym, smult, maxu)
% This file is part of SeDuMi 1.1 by Imre Polik and Oleksandr Romanko
% Copyright (C) 2005 McMaster University, Hamilton, CANADA (since 1.1)
%
% Copyright (C) 2001 Jos F. Sturm (up to 1.05R5)
% Dept. Econometrics & O.R., Tilburg University, the Netherlands.
% Supported by the Netherlands Organization for Scientific Research (NWO).
%
% Affiliation SeDuMi 1.03 and 1.04Beta (2000):
% Dept. Quantitative Economics, Maastricht University, the Netherlands.
%
% Affiliations up to SeDuMi 1.02 (AUG1998):
% CRL, McMaster University, Canada.
% Supported by the Netherlands Organization for Scientific Research (NWO).
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
% 02110-1301, USA
*/
#include <math.h>
#include <string.h>
#include "mex.h"
#include "blksdp.h"
#define LDEN_OUT myplhs[0]
#define D_OUT myplhs[1]
#define NPAROUT 2
#define X_IN prhs[0]
#define D_IN prhs[1]
#define LSYMB_IN prhs[2]
#define SMULT_IN prhs[3]
#define MAXU_IN prhs[4]
#define NPARIN 5
/* ============================================================
DPR1FACT-subroutines: Compact Cholesky for X = diag(d) + p*p'.
several versions, to allow sequential or permuted ordering.
============================================================ */
/* ************************************************************
dpr1fact - Compact Cholesky for X = diag(d) + p*p'/t to
X = L(p,beta) * diag(d_OUT) * L(p,beta)'
where L(p,beta) = eye(m) + tril(p*beta',-1)
INPUT:
n - Order of beta. n = min(m,idep), where idep is the
1st entry where d(idep) = 0 on input. Caller then needs to finish by
pivoting on idep by itself.
mu - mu(m) = 0, mu(i) = max(psqr(i+1:mk)), for i=1:mk-1.
maxu - Controls stability check: we postpone rows such that
max(abs(L)) <= maxu.
UPDATED:
d - Length n vector: the diagonal entries. On input, the old ones,
d(1:n) > 0. On output the updated ones after the factorization.
Remain positive if t > 0.
fi - on input, contains the vector x (=p.^2),
on output it is such that beta(j) = p(j) / fi(j), for
j not in ph2psqr.i.
t - Initial t: set t = 1 for D+p*p', set t = -1 for D-p*p'.
OUTPUT
ph2psqr - The postponed rows j, with corresponding psqr(j). Controled
by maxu.
REMARK:
Since L=eye(m)+tril(p*beta'), beta(n-1) and fi(n-1) are useful only
if m > n: it'll be used in rows n:m-1.
RETURNS: nph2, number of postponed nodes = length(ph2psqr).
************************************************************ */
int dpr1fact(double *fi, double *d, keydouble *ph2psqr, double *pt, const int n,
const double *mu, const double maxu)
{
int nph2;
double dj,fij, muph2, t;
keydouble p2j;
/* ------------------------------------------------------------
fi(j) = x(j) + t*d(j), d_new(j) = fi(j)/t, tnew = fi(j)/d_old(j)
Store j in p2j.k
------------------------------------------------------------ */
t = *pt;
nph2 = 0;
muph2 = 0.0; /* muph2 = max(psqr(postponed_nodes)) */
for(p2j.k = 0; p2j.k < n; p2j.k++){
/* ------------------------------------------------------------
Step j: remains to factor diag(d(j:end)) + p(j:end)*p(j:end)'/t.
The pivot is d(j) + p(j)^2/t = (t*d(j)+x(j))/t.
------------------------------------------------------------ */
dj = d[p2j.k];
p2j.r = fi[p2j.k]; /* p2j = {j, p_j^2} */
fij = p2j.r + t*dj; /* fi(j) = p_j^2 + t*d_j */
/* ------------------------------------------------------------
max SQR of below-diag = [pj^2 * max(p(j+1:end).^2)] / t^2
This should not exceed maxu^2 * pivot^2.
------------------------------------------------------------ */
if(p2j.r * MAX(muph2, mu[p2j.k]) <= SQR(maxu * fij)){
fi[p2j.k] = fij; /* pivot j is stable */
d[p2j.k] = fij / t; /* d(j;NEW) = d_j + (p_j^2 / t). */
t = fij / dj; /* Compute new t for next iter. */
}
else{
ph2psqr[nph2++] = p2j; /* Postpone to phase 2 */
muph2 = MAX(muph2, p2j.r); /* max(ph2psqr.r) */
}
}
*pt = t;
return nph2;
}
/* ************************************************************
dpr1factperm - Compact Cholesky for X = diag(d) + p*p' to
X = L(p,beta) * diag(d_OUT) * L(p,beta)'
where L(p,beta) = eye(m) + tril(p*beta',-1).
Follows the sequence given in "perm"; realligns accepted pivots
from start of "perm", stores rejected ones in ph2psqr.
INPUT:
n - Order of beta. n = min(m,idep), where idep is the
1st entry where d(idep) = 0 on input. Caller then needs to finish by
pivoting on idep by itself.
t - Initial t: set t = 1 for D+p*p', set t = -1 for D-p*p'.
maxu - Controls stability check: we postpone rows such that
max(abs(L)) <= maxu.
mu - max(psqr(perm[i+1:m-1])) for all i=1:n (n <= m). NB: in perm-order.
UPDATED:
perm - pivot sequence. Evaluate pivots perm(0:n-1). On output,
perm(0:n-nph2-1) are the accepted pivots.
d - Length n vector: the diagonal entries. On input, the old ones,
d(1:n) > 0. On output the updated ones after the factorization.
Remain positive if t > 0.
fi - on input, contains the vector x (=p.^2),
on output s.t. beta(j) = p(j) / fi(j) for j=perm[0:n-nph2-1].
OUTPUT
ph2psqr - The postponed rows j, with corresponding psqr(j). Controled
by maxu.
REMARK:
Since L=eye(m)+tril(p*beta'), beta(n-1) and fi(n-1) are useful only
if m > n: it'll be used in rows n:m-1.
RETURNS: nph2, number of postponed nodes = length(ph2psqr).
************************************************************ */
int dpr1factperm(double *fi, double *d, keydouble *ph2psqr, double *pt,
int *perm, const int n, const double *mu, const double maxu)
{
int i, jnz, nph2;
double dj,fij, muph2, t;
keydouble p2j;
/* ------------------------------------------------------------
fi(j) = x(j) + t*d(j), d_new(j) = fi(j)/t, tnew = fi(j)/d_old(j)
Store j in p2j.k
------------------------------------------------------------ */
t = *pt;
nph2 = 0;
muph2 = 0.0;
jnz = 0; /* index into perm_OUT, for accepted pivots */
for(i = 0; i < n; i++){
p2j.k = perm[i];
dj = d[p2j.k];
p2j.r = fi[p2j.k]; /* p2j = {j, p_j^2} */
fij = p2j.r + t*dj; /* fi(j) = p_j^2 + t*d_j */
if(p2j.r * MAX(muph2, mu[i]) <= SQR(maxu * fij)){
fi[p2j.k] = fij; /* pivot j is stable */
perm[jnz++] = p2j.k;
d[p2j.k] = fij / t; /* d(j;NEW) = d_j + (p_j^2 / t). */
t = fij / dj; /* Compute new t for next iter. */
}
else{
ph2psqr[nph2++] = p2j; /* Postpone to phase 2 */
muph2 = MAX(muph2, p2j.r); /* max(ph2psqr.r) */
}
}
mxAssert(jnz + nph2 == n, "");
*pt = t;
return nph2;
}
/* ************************************************************
ph2dpr1fact - Compact Cholesky for X = diag(d) + p*p' to
X = L(p,beta) * diag(d_OUT) * L(p,beta)'
where L(p,beta) = eye(m) + tril(p*beta',-1)
INPUT:
n - Order of psqr (number of phase-2 rows).
t - Initial t: output from 1st phase; is mon. incr.
t >= 1 for D+p*p', whereas -1 <= t < 0 for D-p*p'.
UPDATED:
psqr - Contains the sparse vector (p.^2), where the row-indices
are the postponed row numbers. On output, the r-values are
replaced by fi (so that beta = p ./ fi).
d - the diagonal entries. On input, the old ones,
on output the updated ones after the factorization.
Only those with psqr.i-indices are changed (should be
all positive already on input).
REMARK:
Since L=eye(m)+tril(p*beta'), beta(n-1) and fi(n-1) are useful only
if m > n: it'll be used in rows n:m-1.
************************************************************ */
void ph2dpr1fact(keydouble *psqr, double *d, double *pt, const int n)
{
int j, jnz;
double dj,fij,t;
t = *pt;
/* ------------------------------------------------------------
fi(j) = x(j) + t*d(j), d_new(j) = fi(j)/t, tnew = fi(j)/d_old(j)
------------------------------------------------------------ */
for(jnz = 0; jnz < n; jnz++){
j = (psqr+jnz)->k;
dj = d[j];
fij = ((psqr+jnz)->r += t*dj); /* fi(j) = p_j^2 + t*d_j */
d[j] = fij / t; /* d(j;NEW) = d_j + (p_j^2 / t). */
t = fij / dj; /* Compute new t for next iter. */
}
*pt = t;
}
/* ============================================================
MAIN routine for Compact Cholesky for X = diag(d) + p*p'.
redirects to the dpr1fact subroutines.
============================================================ */
/* ************************************************************
PROCEDURE dodpr1fact - Factors diag +/- rank-1:
(D+t*p*p')(perm) = L * diag(d_NEW(perm)) * L',
L = I+tril(p(perm)*beta',-1).
INPUT
p - length m. We've to factor diag(d)+ (1/t) * p*p'.
t - scalar: 1 for adding p*p', -1 for subtracting p*p'.
maxu - scalar >= 1: The factor L(p,beta) = I+tril(p(perm)*beta',-1)
will be such that max(abs(L)) <= maxu by choosing perm-ordering.
m - length(p).
UPDATED
d - length m. The diagonal. This factors
diag(d_OLD)+t*p*p' = L(p,beta) * diag(d_NEW) * L(p,beta)'
OUTPUT
beta - Length <= m (actual length returned in *pm).
perm - Length m. Only written if RETURN=1, which means that the
original ordering was not maxu-stable. Pivot ordering on p,d.
pn - *pn = length(beta) <= m; n<m only if there are dependent rows.
dep - Length ndep+1. Lists rows i where d(i) == 0. Indices are
ascending, and dep[ndep] >= m is tail of this list. On output,
one entry may be removed, and stored in dep[ndep_OLD].
*pndep - Cardinality of dep. May be decremented on output, if a
dependency could be removed, i.e. if t > 0 and p(dep) != 0.
WORK
psqr - length m float working array, for p.^2 and later "fi".
kdwork - length m working array for storing postponed
rows (rowno and psqr(rowno)), which have to be sorted.
RETURNS 1 if reordered rows into perm; 0 means that we used
the sequential 0:m-1 ordering.
CAUTION: If t < 0, one dependency may be added by the
rank-1 subtraction. The caller should therefore call findnewdep
afterwards (for t < 0).
************************************************************ */
char dodpr1fact(double *beta, int *perm, double *d, double t, const double *p,
const int m, int *pn, int *dep, int *pndep,
const double maxu, double *psqr, keydouble *kdwork)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -