int main(int argc, char* argv[]) { srand((unsigned)time(0)); Vector dVec(5); Matrix m(3, 3); m(0, 0) = -1.0; m(0, 1) = 1.0; m(0, 2) = 2.0; m(1, 0) = 2.0; m(1, 1) = 3.0; m(1, 2) = 3.0; m(2, 0) = 4.0; m(2, 1) = 2.0; m(2, 2) = 1.0; m.print(); dVec.resize(3); dVec[0] = 0.78045432; dVec[1] = 0.27960367; dVec[2] = 0.55920734; Vector d2(3); d2 = dVec*m; d2.print(); std::cout << "\n\n\n"; d2[0] = 3.5765; d2[1] = 2.7377; d2[2] = 2.9590; m = outer(dVec, d2); std::cout << "\n\n outer product \n\n"; m.print(); std::cout << "\n\n\n"; dVec.print(); dVec.resize(20); for (int i = 0; i < 20; i++){ dVec[i] = rand()%100 + 1; } dVec.print(); dVec.sort(); dVec.print(); int dim = 5; double PRECISION = 1e-12; Vector v(dim); v[0] = 0.0; v[1] = 5.4; v[2] = 3.2; v[3] = 0.0; v[4] = 4.1; Matrix P(dim, dim, 0.0); for (int j = 0; j < dim; j++) { P(j, j) = 1.0; } // Turn into identity int k= dim-1; // Keep track of last non-zero entry for (int j = dim-1; j > -1; j--){ // Start at bottom and work up if (fabs(v(j))< PRECISION && k!=j ){ std::cout << "\n Found a zero. \n"; // And swap the two rows P.swapRows(j, k); k--; } } std::cout << "\n\n"; P.print(); v = P*v; std::cout << "\n\n"; v.print(); return 0; }
// ----------------------------------------------------- // --------------- TEST: Numeric Class --------------- // ----------------------------------------------------- // --------------- TEST1: sort_index ------------- // ----------------------------------------------------- bool sort_indexTest(){ int data[] = {3, 8, 5}; std::vector<double> dVec(data, data + sizeof(data) / sizeof(int)); std::vector<double> sorted; std::vector<size_t> res_index; res_index = NumericUtil::sort_index(dVec, sorted); //std::for_each(res_index.begin(), res_index.end(), std::cout << _1 << " "); //std::cout << std::endl; //std::for_each(sorted.begin(), sorted.end(), std::cout << _1 << " "); //std::cout << std::endl; return true; }
// ----------------------------------------------------- // --------------- TEST: BasicUtil Class --------------- // ----------------------------------------------------- // --------------- TEST1: minValueIndex ------------- // ----------------------------------------------------- bool minValueIndexTest(){ int data[] = {3, 2, 1, 5, 4, 2, 1, 0, 7}; std::vector<int> dVec(data, data + sizeof(data) / sizeof(int)); std::vector<int>::iterator result = std::min_element(dVec.begin(), dVec.end()); long index = std::distance(dVec.begin(), result); //std::cout << "The min element is at: " << *result << " at: " << index << std::endl; if (index != 7) { return false; } return true; }
// Interfacing candidates with an Eigen matrix of double x and a vector of objective function value. void to_mat_vec(std::vector<Candidate> &cp, dMat &x, dVec &fvalues, const bool &train) { if (train) std::sort(cp.begin(),cp.end(), [](Candidate const &c1, Candidate const &c2){return c1.get_fvalue() > c2.get_fvalue();}); // descending sort x = dMat(cp.at(0).get_x_size(),cp.size()); fvalues = dVec(cp.size()); for (int i=0;i<(int)cp.size();i++) { x.col(i) = cp.at(i).get_x_dvec().transpose(); fvalues(i) = cp.at(i).get_fvalue(); } }