TEST(ComplexMatrixIOTest,OneByOneMatirx) { ComplexMatrix cm(1,1); cm(0,0).real = 1.0; cm(0,0).imag = 0.5; complexMatrixToBytes(cm,"cm.bin"); ComplexMatrix cm2; bytesToComplexMatrix(cm2, "cm.bin"); EXPECT_EQ(cm.GetRows(), cm2.GetRows()); EXPECT_EQ(cm.GetCols(), cm2.GetCols()); for (int i=0; i<cm.GetRows(); ++i) { for (int j=0; j<cm.GetCols(); ++j) { EXPECT_DOUBLE_EQ(cm(i,j).real, cm2(i,j).real); EXPECT_DOUBLE_EQ(cm(i,j).imag, cm2(i,j).imag); } } }
ChanMappingController::ChanMappingController(QObject *parent) : QObject(parent), currentMode(DAQ::AI60Demux), is_dual(false) { if (!setupDefaults) { setupDefaults = true; // run once -- setup defaults array for (int i = 0; i < (int)DAQ::N_Modes; ++i) { ChanMap & cm (defaultMapping[i]); const unsigned numIntans = DAQ::ModeNumIntans[i], numChansPerIntan = DAQ::ModeNumChansPerIntan[i]; cm.reserve(numIntans*numChansPerIntan); for (int j = 0; j < (int)numIntans; ++j) for (int k = 0; k < (int)numChansPerIntan; ++k) cm.push_back(defaultMappingForIntan(j, k, numChansPerIntan)); ChanMap & cm2 (defaultMapping2[i]); const unsigned numIntans2 = DAQ::ModeNumIntans[i]*2; cm2.reserve(numIntans2*numChansPerIntan); for (int j = 0; j < (int)numIntans2; ++j) for (int k = 0; k < (int)numChansPerIntan; ++k) cm2.push_back(defaultMappingForIntan(j, k, numChansPerIntan)); } } dialogParent = new QDialog(0); dialog = new Ui::ChanMapping; dialog->setupUi(dialogParent); Connect(dialog->loadBut, SIGNAL(clicked()), this, SLOT(loadButPushed())); Connect(dialog->saveBut, SIGNAL(clicked()), this, SLOT(saveButPushed())); resetFromSettings(); }
TEST(ComplexMatrixIOTest,ThousandByThousandMatirx) { ComplexMatrix cm(1000,1000); RandomNumberGenerator random; for (int i=0; i<cm.GetRows(); ++i) { for (int j=0; j<cm.GetCols(); ++j) { cm(i,j) = random.randomComplex(); } } complexMatrixToBytes(cm,"cm.bin"); ComplexMatrix cm2; bytesToComplexMatrix(cm2, "cm.bin"); EXPECT_EQ(cm.GetRows(), cm2.GetRows()); EXPECT_EQ(cm.GetCols(), cm2.GetCols()); for (int i=0; i<cm.GetRows(); ++i) { for (int j=0; j<cm.GetCols(); ++j) { EXPECT_DOUBLE_EQ(cm(i,j).real, cm2(i,j).real); EXPECT_DOUBLE_EQ(cm(i,j).imag, cm2(i,j).imag); } } }
int main(void) { cout << "main by kk. Last updated 20.09.2011\n"; /////////////////////////////////////////////////////// int it1[] = {84, 23, 84, 21, 120, 93, -131, 238}; int it2[] = {249, 24, 82, 3}; int it3[] = {439, 828, 39, 230, 95, 1, -242, 34}; aghMatrix<int>* imptr1 = new aghMatrix<int>(2, 4); aghMatrix<int>* imptr2 = new aghMatrix<int>(4, 1); aghMatrix<int>* imptr3 = new aghMatrix<int>(4, 1); aghMatrix<int>* imptr4 = new aghMatrix<int>(2, 4); aghMatrix<int> imtest1, imtest2, imtest3; imtest1.setItems(4, 1, 249, 24, 82, 3); imtest2.setItems(2, 4, 500+23, 851, 123, 251, 215, 94, -373, 272); imtest3.setItems(2, 1, 28419, 22084); aghMatrix<int>& imref1 = *imptr1; aghMatrix<int>& imref2 = *imptr2; aghMatrix<int>& imref3 = *imptr3; aghMatrix<int>& imref4 = *imptr4; imptr1->setItems(it1); imptr2->setItems(it2); imref3 = imref2; imptr4->setItems(it3); // 1st test - operator przypisania showTestResult(1, imref3 == imtest1); // 2nd test - operator przypisania try { imref3 = imref3; showTestResult(2, imref3 == imref2); } catch(...) { showTestResult(2, false); } // 3rd test - jawne wywolanie destruktora imref2.~aghMatrix(); try { showTestResult(3, imref3 != imref2); } catch(...) { showTestResult(3, false); } // 4th test - destruktor try { delete imptr2; showTestResult(4, true); } catch(...) { showTestResult(4, false); } // 5th test - kopiowanie wartosci showTestResult(5, imref3 == imtest1); // 6th test - operator dodawania try { showTestResult(6, (imref1+imref4) == imtest2); } catch(...) { showTestResult(6, false); } // 7th test - operator mnożenia aghMatrix<int> immulres; try { aghMatrix<int> immultmp = imref1*imref3; immulres = immultmp; } catch(...) { showTestResult(7, false); } showTestResult(7, immulres == imtest3); // 8th test - range test try { immulres(10, 11); immulres(10, -11); } catch(aghException& e) { showTestResult(8, true); } catch(...) { showTestResult(8, false); } // 9th test - dimesion test try { imref1+imref3; } catch(aghException& e) { showTestResult(9, true); } catch(...) { showTestResult(9, false); } // 10th test - dimesion test try { imref1*imref4; } catch(aghException& e) { showTestResult(10, true); } catch(...) { showTestResult(10, false); } ///////////////////////////////////////////////////// char ct[] = {'b', 'k', 'l', 'd', 'h', 'z', 'd', 'j', 'z', 'x', 'c', 'n', 'b', 'k', 'z', 'd', 'w'}; aghMatrix<char> cm1(3, 2); aghMatrix<char> cm2(3, 2); aghMatrix<char> cm3(2, 4); cm1.setItems(ct); cm2.setItems(ct+5); cm3.setItems(ct+8); aghMatrix<char> cmtest1, cmtest2; cmtest1.setItems(3, 2, 'a', 'n', 'u', 'c', 'e', 'b'); cmtest2.setItems(3, 4, 'j', 't', 's', 'r', 's', 'x', 't', 'w', 's', 'v', 'p', 'k'); // 11th test - operator dodawania try { showTestResult(11, (cm1+cm2) == cmtest1); } catch(...) { showTestResult(11, false); } // 12th test - operator mnożenia try { showTestResult(12, (cm1*cm3) == cmtest2); } catch(...) { showTestResult(12, false); } ///////////////////////////////////////////////////// char* cpt[] = {"to", "jest", "tablica", "wyrazow", "o", "rozmiarze", "jedenascie", "ktore", "beda", "elementami", "macierzy"}; aghMatrix<char*> cpm1(2, 3); aghMatrix<char*> cpm2(2, 3); aghMatrix<char*> cpm3(3, 1); aghMatrix<char*> cpm4; aghMatrix<char*> cpm5; aghMatrix<char*>* cpmptr1 = new aghMatrix<char*>(1, 1); aghMatrix<char*> cpmtest1, cpmtest2; cpm1.setItems(cpt); cpm2.setItems(cpt+5); cpm3.setItems(cpt+3); cpmptr1->setItem(0, 0, cpt[5]); cpmtest1.setItems(2, 3, "torzmiae", "jestdnaci", "tablickore", "wyrazobed", "oelmntai", "rozmiaecy"); cpmtest2.setItems(2, 1, "oai", "wyrazomie"); // 13th test - destruktor char* itemptr = cpmptr1->operator()(0,0); itemptr[0] = 'a'; delete cpmptr1; showTestResult(13, strcmp(itemptr, cpt[5]) != 0); // 14th test - operator przypisania p1 cpm5 = cpm4 = cpm3; showTestResult(14, (cpm3 == cpm4) && (cpm3 == cpm5)); // 15th test - operator przypisania p2 cpm5.setItem(0, 0, "nothing"); showTestResult(15, (cpm3 == cpm4) && (cpm3 != cpm5) && (strcmp(cpm3(0,0), "wyrazow") == 0)); // 16th test - operator przypisania cpm5 = cpm3; try { cpm5 = cpm5; showTestResult(16, cpm3 == cpm5); } catch(...) { showTestResult(16, false); } // 17th test - jawne wywolanie destruktora cpm5 = cpm3; cpm5.~aghMatrix(); try { showTestResult(17, (cpm3 != cpm5)); } catch(...) { showTestResult(17, false); } // 18th test - operator dodawania try { showTestResult(18, (cpm1+cpm2) == cpmtest1); } catch(...) { showTestResult(18, false); } // 19th test - operator mnożenia try { showTestResult(19, (cpm1*cpm3) == cpmtest2); } catch(...) { showTestResult(19, false); } ///////////////////////////////////////////////////// aghComplex cmplxt1[] = {aghComplex(1,1), aghComplex(1,2), aghComplex(2,1), aghComplex(2,2), aghComplex(3,3), aghComplex(3,1), aghComplex(1,0), aghComplex(0,1)}; aghComplex cmplxt2[] = {aghComplex(-1,2), aghComplex(0,3), aghComplex(2,6), aghComplex(3,2), aghComplex(3,4)}; aghMatrix<aghComplex> cmplxm1(3, 2); aghMatrix<aghComplex> cmplxm2(2, 1); aghMatrix<aghComplex> cmplxm3(2, 1); aghMatrix<aghComplex> cmplxm4(2, 1); aghMatrix<aghComplex> cmplxmtest1(3, 1); aghMatrix<aghComplex> cmplxmtest2(2, 1); cmplxm1.setItems(cmplxt1); cmplxm2.setItems(cmplxt1+6); cmplxm3.setItems(cmplxt1+3); cmplxmtest1.setItems(cmplxt2); cmplxmtest2.setItems(cmplxt2+3); cmplxm4 = cmplxm2 + cmplxm3; // 20th test - operator dodawania try { showTestResult(20, cmplxm4 == cmplxmtest2); } catch(...) { showTestResult(20, false); } // 21th test - operator mnozenia try { showTestResult(21, (cmplxm1*cmplxm2) == cmplxmtest1); } catch(...) { showTestResult(21, false); } ///////////////////////////////////////////////////// cout << "Finally, this is the end...\n"; return 0; }
template<typename Scalar> void sparse_solvers(int rows, int cols) { double density = (std::max)(8./(rows*cols), 0.01); typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; typedef Matrix<Scalar,Dynamic,1> DenseVector; // Scalar eps = 1e-6; DenseVector vec1 = DenseVector::Random(rows); std::vector<Vector2i> zeroCoords; std::vector<Vector2i> nonzeroCoords; // test triangular solver { DenseVector vec2 = vec1, vec3 = vec1; SparseMatrix<Scalar> m2(rows, cols); DenseMatrix refMat2 = DenseMatrix::Zero(rows, cols); // lower - dense initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeLowerTriangular, &zeroCoords, &nonzeroCoords); VERIFY_IS_APPROX(refMat2.template triangularView<Lower>().solve(vec2), m2.template triangularView<Lower>().solve(vec3)); // upper - dense initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeUpperTriangular, &zeroCoords, &nonzeroCoords); VERIFY_IS_APPROX(refMat2.template triangularView<Upper>().solve(vec2), m2.template triangularView<Upper>().solve(vec3)); VERIFY_IS_APPROX(refMat2.conjugate().template triangularView<Upper>().solve(vec2), m2.conjugate().template triangularView<Upper>().solve(vec3)); { SparseMatrix<Scalar> cm2(m2); //Index rows, Index cols, Index nnz, Index* outerIndexPtr, Index* innerIndexPtr, Scalar* valuePtr MappedSparseMatrix<Scalar> mm2(rows, cols, cm2.nonZeros(), cm2.outerIndexPtr(), cm2.innerIndexPtr(), cm2.valuePtr()); VERIFY_IS_APPROX(refMat2.conjugate().template triangularView<Upper>().solve(vec2), mm2.conjugate().template triangularView<Upper>().solve(vec3)); } // lower - transpose initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeLowerTriangular, &zeroCoords, &nonzeroCoords); VERIFY_IS_APPROX(refMat2.transpose().template triangularView<Upper>().solve(vec2), m2.transpose().template triangularView<Upper>().solve(vec3)); // upper - transpose initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeUpperTriangular, &zeroCoords, &nonzeroCoords); VERIFY_IS_APPROX(refMat2.transpose().template triangularView<Lower>().solve(vec2), m2.transpose().template triangularView<Lower>().solve(vec3)); SparseMatrix<Scalar> matB(rows, rows); DenseMatrix refMatB = DenseMatrix::Zero(rows, rows); // lower - sparse initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeLowerTriangular); initSparse<Scalar>(density, refMatB, matB); refMat2.template triangularView<Lower>().solveInPlace(refMatB); m2.template triangularView<Lower>().solveInPlace(matB); VERIFY_IS_APPROX(matB.toDense(), refMatB); // upper - sparse initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeUpperTriangular); initSparse<Scalar>(density, refMatB, matB); refMat2.template triangularView<Upper>().solveInPlace(refMatB); m2.template triangularView<Upper>().solveInPlace(matB); VERIFY_IS_APPROX(matB, refMatB); // test deprecated API initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeLowerTriangular, &zeroCoords, &nonzeroCoords); VERIFY_IS_APPROX(refMat2.template triangularView<Lower>().solve(vec2), m2.template triangularView<Lower>().solve(vec3)); } }
void resultCombiner(const char * name_dv1, const char * name_cm1, const int length1, const char * name_dv2, const char * name_cm2, const int length2, int ftr = 1, const char * outputNameStub = "output") { TVectorD dv1(length1), dv2(length2); TMatrixT<double> cm1(length1, length1), cm2(length2, length2); double binLimits1[length1][2], binLimits2[length2][2], ccCov1[length1], ccCov2[length1]; readDataVector(name_dv1, dv1, binLimits1, ftr, ccCov1); printf("Read data vector 1 (%d)\n",length1); readDataVector(name_dv2, dv2, binLimits2, ftr, ccCov2); printf("Read data vector 2 (%d)\n",length2); readCovMatrix(name_cm1, cm1); printf("Read covariance matrix 1\n"); readCovMatrix(name_cm2, cm2); printf("Read covariance matrix 2\n"); std::vector<double*> binLimits; std::vector<std::vector<int > > preU; int i1 = 0, i2 = 0; while(i1 < length1 || i2 < length2) { if(i1 < length1 && i2 < length2) { if((binLimits1[i1][1] + binLimits1[i1][0])/2 > binLimits2[i2][0] && (binLimits1[i1][1] + binLimits1[i1][0])/2 < binLimits2[i2][1]) { binLimits.push_back(binLimits1[i1]); std::vector<int> tmp; tmp.push_back(i1); tmp.push_back(i2); preU.push_back(tmp); i1++; i2++; } else if((binLimits1[i1][1] + binLimits1[i1][0])/2 <= binLimits2[i2][0]) { binLimits.push_back(binLimits1[i1]); std::vector<int> tmp; tmp.push_back(i1); tmp.push_back(-1); preU.push_back(tmp); i1++; } else { binLimits.push_back(binLimits2[i2]); std::vector<int> tmp; tmp.push_back(-1); tmp.push_back(i2); preU.push_back(tmp); i2++; } } else if(i1 < length1 && i2 >= length2) { binLimits.push_back(binLimits1[i1]); std::vector<int> tmp; tmp.push_back(i1); tmp.push_back(-1); preU.push_back(tmp); i1++; } else { binLimits.push_back(binLimits2[i2]); std::vector<int> tmp; tmp.push_back(-1); tmp.push_back(i2); preU.push_back(tmp); i2++; } } TVectorD dv(length1 + length2); for(int i = 0; i < length1 + length2; i++) { dv[i] = (i < length1) ? dv1[i] : dv2[i - length1]; } TMatrixT<double> cm(length1 + length2, length1 + length2), U(length1 + length2, preU.size()); for(int i = 0; i < length1; i++) { for(int j = 0; j < length1; j++) { cm[i][j] = cm1[i][j]; } } for(int i = length1; i < length1 + length2; i++) { for(int j = length1; j < length1 + length2; j++) { cm[i][j] = cm2[i - length1][j - length1]; } } for(unsigned int i = 0; i < preU.size(); i++) { if(preU[i][0] >= 0) U[preU[i][0]][i] = 1; if(preU[i][1] >= 0) U[preU[i][1] + length1][i] = 1; if(ftr > 1 && preU[i][0] >= 0 && preU[i][1] >= 0) cm[preU[i][0]][preU[i][1] + length1] = cm[preU[i][1] + length1][preU[i][0]] = ccCov1[preU[i][0]]*ccCov2[preU[i][1]]; } // cm.Print(); TMatrixT<double> Ut(U); Ut.T(); TMatrixT<double> cmInv(cm); cmInv.Invert(); TMatrixT<double> step1 = Ut * cmInv * U; TMatrixT<double> step2 = Ut * cmInv; TMatrixT<double> lambda = step1.Invert() * step2; TVectorD bV = lambda*dv; TMatrixT<double> bcm = (Ut * cmInv * U).Invert(); printf("Done with combination.\n"); //write output FILE *file; char bVoutName[128], CMoutName[128]; sprintf(bVoutName, "%s_data.txt", outputNameStub); file = fopen(bVoutName, "w"); if(file) { fprintf(file, "#\n#%9s %9s %9s %15s %15s\n", "Bin", "Y_min", "Y_max", "Value", "Uncertainty"); for(int i = 0; i < bV.GetNoElements(); i++) { fprintf(file, " %9i %9.2f %9.2f %15e %15e\n", i + 1, binLimits[i][0], binLimits[i][1], bV[i], sqrt(bcm[i][i])); } fclose(file); } sprintf(CMoutName, "%s_covMat.txt", outputNameStub); file = fopen(CMoutName, "w"); if(file) { fprintf(file, "#\n#%9s %9s %15s\n", "Bin i", "Bin j", "Value"); for(int i = 0; i < bcm.GetNrows(); i++) { for(int j = 0; j < bcm.GetNcols(); j++) { fprintf(file, " %9i %9i %15e\n", i + 1, j + 1, bcm[i][j]); } } fclose(file); } printf("Output complete.\n"); }