void ColumnBundle::setSub(int colStart, const ColumnBundle& Y) { myassert(colStart>=0); myassert(colStart<nCols()); myassert(colLength()==Y.colLength()); int nColsSub = std::min(Y.nCols(), nCols()-colStart); callPref(eblas_copy)(dataPref()+colStart*colLength(), Y.dataPref(), nColsSub*colLength()); }
void SPxSolver::qualBoundViolation( Real& maxviol, Real& sumviol) const { maxviol = 0.0; sumviol = 0.0; DVector solu( nCols() ); getPrimal( solu ); for( int col = 0; col < nCols(); ++col ) { assert( lower( col ) <= upper( col )); Real viol = 0.0; if (solu[col] < lower( col )) viol = spxAbs( solu[col] - lower( col )); else if (solu[col] > upper( col )) viol = spxAbs( solu[col] - upper( col )); if (viol > maxviol) maxviol = viol; sumviol += viol; } }
void stfnum::Table::AppendRows(std::size_t nRows_) { std::size_t oldRows=nRows(); rowLabels.resize(oldRows+nRows_); values.resize(oldRows+nRows_); empty.resize(oldRows+nRows_); for (std::size_t nRow = 0; nRow < oldRows + nRows_; ++nRow) { values[nRow].resize(nCols()); empty[nRow].resize(nCols()); } }
// Matrix transpose Matrix Matrix::transpose() const { Matrix result( nCols(), nRows() ); for ( int i = 0; i < nRows(); i++ ) { for ( int j = 0; j < nCols(); j++ ) { result( j, i ) = getElement( i, j ); } } return( result ); }
// Called by other constructors to do the work void ColumnBundle::init(int nc, size_t len, const Basis *b, const QuantumNumber* q, bool onGpu) { ncols = nc; col_length = len; basis = b; qnum = q; if(nCols() == 0) { memFree(); return; } //must be default constructor or assignment to empty ColumnBundle myassert(colLength() != 0); memInit("ColumnBundle", nCols()*colLength(), onGpu); //in base class ManagedMemory }
// equality test bool Matrix::operator==( const Matrix& other ) const { // compare dimensions if ( nRows() != other.nRows() || nCols() != other.nCols() ) return false; // compare elements for ( int i = 0; i < nRows(); i++ ) { for ( int j = 0; j < nCols(); j++ ) { if ( getElement( i, j ) != other.getElement( i, j ) ) return false; } } return true; }
void SPxSolver::qualConstraintViolation(Real& maxviol, Real& sumviol) const { maxviol = 0.0; sumviol = 0.0; DVector solu( nCols() ); getPrimal( solu ); for( int row = 0; row < nRows(); ++row ) { const SVector& rowvec = rowVector( row ); Real val = 0.0; for( int col = 0; col < rowvec.size(); ++col ) val += rowvec.value( col ) * solu[rowvec.index( col )]; Real viol = 0.0; assert(lhs( row ) <= rhs( row )); if (val < lhs( row )) viol = spxAbs(val - lhs( row )); else if (val > rhs( row )) viol = spxAbs(val - rhs( row )); if (viol > maxviol) maxviol = viol; sumviol += viol; } }
double ln_gamma(int i, unifac_solution *s, double T) { if(nCols(s->dat->interactions) == 3) return _ln_gammac(i, s) + _ln_gammar(i, s, T); else return _ln_gammacElec(i, s) + _ln_gammarElec(i, s, T); }
// check if square bool Matrix::isSquare() const { if ( nRows() == nCols() ) return true; else return false; }
void SPxSolver::qualSlackViolation(Real& maxviol, Real& sumviol) const { maxviol = 0.0; sumviol = 0.0; DVector solu( nCols() ); DVector slacks( nRows() ); getPrimal( solu ); getSlacks( slacks ); for( int row = 0; row < nRows(); ++row ) { const SVector& rowvec = rowVector( row ); Real val = 0.0; for( int col = 0; col < rowvec.size(); ++col ) val += rowvec.value( col ) * solu[rowvec.index( col )]; Real viol = spxAbs(val - slacks[row]); if (viol > maxviol) maxviol = viol; sumviol += viol; } }
void matrix::print_real(FILE* fp, const char* fmt) const { const complex* thisData = this->data(); for(int i=0; i<nRows(); i++) { for(int j=0; j<nCols(); j++) fprintf(fp, fmt, thisData[index(i,j)].real()); fprintf(fp,"\n"); } }
void matrix::scan(FILE* fp, const char* fmt) { complex* thisData = this->data(); for(int i=0; i<nRows(); i++) { for(int j=0; j<nCols(); j++) { complex& c = thisData[index(i,j)]; fscanf(fp, fmt, &c.real(), &c.imag()); } } }
ColumnBundle ColumnBundle::getSub(int colStart, int colStop) const { myassert(colStart>=0); myassert(colStop<=nCols()); int nColsSub = colStop - colStart; myassert(nColsSub>0); ColumnBundle ret = this->similar(nColsSub); callPref(eblas_copy)(ret.dataPref(), dataPref()+colStart*colLength(), nColsSub*colLength()); return ret; }
void matrix::scan_real(FILE* fp) { complex* thisData = this->data(); for(int i=0; i<nRows(); i++) { for(int j=0; j<nCols(); j++) { complex& c = thisData[index(i,j)]; fscanf(fp, "%lg", &c.real()); c.imag() = 0; } } }
// matrix addition Matrix& Matrix::operator+=( const Matrix& other ) { // check dimensions assert( nRows() == other.nRows() && nCols() == other.nCols() ); // addition gsl_matrix_add( data, other.data ); return ( *this ); }
// matrix multiplication Matrix Matrix::operator*( const Matrix& other ) const { // check dimensions assert( nCols() == other.nRows() ); // matrix multiplication Matrix result( nRows(), other.nCols() ); gsl_linalg_matmult( data, other.data, result.data ); return result; }
// matrix addition Matrix Matrix::operator+( const Matrix& other ) const { // check dimensions assert( nRows() == other.nRows() && nCols() == other.nCols() ); // addition Matrix result( *this ); gsl_matrix_add( result.data, other.data ); return result; }
// get the row vector vector< double > Matrix::getRow( int row ) const { gsl_vector* v = gsl_vector_alloc( nCols() ); gsl_matrix_get_row( v, data, row ); //qcheng75 vector<double> ret=gsl2vector( v, 0); gsl_vector_free(v); return ret; //return gsl2vector( v ); }
/** * Store the values of a row in a matrix in a vector. * @param A Source matrix * @param row Number of the row to get values from. The first row is Row 0. * @returns A vector with length equal to the number of columns in A */ vector* ExtractRowAsVector(matrix *A, int row) { vector *v; int i; v = CreateVector(nCols(A)); for(i=0; i<len(v); i++) setvalV(v, i, val(A, row, i)); return v; }
bool GisBinFile::readCompressdRow(int row, char* charValues) { char nBytes; if(file_.get(nBytes)) { if((int) nBytes == 4 || (int) nBytes == 8) { setWordSize((int) nBytes); int totBytes = nCols() * dataSize_; gotoPos(row * nBytes + 1L); int rowSize; if((int) nBytes == 4) { int rowPtr, nextRowPtr; read(&rowPtr); read(&nextRowPtr); gotoPos(rowPtr); rowSize = nextRowPtr - rowPtr - 1; } else { off_t rowPtr, nextRowPtr; read(&rowPtr); read(&nextRowPtr); gotoPos(rowPtr); rowSize = nextRowPtr - rowPtr; } char compressFlag; file_.get(compressFlag); // if ( rowSize < totBytes ) if((compressFlag == 0x01) && ((rowSize - 1) < totBytes)) { unsigned int i = 0; while (i < totBytes) { char charCount; file_.get(charCount); char charValue; file_.get(charValue); unsigned int j = 0; for(j = i; j < i + (unsigned char) charCount; j++) charValues[j] = charValue; i += (unsigned char) charCount; } } else this->readNChar(charValues, totBytes); return true; } } return false; }
void matrix::diagonalize(matrix& evecs, diagMatrix& eigs) const { static StopWatch watch("matrix::diagonalize"); watch.start(); myassert(nCols()==nRows()); int N = nRows(); myassert(N > 0); //Check hermiticity const complex* thisData = data(); double errNum=0.0, errDen=0.0; for(int i=0; i<N; i++) for(int j=0; j<N; j++) { errNum += norm(thisData[index(i,j)]-thisData[index(j,i)].conj()); errDen += norm(thisData[index(i,j)]); } double hermErr = sqrt(errNum / (errDen*N)); if(hermErr > 1e-10) { logPrintf("Relative hermiticity error of %le (>1e-10) encountered in diagonalize\n", hermErr); stackTraceExit(1); } char jobz = 'V'; //compute eigenvectors and eigenvalues char range = 'A'; //compute all eigenvalues char uplo = 'U'; //use upper-triangular part matrix A = *this; //copy input matrix (zheevr destroys input matrix) double eigMin = 0., eigMax = 0.; //eigenvalue range (not used for range-type 'A') int indexMin = 0, indexMax = 0; //eignevalue index range (not used for range-type 'A') double absTol = 0.; int nEigsFound; eigs.resize(N); evecs.init(N, N); std::vector<int> iSuppz(2*N); int lwork = (64+1)*N; std::vector<complex> work(lwork); //Magic number 64 obtained by running ILAENV as suggested in doc of zheevr (and taking the max over all N) int lrwork = 24*N; std::vector<double> rwork(lrwork); //from doc of zheevr int liwork = 10*N; std::vector<int> iwork(liwork); //from doc of zheevr int info=0; zheevr_(&jobz, &range, &uplo, &N, A.data(), &N, &eigMin, &eigMax, &indexMin, &indexMax, &absTol, &nEigsFound, eigs.data(), evecs.data(), &N, iSuppz.data(), work.data(), &lwork, rwork.data(), &lrwork, iwork.data(), &liwork, &info); if(info<0) { logPrintf("Argument# %d to LAPACK eigenvalue routine ZHEEVR is invalid.\n", -info); stackTraceExit(1); } if(info>0) { logPrintf("Error code %d in LAPACK eigenvalue routine ZHEEVR.\n", info); stackTraceExit(1); } watch.stop(); }
void SPxBasis::Desc::dump() const { METHOD( "SPxBasis::Desc::dump()" ); int i; // Dump regardless of the verbosity level if this method is called. const SPxOut::Verbosity tmp_verbosity = spxout.getVerbosity(); spxout.setVerbosity( SPxOut::ERROR ); spxout << "DBDESC01 column status: "; for(i = 0; i < nCols(); i++) spxout << colStatus(i); spxout << std::endl; spxout << "DBDESC02 row status: "; for(i = 0; i < nRows(); i++) spxout << rowStatus(i); spxout << std::endl; spxout.setVerbosity( tmp_verbosity ); }
/** @note There will always be a BOUNDS section, even if there are no bounds. */ void SPxLP::writeMPS( std::ostream& p_output, ///< output stream. const NameSet* p_rnames, ///< row names. const NameSet* p_cnames, ///< column names. const DIdxSet* p_intvars) ///< integer variables. const { METHOD("writeMPS"); const char* indicator; char name [16]; char name1[16]; char name2[16]; bool has_ranges = false; int i; int k; // --- NAME Section --- p_output << "NAME MPSDATA" << std::endl; // --- ROWS Section --- p_output << "ROWS" << std::endl; for(i = 0; i < nRows(); i++) { if (lhs(i) == rhs(i)) indicator = "E"; else if ((lhs(i) > -infinity) && (rhs(i) < infinity)) { indicator = "E"; has_ranges = true; } else if (lhs(i) > -infinity) indicator = "G"; else if (rhs(i) < infinity) indicator = "L"; else throw SPxInternalCodeException("XMPSWR02 This should never happen."); writeRecord(p_output, indicator, getRowName(*this, i, p_rnames, name)); } writeRecord(p_output, "N", "MINIMIZE"); // --- COLUMNS Section --- p_output << "COLUMNS" << std::endl; bool has_intvars = (p_intvars != 0) && (p_intvars->size() > 0); for(int j = 0; j < (has_intvars ? 2 : 1); j++) { bool is_intrun = has_intvars && (j == 1); if (is_intrun) p_output << " MARK0001 'MARKER' 'INTORG'" << std::endl; for(i = 0; i < nCols(); i++) { bool is_intvar = has_intvars && (p_intvars->number(i) >= 0); if ( ( is_intrun && !is_intvar) || (!is_intrun && is_intvar)) continue; const SVector& col = colVector(i); int colsize2 = (col.size() / 2) * 2; assert(colsize2 % 2 == 0); for(k = 0; k < colsize2; k += 2) writeRecord(p_output, 0, getColName(*this, i, p_cnames, name), getRowName(*this, col.index(k), p_rnames, name1), col.value(k), getRowName(*this, col.index(k + 1), p_rnames, name2), col.value(k + 1)); if (colsize2 != col.size()) writeRecord(p_output, 0, getColName(*this, i, p_cnames, name), getRowName(*this, col.index(k), p_rnames, name1), col.value(k)); if (isNotZero(maxObj(i))) writeRecord(p_output, 0, getColName(*this, i, p_cnames, name), "MINIMIZE", -maxObj(i)); } if (is_intrun) p_output << " MARK0001 'MARKER' 'INTEND'" << std::endl; } // --- RHS Section --- p_output << "RHS" << std::endl; i = 0; while(i < nRows()) { Real rhsval1 = 0.0; Real rhsval2 = 0.0; for(; i < nRows(); i++) if ((rhsval1 = getRHS(lhs(i), rhs(i))) != 0.0) break; if (i < nRows()) { for(k = i + 1; k < nRows(); k++) if ((rhsval2 = getRHS(lhs(k), rhs(k))) != 0.0) break; if (k < nRows()) writeRecord(p_output, 0, "RHS", getRowName(*this, i, p_rnames, name1), rhsval1, getRowName(*this, k, p_rnames, name2), rhsval2); else writeRecord(p_output, 0, "RHS", getRowName(*this, i, p_rnames, name1), rhsval1); i = k + 1; } } // --- RANGES Section --- if (has_ranges) { p_output << "RANGES" << std::endl; for(i = 0; i < nRows(); i++) if ((lhs(i) > -infinity) && (rhs(i) < infinity)) writeRecord(p_output, "", "RANGE", getRowName(*this, i, p_rnames, name1), rhs(i) - lhs(i)); } // --- BOUNDS Section --- p_output << "BOUNDS" << std::endl; for(i = 0; i < nCols(); i++) { // skip variables that do not appear in the objective function or any constraint const SVector& col = colVector(i); if (col.size() == 0 && isZero(maxObj(i))) continue; if (lower(i) == upper(i)) { writeRecord(p_output, "FX", "BOUND", getColName(*this, i, p_cnames, name1), lower(i)); continue; } if ((lower(i) <= -infinity) && (upper(i) >= infinity)) { writeRecord(p_output, "FR", "BOUND", getColName(*this, i, p_cnames, name1)); continue; } if (lower(i) != 0.0) { if (lower(i) > -infinity) writeRecord(p_output, "LO", "BOUND", getColName(*this, i, p_cnames, name1), lower(i)); else writeRecord(p_output, "MI", "BOUND", getColName(*this, i, p_cnames, name1)); } if (has_intvars && (p_intvars->number(i) >= 0)) { // Integer variables have default upper bound 1.0, but we should write // it nevertheless since CPLEX seems to assume infinity otherwise. writeRecord(p_output, "UP", "BOUND", getColName(*this, i, p_cnames, name1), upper(i)); } else { // Continous variables have default upper bound infinity if (upper(i) < infinity) writeRecord(p_output, "UP", "BOUND", getColName(*this, i, p_cnames, name1), upper(i)); } } // --- ENDATA Section --- p_output << "ENDATA" << std::endl; // Output warning when writing a maximisation problem if(spxSense() == SPxLP::MAXIMIZE) { MSG_WARNING( spxout << "XMPSWR03 Warning: objective function inverted when writing maximization problem in MPS file format" << std::endl; ) }
bool GisBinFile::readCompressdRow(int row, float* floatValues) { char nBytes; if(file_.get(nBytes)) { if((int) nBytes == 4 || (int) nBytes == 8) { setWordSize((int) nBytes); int totBytes = nCols() * dataSize_; unsigned char* expandedValues = new unsigned char[totBytes]; gotoPos(row * nBytes + 1L); int rowSize; if((int) nBytes == 4) { int rowPtr, nextRowPtr; read(&rowPtr); read(&nextRowPtr); gotoPos(rowPtr); rowSize = nextRowPtr - rowPtr - 1; } else { off_t rowPtr, nextRowPtr; read(&rowPtr); read(&nextRowPtr); gotoPos(rowPtr); rowSize = nextRowPtr - rowPtr - 1; } char compressFlag; file_.get(compressFlag); if(compressFlag == 0x31) { unsigned char* charValues = new unsigned char[rowSize]; this->readNChar((char *) charValues, rowSize); if(!GisUncompress(rowSize, charValues, totBytes, expandedValues)) { delete[] charValues; return false; } delete[] charValues; } else this->readNChar((char *) expandedValues, totBytes); int i = 0; int j = 0; while (i < totBytes) { char fvalchar[4]; for(int k = 0; k < 4; k++) { if(swappMode_) fvalchar[3 - k] = expandedValues[i++]; else fvalchar[k] = expandedValues[i++]; } floatValues[j++] = *((float*) &fvalchar[0]); } delete[] expandedValues; return true; } } return false; }
inline T* end(int row) { return (T*)(addressOf0() + row*stride(0) + nCols()*stride(1)); }
inline const T* end(int row) const { return (const T*)(addressOf0() + row*stride(0) + nCols()*stride(1)); }
// get the row vector vector< double > Matrix::getRow( int row ) const { gsl_vector* v = gsl_vector_alloc( nCols() ); gsl_matrix_get_row( v, data, row ); return gsl2vector( v ); }