// gaussian elimination - main function int WINAPI GaussianElimination(double ** ppMatrix, int iRows, int iColumns, GAUSSIAN_ELIMINATION_OUTPUT * pOutputBuffer) { if(ppMatrix == NULL) { return -1; } if(pOutputBuffer->ppSMatrix != NULL) { iColumns += iRows; } SetMatrixDimensions(iRows, iColumns); double ** ppWorkMatrix; if(pOutputBuffer->ppSMatrix == NULL) { ppWorkMatrix = pOutputBuffer->ppReducedEcholonForm; } else { ppWorkMatrix = AllocateMatrixMemory(iColumns + iRows, iRows); } if(!ppWorkMatrix) { return -1; } if(pOutputBuffer->ppSMatrix != NULL) { for(int i = (iColumns - iRows); i < iColumns; i++) { for(int g = 0; g < iRows; g++) { if(g == i - iColumns + iRows) { ppWorkMatrix[i][g] = 1.0f; } else { ppWorkMatrix[i][g] = 0.0f; } } } CopyMatrix(ppWorkMatrix, ppMatrix, iRows, iColumns - iRows); } else { CopyMatrix(ppWorkMatrix, ppMatrix, iRows, iColumns); } POSITION posPivot = StepOne(ppWorkMatrix); //printf("%lf, %lf", ppWorkMatrix[0][0], ppWorkMatrix[0][1]); StepTwo(ppWorkMatrix, &posPivot); //printf("%lf, %lf", ppWorkMatrix[0][0], ppWorkMatrix[0][1]); if(StepThree(ppWorkMatrix, posPivot) != 0) { return -1; } //printf("%lf, %lf", ppWorkMatrix[0][0], ppWorkMatrix[0][1]); StepFour(ppWorkMatrix, posPivot); //printf("%lf, %lf", ppWorkMatrix[0][0], ppWorkMatrix[0][1]); POSITION_ARRAY pivots = StepFive(ppWorkMatrix, posPivot); //printf("%lf, %lf", ppWorkMatrix[0][0], ppWorkMatrix[0][1]); AddPosition(&pivots, posPivot); StepSix(ppWorkMatrix, pivots); //printf("%lf, %lf", ppWorkMatrix[0][0], ppWorkMatrix[0][1]); if(pOutputBuffer->ppSMatrix) { DivideOutputAndSMatrix(ppWorkMatrix, pOutputBuffer); } else { CopyMatrix(pOutputBuffer->ppReducedEcholonForm, ppWorkMatrix, iRows, iColumns); } return 0; }
int main(int argc, char* argv[]) { double** A; A = AllocateMatrixMemory(5, 3); A[0][1] = 2.0; A[2][4] = 4.0; FreeMatrixMemory(5, A); return 0; }
AbstractContinuumMechanicsSolver<DIM>::AbstractContinuumMechanicsSolver(AbstractTetrahedralMesh<DIM, DIM>& rQuadMesh, ContinuumMechanicsProblemDefinition<DIM>& rProblemDefinition, std::string outputDirectory, CompressibilityType compressibilityType) : mrQuadMesh(rQuadMesh), mrProblemDefinition(rProblemDefinition), mOutputDirectory(outputDirectory), mpOutputFileHandler(NULL), mpQuadratureRule(NULL), mpBoundaryQuadratureRule(NULL), mCompressibilityType(compressibilityType), mResidualVector(NULL), mSystemLhsMatrix(NULL), mPreconditionMatrix(NULL) { assert(DIM==2 || DIM==3); //Check that the mesh is Quadratic QuadraticMesh<DIM>* p_quad_mesh = dynamic_cast<QuadraticMesh<DIM>* >(&rQuadMesh); DistributedQuadraticMesh<DIM>* p_distributed_quad_mesh = dynamic_cast<DistributedQuadraticMesh<DIM>* >(&rQuadMesh); if(p_quad_mesh == NULL && p_distributed_quad_mesh == NULL) { EXCEPTION("Continuum mechanics solvers require a quadratic mesh"); } mVerbose = (mrProblemDefinition.GetVerboseDuringSolve() || CommandLineArguments::Instance()->OptionExists("-mech_verbose") || CommandLineArguments::Instance()->OptionExists("-mech_very_verbose") ); mWriteOutput = (mOutputDirectory != ""); if (mWriteOutput) { mpOutputFileHandler = new OutputFileHandler(mOutputDirectory); } // see dox for mProblemDimension mProblemDimension = mCompressibilityType==COMPRESSIBLE ? DIM : DIM+1; mNumDofs = mProblemDimension*mrQuadMesh.GetNumNodes(); AllocateMatrixMemory(); // In general the Jacobian for a mechanics problem is non-polynomial. // We therefore use the highest order integration rule available. mpQuadratureRule = new GaussianQuadratureRule<DIM>(3); // The boundary forcing terms (or tractions) are also non-polynomial in general. // Again, we use the highest order integration rule available. mpBoundaryQuadratureRule = new GaussianQuadratureRule<DIM-1>(3); mCurrentSolution.resize(mNumDofs, 0.0); }
int main(int argc, char **argv) { int *dim_A = new int [2]; dim_A[0] = 5; dim_A[1] = 3; double** A = AllocateMatrixMemory(dim_A[0], dim_A[1]); PopulateMatrixValues(A, dim_A[0], dim_A[1]); int *dim_B = new int [2]; dim_B[0] = 3; dim_B[1] = 2; double** B = AllocateMatrixMemory(dim_B[0], dim_B[1]); PopulateMatrixValues(B, dim_B[0], dim_B[1]); PrintMatrix(A, dim_A[0], dim_A[1]); std::cout << "\n"; PrintMatrix(B, dim_B[0], dim_B[1]); std::cout << "\n"; double** AB = Multiply(A, B, dim_A, dim_B); PrintMatrix(AB, dim_A[0], dim_B[1]); return 0; }
int main(int argc, char * argv[]) { if(argc !=2 ) { Usage(argv[0]); return 1; } //has the user provided the data filename in the command line? std::ifstream read_file(argv[1]); //...then open it for reading assert(read_file.is_open()); double ** A; int rows,cols; read_file >> rows >> cols; //the format of the file is that first the size is given... std::cout << "Dynamically allocating memory for a matrix "<<rows<<"x"<<cols<<"\n"; A = AllocateMatrixMemory(rows, cols); for(int r=0;r<rows; r++) //... and then the elements are in the file { for(int c=0;c<cols;c++) read_file >> A[r][c]; } read_file.close(); //do not forget to close the file std::cout << "Lets see if we read correct,by printing the matrix on the console:\n"; for(int r=0;r<rows; r++) { for(int c=0;c<cols;c++) { std::cout << A[r][c]<< " ";} std::cout << "\n"; } FreeMatrixMemory(rows, A); //do not forget to release the memory allocated from new }
static POSITION_ARRAY StepFive(double ** ppMatrix, POSITION posPivot) { POSITION_ARRAY pivots = {NULL, 0}; int iRowsSave = iRows; int iColumnsSave = iColumns; if(iRows - 1 == 0) { return pivots; // Fertig } if(iColumns - posPivot.iColumn == 0) { return pivots; // Fertig } double ** ppSmallerMatrix = AllocateMatrixMemory(iColumns - posPivot.iColumn - 1, iRows - 1); for(int i = posPivot.iColumn + 1; i < iColumns; i++) { for(int g = 1; g < iRows; g++) { ppSmallerMatrix[i - posPivot.iColumn - 1][g - 1] = ppMatrix[i][g]; } } iRows--; iRowsOffset++; iColumns -= posPivot.iColumn + 1; iColumnsOffset += posPivot.iColumn + 1; POSITION posNewPivot = StepOne(ppSmallerMatrix); if(posNewPivot.iColumn == -1) { // Rücksetzen iRowsOffset -= (iRowsSave - iRows); iRows = iRowsSave; iRowsOffset -= (iColumnsSave - iColumns); iColumns = iColumnsSave; return pivots; // Fertig } StepTwo(ppSmallerMatrix, &posNewPivot); StepThree(ppSmallerMatrix, posNewPivot); StepFour(ppSmallerMatrix, posNewPivot); pivots = StepFive(ppSmallerMatrix, posNewPivot); AddPosition(&pivots, posNewPivot); for(int i = 0; i < pivots.iCount; i++) { pivots.pPositions[i].iColumn += posPivot.iColumn + 1; // Auf neue Grösse anpassen pivots.pPositions[i].iRow += 1; } // Rücksetzen iRowsOffset -= (iRowsSave - iRows); iRows = iRowsSave; iColumnsOffset -= (iColumnsSave - iColumns); iColumns = iColumnsSave; // Eingruppieren der neuen Matrix for(int i = posPivot.iColumn + 1; i < iColumns; i++) { for(int g = 1; g < iRows; g++) { ppMatrix[i][g] = ppSmallerMatrix[i - posPivot.iColumn - 1][g - 1]; } } // Freigeben des Speichers FreeMatrixMemory(ppSmallerMatrix, iColumns - posPivot.iColumn - 1); return pivots; }