本文整理汇总了C++中Matrix::AsColumn方法的典型用法代码示例。如果您正苦于以下问题:C++ Matrix::AsColumn方法的具体用法?C++ Matrix::AsColumn怎么用?C++ Matrix::AsColumn使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix
的用法示例。
在下文中一共展示了Matrix::AsColumn方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: trymat7
void trymat7()
{
// cout << "\nSeventh test of Matrix package\n";
Tracer et("Seventh test of Matrix package");
Tracer::PrintTrace();
int i,j;
DiagonalMatrix D(6);
UpperTriangularMatrix U(6);
for (i=1; i<=6; i++) {
for (j=i; j<=6; j++) U(i,j)=i*i*j-50;
D(i,i)=i*i+i-10;
}
LowerTriangularMatrix L=(U*3.0).t();
SymmetricMatrix S(6);
for (i=1; i<=6; i++) for (j=i; j<=6; j++) S(i,j)=i*i+2.0+j;
Matrix MD=D;
Matrix ML=L;
Matrix MU=U;
Matrix MS=S;
Matrix M(6,6);
for (i=1; i<=6; i++) for (j=1; j<=6; j++) M(i,j)=i*j+i*i-10.0;
{
Tracer et1("Stage 1");
Print(Matrix((S-M)-(MS-M)));
Print(Matrix((-M-S)+(MS+M)));
Print(Matrix((U-M)-(MU-M)));
}
{
Tracer et1("Stage 2");
Print(Matrix((L-M)+(M-ML)));
Print(Matrix((D-M)+(M-MD)));
Print(Matrix((D-S)+(MS-MD)));
Print(Matrix((D-L)+(ML-MD)));
}
{
M=MU.t();
}
LowerTriangularMatrix LY=D.i()*U.t();
{
Tracer et1("Stage 3");
MS=D*LY-M;
Clean(MS,0.00000001);
Print(MS);
L=U.t();
LY=D.i()*L;
MS=D*LY-M;
Clean(MS,0.00000001);
Print(MS);
}
{
Tracer et1("Stage 4");
UpperTriangularMatrix UT(11);
int i, j;
for (i=1; i<=11; i++) for (j=i; j<=11; j++) UT(i,j)=i*i+j*3;
GenericMatrix GM;
Matrix X;
UpperBandMatrix UB(11,3);
UB.Inject(UT);
UT = UB;
UpperBandMatrix UB2 = UB / 8;
GM = UB2-UT/8;
X = GM;
Print(X);
SymmetricBandMatrix SB(11,4);
SB << (UB + UB.t());
X = SB - UT - UT.t();
Print(X);
BandMatrix B = UB + UB.t()*2;
DiagonalMatrix D;
D << B;
X.ReSize(1,1);
X(1,1) = Trace(B)-Sum(D);
Print(X);
X = SB + 5;
Matrix X1=X;
X = SP(UB,X);
Matrix X2 =UB;
X1 = (X1.AsDiagonal() * X2.AsDiagonal()).AsRow()-X.AsColumn().t();
Print(X1);
X1=SB.t();
X2 = B.t();
X = SB.i() * B - X1.i() * X2.t();
Clean(X,0.00000001);
Print(X);
X = SB.i();
X = X * B - X1.i() * X2.t();
Clean(X,0.00000001);
Print(X);
D = 1;
X = SB.i() * SB - D;
Clean(X,0.00000001);
Print(X);
ColumnVector CV(11);
CV << 2 << 6 <<3 << 8 << -4 << 17.5 << 2 << 1 << -2 << 5 << 3.75;
D << 2 << 6 <<3 << 8 << -4 << 17.5 << 2 << 1 << -2 << 5 << 3.75;
X = CV.AsDiagonal();
//.........这里部分代码省略.........
示例2: mc_irf_var
//extern "C"
SEXP mc_irf_var(SEXP varobj, SEXP nsteps, SEXP draws)
{
int m, p, dr=INTEGER(draws)[0], ns=INTEGER(nsteps)[0], T, df, i;
SEXP AR, Y, Bhat, XR, prior, hstar, meanS, output;
// Get # vars/lags/steps/draws/T/df
PROTECT(AR = listElt(varobj, "ar.coefs"));
PROTECT(Y = listElt(varobj, "Y"));
m = INTEGER(getAttrib(AR, R_DimSymbol))[0]; //#vars
p = INTEGER(getAttrib(AR, R_DimSymbol))[2]; //#lags
T = nrows(Y); df = T - m*p - m - 1;
UNPROTECT(2);
// Put coefficients from varobj$Bhat in Bcoefs vector (m^2*p, 1)
PROTECT(Bhat = coerceVector(listElt(varobj, "Bhat"), REALSXP));
Matrix bcoefs = R2Cmat(Bhat, m*p, m);
bcoefs = bcoefs.AsColumn();
UNPROTECT(1);
// Define X(T x m*p) subset of varobj$X and XXinv as solve(X'X)
PROTECT(XR = coerceVector(listElt(varobj,"X"),REALSXP));
Matrix X = R2Cmat(XR, T, m*p), XXinv;
UNPROTECT(1);
// Get the correct moment matrix
PROTECT(prior = listElt(varobj,"prior"));
if(!isNull(prior)){
PROTECT(hstar = coerceVector(listElt(varobj,"hstar"),REALSXP));
XXinv = R2Cmat(hstar, m*p, m*p).i();
UNPROTECT(1);
}
else { XXinv = (X.t()*X).i(); }
UNPROTECT(1);
// Get the transpose of the Cholesky decomp of XXinv
SymmetricMatrix XXinvSym; XXinvSym << XXinv;
XXinv = Cholesky(XXinvSym);
// Cholesky of covariance
PROTECT(meanS = coerceVector(listElt(varobj,"mean.S"),REALSXP));
SymmetricMatrix meanSSym; meanSSym << R2Cmat(meanS, m, m);
Matrix Sigmat = Cholesky(meanSSym);
UNPROTECT(1);
// Matricies needed for the loop
ColumnVector bvec; bvec=0.0;
Matrix sqrtwish, impulse(dr,m*m*ns); impulse = 0.0;
SymmetricMatrix sigmadraw; sigmadraw = 0.0;
IdentityMatrix I(m);
GetRNGstate();
// Main Loop
for (i=1; i<=dr; i++){
// Wishart/Beta draws
sigmadraw << Sigmat*(T*rwish(I,df).i())*Sigmat.t();
sqrtwish = Cholesky(sigmadraw);
bvec = bcoefs+KP(sqrtwish, XXinv)*rnorms(m*m*p);
// IRF computation
impulse.Row(i) = irf_var_from_beta(sqrtwish, bvec, ns).t();
if (!(i%1000)){ Rprintf("Monte Carlo IRF Iteration = %d\n",i); }
} // end main loop
PutRNGstate();
int dims[]={dr,ns,m*m};
PROTECT(output = C2R3D(impulse,dims));
setclass(output,"mc.irf.VAR");
UNPROTECT(1);
return output;
}