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"; }
//-------------------------------------------- void NRLib::CholeskyInvert(SymmetricMatrix& A) //-------------------------------------------- { //NBNB legge til feilmld her, se metode ovenfor int infof = flens::potrf(A.upLo(), A.dim(), A.data(), A.leadingDimension()); int infoi = flens::potri(A.upLo(), A.dim(), A.data(), A.leadingDimension()); if(infof > 0 || infoi > 0) throw NRLib::Exception("Error in Cholesky inversion"); }
inline Matrix<C>::Matrix(const SymmetricMatrix<C>& M) : entries_(M.row_dimension()*M.column_dimension()), rowdim_(M.row_dimension()), coldim_(M.column_dimension()) { for (size_type i(0); i < rowdim_; i++) for (size_type j(0); j <= i; j++) { this->operator () (i, j) = this->operator () (j, i) = M(i, j); } }
void SpectClust::normalizeSum(SymmetricMatrix &A) { RowVector Ones(A.Ncols()); Ones = 1.0; // Calculate the sum of each row matrix style. Could this be a Diagonal Matrix instead? Matrix RowSum = Ones * (A.t()); DiagonalMatrix D(A.Ncols()); D = 0; // Take the inverse square root for(int i = 1; i <= A.Ncols(); i++) { D(i,i) = 1/sqrt(RowSum(1,i)); } Matrix X = D * (A * D); A << X; }
std::vector<int> fiedler_reorder(const SymmetricMatrix& m) { SymmetricMatrix absm=m; const int nrows=m.Nrows(); for (int i=0;i<nrows;++i) { for (int j=0;j<=i;++j){ //absolute value absm.element(i,j)=std::fabs(absm.element(i,j)); } } //laplacian SymmetricMatrix lap(nrows); lap=0.; for (int i=0;i<nrows;++i) lap.element(i,i)=absm.Row(i+1).Sum(); lap-=absm; DiagonalMatrix eigs; Matrix vecs; EigenValues(lap,eigs,vecs); ColumnVector fvec=vecs.Column(2); std::vector<double> fvec_stl(nrows); //copies over fvec to fvec_stl std::copy(&fvec.element(0),&fvec.element(0)+nrows,fvec_stl.begin()); std::vector<int> findices; //sorts the data by eigenvalue in ascending order sort_data_to_indices(fvec_stl,findices); return findices; /* BLOCK works with findices*/ }
void SpectClust::fillInDistance(SymmetricMatrix &A, const Matrix &M, const DistanceMetric &dMetric, bool expon) { A.ReSize(M.Ncols()); for(int i = 1; i <= M.Ncols(); i++) { for(int j = i; j <= M.Ncols(); j++) { if(expon) A(j,i) = A(i,j) = exp(-1 * dMetric.dist(M,i,j)); else A(j,i) = A(i,j) = dMetric.dist(M,i,j); } } }
static void tred3(const SymmetricMatrix& X, DiagonalMatrix& D, DiagonalMatrix& E, SymmetricMatrix& A) { Tracer et("Evalue(tred3)"); Real tol = FloatingPointPrecision::Minimum()/FloatingPointPrecision::Epsilon(); int n = X.Nrows(); A = X; D.ReSize(n); E.ReSize(n); Real* ei = E.Store() + n; for (int i = n-1; i >= 0; i--) { Real h = 0.0; Real f; Real* d = D.Store(); Real* a = A.Store() + (i*(i+1))/2; int k = i; while (k--) { f = *a++; *d++ = f; h += square(f); } if (h <= tol) { *(--ei) = 0.0; h = 0.0; } else { Real g = sign(-sqrt(h), f); *(--ei) = g; h -= f*g; f -= g; *(d-1) = f; *(a-1) = f; f = 0.0; Real* dj = D.Store(); Real* ej = E.Store(); int j; for (j = 0; j < i; j++) { Real* dk = D.Store(); Real* ak = A.Store()+(j*(j+1))/2; Real g = 0.0; k = j; while (k--) g += *ak++ * *dk++; k = i-j; int l = j; while (k--) { g += *ak * *dk++; ak += ++l; } g /= h; *ej++ = g; f += g * *dj++; } Real hh = f / (2 * h); Real* ak = A.Store(); dj = D.Store(); ej = E.Store(); for (j = 0; j < i; j++) { f = *dj++; g = *ej - hh * f; *ej++ = g; Real* dk = D.Store(); Real* ek = E.Store(); k = j+1; while (k--) { *ak++ -= (f * *ek++ + g * *dk++); } } } *d = *a; *a = h; } }
ReturnMatrix Cholesky(const SymmetricMatrix& S) { REPORT Tracer trace("Cholesky"); int nr = S.Nrows(); LowerTriangularMatrix T(nr); Real* s = S.Store(); Real* t = T.Store(); Real* ti = t; for (int i=0; i<nr; i++) { Real* tj = t; Real sum; int k; for (int j=0; j<i; j++) { Real* tk = ti; sum = 0.0; k = j; while (k--) { sum += *tj++ * *tk++; } *tk = (*s++ - sum) / *tj++; } sum = 0.0; k = i; while (k--) { sum += square(*ti++); } Real d = *s++ - sum; if (d<=0.0) Throw(NPDException(S)); *ti++ = sqrt(d); } T.release(); return T.for_return(); }
bool SpectClust::findNLargestSymEvals(const SymmetricMatrix &W, int numLamda, std::vector<Numeric> &eVals, Matrix &EVec) { bool converged = false; eVals.clear(); eVals.reserve(numLamda); DiagonalMatrix D(W.Ncols()); Matrix E; try { EigenValues(W, D, E); converged = true; EVec.ReSize(W.Ncols(), numLamda); int count = 0; for(int i = W.Ncols(); i > W.Ncols() - numLamda; i--) { eVals.push_back(D(i)); EVec.Column(++count) << E.Column(i); } } catch(const Exception &e) { Err::errAbort("Exception: " + ToStr(e.what())); } catch(...) { Err::errAbort("Yikes couldn't calculate eigen vectors."); } return converged; }
int AccpmLASymmLinSolve(SymmetricMatrix &A, RealMatrix &X, const RealMatrix &B) { LaLowerTriangMatDouble L(A); char uplo = 'L'; long int n = A.size(0); long int lda = A.inc(0) * A.gdim(0); long int nrhs = B.size(1); long int ldb = B.inc(0) * B.gdim(0); long int info; X.inject(B); F77NAME(dposv) (&uplo, &n, &nrhs, &L(0,0), &lda, &X(0,0), &ldb, &info); if (info > 0) { std::cerr << "AccpmLASymmLinSolve: Symmetric Matrix is not positive-definite." << std::endl; } else if (info < 0) { std::cerr << "AccpmLASymmLinSolve: argument " << -info << " is invalid" << std::endl; } return info; }
void Print(const SymmetricMatrix& X) { ++PCN; cout << "\nMatrix type: " << X.Type().Value() << " ("; cout << X.Nrows() << ", "; cout << X.Ncols() << ")\n\n"; if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; } int nr=X.Nrows(); int nc=X.Ncols(); for (int i=1; i<=nr; i++) { int j; for (j=1; j<i; j++) cout << X(j,i) << "\t"; for (j=i; j<=nc; j++) cout << X(i,j) << "\t"; cout << "\n"; } cout << flush; ++PCZ; }
void Jacobi(const SymmetricMatrix& X, DiagonalMatrix& D, SymmetricMatrix& A, Matrix& V, bool eivec) { Real epsilon = FloatingPointPrecision::Epsilon(); Tracer et("Jacobi"); REPORT int n = X.Nrows(); DiagonalMatrix B(n), Z(n); D.resize(n); A = X; if (eivec) { REPORT V.resize(n,n); D = 1.0; V = D; } B << A; D = B; Z = 0.0; A.Inject(Z); bool converged = false; for (int i=1; i<=50; i++) { Real sm=0.0; Real* a = A.Store(); int p = A.Storage(); while (p--) sm += fabs(*a++); // have previously zeroed diags if (sm==0.0) { REPORT converged = true; break; } Real tresh = (i<4) ? 0.2 * sm / square(n) : 0.0; a = A.Store(); for (p = 0; p < n; p++) { Real* ap1 = a + (p*(p+1))/2; Real& zp = Z.element(p); Real& dp = D.element(p); for (int q = p+1; q < n; q++) { Real* ap = ap1; Real* aq = a + (q*(q+1))/2; Real& zq = Z.element(q); Real& dq = D.element(q); Real& apq = A.element(q,p); Real g = 100 * fabs(apq); Real adp = fabs(dp); Real adq = fabs(dq); if (i>4 && g < epsilon*adp && g < epsilon*adq) { REPORT apq = 0.0; } else if (fabs(apq) > tresh) { REPORT Real t; Real h = dq - dp; Real ah = fabs(h); if (g < epsilon*ah) { REPORT t = apq / h; } else { REPORT Real theta = 0.5 * h / apq; t = 1.0 / ( fabs(theta) + sqrt(1.0 + square(theta)) ); if (theta<0.0) { REPORT t = -t; } } Real c = 1.0 / sqrt(1.0 + square(t)); Real s = t * c; Real tau = s / (1.0 + c); h = t * apq; zp -= h; zq += h; dp -= h; dq += h; apq = 0.0; int j = p; while (j--) { g = *ap; h = *aq; *ap++ = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau); } int ip = p+1; j = q-ip; ap += ip++; aq++; while (j--) { g = *ap; h = *aq; *ap = g-s*(h+g*tau); *aq++ = h+s*(g-h*tau); ap += ip++; } if (q < n-1) // last loop is non-empty { int iq = q+1; j = n-iq; ap += ip++; aq += iq++; for (;;) { g = *ap; h = *aq; *ap = g-s*(h+g*tau); *aq = h+s*(g-h*tau); if (!(--j)) break; ap += ip++; aq += iq++; } } if (eivec) { REPORT RectMatrixCol VP(V,p); RectMatrixCol VQ(V,q); Rotate(VP, VQ, tau, s); } } } } B = B + Z; D = B; Z = 0.0; } if (!converged) Throw(ConvergenceException(X)); if (eivec) SortSV(D, V, true); else SortAscending(D); }
/* * s o l v e O q p B e n c h m a r k */ returnValue solveOqpBenchmark( int_t nQP, int_t nV, const real_t* const _H, const real_t* const g, const real_t* const lb, const real_t* const ub, BooleanType isSparse, BooleanType useHotstarts, const Options& options, int_t maxAllowedNWSR, real_t& maxNWSR, real_t& avgNWSR, real_t& maxCPUtime, real_t& avgCPUtime, real_t& maxStationarity, real_t& maxFeasibility, real_t& maxComplementarity ) { int_t k; /* I) SETUP AUXILIARY VARIABLES: */ /* 1) Keep nWSR and store current and maximum number of * working set recalculations in temporary variables */ int_t nWSRcur; real_t CPUtimeLimit = maxCPUtime; real_t CPUtimeCur = CPUtimeLimit; real_t stat, feas, cmpl; maxNWSR = 0; avgNWSR = 0; maxCPUtime = 0.0; avgCPUtime = 0.0; maxStationarity = 0.0; maxFeasibility = 0.0; maxComplementarity = 0.0; /* 2) Pointers to data of current QP ... */ const real_t* gCur; const real_t* lbCur; const real_t* ubCur; /* 3) Vectors for solution obtained by qpOASES. */ real_t* x = new real_t[nV]; real_t* y = new real_t[nV]; //real_t obj; /* 4) Prepare matrix objects */ SymmetricMatrix *H; real_t* H_cpy = new real_t[nV*nV]; memcpy( H_cpy,_H, ((uint_t)(nV*nV))*sizeof(real_t) ); if ( isSparse == BT_TRUE ) { SymSparseMat *Hs; H = Hs = new SymSparseMat(nV, nV, nV, H_cpy); Hs->createDiagInfo(); delete[] H_cpy; } else { H = new SymDenseMat(nV, nV, nV, const_cast<real_t *>(H_cpy)); } H->doFreeMemory( ); /* II) SETUP QPROBLEM OBJECT */ QProblemB qp( nV ); qp.setOptions( options ); //qp.setPrintLevel( PL_LOW ); /* III) RUN BENCHMARK SEQUENCE: */ returnValue returnvalue; for( k=0; k<nQP; ++k ) { //if ( k%50 == 0 ) // printf( "%d\n",k ); /* 1) Update pointers to current QP data. */ gCur = &( g[k*nV] ); lbCur = &( lb[k*nV] ); ubCur = &( ub[k*nV] ); /* 2) Set nWSR and maximum CPU time. */ nWSRcur = maxAllowedNWSR; CPUtimeCur = CPUtimeLimit; /* 3) Solve current QP. */ if ( ( k == 0 ) || ( useHotstarts == BT_FALSE ) ) { /* initialise */ returnvalue = qp.init( H,gCur,lbCur,ubCur, nWSRcur,&CPUtimeCur ); if ( ( returnvalue != SUCCESSFUL_RETURN ) && ( returnvalue != RET_MAX_NWSR_REACHED ) ) { delete H; delete[] y; delete[] x; return THROWERROR( returnvalue ); } } else { /* hotstart */ returnvalue = qp.hotstart( gCur,lbCur,ubCur, nWSRcur,&CPUtimeCur ); if ( ( returnvalue != SUCCESSFUL_RETURN ) && ( returnvalue != RET_MAX_NWSR_REACHED ) ) { delete H; delete[] y; delete[] x; return THROWERROR( returnvalue ); } } /* 4) Obtain solution vectors and objective function value ... */ qp.getPrimalSolution( x ); qp.getDualSolution( y ); //obj = qp.getObjVal( ); /* 5) Compute KKT residuals */ getKktViolation( nV, _H,gCur,lbCur,ubCur, x,y, stat,feas,cmpl ); /* 6) update maximum values. */ if ( nWSRcur > maxNWSR ) maxNWSR = nWSRcur; if (stat > maxStationarity) maxStationarity = stat; if (feas > maxFeasibility) maxFeasibility = feas; if (cmpl > maxComplementarity) maxComplementarity = cmpl; if ( CPUtimeCur > maxCPUtime ) maxCPUtime = CPUtimeCur; avgNWSR += nWSRcur; avgCPUtime += CPUtimeCur; } avgNWSR /= nQP; avgCPUtime /= ((double)nQP); delete H; delete[] y; delete[] x; return SUCCESSFUL_RETURN; }
void trymatd() { Tracer et("Thirteenth test of Matrix package"); Tracer::PrintTrace(); Matrix X(5,20); int i,j; for (j=1;j<=20;j++) X(1,j) = j+1; for (i=2;i<=5;i++) for (j=1;j<=20; j++) X(i,j) = (long)X(i-1,j) * j % 1001; SymmetricMatrix S; S << X * X.t(); Matrix SM = X * X.t() - S; Print(SM); LowerTriangularMatrix L = Cholesky(S); Matrix Diff = L*L.t()-S; Clean(Diff, 0.000000001); Print(Diff); { Tracer et1("Stage 1"); LowerTriangularMatrix L1(5); Matrix Xt = X.t(); Matrix Xt2 = Xt; QRZT(X,L1); Diff = L - L1; Clean(Diff,0.000000001); Print(Diff); UpperTriangularMatrix Ut(5); QRZ(Xt,Ut); Diff = L - Ut.t(); Clean(Diff,0.000000001); Print(Diff); Matrix Y(3,20); for (j=1;j<=20;j++) Y(1,j) = 22-j; for (i=2;i<=3;i++) for (j=1;j<=20; j++) Y(i,j) = (long)Y(i-1,j) * j % 101; Matrix Yt = Y.t(); Matrix M,Mt; Matrix Y2=Y; QRZT(X,Y,M); QRZ(Xt,Yt,Mt); Diff = Xt - X.t(); Clean(Diff,0.000000001); Print(Diff); Diff = Yt - Y.t(); Clean(Diff,0.000000001); Print(Diff); Diff = Mt - M.t(); Clean(Diff,0.000000001); Print(Diff); Diff = Y2 * Xt2 * S.i() - M * L.i(); Clean(Diff,0.000000001); Print(Diff); } ColumnVector C1(5); { Tracer et1("Stage 2"); X.ReSize(5,5); for (j=1;j<=5;j++) X(1,j) = j+1; for (i=2;i<=5;i++) for (j=1;j<=5; j++) X(i,j) = (long)X(i-1,j) * j % 1001; for (i=1;i<=5;i++) C1(i) = i*i; CroutMatrix A = X; ColumnVector C2 = A.i() * C1; C1 = X.i() * C1; X = C1 - C2; Clean(X,0.000000001); Print(X); } { Tracer et1("Stage 3"); X.ReSize(7,7); for (j=1;j<=7;j++) X(1,j) = j+1; for (i=2;i<=7;i++) for (j=1;j<=7; j++) X(i,j) = (long)X(i-1,j) * j % 1001; C1.ReSize(7); for (i=1;i<=7;i++) C1(i) = i*i; RowVector R1 = C1.t(); Diff = R1 * X.i() - ( X.t().i() * R1.t() ).t(); Clean(Diff,0.000000001); Print(Diff); } { Tracer et1("Stage 4"); X.ReSize(5,5); for (j=1;j<=5;j++) X(1,j) = j+1; for (i=2;i<=5;i++) for (j=1;j<=5; j++) X(i,j) = (long)X(i-1,j) * j % 1001; C1.ReSize(5); for (i=1;i<=5;i++) C1(i) = i*i; CroutMatrix A1 = X*X; ColumnVector C2 = A1.i() * C1; C1 = X.i() * C1; C1 = X.i() * C1; X = C1 - C2; Clean(X,0.000000001); Print(X); } { Tracer et1("Stage 5"); int n = 40; SymmetricBandMatrix B(n,2); B = 0.0; for (i=1; i<=n; i++) { B(i,i) = 6; if (i<=n-1) B(i,i+1) = -4; if (i<=n-2) B(i,i+2) = 1; } B(1,1) = 5; B(n,n) = 5; SymmetricMatrix A = B; ColumnVector X(n); X(1) = 429; for (i=2;i<=n;i++) X(i) = (long)X(i-1) * 31 % 1001; X = X / 100000L; // the matrix B is rather ill-conditioned so the difficulty is getting // good agreement (we have chosen X very small) may not be surprising; // maximum element size in B.i() is around 1400 ColumnVector Y1 = A.i() * X; LowerTriangularMatrix C1 = Cholesky(A); ColumnVector Y2 = C1.t().i() * (C1.i() * X) - Y1; Clean(Y2, 0.000000001); Print(Y2); UpperTriangularMatrix CU = C1.t().i(); LowerTriangularMatrix CL = C1.i(); Y2 = CU * (CL * X) - Y1; Clean(Y2, 0.000000001); Print(Y2); Y2 = B.i() * X - Y1; Clean(Y2, 0.000000001); Print(Y2); LowerBandMatrix C2 = Cholesky(B); Matrix M = C2 - C1; Clean(M, 0.000000001); Print(M); ColumnVector Y3 = C2.t().i() * (C2.i() * X) - Y1; Clean(Y3, 0.000000001); Print(Y3); CU = C1.t().i(); CL = C1.i(); Y3 = CU * (CL * X) - Y1; Clean(Y3, 0.000000001); Print(Y3); Y3 = B.i() * X - Y1; Clean(Y3, 0.000000001); Print(Y3); SymmetricMatrix AI = A.i(); Y2 = AI*X - Y1; Clean(Y2, 0.000000001); Print(Y2); SymmetricMatrix BI = B.i(); BandMatrix C = B; Matrix CI = C.i(); M = A.i() - CI; Clean(M, 0.000000001); Print(M); M = B.i() - CI; Clean(M, 0.000000001); Print(M); M = AI-BI; Clean(M, 0.000000001); Print(M); M = AI-CI; Clean(M, 0.000000001); Print(M); M = A; AI << M; M = AI-A; Clean(M, 0.000000001); Print(M); C = B; BI << C; M = BI-B; Clean(M, 0.000000001); Print(M); } { Tracer et1("Stage 5"); SymmetricMatrix A(4), B(4); A << 5 << 1 << 4 << 2 << 1 << 6 << 1 << 0 << 1 << 7; B << 8 << 1 << 5 << 1 << 0 << 9 << 2 << 1 << 0 << 6; LowerTriangularMatrix AB = Cholesky(A) * Cholesky(B); Matrix M = Cholesky(A) * B * Cholesky(A).t() - AB*AB.t(); Clean(M, 0.000000001); Print(M); M = A * Cholesky(B); M = M * M.t() - A * B * A; Clean(M, 0.000000001); Print(M); } { Tracer et1("Stage 6"); int N=49; int i; SymmetricBandMatrix S(N,1); Matrix B(N,N+1); B=0; for (i=1;i<=N;i++) { S(i,i)=1; B(i,i)=1; B(i,i+1)=-1; } for (i=1;i<N; i++) S(i,i+1)=-.5; DiagonalMatrix D(N+1); D = 1; B = B.t()*S.i()*B - (D-1.0/(N+1))*2.0; Clean(B, 0.000000001); Print(B); } { Tracer et1("Stage 7"); // See if you can pass a CroutMatrix to a function Matrix A(4,4); A.Row(1) << 3 << 2 << -1 << 4; A.Row(2) << -8 << 7 << 2 << 0; A.Row(3) << 2 << -2 << 3 << 1; A.Row(4) << -1 << 5 << 2 << 2; CroutMatrix B = A; Matrix C = A * Inverter(B) - IdentityMatrix(4); Clean(C, 0.000000001); Print(C); } // cout << "\nEnd of Thirteenth test\n"; }
void init_illum (int ndim, ColumnVector& x) { int i, j, k; double h,rij2,midx,midy,dx,dy,slope1,slope2,theta1,theta2,theta,scale; double dtmp, Ides; // allocate storage m = ndim; n = 11; Ides = 2.0; patch = new double*[n+1]; for (i=0; i<=n; i++) patch[i] = new double[2]; lamp = new double*[m+1]; for (i=0; i<=m; i++) lamp[i] = new double[2]; A = new double*[n+1]; for (i=0; i<=n; i++) A[i] = new double[m+1]; // initializing the patches and lamps patch[0][0] = 0.0; patch[0][1] = 0.0; patch[1][0] = 0.0909; patch[1][1] = 0.1; patch[2][0] = 0.1818; patch[2][1] = 0.2; patch[3][0] = 0.2727; patch[3][1] = 0.2; patch[4][0] = 0.3636; patch[4][1] = 0.1; patch[5][0] = 0.4545; patch[5][1] = 0.2; patch[6][0] = 0.5455; patch[6][1] = 0.3; patch[7][0] = 0.6364; patch[7][1] = 0.2; patch[8][0] = 0.7273; patch[8][1] = 0.0; patch[9][0] = 0.8182; patch[9][1] = 0.0; patch[10][0] = 0.9091; patch[10][1] = 0.2; patch[11][0] = 1.0; patch[11][1] = 0.1; lamp[1][0] = 0.1; lamp[1][1] = 1.0; lamp[2][0] = 0.3; lamp[2][1] = 1.1; lamp[3][0] = 0.4; lamp[3][1] = 0.6; lamp[4][0] = 0.6; lamp[4][1] = 0.9; lamp[5][0] = 0.8; lamp[5][1] = 0.9; lamp[6][0] = 0.9; lamp[6][1] = 1.2; lamp[7][0] = 0.95; lamp[7][1] = 1.0; // initialize the A matrix for (i=1; i<=n; i++) { midx = patch[i][0] - patch[i-1][0]; midy = patch[i][1] - patch[i-1][1]; if (midx == 0.0) { cout << "Error : patches cannot overlap each other. \n"; exit(-1); } slope1 = midy / midx; theta1 = atan(slope1); midx = 0.5 * midx + patch[i-1][0]; midy = 0.5 * midy + patch[i-1][1]; for (j=1; j<=m; j++) { dx = lamp[j][0] - midx; dy = lamp[j][1] - midy; rij2 = dx * dx + dy * dy; if (dx == 0.0) theta2 = dy / fabs(dy) * M_PI * 0.5; else { slope2 = dy / dx; theta2 = atan(slope2); } if (theta2 < 0.0) theta2 = theta2 + M_PI; theta = M_PI * 0.5 - (theta2 - theta1); scale = cos(theta); if (scale < 0.0) A[i][j] = 0.0; else A[i][j] = scale / (rij2 * Ides); } } // initialize x for (i=1; i<=m; i++) x(i) = 0.5; // initialize the Hessian HH.ReSize(m); for (i=1; i<=m; i++) { for (j=1; j<=m; j++) { dtmp = 0.0; for (k=1; k<=n; k++) dtmp += A[k][i] * A[k][j]; HH(i,j) = dtmp; } } }
void trymatd() { Tracer et("Thirteenth test of Matrix package"); Tracer::PrintTrace(); Matrix X(5,20); int i,j; for (j=1;j<=20;j++) X(1,j) = j+1; for (i=2;i<=5;i++) for (j=1;j<=20; j++) X(i,j) = (long)X(i-1,j) * j % 1001; SymmetricMatrix S; S << X * X.t(); Matrix SM = X * X.t() - S; Print(SM); LowerTriangularMatrix L = Cholesky(S); Matrix Diff = L*L.t()-S; Clean(Diff, 0.000000001); Print(Diff); { Tracer et1("Stage 1"); LowerTriangularMatrix L1(5); Matrix Xt = X.t(); Matrix Xt2 = Xt; QRZT(X,L1); Diff = L - L1; Clean(Diff,0.000000001); Print(Diff); UpperTriangularMatrix Ut(5); QRZ(Xt,Ut); Diff = L - Ut.t(); Clean(Diff,0.000000001); Print(Diff); Matrix Y(3,20); for (j=1;j<=20;j++) Y(1,j) = 22-j; for (i=2;i<=3;i++) for (j=1;j<=20; j++) Y(i,j) = (long)Y(i-1,j) * j % 101; Matrix Yt = Y.t(); Matrix M,Mt; Matrix Y2=Y; QRZT(X,Y,M); QRZ(Xt,Yt,Mt); Diff = Xt - X.t(); Clean(Diff,0.000000001); Print(Diff); Diff = Yt - Y.t(); Clean(Diff,0.000000001); Print(Diff); Diff = Mt - M.t(); Clean(Diff,0.000000001); Print(Diff); Diff = Y2 * Xt2 * S.i() - M * L.i(); Clean(Diff,0.000000001); Print(Diff); } ColumnVector C1(5); { Tracer et1("Stage 2"); X.ReSize(5,5); for (j=1;j<=5;j++) X(1,j) = j+1; for (i=2;i<=5;i++) for (j=1;j<=5; j++) X(i,j) = (long)X(i-1,j) * j % 1001; for (i=1;i<=5;i++) C1(i) = i*i; CroutMatrix A = X; ColumnVector C2 = A.i() * C1; C1 = X.i() * C1; X = C1 - C2; Clean(X,0.000000001); Print(X); } { Tracer et1("Stage 3"); X.ReSize(7,7); for (j=1;j<=7;j++) X(1,j) = j+1; for (i=2;i<=7;i++) for (j=1;j<=7; j++) X(i,j) = (long)X(i-1,j) * j % 1001; C1.ReSize(7); for (i=1;i<=7;i++) C1(i) = i*i; RowVector R1 = C1.t(); Diff = R1 * X.i() - ( X.t().i() * R1.t() ).t(); Clean(Diff,0.000000001); Print(Diff); } { Tracer et1("Stage 4"); X.ReSize(5,5); for (j=1;j<=5;j++) X(1,j) = j+1; for (i=2;i<=5;i++) for (j=1;j<=5; j++) X(i,j) = (long)X(i-1,j) * j % 1001; C1.ReSize(5); for (i=1;i<=5;i++) C1(i) = i*i; CroutMatrix A1 = X*X; ColumnVector C2 = A1.i() * C1; C1 = X.i() * C1; C1 = X.i() * C1; X = C1 - C2; Clean(X,0.000000001); Print(X); } { Tracer et1("Stage 5"); int n = 40; SymmetricBandMatrix B(n,2); B = 0.0; for (i=1; i<=n; i++) { B(i,i) = 6; if (i<=n-1) B(i,i+1) = -4; if (i<=n-2) B(i,i+2) = 1; } B(1,1) = 5; B(n,n) = 5; SymmetricMatrix A = B; ColumnVector X(n); X(1) = 429; for (i=2;i<=n;i++) X(i) = (long)X(i-1) * 31 % 1001; X = X / 100000L; // the matrix B is rather ill-conditioned so the difficulty is getting // good agreement (we have chosen X very small) may not be surprising; // maximum element size in B.i() is around 1400 ColumnVector Y1 = A.i() * X; LowerTriangularMatrix C1 = Cholesky(A); ColumnVector Y2 = C1.t().i() * (C1.i() * X) - Y1; Clean(Y2, 0.000000001); Print(Y2); UpperTriangularMatrix CU = C1.t().i(); LowerTriangularMatrix CL = C1.i(); Y2 = CU * (CL * X) - Y1; Clean(Y2, 0.000000001); Print(Y2); Y2 = B.i() * X - Y1; Clean(Y2, 0.000000001); Print(Y2); LowerBandMatrix C2 = Cholesky(B); Matrix M = C2 - C1; Clean(M, 0.000000001); Print(M); ColumnVector Y3 = C2.t().i() * (C2.i() * X) - Y1; Clean(Y3, 0.000000001); Print(Y3); CU = C1.t().i(); CL = C1.i(); Y3 = CU * (CL * X) - Y1; Clean(Y3, 0.000000001); Print(Y3); Y3 = B.i() * X - Y1; Clean(Y3, 0.000000001); Print(Y3); SymmetricMatrix AI = A.i(); Y2 = AI*X - Y1; Clean(Y2, 0.000000001); Print(Y2); SymmetricMatrix BI = B.i(); BandMatrix C = B; Matrix CI = C.i(); M = A.i() - CI; Clean(M, 0.000000001); Print(M); M = B.i() - CI; Clean(M, 0.000000001); Print(M); M = AI-BI; Clean(M, 0.000000001); Print(M); M = AI-CI; Clean(M, 0.000000001); Print(M); M = A; AI << M; M = AI-A; Clean(M, 0.000000001); Print(M); C = B; BI << C; M = BI-B; Clean(M, 0.000000001); Print(M); } { Tracer et1("Stage 5"); SymmetricMatrix A(4), B(4); A << 5 << 1 << 4 << 2 << 1 << 6 << 1 << 0 << 1 << 7; B << 8 << 1 << 5 << 1 << 0 << 9 << 2 << 1 << 0 << 6; LowerTriangularMatrix AB = Cholesky(A) * Cholesky(B); Matrix M = Cholesky(A) * B * Cholesky(A).t() - AB*AB.t(); Clean(M, 0.000000001); Print(M); M = A * Cholesky(B); M = M * M.t() - A * B * A; Clean(M, 0.000000001); Print(M); } { Tracer et1("Stage 6"); int N=49; int i; SymmetricBandMatrix S(N,1); Matrix B(N,N+1); B=0; for (i=1;i<=N;i++) { S(i,i)=1; B(i,i)=1; B(i,i+1)=-1; } for (i=1;i<N; i++) S(i,i+1)=-.5; DiagonalMatrix D(N+1); D = 1; B = B.t()*S.i()*B - (D-1.0/(N+1))*2.0; Clean(B, 0.000000001); Print(B); } { Tracer et1("Stage 7"); // Copying and moving CroutMatrix Matrix A(7,7); A.Row(1) << 3 << 2 << -1 << 4 << -3 << 5 << 9; A.Row(2) << -8 << 7 << 2 << 0 << 7 << 0 << -1; A.Row(3) << 2 << -2 << 3 << 1 << 9 << 0 << 3; A.Row(4) << -1 << 5 << 2 << 2 << 5 << -1 << 2; A.Row(5) << 4 << -4 << 1 << 9 << -8 << 7 << 5; A.Row(6) << 1 << -2 << 5 << -1 << -2 << 5 << 1; A.Row(7) << -6 << 3 << -1 << 8 << -1 << 2 << 2; RowVector D(30); D = 0; Real x = determinant(A); CroutMatrix B = A; D(1) = determinant(B) / x - 1; Matrix C = A * Inverter1(B) - IdentityMatrix(7); Clean(C, 0.000000001); Print(C); // Test copy constructor (in Inverter2 and ordinary copy) CroutMatrix B1; B1 = B; D(2) = determinant(B1) / x - 1; C = A * Inverter2(B1) - IdentityMatrix(7); Clean(C, 0.000000001); Print(C); // Do it again with release B.release(); B1 = B; D(2) = B.nrows(); D(3) = B.ncols(); D(4) = B.size(); D(5) = determinant(B1) / x - 1; B1.release(); C = A * Inverter2(B1) - IdentityMatrix(7); D(6) = B1.nrows(); D(7) = B1.ncols(); D(8) = B1.size(); Clean(C, 0.000000001); Print(C); // see if we get an implicit invert B1 = -A; D(9) = determinant(B1) / x + 1; // odd number of rows - sign will change C = -A * Inverter2(B1) - IdentityMatrix(7); Clean(C, 0.000000001); Print(C); // check for_return B = LU1(A); B1 = LU2(A); CroutMatrix B2 = LU3(A); C = A * B.i() - IdentityMatrix(7); Clean(C, 0.000000001); Print(C); D(10) = (B == B1 ? 0 : 1) + (B == B2 ? 0 : 1); // check lengths D(13) = B.size()-49; // check release(2) B1.release(2); B2 = B1; D(15) = B == B2 ? 0 : 1; CroutMatrix B3 = B1; D(16) = B == B3 ? 0 : 1; D(17) = B1.size(); // some oddments B1 = B; B1 = B1.i(); C = A - B1.i(); Clean(C, 0.000000001); Print(C); B1 = B; B1.release(); B1 = B1; B2 = B1; D(19) = B == B1 ? 0 : 1; D(20) = B == B2 ? 0 : 1; B1.cleanup(); B2 = B1; D(21) = B1.size(); D(22) = B2.size(); GenericMatrix GM = B; C = A.i() - GM.i(); Clean(C, 0.000000001); Print(C); B1 = GM; D(23) = B == B1 ? 0 : 1; B1 = A * 0; B2 = B1; D(24) = B2.is_singular() ? 0 : 1; // check release again - see if memory moves const Real* d = B.const_data(); const int* i = B.const_data_indx(); B1 = B; const Real* d1 = B1.const_data(); const int* i1 = B1.const_data_indx(); B1.release(); B2 = B1; const Real* d2 = B2.const_data(); const int* i2 = B2.const_data_indx(); D(25) = (d != d1 ? 0 : 1) + (d1 == d2 ? 0 : 1) + (i != i1 ? 0 : 1) + (i1 == i2 ? 0 : 1); Clean(D, 0.000000001); Print(D); } { Tracer et1("Stage 8"); // Same for BandLUMatrix BandMatrix A(7,3,2); A.Row(1) << 3 << 2 << -1; A.Row(2) << -8 << 7 << 2 << 0; A.Row(3) << 2 << -2 << 3 << 1 << 9; A.Row(4) << -1 << 5 << 2 << 2 << 5 << -1; A.Row(5) << -4 << 1 << 9 << -8 << 7 << 5; A.Row(6) << 5 << -1 << -2 << 5 << 1; A.Row(7) << 8 << -1 << 2 << 2; RowVector D(30); D = 0; Real x = determinant(A); BandLUMatrix B = A; D(1) = determinant(B) / x - 1; Matrix C = A * Inverter1(B) - IdentityMatrix(7); Clean(C, 0.000000001); Print(C); // Test copy constructor (in Inverter2 and ordinary copy) BandLUMatrix B1; B1 = B; D(2) = determinant(B1) / x - 1; C = A * Inverter2(B1) - IdentityMatrix(7); Clean(C, 0.000000001); Print(C); // Do it again with release B.release(); B1 = B; D(2) = B.nrows(); D(3) = B.ncols(); D(4) = B.size(); D(5) = determinant(B1) / x - 1; B1.release(); C = A * Inverter2(B1) - IdentityMatrix(7); D(6) = B1.nrows(); D(7) = B1.ncols(); D(8) = B1.size(); Clean(C, 0.000000001); Print(C); // see if we get an implicit invert B1 = -A; D(9) = determinant(B1) / x + 1; // odd number of rows - sign will change C = -A * Inverter2(B1) - IdentityMatrix(7); Clean(C, 0.000000001); Print(C); // check for_return B = LU1(A); B1 = LU2(A); BandLUMatrix B2 = LU3(A); C = A * B.i() - IdentityMatrix(7); Clean(C, 0.000000001); Print(C); D(10) = (B == B1 ? 0 : 1) + (B == B2 ? 0 : 1); // check lengths D(11) = B.bandwidth().lower()-3; D(12) = B.bandwidth().upper()-2; D(13) = B.size()-42; D(14) = B.size2()-21; // check release(2) B1.release(2); B2 = B1; D(15) = B == B2 ? 0 : 1; BandLUMatrix B3 = B1; D(16) = B == B3 ? 0 : 1; D(17) = B1.size(); // Compare with CroutMatrix CroutMatrix CM = A; C = CM.i() - B.i(); Clean(C, 0.000000001); Print(C); D(18) = determinant(CM) / x - 1; // some oddments B1 = B; CM = B1.i(); C = A - CM.i(); Clean(C, 0.000000001); Print(C); B1 = B; B1.release(); B1 = B1; B2 = B1; D(19) = B == B1 ? 0 : 1; D(20) = B == B2 ? 0 : 1; B1.cleanup(); B2 = B1; D(21) = B1.size(); D(22) = B2.size(); GenericMatrix GM = B; C = A.i() - GM.i(); Clean(C, 0.000000001); Print(C); B1 = GM; D(23) = B == B1 ? 0 : 1; B1 = A * 0; B2 = B1; D(24) = B2.is_singular() ? 0 : 1; // check release again - see if memory moves const Real* d = B.const_data(); const Real* dd = B.const_data(); const int* i = B.const_data_indx(); B1 = B; const Real* d1 = B1.const_data(); const Real* dd1 = B1.const_data(); const int* i1 = B1.const_data_indx(); B1.release(); B2 = B1; const Real* d2 = B2.const_data(); const Real* dd2 = B2.const_data(); const int* i2 = B2.const_data_indx(); D(25) = (d != d1 ? 0 : 1) + (d1 == d2 ? 0 : 1) + (dd != dd1 ? 0 : 1) + (dd1 == dd2 ? 0 : 1) + (i != i1 ? 0 : 1) + (i1 == i2 ? 0 : 1); Clean(D, 0.000000001); Print(D); } { Tracer et1("Stage 9"); // Modification of Cholesky decomposition int i, j; // Build test matrix Matrix X(100, 10); MultWithCarry mwc; // Uniform random number generator for (i = 1; i <= 100; ++i) for (j = 1; j <= 10; ++j) X(i, j) = 2.0 * (mwc.Next() - 0.5); Matrix X1 = X; // save copy // Form sums of squares and products matrix and Cholesky decompose SymmetricMatrix A; A << X.t() * X; UpperTriangularMatrix U1 = Cholesky(A).t(); // Do QR decomposition of X and check we get same triangular matrix UpperTriangularMatrix U2; QRZ(X, U2); Matrix Diff = U1 - U2; Clean(Diff, 0.000000001); Print(Diff); // Try adding new row to X and updating triangular matrix RowVector NewRow(10); for (j = 1; j <= 10; ++j) NewRow(j) = 2.0 * (mwc.Next() - 0.5); UpdateCholesky(U2, NewRow); X = X1 & NewRow; QRZ(X, U1); Diff = U1 - U2; Clean(Diff, 0.000000001); Print(Diff); // Try removing two rows and updating triangular matrix DowndateCholesky(U2, X1.Row(20)); DowndateCholesky(U2, X1.Row(35)); X = X1.Rows(1,19) & X1.Rows(21,34) & X1.Rows(36,100) & NewRow; QRZ(X, U1); Diff = U1 - U2; Clean(Diff, 0.000000001); Print(Diff); // Circular shifts CircularShift(X, 3,6); CircularShift(X, 5,5); CircularShift(X, 4,5); CircularShift(X, 1,6); CircularShift(X, 6,10); } { Tracer et1("Stage 10"); // Try updating QRZ, QRZT decomposition TestUpdateQRZ tuqrz1(10, 100, 50, 25); tuqrz1.DoTest(); tuqrz1.Reset(); tuqrz1.ClearRow(1); tuqrz1.DoTest(); tuqrz1.Reset(); tuqrz1.ClearRow(1); tuqrz1.ClearRow(2); tuqrz1.DoTest(); tuqrz1.Reset(); tuqrz1.ClearRow(5); tuqrz1.ClearRow(6); tuqrz1.DoTest(); tuqrz1.Reset(); tuqrz1.ClearRow(10); tuqrz1.DoTest(); TestUpdateQRZ tuqrz2(15, 100, 0, 0); tuqrz2.DoTest(); tuqrz2.Reset(); tuqrz2.ClearRow(1); tuqrz2.DoTest(); tuqrz2.Reset(); tuqrz2.ClearRow(1); tuqrz2.ClearRow(2); tuqrz2.DoTest(); tuqrz2.Reset(); tuqrz2.ClearRow(5); tuqrz2.ClearRow(6); tuqrz2.DoTest(); tuqrz2.Reset(); tuqrz2.ClearRow(15); tuqrz2.DoTest(); TestUpdateQRZ tuqrz3(5, 0, 10, 0); tuqrz3.DoTest(); } // cout << "\nEnd of Thirteenth test\n"; }
ReturnMatrix Returner4(const GenericMatrix& GM) { SymmetricMatrix M = GM+4; M.Release(); return M; }
static void tred2(const SymmetricMatrix& A, DiagonalMatrix& D, DiagonalMatrix& E, Matrix& Z) { Tracer et("Evalue(tred2)"); Real tol = FloatingPointPrecision::Minimum()/FloatingPointPrecision::Epsilon(); int n = A.Nrows(); Z.ReSize(n,n); Z.Inject(A); D.ReSize(n); E.ReSize(n); Real* z = Z.Store(); int i; for (i=n-1; i > 0; i--) // i=0 is excluded { Real f = Z.element(i,i-1); Real g = 0.0; int k = i-1; Real* zik = z + i*n; while (k--) g += square(*zik++); Real h = g + square(f); if (g <= tol) { E.element(i) = f; h = 0.0; } else { g = sign(-sqrt(h), f); E.element(i) = g; h -= f*g; Z.element(i,i-1) = f-g; f = 0.0; Real* zji = z + i; Real* zij = z + i*n; Real* ej = E.Store(); int j; for (j=0; j<i; j++) { *zji = (*zij++)/h; g = 0.0; Real* zjk = z + j*n; zik = z + i*n; k = j; while (k--) g += *zjk++ * (*zik++); k = i-j; while (k--) { g += *zjk * (*zik++); zjk += n; } *ej++ = g/h; f += g * (*zji); zji += n; } Real hh = f / (h + h); zij = z + i*n; ej = E.Store(); for (j=0; j<i; j++) { f = *zij++; g = *ej - hh * f; *ej++ = g; Real* zjk = z + j*n; Real* zik = z + i*n; Real* ek = E.Store(); k = j+1; while (k--) *zjk++ -= ( f*(*ek++) + g*(*zik++) ); } } D.element(i) = h; } D.element(0) = 0.0; E.element(0) = 0.0; for (i=0; i<n; i++) { if (D.element(i) != 0.0) { for (int j=0; j<i; j++) { Real g = 0.0; Real* zik = z + i*n; Real* zkj = z + j; int k = i; while (k--) { g += *zik++ * (*zkj); zkj += n; } Real* zki = z + i; zkj = z + j; k = i; while (k--) { *zkj -= g * (*zki); zkj += n; zki += n; } } } Real* zij = z + i*n; Real* zji = z + i; int j = i; while (j--) { *zij++ = 0.0; *zji = 0.0; zji += n; } D.element(i) = *zij; *zij = 1.0; } }
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; U1.SubMatrix(4,8,5,9) /= 2; U1.SubMatrix(4,8,5,9) += 388 * V; U1.SubMatrix(4,8,5,9) *= 2; U1.SubMatrix(4,8,5,9) += V; U1 -= U; UpperTriangularMatrix U2 = U1; U1 << U1.SubMatrix(4,8,5,9); U2.SubMatrix(4,8,5,9) -= U1; Print(U2); U1 -= (777*V); Print(U1); U1 = U; U1.SubMatrix(4,8,5,9) -= U.SymSubMatrix(1,5); U1 -= U; U2 = U1; U1 << U1.SubMatrix(4,8,5,9); U2.SubMatrix(4,8,5,9) -= U1; Print(U2); U1 += V; Print(U1); U1 = U; U1.SubMatrix(3,10,15,19) += 29; U1 -= U; Matrix X = U1.SubMatrix(3,10,15,19); X -= 29; Print(X); U1.SubMatrix(3,10,15,19) *= 0; Print(U1); LowerTriangularMatrix L = U.t(); LowerTriangularMatrix M = L.SymSubMatrix(1,5); LowerTriangularMatrix L1 = L; L1.SubMatrix(5,9,4,8) /= 2; L1.SubMatrix(5,9,4,8) += 388 * M; L1.SubMatrix(5,9,4,8) *= 2; L1.SubMatrix(5,9,4,8) += M; L1 -= L; LowerTriangularMatrix L2 = L1; L1 << L1.SubMatrix(5,9,4,8); L2.SubMatrix(5,9,4,8) -= L1; Print(L2); L1 -= (777*M); Print(L1); L1 = L; L1.SubMatrix(5,9,4,8) -= L.SymSubMatrix(1,5); L1 -= L; L2 =L1; L1 << L1.SubMatrix(5,9,4,8); L2.SubMatrix(5,9,4,8) -= L1; Print(L2); L1 += M; Print(L1); L1 = L; L1.SubMatrix(15,19,3,10) -= 29; L1 -= L; X = L1.SubMatrix(15,19,3,10); X += 29; Print(X); L1.SubMatrix(15,19,3,10) *= 0; Print(L1); } { Tracer et1("Stage 11"); // more tests on submatrices Matrix M(20,30); for (i=1; i<=20; i++) for (j=1; j<=30; j++) M(i,j)=100 * i + j; Matrix M1 = M; for (j=1; j<=30; j++) { ColumnVector CV = 3 * M1.Column(j); M.Column(j) += CV; } for (i=1; i<=20; i++) { RowVector RV = 5 * M1.Row(i); M.Row(i) -= RV; } M += M1; Print(M); } { Tracer et1("Stage 12"); // more tests on Release Matrix M(20,30); for (i=1; i<=20; i++) for (j=1; j<=30; j++) M(i,j)=100 * i + j; Matrix M1 = M; M.Release(); Matrix M2 = M; Matrix X = M; Print(X); X = M1 - M2; Print(X); #ifndef DONT_DO_NRIC nricMatrix N = M1; nricMatrix N1 = N; N.Release(); nricMatrix N2 = N; nricMatrix Y = N; Print(Y); Y = N1 - N2; Print(Y); N = M1 / 2; N1 = N * 2; N.Release(); N2 = N * 2; Y = N; Print(N); Y = (N1 - M1) | (N2 - M1); Print(Y); #endif } { Tracer et("Stage 13"); // test sum of squares of rows or columns MultWithCarry mwc; DiagonalMatrix DM; Matrix X; // rectangular matrix Matrix A(20, 15); FillWithValues(mwc, A); // sum of squares of rows DM << A * A.t(); ColumnVector CV = A.sum_square_rows(); X = CV - DM.AsColumn(); Clean(X, 0.000000001); Print(X); DM << A.t() * A; RowVector RV = A.sum_square_columns(); X = RV - DM.AsRow(); Clean(X, 0.000000001); Print(X); X = RV - A.t().sum_square_rows().t(); Clean(X, 0.000000001); Print(X); X = CV - A.t().sum_square_columns().t(); Clean(X, 0.000000001); Print(X); // UpperTriangularMatrix A.ReSize(17,17); FillWithValues(mwc, A); UpperTriangularMatrix UT; UT << A; Matrix A1 = UT; X = UT.sum_square_rows() - A1.sum_square_rows(); Print(X); X = UT.sum_square_columns() - A1.sum_square_columns(); Print(X); // LowerTriangularMatrix LowerTriangularMatrix LT; LT << A; A1 = LT; X = LT.sum_square_rows() - A1.sum_square_rows(); Print(X); X = LT.sum_square_columns() - A1.sum_square_columns(); Print(X); // SymmetricMatrix SymmetricMatrix SM; SM << A; A1 = SM; X = SM.sum_square_rows() - A1.sum_square_rows(); Print(X); X = SM.sum_square_columns() - A1.sum_square_columns(); Print(X); // DiagonalMatrix DM << A; A1 = DM; X = DM.sum_square_rows() - A1.sum_square_rows(); Print(X); X = DM.sum_square_columns() - A1.sum_square_columns(); Print(X); // BandMatrix BandMatrix BM(17, 3, 5); BM.Inject(A); A1 = BM; X = BM.sum_square_rows() - A1.sum_square_rows(); Print(X); X = BM.sum_square_columns() - A1.sum_square_columns(); Print(X); // SymmetricBandMatrix SymmetricBandMatrix SBM(17, 4); SBM.Inject(A); A1 = SBM; X = SBM.sum_square_rows() - A1.sum_square_rows(); Print(X); X = SBM.sum_square_columns() - A1.sum_square_columns(); Print(X); // IdentityMatrix IdentityMatrix IM(29); X = IM.sum_square_rows() - 1; Print(X); X = IM.sum_square_columns() - 1; Print(X); // Matrix with zero rows A1.ReSize(0,10); X.ReSize(1,10); X = 0; X -= A1.sum_square_columns(); Print(X); X.ReSize(0,1); X -= A1.sum_square_rows(); Print(X); // Matrix with zero columns A1.ReSize(10,0); X.ReSize(10,1); X = 0; X -= A1.sum_square_rows(); Print(X); X.ReSize(1,0); X -= A1.sum_square_columns(); Print(X); } { Tracer et("Stage 14"); // test extend orthonormal MultWithCarry mwc; Matrix A(20,5); FillWithValues(mwc, A); // Orthonormalise UpperTriangularMatrix R; Matrix A_old = A; QRZ(A,R); // Check decomposition Matrix X = A * R - A_old; Clean(X, 0.000000001); Print(X); // Check orthogonality X = A.t() * A - IdentityMatrix(5); Clean(X, 0.000000001); Print(X); // Try orthonality extend SquareMatrix A1(20); A1.Columns(1,5) = A; extend_orthonormal(A1,5); // check columns unchanged X = A - A1.Columns(1,5); Print(X); // Check orthogonality X = A1.t() * A1 - IdentityMatrix(20); Clean(X, 0.000000001); Print(X); X = A1 * A1.t() - IdentityMatrix(20); Clean(X, 0.000000001); Print(X); // Test with smaller number of columns Matrix A2(20,15); A2.Columns(1,5) = A; extend_orthonormal(A2,5); // check columns unchanged X = A - A2.Columns(1,5); Print(X); // Check orthogonality X = A2.t() * A2 - IdentityMatrix(15); Clean(X, 0.000000001); Print(X); // check it works with no columns to start with A2.ReSize(100,100); extend_orthonormal(A2,0); // Check orthogonality X = A2.t() * A2 - IdentityMatrix(100); Clean(X, 0.000000001); Print(X); X = A2 * A2.t() - IdentityMatrix(100); Clean(X, 0.000000001); Print(X); } // cout << "\nEnd of twelfth test\n"; }
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"; }
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(); X = X-D; Print(X); SymmetricBandMatrix SB1(11,7); SB1 = 5; SymmetricBandMatrix SB2 = SB1 + D; X.ReSize(11,11); X=0; for (i=1; i<=11; i++) for (j=1; j<=11; j++) { if (abs(i-j)<=7) X(i,j)=5; if (i==j) X(i,j)+=CV(i); } SymmetricMatrix SM; SM.ReSize(11); SM=SB; SB = SB+SB2; X1 = SM+X-SB; Print(X1); SB2=0; X2=SB2; X1=SB; Print(X2); for (i=1; i<=11; i++) SB2.Column(i)<<SB.Column(i); X1=X1-SB2; Print(X1); X = SB; SB2.ReSize(11,4); SB2 = SB*5; SB2 = SB + SB2; X1 = X*6 - SB2; Print(X1); X1 = SP(SB,SB2/3); X1=X1-SP(X,X*2); Print(X1); X1 = SP(SB2/6,X*2); X1=X1-SP(X*2,X); Print(X1); } { // test the simple integer array class Tracer et("Stage 5"); ColumnVector Test(10); Test = 0.0; int i; SimpleIntArray A(100); for (i = 0; i < 100; i++) A[i] = i*i+1; SimpleIntArray B(100), C(50), D; B = A; A.ReSize(50, true); C = A; A.ReSize(150, true); D = A; for (i = 0; i < 100; i++) if (B[i] != i*i+1) Test(1)=1; for (i = 0; i < 50; i++) if (C[i] != i*i+1) Test(2)=1; for (i = 0; i < 50; i++) if (D[i] != i*i+1) Test(3)=1; for (i = 50; i < 150; i++) if (D[i] != 0) Test(3)=1; A.resize(75); A = A.size(); for (i = 0; i < 75; i++) if (A[i] != 75) Test(4)=1; A.resize(25); A = A.size(); for (i = 0; i < 25; i++) if (A[i] != 25) Test(5)=1; A.ReSize(25); A = 23; for (i = 0; i < 25; i++) if (A[i] != 23) Test(6)=1; A.ReSize(0); A.ReSize(15); A = A.Size(); for (i = 0; i < 15; i++) if (A[i] != 15) Test(7)=1; const SimpleIntArray E = B; for (i = 0; i < 100; i++) if (E[i] != i*i+1) Test(8)=1; SimpleIntArray F; F.resize_keep(5); for (i = 0; i < 5; i++) if (F[i] != 0) Test(9)=1; Print(Test); } { // testing RealStarStar Tracer et("Stage 6"); MultWithCarry MWC; Matrix A(10, 12), B(12, 15), C(10, 15); FillWithValues(MWC, A); FillWithValues(MWC, B); ConstRealStarStar a(A); ConstRealStarStar b(B); RealStarStar c(C); c_matrix_multiply(10,12,15,a,b,c); Matrix X = C - A * B; Clean(X,0.00000001); Print(X); A.ReSize(11, 10); B.ReSize(10,8); C.ReSize(11,8); FillWithValues(MWC, A); FillWithValues(MWC, B); C = -1; c_matrix_multiply(11,10,8, ConstRealStarStar(A),ConstRealStarStar(B),RealStarStar(C)); X = C - A * B; Clean(X,0.00000001); Print(X); } { // testing resize_keep Tracer et("Stage 7"); Matrix X, Y; MultWithCarry MWC; X.resize(20,35); FillWithValues(MWC, X); Matrix M(20,35); M = X; X = M.submatrix(1,15,1,25); M.resize_keep(15,25); Y = X - M; Print(Y); M.resize_keep(15,25); Y = X - M; Print(Y); Y.resize(29,27); Y = 0; Y.submatrix(1,15,1,25) = X; M.resize_keep(29,27); Y -= M; Print(Y); M.resize_keep(0,5); M.resize_keep(10,10); Print(M); M.resize_keep(15,0); M.resize_keep(10,10); Print(M); X.resize(20,35); FillWithValues(MWC, X); M = X; M.resize_keep(38,17); Y.resize(38,17); Y = 0; Y.submatrix(1,20,1,17) = X.submatrix(1,20,1,17); Y -= M; Print(Y); X.resize(40,12); FillWithValues(MWC, X); M = X; M.resize_keep(38,17); Y.resize(38,17); Y = 0; Y.submatrix(1,38,1,12) = X.submatrix(1,38,1,12); Y -= M; Print(Y); #ifndef DONT_DO_NRIC X.resize(20,35); FillWithValues(MWC, X); nricMatrix nM(20,35); nM = X; X = nM.submatrix(1,15,1,25); nM.resize_keep(15,25); Y = X - nM; Print(Y); nM.resize_keep(15,25); Y = X - nM; Print(Y); Y.resize(29,27); Y = 0; Y.submatrix(1,15,1,25) = X; nM.resize_keep(29,27); Y -= nM; Print(Y); nM.resize_keep(0,5); nM.resize_keep(10,10); Print(nM); nM.resize_keep(15,0); nM.resize_keep(10,10); Print(nM); X.resize(20,35); FillWithValues(MWC, X); nM = X; nM.resize_keep(38,17); Y.resize(38,17); Y = 0; Y.submatrix(1,20,1,17) = X.submatrix(1,20,1,17); Y -= nM; Print(Y); X.resize(40,12); FillWithValues(MWC, X); nM = X; nM.resize_keep(38,17); Y.resize(38,17); Y = 0; Y.submatrix(1,38,1,12) = X.submatrix(1,38,1,12); Y -= nM; Print(Y); #endif X.resize(20,20); FillWithValues(MWC, X); SquareMatrix SQM(20); SQM << X; X = SQM.sym_submatrix(1,13); SQM.resize_keep(13); Y = X - SQM; Print(Y); SQM.resize_keep(13); Y = X - SQM; Print(Y); Y.resize(23,23); Y = 0; Y.sym_submatrix(1,13) = X; SQM.resize_keep(23,23); Y -= SQM; Print(Y); SQM.resize_keep(0); SQM.resize_keep(50); Print(SQM); X.resize(20,20); FillWithValues(MWC, X); SymmetricMatrix SM(20); SM << X; X = SM.sym_submatrix(1,13); SM.resize_keep(13); Y = X - SM; Print(Y); SM.resize_keep(13); Y = X - SM; Print(Y); Y.resize(23,23); Y = 0; Y.sym_submatrix(1,13) = X; SM.resize_keep(23); Y -= SM; Print(Y); SM.resize_keep(0); SM.resize_keep(50); Print(SM); X.resize(20,20); FillWithValues(MWC, X); LowerTriangularMatrix LT(20); LT << X; X = LT.sym_submatrix(1,13); LT.resize_keep(13); Y = X - LT; Print(Y); LT.resize_keep(13); Y = X - LT; Print(Y); Y.resize(23,23); Y = 0; Y.sym_submatrix(1,13) = X; LT.resize_keep(23); Y -= LT; Print(Y); LT.resize_keep(0); LT.resize_keep(50); Print(LT); X.resize(20,20); FillWithValues(MWC, X); UpperTriangularMatrix UT(20); UT << X; X = UT.sym_submatrix(1,13); UT.resize_keep(13); Y = X - UT; Print(Y); UT.resize_keep(13); Y = X - UT; Print(Y); Y.resize(23,23); Y = 0; Y.sym_submatrix(1,13) = X; UT.resize_keep(23); Y -= UT; Print(Y); UT.resize_keep(0); UT.resize_keep(50); Print(UT); X.resize(20,20); FillWithValues(MWC, X); DiagonalMatrix DM(20); DM << X; X = DM.sym_submatrix(1,13); DM.resize_keep(13); Y = X - DM; Print(Y); DM.resize_keep(13); Y = X - DM; Print(Y); Y.resize(23,23); Y = 0; Y.sym_submatrix(1,13) = X; DM.resize_keep(23); Y -= DM; Print(Y); DM.resize_keep(0); DM.resize_keep(50); Print(DM); X.resize(1,20); FillWithValues(MWC, X); RowVector RV(20); RV << X; X = RV.columns(1,13); RV.resize_keep(13); Y = X - RV; Print(Y); RV.resize_keep(13); Y = X - RV; Print(Y); Y.resize(1,23); Y = 0; Y.columns(1,13) = X; RV.resize_keep(1,23); Y -= RV; Print(Y); RV.resize_keep(0); RV.resize_keep(50); Print(RV); X.resize(20,1); FillWithValues(MWC, X); ColumnVector CV(20); CV << X; X = CV.rows(1,13); CV.resize_keep(13); Y = X - CV; Print(Y); CV.resize_keep(13); Y = X - CV; Print(Y); Y.resize(23,1); Y = 0; Y.rows(1,13) = X; CV.resize_keep(23,1); Y -= CV; Print(Y); CV.resize_keep(0); CV.resize_keep(50); Print(CV); } // cout << "\nEnd of seventh test\n"; }
template<class T> inline SymmetricMatrix<T,3> inertia_tensor_from_covariance(const SymmetricMatrix<T,3>& covariance) { return covariance.trace()-covariance; }}