📄 optim_private_trust.c
字号:
*/
mlfIndexAssign(&alpha, "(?)", arg, mlfScalar(0.0));
/*
* end
*/
}
/*
* w = eigval + lam;
*/
mlfAssign(&w, mlfPlus(eigval, lam));
/*
* arg1 = (w==0) & (alpha == 0); arg2 = (w==0) & (alpha ~= 0);
*/
mlfAssign(
&arg1,
mlfAnd(mlfEq(w, mlfScalar(0.0)), mlfEq(alpha, mlfScalar(0.0))));
mlfAssign(
&arg2,
mlfAnd(mlfEq(w, mlfScalar(0.0)), mlfNe(alpha, mlfScalar(0.0))));
/*
* coeff(w~=0) = alpha(w~=0) ./ w(w~=0);
*/
mlfIndexAssign(
&coeff,
"(?)",
mlfNe(w, mlfScalar(0.0)),
mlfRdivide(
mlfIndexRef(alpha, "(?)", mlfNe(w, mlfScalar(0.0))),
mlfIndexRef(w, "(?)", mlfNe(w, mlfScalar(0.0)))));
/*
* coeff(arg1) = zeros(length(arg1(arg1>0)),1);
*/
mlfIndexAssign(
&coeff,
"(?)",
arg1,
mlfZeros(
mlfLength(mlfIndexRef(arg1, "(?)", mlfGt(arg1, mlfScalar(0.0)))),
mlfScalar(1.0),
NULL));
/*
* coeff(arg2) = Inf *ones(length(arg2(arg2>0)),1);
*/
mlfIndexAssign(
&coeff,
"(?)",
arg2,
mlfMtimes(
mlfInf(),
mlfOnes(
mlfLength(mlfIndexRef(arg2, "(?)", mlfGt(arg2, mlfScalar(0.0)))),
mlfScalar(1.0),
NULL)));
/*
* coeff(coeff==NaN)=zeros(length(coeff(coeff==NaN)),1);
*/
mlfIndexAssign(
&coeff,
"(?)",
mlfEq(coeff, mlfNan()),
mlfZeros(
mlfLength(mlfIndexRef(coeff, "(?)", mlfEq(coeff, mlfNan()))),
mlfScalar(1.0),
NULL));
/*
* coeff(coeff==NaN)=zeros(length(coeff(coeff==NaN)),1);
*/
mlfIndexAssign(
&coeff,
"(?)",
mlfEq(coeff, mlfNan()),
mlfZeros(
mlfLength(mlfIndexRef(coeff, "(?)", mlfEq(coeff, mlfNan()))),
mlfScalar(1.0),
NULL));
/*
* s = V*coeff; nrms = norm(s);
*/
mlfAssign(&s, mlfMtimes(V, coeff));
mlfAssign(&nrms, mlfNorm(s, NULL));
/*
* if (key > 2) & (nrms < .8*delta)
*/
{
mxArray * a_ = mclInitialize(mlfGt(key, mlfScalar(2.0)));
if (mlfTobool(a_)
&& mlfTobool(
mlfAnd(
a_, mlfLt(nrms, mlfMtimes(mlfScalar(.8), delta))))) {
mxDestroyArray(a_);
/*
* beta = sqrt(delta^2 - nrms^2);
*/
mlfAssign(
&beta,
mlfSqrt(
mlfMinus(
mlfMpower(delta, mlfScalar(2.0)),
mlfMpower(nrms, mlfScalar(2.0)))));
/*
* s = s + beta*sig*V(:,jmin);
*/
mlfAssign(
&s,
mlfPlus(
s,
mlfMtimes(
mlfMtimes(beta, sig),
mlfIndexRef(V, "(?,?)", mlfCreateColonIndex(), jmin))));
} else {
mxDestroyArray(a_);
}
/*
* end
*/
}
/*
* if (key > 2) & (nrms > 1.2*delta)
*/
{
mxArray * a_ = mclInitialize(mlfGt(key, mlfScalar(2.0)));
if (mlfTobool(a_)
&& mlfTobool(
mlfAnd(
a_, mlfGt(nrms, mlfMtimes(mlfScalar(1.2), delta))))) {
mxDestroyArray(a_);
/*
* [b,c,count] = rfzero('seceqn',laminit,itbnd,eigval,alpha,delta,tol);
*/
mlfAssign(
&b,
mlfTrust_rfzero(
&c,
count,
mxCreateString("seceqn"),
laminit,
itbnd,
eigval,
alpha,
delta,
tol,
NULL));
/*
* lambda = b; lam = lambda*(ones(n,1));
*/
mlfAssign(lambda, b);
mlfAssign(
&lam, mlfMtimes(*lambda, mlfOnes(n, mlfScalar(1.0), NULL)));
/*
* w = eigval + lam;
*/
mlfAssign(&w, mlfPlus(eigval, lam));
/*
* arg1 = (w==0) & (alpha == 0); arg2 = (w==0) & (alpha ~= 0);
*/
mlfAssign(
&arg1,
mlfAnd(
mlfEq(w, mlfScalar(0.0)), mlfEq(alpha, mlfScalar(0.0))));
mlfAssign(
&arg2,
mlfAnd(
mlfEq(w, mlfScalar(0.0)), mlfNe(alpha, mlfScalar(0.0))));
/*
* coeff(w~=0) = alpha(w~=0) ./ w(w~=0);
*/
mlfIndexAssign(
&coeff,
"(?)",
mlfNe(w, mlfScalar(0.0)),
mlfRdivide(
mlfIndexRef(alpha, "(?)", mlfNe(w, mlfScalar(0.0))),
mlfIndexRef(w, "(?)", mlfNe(w, mlfScalar(0.0)))));
/*
* coeff(arg1) = zeros(length(arg1(arg1>0)),1);
*/
mlfIndexAssign(
&coeff,
"(?)",
arg1,
mlfZeros(
mlfLength(
mlfIndexRef(arg1, "(?)", mlfGt(arg1, mlfScalar(0.0)))),
mlfScalar(1.0),
NULL));
/*
* coeff(arg2) = Inf *ones(length(arg2(arg2>0)),1);
*/
mlfIndexAssign(
&coeff,
"(?)",
arg2,
mlfMtimes(
mlfInf(),
mlfOnes(
mlfLength(
mlfIndexRef(arg2, "(?)", mlfGt(arg2, mlfScalar(0.0)))),
mlfScalar(1.0),
NULL)));
/*
* coeff(coeff==NaN)=zeros(length(coeff(coeff==NaN)),1);
*/
mlfIndexAssign(
&coeff,
"(?)",
mlfEq(coeff, mlfNan()),
mlfZeros(
mlfLength(
mlfIndexRef(coeff, "(?)", mlfEq(coeff, mlfNan()))),
mlfScalar(1.0),
NULL));
/*
* s = V*coeff; nrms = norm(s);
*/
mlfAssign(&s, mlfMtimes(V, coeff));
mlfAssign(&nrms, mlfNorm(s, NULL));
} else {
mxDestroyArray(a_);
}
/*
* end
*/
}
/*
* end
*/
}
/*
* val = g'*s + (.5*s)'*(H*s);
*/
mlfAssign(
val,
mlfPlus(
mlfMtimes(mlfCtranspose(g), s),
mlfMtimes(
mlfCtranspose(mlfMtimes(mlfScalar(.5), s)), mlfMtimes(H, s))));
mclValidateOutputs(
"optim/private/trust", 5, nargout_, &s, val, posdef, count, lambda);
mxDestroyArray(D);
mxDestroyArray(H);
mxDestroyArray(V);
mxDestroyArray(alpha);
mxDestroyArray(arg);
mxDestroyArray(arg1);
mxDestroyArray(arg2);
mxDestroyArray(b);
mxDestroyArray(beta);
mxDestroyArray(c);
mxDestroyArray(coeff);
mxDestroyArray(eigval);
mxDestroyArray(itbnd);
mxDestroyArray(jmin);
mxDestroyArray(key);
mxDestroyArray(lam);
mxDestroyArray(laminit);
mxDestroyArray(mineig);
mxDestroyArray(n);
mxDestroyArray(nrms);
mxDestroyArray(sig);
mxDestroyArray(tol);
mxDestroyArray(tol2);
mxDestroyArray(vval);
mxDestroyArray(w);
/*
*
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* function[value] = seceqn(lambda,eigval,alpha,delta);
* %SEC Secular equation
* %
* % value = SEC(lambda,eigval,alpha,delta) returns the value
* % of the secular equation at a set of m points lambda
* %
* %
*
*
* %
* m = length(lambda); n = length(eigval);
* unn = ones(n,1); unm = ones(m,1);
* M = eigval*unm' + unn*lambda'; MC = M;
* MM = alpha*unm';
* M(M~=0) = MM(M~=0) ./ M(M~=0);
* M(MC==0) = Inf*ones(size(MC(MC==0)));
* M = M.*M;
* value = sqrt(unm ./ (M'*unn));
* value(value==NaN) = zeros(length(value(value==NaN)),1);
* value = (1/delta)*unm - value;
*
*
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*/
return s;
}
/*
* The function "mlfOptim_private_trust" contains the normal interface for the
* "optim/private/trust" M-function from file
* "C:\MATLABR11\toolbox\optim\private\trust.m" (lines 1-114). This function
* processes any input arguments and passes them to the implementation version
* of the function, appearing above.
*/
mxArray * mlfOptim_private_trust(mxArray * * val,
mxArray * * posdef,
mxArray * * count,
mxArray * * lambda,
mxArray * g,
mxArray * H,
mxArray * delta) {
int nargout = 1;
mxArray * s = mclGetUninitializedArray();
mxArray * val__ = mclGetUninitializedArray();
mxArray * posdef__ = mclGetUninitializedArray();
mxArray * count__ = mclGetUninitializedArray();
mxArray * lambda__ = mclGetUninitializedArray();
mlfEnterNewContext(4, 3, val, posdef, count, lambda, g, H, delta);
if (val != NULL) {
++nargout;
}
if (posdef != NULL) {
++nargout;
}
if (count != NULL) {
++nargout;
}
if (lambda != NULL) {
++nargout;
}
s
= Moptim_private_trust(
&val__, &posdef__, &count__, &lambda__, nargout, g, H, delta);
mlfRestorePreviousContext(4, 3, val, posdef, count, lambda, g, H, delta);
if (val != NULL) {
mclCopyOutputArg(val, val__);
} else {
mxDestroyArray(val__);
}
if (posdef != NULL) {
mclCopyOutputArg(posdef, posdef__);
} else {
mxDestroyArray(posdef__);
}
if (count != NULL) {
mclCopyOutputArg(count, count__);
} else {
mxDestroyArray(count__);
}
if (lambda != NULL) {
mclCopyOutputArg(lambda, lambda__);
} else {
mxDestroyArray(lambda__);
}
return mlfReturnValue(s);
}
/*
* The function "mlxOptim_private_trust" contains the feval interface for the
* "optim/private/trust" M-function from file
* "C:\MATLABR11\toolbox\optim\private\trust.m" (lines 1-114). The feval
* function calls the implementation version of optim/private/trust through
* this function. This function processes any input arguments and passes them
* to the implementation version of the function, appearing above.
*/
void mlxOptim_private_trust(int nlhs,
mxArray * plhs[],
int nrhs,
mxArray * prhs[]) {
mxArray * mprhs[3];
mxArray * mplhs[5];
int i;
if (nlhs > 5) {
mlfError(
mxCreateString(
"Run-time Error: File: optim/private/trust Line: 1 Colu"
"mn: 0 The function \"optim/private/trust\" was called "
"with more than the declared number of outputs (5)"));
}
if (nrhs > 3) {
mlfError(
mxCreateString(
"Run-time Error: File: optim/private/trust Line: 1 Col"
"umn: 0 The function \"optim/private/trust\" was calle"
"d with more than the declared number of inputs (3)"));
}
for (i = 0; i < 5; ++i) {
mplhs[i] = NULL;
}
for (i = 0; i < 3 && i < nrhs; ++i) {
mprhs[i] = prhs[i];
}
for (; i < 3; ++i) {
mprhs[i] = NULL;
}
mlfEnterNewContext(0, 3, mprhs[0], mprhs[1], mprhs[2]);
mplhs[0]
= Moptim_private_trust(
&mplhs[1],
&mplhs[2],
&mplhs[3],
&mplhs[4],
nlhs,
mprhs[0],
mprhs[1],
mprhs[2]);
mlfRestorePreviousContext(0, 3, mprhs[0], mprhs[1], mprhs[2]);
plhs[0] = mplhs[0];
for (i = 1; i < 5 && i < nlhs; ++i) {
plhs[i] = mplhs[i];
}
for (; i < 5; ++i) {
mxDestroyArray(mplhs[i]);
}
}
/*
* The function "Mtrust_seceqn" is the implementation version of the
* "trust/seceqn" M-function from file
* "C:\MATLABR11\toolbox\optim\private\trust.m" (lines 114-138). It contains
* the actual compiled code for that M-function. It is a static function and
* must only be called from one of the interface functions, appearing below.
*/
/*
* function[value] = seceqn(lambda,eigval,alpha,delta);
*/
static mxArray * Mtrust_seceqn(int nargout_,
mxArray * lambda,
mxArray * eigval,
mxArray * alpha,
mxArray * delta) {
mxArray * value = mclGetUninitializedArray();
mxArray * M = mclGetUninitializedArray();
mxArray * MC = mclGetUninitializedArray();
mxArray * MM = mclGetUninitializedArray();
mxArray * m = mclGetUninitializedArray();
mxArray * n = mclGetUninitializedArray();
mxArray * unm = mclGetUninitializedArray();
mxArray * unn = mclGetUninitializedArray();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -