Skip to content

Commit

Permalink
Some bug fixes (matrix expansion)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-i-gassmann committed Dec 12, 2017
1 parent 30cb0b0 commit d2ae03a
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 193 deletions.
6 changes: 3 additions & 3 deletions OS/data/osilFiles/testMatricesAndCones.osil
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@
</value>
</constantElements>
</matrix>
<matrix numberOfRows="30" numberOfColumns="24" name="35-Expansion test 2 - Base matrices">
<matrix numberOfRows="25" numberOfColumns="24" name="35-Expansion test 2 - Base matrices">
<blocks numberOfBlocks="20">
<colOffset numberOfEl="5">
<el mult="5" incr="6">0</el>
Expand Down Expand Up @@ -2447,7 +2447,7 @@
<transformation>
<matrixTimes>
<matrixReference idx="0"></matrixReference>
<matrixReference idx="7"></matrixReference>
<matrixReference idx="1"></matrixReference>
</matrixTimes>
</transformation>
</matrix>
Expand Down Expand Up @@ -2602,7 +2602,7 @@
</value>
</constantElements>
</matrix>
<matrix numberOfColumns="4" numberOfRows="3" name="63-MatrixSum 60+61+62">
<matrix numberOfColumns="3" numberOfRows="12" name="63-MatrixSum 60+61+62">
<transformation>
<matrixSum>
<matrixReference idx="60"></matrixReference>
Expand Down
7 changes: 4 additions & 3 deletions OS/src/OSCommonInterfaces/OSInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -3629,9 +3629,10 @@ class OSInstance
* The default value does not change the symmetry
* @return the (nonzero) matrix elements.
*/
GeneralSparseMatrix* getExpandedMatrix(int n, bool rowMajor_ = false,
ENUM_MATRIX_TYPE convertTo_ = ENUM_MATRIX_TYPE_unknown,
ENUM_MATRIX_SYMMETRY symmetry_ = ENUM_MATRIX_SYMMETRY_unknown);
GeneralSparseMatrix*
getExpandedMatrix(int n, bool rowMajor_ = false,
ENUM_MATRIX_TYPE convertTo_ = ENUM_MATRIX_TYPE_unknown,
ENUM_MATRIX_SYMMETRY symmetry_ = ENUM_MATRIX_SYMMETRY_unknown);

/**
* Get the (nonzero) elements of the matrix in row major form.
Expand Down
192 changes: 121 additions & 71 deletions OS/src/OSCommonInterfaces/OSMatrix.cpp

Large diffs are not rendered by default.

32 changes: 24 additions & 8 deletions OS/src/OSCommonInterfaces/OSMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,10 @@ class GeneralSparseMatrix
/**
* isTranspose holds whether the stored elements represent the transposed matrix.
* the default is false.
*
* @remark The transpose of a matrix is of course closely related to the matrix itself.
* In fact, a column-wise representation of the transpose has data arrays that are
* identical to a row-wise representation of the original matrix and vice versa.
*/
bool isTranspose;

Expand Down Expand Up @@ -2433,6 +2437,14 @@ class GeneralSparseMatrix
*/
GeneralSparseMatrix* convertSymmetry(ENUM_MATRIX_SYMMETRY symmetry_);

/**
* A method to transpose a GeneralSparseMatrix, which involves swapping the dimensions
* The result is stored into another generalSparseMatrix with the same type of values
* @param copyValues_ determines whether the arrays need to be duplicated
* @return the transformed matrix
*/
GeneralSparseMatrix* convertToTranspose(bool copyValues_);

/**
* A method to change the type of symmetry used in storing a generalSparseMatrix
* The result is stored into another generalSparseMatrix with the same type of values
Expand Down Expand Up @@ -2658,18 +2670,21 @@ class MatrixType : public MatrixNode

/**
* a utility routine to expand a matrix into one of several different forms
* the expansions are held in expandedMatrixByElements, a std::vector of GeneralSparseMatrix
* The expansions are held in expandedMatrixByElements, a std::vector of GeneralSparseMatrix
* @param mtxIdx provides pointers to all defined matrices for use within transformations.
* @param rowMajor_ controls whether the matrix should be expanded into row or column major format
* @param convertTo_ controls whether elements should be converted from one type to another
* @param symmetry_ controls whether a particular type of symmetry should be enforced
* The default value does not change the symmetry
* The default value does not change the symmetry
* @param transpose_ controls whether the expansion is of the matrix or its transpose
* The default is to expand the matrix in its natural form
* @return the index in the collection of expanded matrices corresponding to the current expansion
* @remark the return value is -1 in case of any error
*/
int getExpandedMatrix(OSMatrix** mtxIdx, bool rowMajor_,
ENUM_MATRIX_TYPE convertTo_ = ENUM_MATRIX_TYPE_unknown,
ENUM_MATRIX_SYMMETRY symmetry_ = ENUM_MATRIX_SYMMETRY_unknown);
ENUM_MATRIX_SYMMETRY symmetry_ = ENUM_MATRIX_SYMMETRY_unknown,
bool transpose_ = false);


GeneralSparseMatrix* getMatrixBlockInColumnMajorForm(int columnIdx, int rowIdx,
Expand Down Expand Up @@ -2989,11 +3004,12 @@ class MatrixType : public MatrixNode
* @return the blocks as an ExpandedMatrixBlocks object, which is essentially
* an array of general sparse matrices.
*/
ExpandedMatrixBlocks* disassembleMatrix(int* rowPartition, int rowPartitionSize,
int* colPartition, int colPartitionSize,
OSMatrix** mtxIdx, bool rowMajor_,
ENUM_MATRIX_TYPE valueType_ = ENUM_MATRIX_TYPE_unknown,
ENUM_MATRIX_SYMMETRY symmetry_ = ENUM_MATRIX_SYMMETRY_unknown);
ExpandedMatrixBlocks*
disassembleMatrix(int* rowPartition, int rowPartitionSize,
int* colPartition, int colPartitionSize,
OSMatrix** mtxIdx, bool rowMajor_,
ENUM_MATRIX_TYPE valueType_ = ENUM_MATRIX_TYPE_unknown,
ENUM_MATRIX_SYMMETRY symmetry_ = ENUM_MATRIX_SYMMETRY_unknown);

/**
* A function to check for the equality of two objects
Expand Down
49 changes: 33 additions & 16 deletions OS/src/OSCommonInterfaces/OSnLNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3120,12 +3120,14 @@ GeneralSparseMatrix* OSnLMNodeMatrixPlus::expandNode(OSMatrix** mtxLoc, bool ro
{
returnMtx->startSize = majorDim + 1;
returnMtx->start = new int[returnMtx->startSize];
returnMtx->start[0] = 0;
returnMtx->start[0] = 0;

val = new double[minorDim];

for (j=0; j < minorDim; ++j)
{
val[j] = 0.0;
}

// phase 0: count the number of nonzeroes and allocate space
for (i=0; i < majorDim; ++i)
Expand Down Expand Up @@ -3193,11 +3195,18 @@ GeneralSparseMatrix* OSnLMNodeMatrixPlus::expandNode(OSMatrix** mtxLoc, bool ro
}

else
{
cout << "Unsupported element type for OSnLMNodeMatrixPlus" << endl;
throw ErrorClass("Unsupported element type for OSnLMNodeMatrixPlus");
}

#ifndef NDEBUG
if (nonz != returnMtx->valueSize)
{
cout << "OSnLMNodeMatrixPlus::expandNode(): Number of nonzeroes is inconsistent";
cout << " - Have " << nonz << " expected " << returnMtx->valueSize << endl;
throw ErrorClass("OSnLMNodeMatrixPlus::expandNode(): Number of nonzeroes is inconsistent");
}
#endif

if (loc != NULL)
Expand Down Expand Up @@ -3324,11 +3333,12 @@ GeneralSparseMatrix* OSnLMNodeMatrixSum::expandNode(OSMatrix** mtxLoc, bool rowM
for (j=0; j < minorDim; ++j)
loc[j] = -1;

// nonz = 0;

for (k=1; k<inumberOfChildren; ++k)
{
summandA = returnMtx;
summandB = ((OSnLMNode*)m_mChildren[1])->expandNode(mtxLoc, rowMajor_,
summandB = ((OSnLMNode*)m_mChildren[k])->expandNode(mtxLoc, rowMajor_,
convertTo_, symmetry_);
if (summandB == NULL) throw ErrorClass(
"OSnLMNodeMatrixSum::expandNode(): Error expanding matrix");
Expand All @@ -3340,7 +3350,7 @@ GeneralSparseMatrix* OSnLMNodeMatrixSum::expandNode(OSMatrix** mtxLoc, bool rowM
returnMtx = new GeneralSparseMatrix();

returnMtx->numberOfRows = summandA->numberOfRows;
returnMtx->numberOfColumns = summandB->numberOfColumns;
returnMtx->numberOfColumns = summandA->numberOfColumns;
returnMtx->isRowMajor = rowMajor_;
returnMtx->symmetry = symmetry_;

Expand All @@ -3363,7 +3373,8 @@ GeneralSparseMatrix* OSnLMNodeMatrixSum::expandNode(OSMatrix** mtxLoc, bool rowM
returnMtx->start = new int[returnMtx->startSize];
returnMtx->start[0] = 0;

// phase 0: count the number of nonzeroes and allocate space
// phase 0: update the number of nonzeroes and allocate space
nonz = 0;
for (i=0; i < majorDim; ++i)
{
for (j = summandA->start[i]; j < summandA->start[i+1]; ++j)
Expand Down Expand Up @@ -4656,13 +4667,13 @@ GeneralSparseMatrix* OSnLMNodeMatrixLowerTriangle::expandNode(OSMatrix** mtxLoc,

if (rowMajor_)
{
majorDim = returnMtx->numberOfRows;
minorDim = returnMtx->numberOfColumns;
majorDim = baseMtx->numberOfRows;
minorDim = baseMtx->numberOfColumns;
}
else
{
majorDim = returnMtx->numberOfColumns;
minorDim = returnMtx->numberOfRows;
majorDim = baseMtx->numberOfColumns;
minorDim = baseMtx->numberOfRows;
}

returnMtx->startSize = majorDim + 1;
Expand All @@ -4681,7 +4692,7 @@ GeneralSparseMatrix* OSnLMNodeMatrixLowerTriangle::expandNode(OSMatrix** mtxLoc,
for (j = baseMtx->start[i]; j < baseMtx->start[i+1]; ++j)
{
if (baseMtx->index[j] <= i - min)
nonz ++;
nonz++;
}
returnMtx->start[i+1] = nonz;
}
Expand All @@ -4693,7 +4704,7 @@ GeneralSparseMatrix* OSnLMNodeMatrixLowerTriangle::expandNode(OSMatrix** mtxLoc,
for (j = baseMtx->start[i]; j < baseMtx->start[i+1]; ++j)
{
if (baseMtx->index[j] >= i + min)
nonz ++;
nonz++;
}
returnMtx->start[i+1] = nonz;
}
Expand All @@ -4717,7 +4728,7 @@ GeneralSparseMatrix* OSnLMNodeMatrixLowerTriangle::expandNode(OSMatrix** mtxLoc,
returnMtx->index[returnMtx->start[i]] = baseMtx->index[j];
if (!returnMtx->copyValue(baseMtx, j, returnMtx->start[i]))
throw ErrorClass("Error copying a value in OSnLMNodeMatrixLowerTriangle::expandNode()");
returnMtx->start[i] ++;
returnMtx->start[i]++;
}
}
}
Expand All @@ -4733,7 +4744,7 @@ GeneralSparseMatrix* OSnLMNodeMatrixLowerTriangle::expandNode(OSMatrix** mtxLoc,
returnMtx->index[returnMtx->start[i]] = baseMtx->index[j];
if (!returnMtx->copyValue(baseMtx, j, returnMtx->start[i]))
throw ErrorClass("Error copying a value in OSnLMNodeMatrixLowerTriangle::expandNode()");
returnMtx->start[i] ++;
returnMtx->start[i]++;
}
}
}
Expand All @@ -4745,6 +4756,9 @@ GeneralSparseMatrix* OSnLMNodeMatrixLowerTriangle::expandNode(OSMatrix** mtxLoc,
returnMtx->start[i] = returnMtx->start[i-1];
}
returnMtx->start[0] = 0;
#ifndef NDEBUG
returnMtx->printMatrix();
#endif

return returnMtx;
}
Expand Down Expand Up @@ -4956,13 +4970,13 @@ GeneralSparseMatrix* OSnLMNodeMatrixUpperTriangle::expandNode(OSMatrix** mtxLoc,

if (rowMajor_)
{
majorDim = returnMtx->numberOfRows;
minorDim = returnMtx->numberOfColumns;
majorDim = baseMtx->numberOfRows;
minorDim = baseMtx->numberOfColumns;
}
else
{
majorDim = returnMtx->numberOfColumns;
minorDim = returnMtx->numberOfRows;
majorDim = baseMtx->numberOfColumns;
minorDim = baseMtx->numberOfRows;
}

returnMtx->startSize = majorDim + 1;
Expand Down Expand Up @@ -5045,6 +5059,9 @@ GeneralSparseMatrix* OSnLMNodeMatrixUpperTriangle::expandNode(OSMatrix** mtxLoc,
returnMtx->start[i] = returnMtx->start[i-1];
}
returnMtx->start[0] = 0;
#ifndef NDEBUG
returnMtx->printMatrix();
#endif

return returnMtx;
}
Expand Down
12 changes: 6 additions & 6 deletions OS/src/OSParsers/OSParseosgl.y.syntax
Original file line number Diff line number Diff line change
Expand Up @@ -876,11 +876,11 @@ matrixStart: MATRIXSTART
{
if (osglData->matrixCounter >= osglData->numberOfMatrices)
throw ErrorClass("more matrices than specified");
osglData->symmetryPresent = false;
osglData->typePresent = false;
osglData->numberOfRowsPresent = false;
osglData->numberOfColumnsPresent = false;
osglData->namePresent = false;
osglData->symmetryPresent = false;
osglData->mtxConstructorVec.clear();
osglData->mtxBlocksVec.clear();
osglData->mtxBlkVec.clear();
Expand All @@ -907,7 +907,7 @@ matrixAttributes: matrixAttributeList
((OSMatrix*)osglData->tempC)->numberOfColumns = osglData->numberOfColumns;
if (osglData->symmetryPresent == true)
{
// if (osglData->symmetry == "default")
// if (osglData->symmetry == "unknown")
// osglData->symmetry = "none";
((OSMatrix*)osglData->tempC)->symmetry
= (ENUM_MATRIX_SYMMETRY)returnMatrixSymmetry(osglData->symmetry);
Expand Down Expand Up @@ -971,7 +971,7 @@ matrixWithMatrixVarIdxAttributes: matrixWithMatrixVarIdxATTList
((OSMatrixWithMatrixVarIdx*)osglData->tempC)->matrixVarIdx = osglData->matrixVarIdx;
if (osglData->symmetryPresent == true)
{
if (osglData->symmetry == "default")
if (osglData->symmetry == "unknown")
osglData->symmetry = "none";
((OSMatrix*)osglData->tempC)->symmetry
= (ENUM_MATRIX_SYMMETRY)returnMatrixSymmetry(osglData->symmetry);
Expand Down Expand Up @@ -1038,7 +1038,7 @@ matrixWithMatrixObjIdxAttributes: matrixWithMatrixObjIdxATTList
((OSMatrixWithMatrixObjIdx*)osglData->tempC)->matrixObjIdx = osglData->matrixObjIdx;
if (osglData->symmetryPresent == true)
{
if (osglData->symmetry == "default")
if (osglData->symmetry == "unknown")
osglData->symmetry = "none";
((OSMatrix*)osglData->tempC)->symmetry
= (ENUM_MATRIX_SYMMETRY)returnMatrixSymmetry(osglData->symmetry);
Expand Down Expand Up @@ -1102,7 +1102,7 @@ matrixWithMatrixConIdxAttributes: matrixWithMatrixConIdxATTList
((OSMatrixWithMatrixConIdx*)osglData->tempC)->matrixConIdx = osglData->matrixConIdx;
if (osglData->symmetryPresent == true)
{
if (osglData->symmetry == "default")
if (osglData->symmetry == "unknown")
osglData->symmetry = "none";
((OSMatrix*)osglData->tempC)->symmetry
= (ENUM_MATRIX_SYMMETRY)returnMatrixSymmetry(osglData->symmetry);
Expand Down Expand Up @@ -2364,7 +2364,7 @@ matrixBlockAtt:
parserData->parser_errors += addErrorMsg( NULL, parserObjects, "symmetry type not recognized");
parserData->errorText = NULL;
if (osglData->symmetry == "none")
osglData->symmetry = "default";
osglData->symmetry = "unknown";
((MatrixBlock*)osglData->tempC)->symmetry
= (ENUM_MATRIX_SYMMETRY)returnMatrixSymmetry(osglData->symmetry);
}
Expand Down
Loading

0 comments on commit d2ae03a

Please sign in to comment.