Exemplo n.º 1
0
	/** The method guarantees that the physical dimensions will not shrink.
	* Moreover, if growth is necessary, it will be in geometrically increasing steps.
	*/
	void AutoMatrix::upSize ( const size_t rows, const size_t cols ) {
		const size_t
			physicalRows = rows <= countRows() ? countRows() : upperPowerOf2( rows ),
			physicalCols = cols <= matrix.tda ? matrix.tda : upperPowerOf2( cols );
		exactSize( physicalRows, physicalCols );
		gslInit( rows, cols, matrix.data, matrix.tda );
	}
Exemplo n.º 2
0
	Vector Matrix::superdiagonalVector ( const size_t offset ) {
		// To circumvent GSL limitation to allow 0-dimensional vectors
		// This use of static data relies on the immutability of 0-dim Vectors.
		static Vector nullDimVector( 0, NULL, 0 );
		if ( 0 == countRows() ) return nullDimVector;
		return Vector( gsl_matrix_superdiagonal( &matrix, offset ) );
	}
Exemplo n.º 3
0
	Matrix Matrix::subMatrix ( const size_t row, const size_t col, const size_t rows, const size_t cols ) {
		// Circumvent GSL limitation to allow 0-row and/or 0-column matrices
		if ( 0 == rows || 0 == cols ) {
			assert( row + rows <= countRows() );
			assert( col + cols <= countColumns() );
			return Matrix( rows, cols, matrix.data + row * matrix.tda + col, matrix.tda );
		} else {
			return Matrix( gsl_matrix_submatrix( &matrix, row, col, rows, cols ) );
		}
	}
Exemplo n.º 4
0
	/** Note that GSL has rows and columns in C convention ordering. */
	bool Matrix::gslInit ( const size_t rows, const size_t cols, double *base, const size_t tda ) {
		bool invalidates
			= base != matrix.data
			|| rows < countRows()
			|| cols < countColumns()
			|| ( tda != matrix.tda && 1 < countRows() );
		// Circumvent GSL limitation to allow 0-row and/or 0-column matrices
		if ( 0 == rows || 0 == cols ) {
			assert( 0 <= rows );
			assert( 0 <= cols );
			assert( cols <= tda );
			matrix.data = base;
			matrix.size1 = rows;
			matrix.size2 = cols;
			matrix.tda = tda;
		} else {
			*static_cast<gsl_matrix_view*>( this ) = gsl_matrix_view_array_with_tda( base, rows, cols, tda );
		}
		return invalidates;
	}
Exemplo n.º 5
0
	void AutoMatrix::removeRow ( const size_t row ) {
		const size_t
			rows = countRows(),
			cols = countColumns();
		for ( size_t i = row + 1; i < rows; ++i ) {
			const Vector rowSource = rowVector( i );
			Vector rowTarget = rowVector( i - 1 );
			rowTarget.copy( rowSource );
		}
		upSize( rows - 1, cols );
	}
Exemplo n.º 6
0
	void AutoMatrix::removeColumn ( const size_t col ) {
		const size_t
			rows = countRows(),
			cols = countColumns();
		for ( size_t i = col + 1; i < cols; ++i ) {
			const Vector colSource = columnVector( i );
			Vector colTarget = columnVector( i - 1 );
			colTarget.copy( colSource );
		}
		upSize( rows, cols - 1 );
	}
Exemplo n.º 7
0
	void AutoMatrix::exactSize ( const size_t rows, const size_t cols ) {
		const size_t required = calculateSize( rows, cols );
		double *newArray;
		if ( required == size ) {	// Shortcut: no malloc necessary
			if ( cols <= matrix.tda ) {	// move towards begin
				const size_t currentRows = countRows();
				const size_t blockSize = cols * sizeof( double );
				const double *source = matrix.data;
				double *target = matrix.data;
				for ( size_t row = 0; ++row < currentRows; ) {
					// For row = 0, no copy is needed, so the pre-increment of row and pointers is what we want
					memmove( target += cols, source += matrix.tda, blockSize );
				}
			} else {	// move towards end requires opposite loop direction (new cols bigger, so new rows smaller)
				const size_t currentCols = countColumns();
				const size_t blockSize = currentCols * sizeof( double );
				const double *source = &matrix.data[ matrix.tda * rows ];
				double *target = &matrix.data[ rows * cols ];
				// Guard against unsigned underflow
				if ( 0 < rows ) {
					for ( size_t row = rows; --row >= 1; ) {
						memmove( target -= cols, source -= matrix.tda, blockSize );
					}
				}
			}
			newArray = matrix.data;
		} else {
			newArray = static_cast<double*>( malloc( required ) );
			assert( NULL != newArray );
			const size_t copyRows = min( rows, countRows() );
			const size_t blockSize = min( cols, countColumns() ) * sizeof( double );
			for ( size_t row = 0; row < copyRows; ++row ) {
				memcpy( &newArray[ cols * row ], &matrix.data[ matrix.tda * row ], blockSize );
			}
			free( matrix.data );
			size = required;
		}
		gslInit( rows, cols, newArray, cols );	// update GSL struct variables
	}
Exemplo n.º 8
0
	void Matrix::fill ( const double *array ) {
		const size_t cols = countColumns();
		const size_t rows = countRows();
		if ( matrix.tda == cols || 1 >= rows ) {	// packed is copied as one lump
			const size_t blockSize = rows * cols * sizeof( double );
			memcpy( matrix.data, array, blockSize );
		} else {
			const size_t blockSize = cols * sizeof( double );
			double *target = matrix.data;
			for ( size_t row = 0; row < rows; ++row ) {
				memcpy( target, array, blockSize );
				array += cols;
				target += matrix.tda;
			}
		}
	}
Exemplo n.º 9
0
void RowLoader::triggerRowCountDetermination(int token)
{
    std::unique_lock<std::mutex> lk(m);

    num_tasks++;
    nosync_ensureDbAccess();

    // do a count query to get the full row count in a fast manner
    row_counter = std::async(std::launch::async, [this, token]() {
        auto nrows = countRows();
        if(nrows >= 0)
            emit rowCountComplete(token, nrows);

        std::lock_guard<std::mutex> lk(m);
        nosync_taskDone();
    });
}
Exemplo n.º 10
0
//---------------------------------------------------------------------------
int32_t CSVFile::afterOpen(void)
{
	//-------------------------------------------------------
	// Check if we opened this with CREATE and write the
	// FITini Header and position to Write Start.
	if(fileMode == CREATE && parent == nullptr)
	{
		STOP(("Cannot write CSV files at present."));
	}
	else
	{
		//------------------------------------------------------
		// Find out how many Rows and cols we have
		totalRows = countRows();
		totalCols = countCols();
	}
	return(NO_ERROR);
}
Exemplo n.º 11
0
void 
GLFont::drawText(const std::string &text, bool hcentered, bool vcentered) const
{
  if (vcentered) {
    unsigned rows=countRows(text);
    glTranslatef(0,float(rows)/2.0f-1.0f,0);
  }
  std::string::size_type pos(text.find_first_of('\n'));
  if (pos!=std::string::npos) {
    std::string row(text,0,pos);
    drawTextRow(row,hcentered);
    if (pos+1<text.size()) {
      glTranslatef(0,-1,0);
      drawText(std::string(text,pos+1),hcentered);
    }
    return;
  }
  drawTextRow(text,hcentered);
}
Exemplo n.º 12
0
	void Matrix::extractQ ( const Vector& tau, const Matrix& qr ) {
		const size_t
			rows = countRows(),
			cols = countColumns(),
			qrRows = qr.countRows(),
			qrCols = qr.countColumns(),
			tauDims = tau.countDimensions();
		assert( qrRows >= qrCols );
		assert( qrCols == tauDims );
		assert( rows == qrRows );
		assert( cols == qrCols || cols == qrRows );
		fill( 0.0 );
		Vector diagonal = diagonalVector();
		diagonal.fill( 1.0 );
		for ( size_t col = qrCols; 0 < col--; ) {
			Matrix affected = subMatrix( col, col, rows - col, cols - col );
			affected.householderTransform(
				tau.get( col ),
				const_cast<Matrix&>( qr ).columnVector( col ).subVector( col, rows - col )
			);
		}
	}
Exemplo n.º 13
0
	AutoMatrix::AutoMatrix ( const AutoMatrix& original ) : Matrix(
		original.countRows(),
		original.countColumns(),
		static_cast<double*>( malloc( original.size ) ),
		original.matrix.tda
	), size( original.size ) {
		const size_t
			rows = countRows(),
			cols = countColumns();
		if ( cols == matrix.tda ) {
			// copy one big lump
			memcpy( matrix.data, original.matrix.data, calculateSize( rows, cols ) );
		} else {
			// copy row by row
			const size_t blockSize = cols * sizeof( double );
			const double *source = original.matrix.data;
			double *target = matrix.data;
			for ( size_t row = 0; row < rows; ++row ) {
				memcpy( target, source, blockSize );
				source += original.matrix.tda;
				target += matrix.tda;
			}
		}
	}
Exemplo n.º 14
0
main(int argc, char *argv[]) {

   int rows = countRows(argv[1]);
   int columns = countColumns(argv[1]);
   
   printf("Rows   : %d\n", rows);
   printf("Columns: %d\n", columns);
   
   float tableau[rows][columns];

   /* DIAGNOSTICS */

   int i,j;
   int count = 1; // contatore Tableau

   /* Print Tableau */
   printf("\n=== PRINT TABLEAU BEFORE IMPORT [MAIN] ===\n");
   for (i = 0; i < rows; i++) {
      for (j = 0; j < columns; j++)
         printf("(%d,%d): %.3f\t", i, j, tableau[i][j]);
      printf("\n");
   }

   printf("\nTableau Address: %p\n", tableau);

   /* Memory Addresses */
   printf("\n=== MEMORY ADDRESSES BEFORE IMPORT [MAIN]===\n");
   for (i = 0; i < rows; i++) {
      for (j = 0; j < columns; j++)
         printf("(%d,%d): %p\t", i, j, &(tableau[i][j]));
      printf("\n");
   }

   import(rows,columns,tableau);

   /* Memory Addresses */
   printf("\n=== MEMORY ADDRESSES AFTER IMPORT [MAIN]===\n");
   for (i = 0; i < rows; i++) {
      for (j = 0; j < columns; j++)
         printf("(%d,%d): %p\t", i, j, &(tableau[i][j]));
      printf("\n");
   }

   /* Print Tableau */
   printf("\n=== PRINT TABLEAU AFTER IMPORT [MAIN] ===\n");
   for (i = 0; i < rows; i++) {
      for (j = 0; j < columns; j++)
         printf("(%d,%d): %.3f\t", i, j, tableau[i][j]);
      printf("\n");
   }

   /* === SIMPLEX === */

   int unbounded = 0;
   int optimal = 0;

   float theta,ratio;

   int k,h,sw;

   while ((optimal < (columns - 1)) && (unbounded < (rows - 1))) {
      for (j = 1; j < columns; j++)
         if (tableau[0][j] >= 0)
            optimal++;  // se tutti i costi ridotti sono > 0, allora siamo nell'ottimo
         else { // altrimenti scelgo una variabile fuori base con costo ridotto < 0 (Bland)
            optimal = 0;
            h = j; // salvo l'indice della colonna della variabile fuori base scelta
            for (i = 1; i < rows; i++)
               if (tableau[i][h] <= 0) // se ogni riga della colonna della variabile fuori base e' <= 0, allora e' illimitato
                  unbounded++;
               else {
                  unbounded = 0;
                  sw = 0;
                  for (i = 1; i < rows; i++) { // altrimenti calcola i rapporti ed il theta
                     if (tableau[i][h] > 0 && sw == 0) {
                        theta = tableau[i][0] / tableau[i][h];
                        k = i;
                        sw++;
                     }
                     else if (tableau[i][h] > 0) {
                       ratio = tableau[i][0] / tableau[i][h];
                       if (ratio < theta) {
                          theta = ratio;
                          k = i;
                       }
                     }    
                  }
               }
            
            pivot(k, h, rows, columns, tableau);
            
            printf("\n=== PRINT TABLEAU %d ===\n", count++);
            for (i = 0; i < rows; i++) {
               for (j = 0; j < columns; j++)
                  printf("(%d,%d): %.3f\t", i, j, tableau[i][j]);
               printf("\n");
            }
         }    
   }

   return 0;
}
Exemplo n.º 15
0
void tva::Doc::slSolveStaticTask()
{
	//tva::util::showMsg("slSolveStaticTask");
	slMakeMatrises();
	//
	//if (!model.gM.hasM)
	//{
	//	tva::util::showMsg("matr M not ready");
	//	return;
	//}
	if (!model.gM.hasK)
	{
		tva::util::showMsg("matr K not ready");
		return;
	}
	auto K2=model.gM.getK();
	{//correction K
		static const double multStuck=100.0;
		K2.getElem(0,0) *= multStuck;
		//K2.getElem(0,1) *= multStuck;
		//K2.getElem(1,0) *= multStuck;
	}
	//make F_outer
	tva::tMatr F_outer;
	F_outer.resize(K2.countRows(),1);
	{
		static const double F_r=-1.0;
		F_outer.fill(0.0);
		F_outer.getElem(K2.countRows()-1,0) = F_r;
	}
	//solve
	auto b=K2.getSolve(F_outer);
	//
	tva::postProc::MatrToWidgetTable(b, tableWidget_23);
	//extract vector
	std::vector<double> stdX,stdB;
	for(int i=0; i<b.countRows(); ++i)
	{
		stdX.push_back(i);
		stdB.push_back(b.getElem(i,0));
	}
	//
	//auto p = std::make_pair(stdX, stdB);
	{//plot staticTask
		typedef tva::chartSetup::chartStyle chStyle;
		//
		chStyle style;
		//
		tva::chartSetup::axisStyle asX;
		tva::chartSetup::axisStyle asY;
		asX.title = "index";
		asY.title = "staticDef";
		{
			tva::chartSetup::opPair limY;
			const auto& providerData = stdB;
			limY.first =tva::util::getMinElement(stdB);
			limY.second = tva::util::getMaxElement(stdB);
			if (limY.second.isSettled())
			{
				//limY.second = limY.second.get()*(1+percentLimitCorrection);
			}

			tva::chartSetup::opPair limX;
			limX.first = 0;//tva::util::getMinElement(model.Omega);
			limX.second = stdX.size()+1;

			asY.lim = limY;
			asX.lim = limX;
		}
		style.axStyleX = asX;
		style.axStyleY = asY;
		//
		//tva::chartSetup::opChartStyle loc_style;
		//
		tva::chartSetup::lineStyle lStyle;
		lStyle.color = QColor(0,85,0);
		//
		bool needClear(true);
		bool leftAxis(true);
		postProc::policyChart policy;
		policy.style = style;
		policy.lStyle = lStyle;
		policy.needClear = needClear;
		policy.leftAxis = leftAxis;
		//
		tva::postProc::vec2Chart( policy, stdX, stdB, plotStaticTask.get() );
	}
}
Exemplo n.º 16
0
void loadTOP(char* filename){
	int i;
	printf("Reading topology.\n");
	FILE* topFile = fopen(filename, "r");
	char buffer[buf_size];
	if (topFile != NULL ){
		while(fgets(buffer, buf_size, topFile) != NULL){
			if(strncmp(buffer, "[ atoms ]", 9) == 0){
				printf("Counting atoms...\n");
				sop.aminoCount = countRows(topFile);
				printf("%d found.\n", sop.aminoCount);
			}
			if(strncmp(buffer, "[ bonds ]", 9) == 0){
				printf("Counting covalent bonds...\n");
				sop.bondCount = countRows(topFile);
				printf("%d found.\n", sop.bondCount);
			}
			if(strncmp(buffer, "[ native ]", 10) == 0){
				printf("Counting native contacts...\n");
				sop.nativeCount = countRows(topFile);
				printf("%d found.\n", sop.nativeCount);
			}
			if(strncmp(buffer, "[ pairs ]", 9) == 0){
				printf("Counting pairs...\n");
				sop.pairCount = countRows(topFile);
				printf("%d found.\n", sop.pairCount);
			}
		}
	} else {
		printf("ERROR: cant find topology file '%s'.\n", filename);
		exit(0);
	}


	sop.aminos = (Atom*)calloc(sop.aminoCount, sizeof(Atom));
	sop.bonds = (CovalentBond*)calloc(sop.bondCount, sizeof(CovalentBond));
	sop.natives = (NativeContact*)calloc(sop.nativeCount, sizeof(NativeContact));
	sop.pairs = (PossiblePair*)calloc(sop.pairCount, sizeof(PossiblePair));

	rewind(topFile);
	while(fgets(buffer, buf_size, topFile) != NULL){
		if(strncmp(buffer, "[ atoms ]", 9) == 0){
			fgets(buffer, buf_size, topFile);
			for(i = 0; i < sop.aminoCount; i++){
				readAtom(&sop.aminos[i], topFile);
			}
		}
		if(strncmp(buffer, "[ bonds ]", 9) == 0){
			fgets(buffer, buf_size, topFile);
			for(i = 0; i < sop.bondCount; i++){
				TOPPair pair = readPair(topFile);
				sop.bonds[i].i = pair.i;
				sop.bonds[i].j = pair.j;
				sop.bonds[i].r0 = pair.c0;
			}
		}
		if(strncmp(buffer, "[ native ]", 10) == 0){
			fgets(buffer, buf_size, topFile);
			for(i = 0; i < sop.nativeCount; i++){
				TOPPair pair = readPair(topFile);
				sop.natives[i].i = pair.i;
				sop.natives[i].j = pair.j;
				sop.natives[i].r0 = pair.c0;
				sop.natives[i].eh = pair.c1;

			}
		}
		if(strncmp(buffer, "[ pairs ]", 9) == 0){
			fgets(buffer, buf_size, topFile);
			for(i = 0; i < sop.pairCount; i++){
				TOPPair pair = readPair(topFile);
				sop.pairs[i].i = pair.i;
				sop.pairs[i].j = pair.j;
			}
		}
	}

	fclose(topFile);
	printf("Done reading topology.\n");
}
Exemplo n.º 17
0
	Vector AutoMatrix::newColumn () {
		const size_t col = countColumns();
		upSize( countRows(), col + 1 );
		return columnVector( col );
	}
Exemplo n.º 18
0
	Vector AutoMatrix::newRow () {
		const size_t row = countRows();
		upSize( row + 1, countColumns() );
		return rowVector( row );
	}