本文整理汇总了C++中DiagonalMatrix::AsColumn方法的典型用法代码示例。如果您正苦于以下问题:C++ DiagonalMatrix::AsColumn方法的具体用法?C++ DiagonalMatrix::AsColumn怎么用?C++ DiagonalMatrix::AsColumn使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DiagonalMatrix
的用法示例。
在下文中一共展示了DiagonalMatrix::AsColumn方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test1
void test1(Real* y, Real* x1, Real* x2, int nobs, int npred)
{
cout << "\n\nTest 1 - traditional, bad\n";
// traditional sum of squares and products method of calculation
// but not adjusting means; maybe subject to round-off error
// make matrix of predictor values with 1s into col 1 of matrix
int npred1 = npred+1; // number of cols including col of ones.
Matrix X(nobs,npred1);
X.Column(1) = 1.0;
// load x1 and x2 into X
// [use << rather than = when loading arrays]
X.Column(2) << x1; X.Column(3) << x2;
// vector of Y values
ColumnVector Y(nobs); Y << y;
// form sum of squares and product matrix
// [use << rather than = for copying Matrix into SymmetricMatrix]
SymmetricMatrix SSQ; SSQ << X.t() * X;
// calculate estimate
// [bracket last two terms to force this multiplication first]
// [ .i() means inverse, but inverse is not explicity calculated]
ColumnVector A = SSQ.i() * (X.t() * Y);
// Get variances of estimates from diagonal elements of inverse of SSQ
// get inverse of SSQ - we need it for finding D
DiagonalMatrix D; D << SSQ.i();
ColumnVector V = D.AsColumn();
// Calculate fitted values and residuals
ColumnVector Fitted = X * A;
ColumnVector Residual = Y - Fitted;
Real ResVar = Residual.SumSquare() / (nobs-npred1);
// Get diagonals of Hat matrix (an expensive way of doing this)
DiagonalMatrix Hat; Hat << X * (X.t() * X).i() * X.t();
// print out answers
cout << "\nEstimates and their standard errors\n\n";
// make vector of standard errors
ColumnVector SE(npred1);
for (int i=1; i<=npred1; i++) SE(i) = sqrt(V(i)*ResVar);
// use concatenation function to form matrix and use matrix print
// to get two columns
cout << setw(11) << setprecision(5) << (A | SE) << endl;
cout << "\nObservations, fitted value, residual value, hat value\n";
// use concatenation again; select only columns 2 to 3 of X
cout << setw(9) << setprecision(3) <<
(X.Columns(2,3) | Y | Fitted | Residual | Hat.AsColumn());
cout << "\n\n";
}
示例2: test3
void test3(Real* y, Real* x1, Real* x2, int nobs, int npred)
{
cout << "\n\nTest 3 - Cholesky\n";
// traditional sum of squares and products method of calculation
// with subtraction of means - using Cholesky decomposition
Matrix X(nobs,npred);
X.Column(1) << x1; X.Column(2) << x2;
ColumnVector Y(nobs); Y << y;
ColumnVector Ones(nobs); Ones = 1.0;
RowVector M = Ones.t() * X / nobs;
Matrix XC(nobs,npred);
XC = X - Ones * M;
ColumnVector YC(nobs);
Real m = Sum(Y) / nobs; YC = Y - Ones * m;
SymmetricMatrix SSQ; SSQ << XC.t() * XC;
// Cholesky decomposition of SSQ
LowerTriangularMatrix L = Cholesky(SSQ);
// calculate estimate
ColumnVector A = L.t().i() * (L.i() * (XC.t() * YC));
// calculate estimate of constant term
Real a = m - (M * A).AsScalar();
// Get variances of estimates from diagonal elements of invoice of SSQ
DiagonalMatrix D; D << L.t().i() * L.i();
ColumnVector V = D.AsColumn();
Real v = 1.0/nobs + (L.i() * M.t()).SumSquare();
// Calculate fitted values and residuals
int npred1 = npred+1;
ColumnVector Fitted = X * A + a;
ColumnVector Residual = Y - Fitted;
Real ResVar = Residual.SumSquare() / (nobs-npred1);
// Get diagonals of Hat matrix (an expensive way of doing this)
Matrix X1(nobs,npred1); X1.Column(1)<<Ones; X1.Columns(2,npred1)<<X;
DiagonalMatrix Hat; Hat << X1 * (X1.t() * X1).i() * X1.t();
// print out answers
cout << "\nEstimates and their standard errors\n\n";
cout.setf(ios::fixed, ios::floatfield);
cout << setw(11) << setprecision(5) << a << " ";
cout << setw(11) << setprecision(5) << sqrt(v*ResVar) << endl;
ColumnVector SE(npred);
for (int i=1; i<=npred; i++) SE(i) = sqrt(V(i)*ResVar);
cout << setw(11) << setprecision(5) << (A | SE) << endl;
cout << "\nObservations, fitted value, residual value, hat value\n";
cout << setw(9) << setprecision(3) <<
(X | Y | Fitted | Residual | Hat.AsColumn());
cout << "\n\n";
}
示例3: main
int main()
{
{
// Get the data
ColumnVector X(6);
ColumnVector Y(6);
X << 1 << 2 << 3 << 4 << 6 << 8;
Y << 3.2 << 7.9 << 11.1 << 14.5 << 16.7 << 18.3;
// Do the fit
Model_3pe model(X); // the model object
NonLinearLeastSquares NLLS(model); // the non-linear least squares
// object
ColumnVector Para(3); // for the parameters
Para << 9 << -6 << .5; // trial values of parameters
cout << "Fitting parameters\n";
NLLS.Fit(Y,Para); // do the fit
// Inspect the results
ColumnVector SE; // for the standard errors
NLLS.GetStandardErrors(SE);
cout << "\n\nEstimates and standard errors\n" <<
setw(10) << setprecision(2) << (Para | SE) << endl;
Real ResidualSD = sqrt(NLLS.ResidualVariance());
cout << "\nResidual s.d. = " << setw(10) << setprecision(2) <<
ResidualSD << endl;
SymmetricMatrix Correlations;
NLLS.GetCorrelations(Correlations);
cout << "\nCorrelationMatrix\n" <<
setw(10) << setprecision(2) << Correlations << endl;
ColumnVector Residuals;
NLLS.GetResiduals(Residuals);
DiagonalMatrix Hat;
NLLS.GetHatDiagonal(Hat);
cout << "\nX, Y, Residual, Hat\n" << setw(10) << setprecision(2) <<
(X | Y | Residuals | Hat.AsColumn()) << endl;
// recover var/cov matrix
SymmetricMatrix D;
D << SE.AsDiagonal() * Correlations * SE.AsDiagonal();
cout << "\nVar/cov\n" << setw(14) << setprecision(4) << D << endl;
}
#ifdef DO_FREE_CHECK
FreeCheck::Status();
#endif
return 0;
}
示例4: test4
void test4(Real* y, Real* x1, Real* x2, int nobs, int npred)
{
cout << "\n\nTest 4 - QR triangularisation\n";
// QR triangularisation method
// load data - 1s into col 1 of matrix
int npred1 = npred+1;
Matrix X(nobs,npred1); ColumnVector Y(nobs);
X.Column(1) = 1.0; X.Column(2) << x1; X.Column(3) << x2; Y << y;
// do Householder triangularisation
// no need to deal with constant term separately
Matrix X1 = X; // Want copy of matrix
ColumnVector Y1 = Y;
UpperTriangularMatrix U; ColumnVector M;
QRZ(X1, U); QRZ(X1, Y1, M); // Y1 now contains resids
ColumnVector A = U.i() * M;
ColumnVector Fitted = X * A;
Real ResVar = Y1.SumSquare() / (nobs-npred1);
// get variances of estimates
U = U.i(); DiagonalMatrix D; D << U * U.t();
// Get diagonals of Hat matrix
DiagonalMatrix Hat; Hat << X1 * X1.t();
// print out answers
cout << "\nEstimates and their standard errors\n\n";
ColumnVector SE(npred1);
for (int i=1; i<=npred1; i++) SE(i) = sqrt(D(i)*ResVar);
cout << setw(11) << setprecision(5) << (A | SE) << endl;
cout << "\nObservations, fitted value, residual value, hat value\n";
cout << setw(9) << setprecision(3) <<
(X.Columns(2,3) | Y | Fitted | Y1 | Hat.AsColumn());
cout << "\n\n";
}
示例5: test5
void test5(Real* y, Real* x1, Real* x2, int nobs, int npred)
{
cout << "\n\nTest 5 - singular value\n";
// Singular value decomposition method
// load data - 1s into col 1 of matrix
int npred1 = npred+1;
Matrix X(nobs,npred1); ColumnVector Y(nobs);
X.Column(1) = 1.0; X.Column(2) << x1; X.Column(3) << x2; Y << y;
// do SVD
Matrix U, V; DiagonalMatrix D;
SVD(X,D,U,V); // X = U * D * V.t()
ColumnVector Fitted = U.t() * Y;
ColumnVector A = V * ( D.i() * Fitted );
Fitted = U * Fitted;
ColumnVector Residual = Y - Fitted;
Real ResVar = Residual.SumSquare() / (nobs-npred1);
// get variances of estimates
D << V * (D * D).i() * V.t();
// Get diagonals of Hat matrix
DiagonalMatrix Hat; Hat << U * U.t();
// print out answers
cout << "\nEstimates and their standard errors\n\n";
ColumnVector SE(npred1);
for (int i=1; i<=npred1; i++) SE(i) = sqrt(D(i)*ResVar);
cout << setw(11) << setprecision(5) << (A | SE) << endl;
cout << "\nObservations, fitted value, residual value, hat value\n";
cout << setw(9) << setprecision(3) <<
(X.Columns(2,3) | Y | Fitted | Residual | Hat.AsColumn());
cout << "\n\n";
}
示例6: my_main
int my_main() // called by main()
{
Tracer tr("my_main "); // for tracking exceptions
int n = 7; // this is the order we will work with
int i, j;
// declare a matrix
SymmetricMatrix H(n);
// load values for Hilbert matrix
for (i = 1; i <= n; ++i) for (j = 1; j <= i; ++j)
H(i, j) = 1.0 / (i + j - 1);
// print the matrix
cout << "SymmetricMatrix H" << endl;
cout << setw(10) << setprecision(7) << H << endl;
// calculate its eigenvalues and eigenvectors and print them
Matrix U; DiagonalMatrix D;
EigenValues(H, D, U);
cout << "Eigenvalues of H" << endl;
cout << setw(17) << setprecision(14) << D.AsColumn() << endl;
cout << "Eigenvector matrix, U" << endl;
cout << setw(10) << setprecision(7) << U << endl;
// check orthogonality
cout << "U * U.t() (should be near identity)" << endl;
cout << setw(10) << setprecision(7) << (U * U.t()) << endl;
// check decomposition
cout << "U * D * U.t() (should be near H)" << endl;
cout << setw(10) << setprecision(7) << (U * D * U.t()) << endl;
return 0;
}
示例7: test2
void test2(Real* y, Real* x1, Real* x2, int nobs, int npred)
{
cout << "\n\nTest 2 - traditional, OK\n";
// traditional sum of squares and products method of calculation
// with subtraction of means - less subject to round-off error
// than test1
// make matrix of predictor values
Matrix X(nobs,npred);
// load x1 and x2 into X
// [use << rather than = when loading arrays]
X.Column(1) << x1; X.Column(2) << x2;
// vector of Y values
ColumnVector Y(nobs); Y << y;
// make vector of 1s
ColumnVector Ones(nobs); Ones = 1.0;
// calculate means (averages) of x1 and x2 [ .t() takes transpose]
RowVector M = Ones.t() * X / nobs;
// and subtract means from x1 and x1
Matrix XC(nobs,npred);
XC = X - Ones * M;
// do the same to Y [use Sum to get sum of elements]
ColumnVector YC(nobs);
Real m = Sum(Y) / nobs; YC = Y - Ones * m;
// form sum of squares and product matrix
// [use << rather than = for copying Matrix into SymmetricMatrix]
SymmetricMatrix SSQ; SSQ << XC.t() * XC;
// calculate estimate
// [bracket last two terms to force this multiplication first]
// [ .i() means inverse, but inverse is not explicity calculated]
ColumnVector A = SSQ.i() * (XC.t() * YC);
// calculate estimate of constant term
// [AsScalar converts 1x1 matrix to Real]
Real a = m - (M * A).AsScalar();
// Get variances of estimates from diagonal elements of inverse of SSQ
// [ we are taking inverse of SSQ - we need it for finding D ]
Matrix ISSQ = SSQ.i(); DiagonalMatrix D; D << ISSQ;
ColumnVector V = D.AsColumn();
Real v = 1.0/nobs + (M * ISSQ * M.t()).AsScalar();
// for calc variance of const
// Calculate fitted values and residuals
int npred1 = npred+1;
ColumnVector Fitted = X * A + a;
ColumnVector Residual = Y - Fitted;
Real ResVar = Residual.SumSquare() / (nobs-npred1);
// Get diagonals of Hat matrix (an expensive way of doing this)
Matrix X1(nobs,npred1); X1.Column(1)<<Ones; X1.Columns(2,npred1)<<X;
DiagonalMatrix Hat; Hat << X1 * (X1.t() * X1).i() * X1.t();
// print out answers
cout << "\nEstimates and their standard errors\n\n";
cout.setf(ios::fixed, ios::floatfield);
cout << setw(11) << setprecision(5) << a << " ";
cout << setw(11) << setprecision(5) << sqrt(v*ResVar) << endl;
// make vector of standard errors
ColumnVector SE(npred);
for (int i=1; i<=npred; i++) SE(i) = sqrt(V(i)*ResVar);
// use concatenation function to form matrix and use matrix print
// to get two columns
cout << setw(11) << setprecision(5) << (A | SE) << endl;
cout << "\nObservations, fitted value, residual value, hat value\n";
cout << setw(9) << setprecision(3) <<
(X | Y | Fitted | Residual | Hat.AsColumn());
cout << "\n\n";
}
示例8: trymatc
void trymatc()
{
// cout << "\nTwelfth test of Matrix package\n";
Tracer et("Twelfth test of Matrix package");
Tracer::PrintTrace();
DiagonalMatrix D(15); D=1.5;
Matrix A(15,15);
int i,j;
for (i=1;i<=15;i++) for (j=1;j<=15;j++) A(i,j)=i*i+j-150;
{ A = A + D; }
ColumnVector B(15);
for (i=1;i<=15;i++) B(i)=i+i*i-150.0;
{
Tracer et1("Stage 1");
ColumnVector B1=B;
B=(A*2.0).i() * B1;
Matrix X = A*B-B1/2.0;
Clean(X, 0.000000001); Print(X);
A.ReSize(3,5);
for (i=1; i<=3; i++) for (j=1; j<=5; j++) A(i,j) = i+100*j;
B = A.AsColumn()+10000;
RowVector R = (A+10000).AsColumn().t();
Print( RowVector(R-B.t()) );
}
{
Tracer et1("Stage 2");
B = A.AsColumn()+10000;
Matrix XR = (A+10000).AsMatrix(15,1).t();
Print( RowVector(XR-B.t()) );
}
{
Tracer et1("Stage 3");
B = (A.AsMatrix(15,1)+A.AsColumn())/2.0+10000;
Matrix MR = (A+10000).AsColumn().t();
Print( RowVector(MR-B.t()) );
B = (A.AsMatrix(15,1)+A.AsColumn())/2.0;
MR = A.AsColumn().t();
Print( RowVector(MR-B.t()) );
}
{
Tracer et1("Stage 4");
B = (A.AsMatrix(15,1)+A.AsColumn())/2.0;
RowVector R = A.AsColumn().t();
Print( RowVector(R-B.t()) );
}
{
Tracer et1("Stage 5");
RowVector R = (A.AsColumn()-5000).t();
B = ((R.t()+10000) - A.AsColumn())-5000;
Print( RowVector(B.t()) );
}
{
Tracer et1("Stage 6");
B = A.AsColumn(); ColumnVector B1 = (A+10000).AsColumn() - 10000;
Print(ColumnVector(B1-B));
}
{
Tracer et1("Stage 7");
Matrix X = B.AsMatrix(3,5); Print(Matrix(X-A));
for (i=1; i<=3; i++) for (j=1; j<=5; j++) B(5*(i-1)+j) -= i+100*j;
Print(B);
}
{
Tracer et1("Stage 8");
A.ReSize(7,7); D.ReSize(7);
for (i=1; i<=7; i++) for (j=1; j<=7; j++) A(i,j) = i*j*j;
for (i=1; i<=7; i++) D(i,i) = i;
UpperTriangularMatrix U; U << A;
Matrix X = A; for (i=1; i<=7; i++) X(i,i) = i;
A.Inject(D); Print(Matrix(X-A));
X = U; U.Inject(D); A = U; for (i=1; i<=7; i++) X(i,i) = i;
Print(Matrix(X-A));
}
{
Tracer et1("Stage 9");
A.ReSize(7,5);
for (i=1; i<=7; i++) for (j=1; j<=5; j++) A(i,j) = i+100*j;
Matrix Y = A; Y = Y - ((const Matrix&)A); Print(Y);
Matrix X = A;
Y = A; Y = ((const Matrix&)X) - A; Print(Y); Y = 0.0;
Y = ((const Matrix&)X) - ((const Matrix&)A); Print(Y);
}
{
Tracer et1("Stage 10");
// some tests on submatrices
UpperTriangularMatrix U(20);
for (i=1; i<=20; i++) for (j=i; j<=20; j++) U(i,j)=100 * i + j;
UpperTriangularMatrix V = U.SymSubMatrix(1,5);
UpperTriangularMatrix U1 = U;
//.........这里部分代码省略.........