void MatrixHeatImaging(const DoubleMatrix &m, double minimum, double maximum, QPixmap &pixmap, int width, int height) { pixmap = QPixmap(QSize(width, height)); pixmap.fill(Qt::white); QPainter painter(&pixmap); unsigned int rows = m.rows(); unsigned int cols = m.cols(); for (unsigned int j=0; j<rows; j++) { for (unsigned int i=0; i<cols; i++) { double u = m.at(j,i); double ratio = 0.0; if (minimum!=maximum) ratio = 2.0 * (u-minimum) / (maximum - minimum); int b = int(MAX(0, 255*(1 - ratio))); int r = int(MAX(0, 255*(ratio - 1))); int g = 255 - b - r; QColor c(r, g, b); painter.setPen(c); painter.drawPoint(i,height-j-1); } } }
static DoubleMatrix funExactLambda_b(const DoubleMatrix &dvecAlp, const DoubleMatrix &dvecB, const DoubleMatrix &dvecY, const bool withD){ // Updated 1-8-01 Alyssa // fixed for const correctness DoubleMatrix term1(1, dvecB.nr()), term2(1, dvecB.nr()); const double *pdAlp = dvecAlp.data(); const double *pdB = dvecB.data(); const double *pdY = dvecY.data(); double *pdTerm1 = term1.data(); double *pdTerm2 = term2.data(); term1.fill(0.0); term2.fill(0.0); pdTerm1[0] = 1.0/pdB[0] -0.5 * (pow(pdB[0],-2.0)*pow((pdY[0]-pdAlp[1]-pdB[1]),2.0) +pow(pdB[0],-2.0)*pow((pdY[1]-pdAlp[1]-pdB[1]),2.0)); pdTerm1[1] = -(pdY[0]+pdY[1]-2.0*pdAlp[1]-2.0*pdB[1])/pdB[0] ; if( withD ){ //pdAns[0] = 1.0/pdB[0] - pow((1.0 - pdAlp[1] - pdB[1]*pdB[1]), 2.0) + pdB[0]/pdAlp[0]; //pdAns[1] = -2.0 * (1.0 - pdAlp[1] - pdB[1]) / pdB[0] + pdB[1]/pdAlp[0]; pdTerm2[0] = pdB[0]/pdAlp[0]; pdTerm2[1] = pdB[1]/pdAlp[0]; return add( term1, term2 ); } else{ return term1; } }
/*! * \brief assertSizeEqual Asserts that the two matrices * given as parameters are of equal size. * Throws an invalid argument exception if that is not the case. * \param A First matrix. * \param B Second matrix. */ void assertSizeEqual(const DoubleMatrix &A, const DoubleMatrix &B) { if(A.size() != B.size()) { throw std::invalid_argument("Matrices are not of equal size!"); } }
MatlabEngine::DoubleMatrix MatlabEngine::GetMatrix( const string& inExp ) { DoubleMatrix result; mxArray* ans = GetMxArray( inExp ); if( ans ) { int numDims = mxGetNumberOfDimensions( ans ); const int* dims = mxGetDimensions( ans ); if( numDims != 2 ) bcierr << "Can only handle two dimensions" << endl; result.resize( dims[ 0 ], vector<double>( dims[ 1 ] ) ); double* value = mxGetPr( ans ); if( value ) { int indices[] = { 0, 0 }; for( size_t i = 0; i < result.size(); ++i ) { indices[ 0 ] = i; for( size_t j = 0; j < result[ i ].size(); ++j ) { indices[ 1 ] = j; result[ i ][ j ] = value[ mxCalcSingleSubscript( ans, 2, indices ) ]; } } } mxDestroyArray( ans ); } return result; }
void TimeWarp::createCombinedMatrix(DoubleMatrix myChromaMatrix, DoubleVector energyVector, DoubleMatrix* chromaEnergyMatrix){ chromaEnergyMatrix->clear(); int sizeRatio = energyVector.size() / myChromaMatrix.size();// printf("COMBINE: size of my chroma is %i\n", (int) myChromaMatrix.size());// energyVector.size() / myChromaMatrix.size(); printf("COMBINED: size ratio of energy to chroma is %i \n", sizeRatio); int chromaSize = myChromaMatrix.size(); // printf("index is %i\n", index); for (int i = 0;i < energyVector.size();i++){ DoubleVector d; int index = min(chromaSize-1, (int) floor(i/sizeRatio)); for (int y = 0;y < 12;y++){ d.push_back(myChromaMatrix[index][y]);// } d.push_back(energyVector[i]); (*chromaEnergyMatrix).push_back(d); } printf("COMBINED: size of chroma energy is %i\n", (int)(*chromaEnergyMatrix).size()); // int x = (int)(*chromaEnergyMatrix).size()/3; // printf("energy[%i] %f \n", x, energyVector[x]); /* for (int y = 0;y < 13;y++){ printf("chroma[%i][%i] %f \n", x, y, myChromaMatrix[x/sizeRatio][y]); printf("c[%i][%i] %f \n", x, y, (*chromaEnergyMatrix)[x][y]); } printf("\n"); */ }
void TelluriumData::setData(const DoubleMatrix& theData) { mTheData = theData; mColumnNames.clear(); for (int k = 0; k < theData.getColNames().size(); ++k) { mColumnNames.add(theData.getColNames().at(k)); } RRPLOG(Logger::LOG_DEBUG) << "Simulation Data =========== \n" << mTheData; check(); }
void EXPECT_MATRIX_DOUBLE_EQ(DoubleMatrix A, DoubleMatrix B, string message = ""){ EXPECT_EQ(A.size(), B.size()) << "Number of lines is different. " << message; EXPECT_EQ(A[0].size(), B[0].size()) << "Number of columns is different. " << message; unsigned int N = A.size(); unsigned int M = A[0].size(); for (unsigned int i = 0; i < N; i++){ for (unsigned int j = 0; j < M; j++) EXPECT_FLOAT_EQ(A[i][j], B[i][j]) << message; } }
void StopRule::multiple (DoubleMatrix &mat1, DoubleVector &vec2, DoubleVector &proVec) { int row_, col_; proVec.resize(mat1.size()); for (row_ = 0; row_ < mat1.size (); row_ ++) { proVec[row_] = 0.0; for (col_ = 0; col_ < mat1[0].size(); col_ ++) proVec[row_] += mat1[row_][col_] * vec2[col_]; } }
double MatlabEngine::GetScalar( const string& inExp ) { DoubleMatrix result = GetMatrix( inExp ); if( result.empty() || result[ 0 ].empty() ) { bcierr << "Could not get scalar value \"" << inExp << "\"" << endl; result.resize( 1, vector<double>( 1 ) ); } return result[ 0 ][ 0 ]; }
// T^ijk = T^ijl M_lk Tensor Tensor::operator*(const DoubleMatrix & M) const { if (M.displayRows() != 3 || M.displayCols() !=3) { ostringstream ii; ii << "Tensor " << *this << " * matrix" << M << "of wrong size.\n"; throw ii.str(); } Tensor T; int i; for (i=1; i<=3; i++) T(i) = display(i) * M; return T; }
void lmWorker::calculateCovariance() { //Check if Hessain is invertible DoubleMatrix mat = mTheHost.mHessian.getValue(); ls::ComplexMatrix temp(mat); //Get a complex matrix from a double one. Imag part is zero ls::ComplexMatrix Inv = GetInverse(temp); DoubleMatrix temp2(mat.RSize(), mat.CSize()); temp2 = Inv; mTheHost.mCovarianceMatrix.setValue(temp2); }
void AkronItimesCTest::testAkronItimesCVal() { const int m = 2; const int n = 3; const int k = 1; int i; double seq[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30}; DoubleMatrix A(m,n); DoubleMatrix I = identity(n); DoubleMatrix C(n*n,k); std::copy(seq, seq+m*n, A.data()); std::copy(seq, seq+n*n*k, C.data()); DoubleMatrix AIC = AkronItimesC(A,I,C); CPPUNIT_ASSERT_EQUAL(m*n, AIC.nr()); CPPUNIT_ASSERT_EQUAL(k, AIC.nc()); DoubleMatrix ABC = AkronBtimesC(A,I,C); CPPUNIT_ASSERT_EQUAL(m*n, ABC.nr()); CPPUNIT_ASSERT_EQUAL(k, ABC.nc()); //std::cout << "ABC=" << ABC << std::endl; //std::cout << "AIC=" << AIC << std::endl; for( i = 0; i < m*n*k; i++ ) { CPPUNIT_ASSERT_EQUAL(ABC.data()[i], AIC.data()[i]); } }
// Does T^ijk = T^ljk M_li Tensor Tensor::product(const DoubleMatrix & M) const { if (M.displayRows() != 3 || M.displayCols() !=3) { ostringstream ii; ii << "Tensor " << *this << " * matrix" << M << "of wrong size.\n"; throw ii.str(); } Tensor T; int i,j,k,l; for (i=1; i<=3; i++) for (j=1; j<=3; j++) for (k=1; k<=3; k++) for (l=1; l<=3; l++) T(i, j, k) = T(i, j, k) + display(l, j, k) * M.display(l, i); return T; }
// T^kij = M_il T^klj Tensor operator*(const DoubleMatrix &M, const Tensor &T) { if (M.displayRows() !=3 || M.displayCols() != 3) { ostringstream ii; ii << "DoubleMatrix " << M << " * Tensor" << T << "of wrong size\n"; throw ii.str(); } Tensor temp; int i,j,k,l; for(k=1; k<=3; k++) for(i=1; i<=3; i++) for(j=1; j<=3; j++) for(l=1; l<=3; l++) temp(k, i, j) += M.display(i, l) * T.display(k, l, j); return temp; }
void StopRule::multiple (DoubleMatrix &mat1, DoubleMatrix &mat2, DoubleMatrix &proMat) { int row_, col_; //proMat.setLimit (mat1.getNRow (), mat2.getNCol ()); proMat.resize(mat1.size()); int nrow_ = proMat.size(); int ncol_ = mat2[0].size(); for (row_ = 0; row_ < proMat.size(); row_++) proMat[row_].resize(ncol_); for (row_ = 0; row_ < nrow_; row_ ++) for (col_ = 0; col_ < ncol_; col_ ++) { proMat[row_][col_] = 0.0; for (int count_ = 0; count_ < mat1[0].size(); count_ ++) { proMat[row_][col_] += mat1[row_][count_] * mat2[count_][col_]; // std::cout << mat1[row_][count_] << " --> " << mat2[count_][col_] << endl; } } }
void chartData(std::vector<double>& results, bool cluster, int subjectBond, DoubleMatrix data, long* maturities, int date_tolerance, int lower_date, int upper_date, int length){ DoubleMatrix lowerBonds; DoubleMatrix upperBonds; std::vector<double>timeSeries; upperAndLowerBonds(cluster, subjectBond, data, maturities, date_tolerance, lower_date, upper_date, upperBonds, lowerBonds); flyTimeSeries(results, lowerBonds, data.at(subjectBond), upperBonds, length); }
void HeatControl2Delta::write(const char *fileName, const DoubleMatrix& m) { FILE* f = fopen(fileName, "w"); for (unsigned int j=0; j<m.rows(); j++) { for (unsigned int i=0; i<m.cols(); i++) { if (i==0) fprintf(f, "%.10f", m[j][i]); else fprintf(f, " %.10f", m[j][i]); } fprintf(f, "\n"); } fclose(f); }
/*! * \brief fillMatrixWithZeros Fills matrix * with double-precision zeros. * \param matrix Matrix to fill. * \param size Requested matrices size after the initialization * (i.e. how many values to insert). */ void fillMatrixWithZeros(DoubleMatrix &matrix, unsigned int size) { for (unsigned int i = 0; i < size; ++i) { matrix.push_back(0.0); } }
/*! * \brief fillMatrixWithRandomValues Fills matrix * with pseudo-random double precision floating-point values. * \param matrix Matrix to fill. * \param size Requested matrices size after the initialization * (i.e. how many values to insert). */ void fillMatrixWithRandomValues(DoubleMatrix &matrix, unsigned int size) { for (unsigned int i = 0; i < size; ++i) { matrix.push_back(getRandomValue()); } }
int TimeWarp::getMinimumIndexOfRowFromMatrix(int j, DoubleMatrix& matrix){ int minimumIndex = 0; double minimumValue = 0; int width = matrix.size(); if (j >= 0 && width > 0 && j < matrix[0].size()){//&& i < matrix.size() //int height = (*matrix)[i].size(); int i = 0; minimumValue = matrix[i][j]; while (i < width){ if (matrix[i][j] < minimumValue){ minimumIndex = i; minimumValue = matrix[i][j]; }//end if new value i++; } }else{ printf("ERROR FROM GETTING MINIMIM!!! - zero - out of bounds, received %i and size is %i\n", j, (int)matrix.size()); } // printf("minimum row index is %i\n", minimumIndex); return minimumIndex; }
long _stdcall getChartData(int bond_id, bool cluster, double* arr, unsigned char bonds[], int numbonds, long* maturities, int microWidth, int dateTolerance, int clusterLower, int clusterUpper, int history){ SimpleRefData bbg(1); DoubleMatrix yieldData; std::vector <std::string> bond_vec; for (int b = 0; b < numbonds; b++){ std::string prefix = "/isin/"; std::string suffix; suffix.clear(); for (int j = 0; j < ISIN_LENGTH; j++){ char a = bonds[b + j*(numbonds + 1)]; suffix += a; } bond_vec.push_back((prefix + suffix).c_str()); } double** yieldArray; yieldArray = new double*[numbonds+1]; for (int i = 0; i <= numbonds; i++){ yieldArray[i] = new double[history]; } int days_data = bbg.runHistData(yieldArray, bond_vec, history); yieldData.resize(numbonds); for (int i = 0; i <= numbonds; i++){ yieldData[i].resize(days_data); } for (int i = 0; i <= numbonds; i++){ for (int j = 0; j < days_data; j++){ yieldData.at(i).at(j) = yieldArray[i][j]; } } std::vector<double> dataLocal; if (cluster){ chartData(dataLocal, cluster, bond_id, yieldData, maturities, 0, clusterLower, clusterUpper, days_data); } else{ chartData(dataLocal, cluster, bond_id, yieldData, maturities, dateTolerance, microWidth, microWidth, days_data); } for (int i = 0; i < days_data; i++){ arr[i] = dataLocal.at(i); } return days_data; }
/*------------------------------------------------------------------------ * Function definition *------------------------------------------------------------------------*/ static DoubleMatrix getElements( const DoubleMatrix &original, int offset, int howMany ) { DoubleMatrix dvecPart(howMany, 1); const double *pdOriginal = original.data(); double *pdPart = dvecPart.data(); for( int i=0; i<howMany; i++ ) pdPart[i] = pdOriginal[offset+i]; return dvecPart; }
double IFunctional::fx(const DoubleVector &pv) const { IFunctional* ifunc = const_cast<IFunctional*>(this); ifunc->fromVector(pv, ifunc->mParameter); //functional->mParameter.fromVector(prms); ifunc->forward->setParameter(mParameter); ifunc->backward->setParameter(mParameter); DoubleMatrix u; vector<ExtendedSpaceNode2D> info; ifunc->forward->calculateMVD(u, info, true); double intgrl = integral(u); u.clear(); return intgrl + regEpsilon*norm() + r*penalty(info); }
DoubleMatrix mult(DoubleMatrix& m2, ComplexMatrix& m1) { // Check dimensions unsigned int m1_nRows = m1.numRows(); unsigned int m2_nRows = m2.numRows(); unsigned int m1_nColumns = m1.numCols(); unsigned int m2_nColumns = m2.numCols(); if (m1.size() == 0) { return real(m1); } if (m2.size() == 0) { return m2; } DoubleMatrix result(m1_nRows, m2_nColumns); if (m1_nColumns == m2_nRows) { for (unsigned int row = 0; row < result.numRows(); row++) { for (unsigned int col = 0; col < m2_nColumns; col++) { double sum = 0.0; for (unsigned int k = 0; k < m1_nColumns; k++) { sum = sum + (real(m1[row][k]) * m2[k][col]); } result[row][col] = sum; } } return result; } if (m1_nRows == m2_nColumns) { return mult(m2, m1); } throw ("Incompatible matrix operands to multiply"); }
void run_ensemble(const char* fname, unsigned long seed) { try { RoadRunner r(fname); DoubleMatrix result = ensemble(r, 5, seed, 0, 10, 150); cout.precision(6); for(int row = 0; row < result.numRows(); ++row) { for(int col = 0; col < result.numCols(); ++col) { cout << result(row, col) << ", "; } cout << endl; } } catch (std::exception& e) { cout << "Error running ensemble: " << e.what() << endl; } }
// l labels the position the vector index goes in. // After that, indices are cyclic. Tensor outerProduct(const DoubleVector &V, const DoubleMatrix & M, int l) { Tensor temp; int i, j, k; for (i=1; i<=3; i++) for (j=1; j<=3; j++) for (k=1; k<=3; k++) { switch(l) { case 1: temp(i, j, k) = V.display(i) * M.display(j, k); break; case 2: temp(i, j, k) = V.display(j) * M.display(k, i); break; case 3: temp(i, j, k) = V.display(k) * M.display(i, j); break; default: ostringstream ii; ii << "Trying to outer product " << l << "th element of tensor"; throw ii.str(); break; } } return temp; }
// T^kij=M^kl T^lij Tensor Tensor::raise(const DoubleMatrix & M) const { Tensor temp; int i,k,j,l; for(k=1; k<=3; k++) for(i=1; i<=3; i++) for(j=1; j<=3; j++) for(l=1; l<=3; l++) temp(k, i, j) += M.display(k, l) * this -> display(l, i, j); return temp; }
void TimeWarp::calculateCausalChromaSimilarityMatrix(DoubleMatrix& firstChromaMatrix, DoubleMatrix& secondChromaMatrix, DoubleMatrix& simMatrix){ //calculates the chroma only similarity matrix //but we have already done some, so is extending it... int size = 0; if (simMatrix.size() > 0){ size = simMatrix[0].size(); } if (secondChromaMatrix.size() > size ){ for (int x = 0;x < firstChromaMatrix.size();x++){ if (x < simMatrix.size()){ //i.e. entries already exist for (int y = (int)simMatrix[x].size();y < secondChromaMatrix.size();y++){ double distance; if (useDotProduct) distance = getChromaSimilarity(x, y, &firstChromaMatrix, &secondChromaMatrix); else distance = getEuclideanDistance(x, y, &firstChromaMatrix, &secondChromaMatrix); printf("putting one back X %i Y %i dist %f \n", x, y, distance); simMatrix[x].push_back(distance); } } else { DoubleVector d; for (int y = 0;y < secondChromaMatrix.size();y++){ double distance; if (useDotProduct) distance = getChromaSimilarity(x, y, &firstChromaMatrix, &secondChromaMatrix); else distance = getEuclideanDistance(x, y, &firstChromaMatrix, &secondChromaMatrix); printf("new row X %i Y %i dist %f\n", x, y, distance); d.push_back( distance); } simMatrix.push_back(d); } } } if (size > 0) printf("Causial CHROMA ONLY SIM SIZE %i x %i; ", (int)simMatrix.size(), (int) simMatrix[0].size()); printf("First matrix SIZE %i , SECOND size %i\n", (int)firstChromaMatrix.size(), (int) secondChromaMatrix.size()); }
void StopRule::multiple (DoubleVector &vec1, DoubleVector &vec2, DoubleMatrix &proMat) { int row_, col_; proMat.resize(vec1.size()); for (row_ = 0; row_ < vec1.size(); row_++) proMat[row_].resize(vec2.size()); for (row_ = 0; row_ < vec1.size(); row_ ++) for (col_ = 0; col_ < vec2.size(); col_ ++) proMat[row_][col_] = vec1[row_] * vec2[col_]; }
/*! * \brief sumMatrices Sums the two matrices given as parameters in parallel. * Spawns threadCount threads for this operation. * \param A First matrix - a vector of double precision floating-point values. * \param B Second matrix. * \param threadCount Number of threads to spawn in the parallel OpenMP block. * \return Third matrix - the result of addition of A and B matrices. */ const DoubleMatrix *sumMatrices(const DoubleMatrix &A, const DoubleMatrix &B, unsigned int threadCount) { assertSizeEqual(A, B); DoubleMatrix *C = new DoubleMatrix(); fillMatrixWithZeros(*C, A.size()); #pragma omp parallel for default(none) shared(A, B, C) num_threads(threadCount) for (unsigned int k = 0; k < 10000; ++k) { for (unsigned int i = 0; i < A.size(); i++) { C->at(i) = A.at(i) + B.at(i); } } return C; }