[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [analitza/aucahuasi/matrixctrs] analitza: Clean code of commands.
From: Percy_Camilo_Triveño_Aucahuasi <percy.camilo.ta () gmail ! com>
Date: 2014-05-30 6:30:13
Message-ID: E1WqGKT-0005ub-3B () scm ! kde ! org
[Download RAW message or body]
Git commit 663205a6d82fd6f5897e54a76807117ad99b483a by Percy Camilo Triveño \
Aucahuasi. Committed on 30/05/2014 at 06:22.
Pushed by aucahuasi into branch 'aucahuasi/matrixctrs'.
Clean code of commands.
M +2 -1 analitza/analyzer.cpp
M +6 -192 analitza/commands/blockmatrixcommands.cpp
M +35 -222 analitza/commands/matrixcommands.cpp
M +0 -11 analitza/commands/matrixcommands.h
M +3 -16 analitza/commands/matrixqueries.cpp
M +2 -1 analitza/commands/vectorcommands.h
M +24 -21 analitza/tests/matrixtest.cpp
http://commits.kde.org/analitza/663205a6d82fd6f5897e54a76807117ad99b483a
diff --git a/analitza/analyzer.cpp b/analitza/analyzer.cpp
index 600b256..ca9ddbf 100644
--- a/analitza/analyzer.cpp
+++ b/analitza/analyzer.cpp
@@ -160,9 +160,10 @@ void Analyzer::registerBuiltinMethods()
m_builtin.insertFunction(RangeCommand::id, RangeCommand::type, new RangeCommand);
m_builtin.insertFunction(VectorCommand::id, VectorCommand::type, new \
VectorCommand); m_builtin.insertFunction(MatrixCommand::id, MatrixCommand::type, new \
MatrixCommand);
- m_builtin.insertFunction(ZeroMatrixCommand::id, ZeroMatrixCommand::type, new \
ZeroMatrixCommand); + m_builtin.insertFunction(BlockMatrixCommand::id, \
BlockMatrixCommand::type, new BlockMatrixCommand); \
m_builtin.insertFunction(IdentityMatrixCommand::id, IdentityMatrixCommand::type, new \
IdentityMatrixCommand); m_builtin.insertFunction(DiagonalMatrixCommand::id, \
DiagonalMatrixCommand::type, new DiagonalMatrixCommand); \
+ m_builtin.insertFunction(BlockDiagonalMatrixCommand::id, \
BlockDiagonalMatrixCommand::type, new BlockDiagonalMatrixCommand); \
m_builtin.insertFunction(TridiagonalMatrixCommand::id, \
TridiagonalMatrixCommand::type, new TridiagonalMatrixCommand); \
m_builtin.insertFunction(IsZeroMatrixCommand::id, IsZeroMatrixCommand::type, new \
IsZeroMatrixCommand); m_builtin.insertFunction(IsIdentityMatrixCommand::id, \
IsIdentityMatrixCommand::type, new IsIdentityMatrixCommand);
diff --git a/analitza/commands/blockmatrixcommands.cpp \
b/analitza/commands/blockmatrixcommands.cpp index e13da3e..b6e001d 100644
--- a/analitza/commands/blockmatrixcommands.cpp
+++ b/analitza/commands/blockmatrixcommands.cpp
@@ -80,7 +80,7 @@ static const QString MATRIX_SIZE_ERROR_MESSAGE = \
QCoreApplication::tr("Matrix di
//BEGIN FillMatrixConstructor
-const QString BlockMatrixCommand::id = QString("matrix");
+const QString BlockMatrixCommand::id = QString("blockmatrix");
const ExpressionType BlockMatrixCommand::type = variadicFunctionType(MatrixType);
//TODO better error messages
Expression BlockMatrixCommand::operator()(const QList< Analitza::Expression >& args)
@@ -89,65 +89,13 @@ Expression BlockMatrixCommand::operator()(const QList< \
Analitza::Expression >& a
const int nargs = args.size();
- switch(nargs) {
+ switch(nargs) {
case 0: {
ret.addError(QCoreApplication::tr("Invalid parameter count for \
'%1'").arg(BlockMatrixCommand::id));
return ret;
} break;
- case 1: {
- if (args.at(0).tree()->type() == Analitza::Object::value) {
- const Analitza::Cn *nobj = static_cast<const Analitza::Cn*>(args.at(0).tree());
-
- if (nobj->isInteger() && nobj->value() > 0) {
- Analitza::Matrix *matrix = new Analitza::Matrix();
- const int n = nobj->intValue();
- AnalitzaUtils::fillMatrix(matrix, n, n, 0);
- ret.setTree(matrix);
- } else
- ret.addError(MATRIX_SIZE_ERROR_MESSAGE);
-
- return ret;
- }
- } break;
- case 2: {
- if (args.at(0).tree()->type() == Analitza::Object::value && \
args.at(1).tree()->type() == Analitza::Object::value) {
- const Analitza::Cn *nrowsobj = static_cast<const \
Analitza::Cn*>(args.at(0).tree());
- const Analitza::Cn *ncolsobj = static_cast<const \
Analitza::Cn*>(args.at(1).tree());
-
- if (nrowsobj->isInteger() && ncolsobj->isInteger() && nrowsobj->value() > 0 && \
ncolsobj->value() > 0) {
- Analitza::Matrix *matrix = new Analitza::Matrix();
- AnalitzaUtils::fillMatrix(matrix, nrowsobj->intValue(), ncolsobj->intValue(), \
0);
- ret.setTree(matrix);
- } else
- ret.addError(MATRIX_SIZE_ERROR_MESSAGE);
-
- return ret;
- }
- } break;
- case 3: {
- if (args.at(0).tree()->type() == Analitza::Object::value &&
- args.at(1).tree()->type() == Analitza::Object::value &&
- args.at(2).tree()->type() == Analitza::Object::value) {
- const Analitza::Cn *nrowsobj = static_cast<const \
Analitza::Cn*>(args.at(0).tree());
- const Analitza::Cn *ncolsobj = static_cast<const \
Analitza::Cn*>(args.at(1).tree());
-
- if (nrowsobj->isInteger() && ncolsobj->isInteger() && nrowsobj->value() > 0 && \
ncolsobj->value() > 0) {
- Analitza::Matrix *matrix = new Analitza::Matrix();
- AnalitzaUtils::fillMatrix(matrix, nrowsobj->intValue(), ncolsobj->intValue(), \
static_cast<const Analitza::Cn*>(args.last().tree())->value());
- ret.setTree(matrix);
- } else
- ret.addError(MATRIX_SIZE_ERROR_MESSAGE);
-
- return ret;
- }
- } break;
- }
-
- Q_ASSERT(nargs > 0);
- Q_ASSERT(ret.toString().isEmpty());
- Q_ASSERT(ret.isCorrect());
-
+ }
//BEGIN commom
const Analitza::Object::ObjectType firstArgType = args.first().tree()->type();
@@ -310,50 +258,7 @@ Expression BlockMatrixCommand::operator()(const QList< \
Analitza::Expression >& a return ret;
}
} break;
- default: { // try to build a normal matrix
-
-
-
-
- bool allrows = true; // assumes all are rows
- Analitza::Matrix *matrix = new Analitza::Matrix();
-
- for (int i = 0; i < nargs && allrows; ++i) {
- if (args.at(i).tree()->type() == firstArgType)
- {
- const Analitza::MatrixRow *row = static_cast<const \
Analitza::MatrixRow*>(args.at(i).tree());
-
- if (row->size() == firstVectorSize)
- matrix->appendBranch(row->copy());
- else {
- allrows = false;
- ret.addError(QCoreApplication::tr("All matrixrow elements must have the same \
size"));
- }
- }
- else
- allrows = false;
- }
- if (!ret.isCorrect()) {
- delete matrix;
-
- return ret;
- } else if (allrows) {
- if (isVector) {
- QString* error=0;
- ret.setTree(Analitza::Operations::reduceUnary(Analitza::Operator::transpose, \
matrix, &error));
- delete matrix;
-
- Q_ASSERT(error == 0);
- } else
- ret.setTree(matrix);
-
- return ret;
- } else {
- ret.addError("Every argument must be a matrixrow element");
- delete matrix;
- }
- } break;
}
} else
ret.addError("we dont allow empty vector or rows"); // TODO better message
@@ -368,7 +273,7 @@ Expression BlockMatrixCommand::operator()(const QList< \
Analitza::Expression >& a
//BEGIN DiagonalMatrixConstructor
-const QString BlockDiagonalMatrixCommand::id = QString("diag");
+const QString BlockDiagonalMatrixCommand::id = QString("blockdiag");
const ExpressionType BlockDiagonalMatrixCommand::type = \
variadicFunctionType(VectorAndMatrixAlternatives);
Expression BlockDiagonalMatrixCommand::operator()(const QList< Analitza::Expression \
>& args) @@ -378,86 +283,13 @@ Expression \
> BlockDiagonalMatrixCommand::operator()(const QList< Analitza::Express
int nargs = args.size();
bool byvector = false;
- switch(nargs) {
+ switch(nargs) {
case 0: {
ret.addError(QCoreApplication::tr("Invalid parameter count for \
'%1'").arg(BlockDiagonalMatrixCommand::id));
return ret;
} break;
- case 1: {
- if (args.first().tree()->type() == Analitza::Object::matrix) {
- const Analitza::Matrix *matrix = static_cast<const \
Analitza::Matrix*>(args.first().tree());
- const int n = std::min(matrix->rowCount(), matrix->columnCount());
-
- Analitza::Vector *diagonal = new Analitza::Vector(n);
-
- for (int i = 0; i < n; ++i)
- diagonal->appendBranch(matrix->at(i, i)->copy());
-
- ret.setTree(diagonal);
-
- return ret;
- } else if (args.first().tree()->type() == Analitza::Object::vector)
- byvector = true;
- } break;
- case 2: {
- if (args.first().tree()->type() == Analitza::Object::matrix && \
args.last().tree()->type() == Analitza::Object::value) {
- const Analitza::Cn *nobj = static_cast<const Analitza::Cn*>(args.last().tree());
-
- if (nobj->isInteger()) {
- const Analitza::Matrix *matrix = static_cast<const \
Analitza::Matrix*>(args.first().tree());
- const int nrows = matrix->rowCount();
- const int ncols = matrix->columnCount();
- const int npos = nobj->value();
- const int absnpos = std::abs(npos);
- const int absnpos1 = absnpos + 1;
- const bool isneg = npos < 0;
-
- int n = 0; // or until/to
- int rowoffset = 0;
- int coloffset = 0;
-
- if (isneg) {
- if (absnpos1 > nrows) {
- ret.addError("The nth diagonal index must be less than the row count");
- return ret;
- }
-
- n = std::min(nrows - absnpos, ncols);
- rowoffset = absnpos;
- } else { // square matrix case too
- if (absnpos1 > ncols) {
- ret.addError("The nth diagonal index must be less than the column count");
- return ret;
- }
-
- n = std::min(nrows, ncols - absnpos);
- coloffset = absnpos;
- }
-
- Analitza::Vector *diagonal = new Analitza::Vector(n);
-
- for (int i = 0; i < n; ++i)
- diagonal->appendBranch(matrix->at(rowoffset + i, coloffset + i)->copy());
-
- ret.setTree(diagonal);
- }
- else
- ret.addError(QCoreApplication::tr("nth diagonal index must be integer \
number"));
-
- return ret;
- } else if (args.last().tree()->type() != Analitza::Object::value && \
args.last().tree()->type() != Analitza::Object::matrix) {
- ret.addError(QCoreApplication::tr("to specifi the diag index you must use \
positve integer value")); //TODO better messages
-
- return ret;
- }
- } break;
- }
-
- Q_ASSERT(nargs > 0);
- Q_ASSERT(ret.toString().isEmpty());
- Q_ASSERT(ret.isCorrect());
-
+ }
const Analitza::Vector *v = byvector? static_cast<const \
Analitza::Vector*>(args.first().tree()) : 0;
if (byvector) nargs = v->size();
@@ -535,24 +367,6 @@ Expression BlockDiagonalMatrixCommand::operator()(const QList< \
Analitza::Express }
//END block diag matrix
- Q_ASSERT(ret.toString().isEmpty());
- Q_ASSERT(ret.isCorrect());
-
- Analitza::Matrix *matrix = new Analitza::Matrix();
-
- for (int i = 0; i < nargs; ++i) {
- Analitza::MatrixRow *row = new Analitza::MatrixRow(nargs);
-
- for (int j = 0; j < nargs; ++j)
- if (i == j)
- row->appendBranch(byvector? v->at(j)->copy() : args.at(j).tree()->copy());
- else
- row->appendBranch(new Analitza::Cn(0));
-
- matrix->appendBranch(row);
- }
-
- ret.setTree(matrix);
return ret;
}
diff --git a/analitza/commands/matrixcommands.cpp \
b/analitza/commands/matrixcommands.cpp index b568fbb..b352a86 100644
--- a/analitza/commands/matrixcommands.cpp
+++ b/analitza/commands/matrixcommands.cpp
@@ -85,7 +85,7 @@ Expression MatrixCommand::operator()(const QList< \
Analitza::Expression >& args)
return ret;
} break;
- case 1: {
+ case 1: { // build square matrix filled with zeros
if (args.at(0).tree()->type() == Analitza::Object::value) {
const Analitza::Cn *nobj = static_cast<const Analitza::Cn*>(args.at(0).tree());
@@ -100,7 +100,7 @@ Expression MatrixCommand::operator()(const QList< \
Analitza::Expression >& args) return ret;
}
} break;
- case 2: {
+ case 2: { // build rectangular matrix filled with zeros
if (args.at(0).tree()->type() == Analitza::Object::value && \
args.at(1).tree()->type() == Analitza::Object::value) {
const Analitza::Cn *nrowsobj = static_cast<const \
Analitza::Cn*>(args.at(0).tree());
const Analitza::Cn *ncolsobj = static_cast<const \
Analitza::Cn*>(args.at(1).tree()); @@ -115,7 +115,7 @@ Expression \
MatrixCommand::operator()(const QList< Analitza::Expression >& args) return ret;
}
} break;
- case 3: {
+ case 3: { // build square matrix filled with a fixed value
if (args.at(0).tree()->type() == Analitza::Object::value &&
args.at(1).tree()->type() == Analitza::Object::value &&
args.at(2).tree()->type() == Analitza::Object::value) {
@@ -138,7 +138,6 @@ Expression MatrixCommand::operator()(const QList< \
Analitza::Expression >& args) Q_ASSERT(ret.toString().isEmpty());
Q_ASSERT(ret.isCorrect());
- //BEGIN commom
const Analitza::Object::ObjectType firstArgType = args.first().tree()->type();
if (firstArgType == Analitza::Object::vector || firstArgType == \
Analitza::Object::matrixrow) { @@ -149,207 +148,46 @@ Expression \
MatrixCommand::operator()(const QList< Analitza::Expression >& args) const int \
firstVectorSize = firstVector->size(); const Analitza::Object::ObjectType \
firstVectorElementType = firstVector->at(0)->type();
- switch(firstVectorElementType) {
- case Analitza::Object::matrix: { // try to build a block matrix
- const Analitza::Matrix *firstBlock = static_cast<const \
Analitza::Matrix*>(firstVector->at(0));
-
- bool isCorrect = true; // this flag tells if is ok to build the block matrix
- int nrows = 0;
- int ncols = 0;
- int blockpattern[firstVectorSize]; // if vectors(matrixrow) this tells the \
row(column) pattern
-
- const int blocklength = isVector? firstBlock->columnCount() : \
firstBlock->rowCount();
-
- // we need to know the pattern first, this run only on first arg (first vector) \
- for (int blockIndex = 0; blockIndex < firstVectorSize && isCorrect; \
++blockIndex) {
- if (firstVector->at(blockIndex)->type() == Analitza::Object::matrix) {
- const Analitza::Matrix* block = static_cast<const \
Analitza::Matrix*>(firstVector->at(blockIndex));
-
- if (block->rowCount() > 0 && block->columnCount() > 0) {
- const int currentlength = isVector? block->columnCount() : \
block->rowCount();
- const int currentpattern = isVector? block->rowCount() : \
block->columnCount();
-
- if (currentlength == blocklength) {
- blockpattern[blockIndex] = isVector? block->rowCount() : \
block->columnCount();
-
- if (isVector)
- nrows += blockpattern[blockIndex];
- else
- ncols += blockpattern[blockIndex];
- } else {
- isCorrect = false;
- ret.addError(QCoreApplication::tr("bloques rows .. altura o numrows debe \
ser same"));
- }
- } else {
- isCorrect = false;
- ret.addError(QCoreApplication::tr("no se aceptan bloques/matrices vacias"));
- }
- } else {
- ret.addError(QCoreApplication::tr("not all are matrix i cant build a block \
matrix"));
- isCorrect = false;
- }
- }
-
- // check if all args are ok to build a block matrix
- for (int argIndex = 0; argIndex < nargs && isCorrect; ++argIndex) {
- const Analitza::Object::ObjectType currentArgType = \
args.at(argIndex).tree()->type();
- const Analitza::Vector *vector = static_cast<const \
Analitza::Vector*>(args.at(argIndex).tree());
-
- if (currentArgType == firstArgType) {
- if (vector->size() > 0) {
- if (vector->size() == firstVectorSize) {
- const Analitza::Matrix *currentFirstBlock = static_cast<const \
Analitza::Matrix*>(vector->at(0));
- const int blocklength = isVector? currentFirstBlock->columnCount() : \
currentFirstBlock->rowCount();
-
- for (int blockIndex = 0; blockIndex < firstVectorSize && isCorrect; \
++blockIndex) {
- if (vector->at(blockIndex)->type() == Analitza::Object::matrix) {
- const Analitza::Matrix* block = static_cast<const \
Analitza::Matrix*>(vector->at(blockIndex));
-
- if (block->rowCount() > 0 && block->columnCount() > 0) {
- const int currentlength = isVector? block->columnCount() : \
block->rowCount();
- const int currentpattern = isVector? block->rowCount() : \
block->columnCount();
-
- if (currentlength != blocklength) {
- isCorrect = false;
- ret.addError(QCoreApplication::tr("cols cada block de row debe ser \
igual de row a row"));
- } else if (blockpattern[blockIndex] != currentpattern) {
- isCorrect = false;
- ret.addError(QCoreApplication::tr("bloques rows .. altura o numrows \
debe ser same"));
- }
- } else {
- isCorrect = false;
- ret.addError(QCoreApplication::tr("no se aceptan bloques/matrices \
vacias"));
- }
- } else {
- isCorrect = false;
- ret.addError(QCoreApplication::tr("no se aceptan bloques/matrices \
vacias"));
- }
- }
-
- if (isCorrect)
- if (isVector)
- ncols += blocklength;
- else
- nrows += blocklength;
- } else {
- isCorrect = false;
- ret.addError(QCoreApplication::tr("all argument (vec or row) must have the \
same size"));
- }
- } else {
- ret.addError("we dont allow empty vector or rows"); // TODO better message
- isCorrect = false;
- }
- } else {
- isCorrect = false;
- ret.addError(QCoreApplication::tr("all argument must be of the same type: all \
rows or all vectors"));
- }
- }
-
- if (isCorrect) {
- Analitza::Matrix *matrix = new Analitza::Matrix();
-
- QVector< QVector< const Analitza::Object* > > objmatrix(nrows, QVector< const \
Analitza::Object* >(ncols, 0));
-
- int nrowsoffset = isVector? nrows : 0;
- int ncolsoffset = isVector? 0 : ncols;
-
- for (int argIndex = 0; argIndex < nargs && isCorrect; ++argIndex) {
- const Analitza::Vector *vector = static_cast<const \
Analitza::Vector*>(args.at(argIndex).tree());
-
- int blockpattern = 0;
-
- if (isVector)
- nrowsoffset = 0;
- else
- ncolsoffset = 0;
-
- for (int blockIndex = 0; blockIndex < firstVectorSize && isCorrect; \
++blockIndex) {
- const Analitza::Matrix* block = static_cast<const \
Analitza::Matrix*>(vector->at(blockIndex));
- const int m = block->rowCount();
- const int n = block->columnCount();
-
- blockpattern = isVector? n : m;
-
- for (int i = 0; i < m; ++i)
- for (int j = 0; j < n; ++j)
- objmatrix[i+nrowsoffset][j+ncolsoffset] = block->at(i,j);
-
- if (isVector)
- nrowsoffset += m;
- else if (blockIndex == 0) // el patron de cols se define en el primer \
matrixrow
- ncolsoffset += n;
- }
-
- if (!isVector)
- nrowsoffset += blockpattern;
- else if (argIndex == 0)
- ncolsoffset += blockpattern;
- }
-
- for (int i = 0; i < nrows; ++i) {
- Analitza::MatrixRow *row = new Analitza::MatrixRow(ncols);
-
- for (int j = 0; j < ncols; ++j)
- row->appendBranch(objmatrix[i][j]->copy());
-
- matrix->appendBranch(row);
- }
-
- ret.setTree(matrix);
-
- return ret;
- }
- } break;
- default: { // try to build a normal matrix
-
-
-
-
- bool allrows = true; // assumes all are rows
- Analitza::Matrix *matrix = new Analitza::Matrix();
+ bool iscorrect = true; // assumes all are rows
+ Analitza::Matrix *matrix = new Analitza::Matrix();
+
+ for (int i = 0; i < nargs && iscorrect; ++i) {
+ if (args.at(i).tree()->type() == firstArgType)
+ {
+ const Analitza::MatrixRow *row = static_cast<const \
Analitza::MatrixRow*>(args.at(i).tree());
- for (int i = 0; i < nargs && allrows; ++i) {
- if (args.at(i).tree()->type() == firstArgType)
- {
- const Analitza::MatrixRow *row = static_cast<const \
Analitza::MatrixRow*>(args.at(i).tree());
-
- if (row->size() == firstVectorSize)
- matrix->appendBranch(row->copy());
- else {
- allrows = false;
- ret.addError(QCoreApplication::tr("All matrixrow elements must have the same \
size"));
- }
- }
- else
- allrows = false;
+ if (row->size() == firstVectorSize)
+ matrix->appendBranch(row->copy());
+ else {
+ iscorrect = false;
+ ret.addError(QCoreApplication::tr("All matrixrow elements must have the same \
size")); }
+ }
+ else {
+ iscorrect = false;
+ ret.addError(QCoreApplication::tr("Not all are rows or vectors"));
+ }
+ }
+
+ if (!ret.isCorrect())
+ delete matrix;
+ else if (iscorrect) {
+ if (isVector) {
+ QString* error=0;
+ ret.setTree(Analitza::Operations::reduceUnary(Analitza::Operator::transpose, \
matrix, &error)); + delete matrix;
- if (!ret.isCorrect()) {
- delete matrix;
-
- return ret;
- } else if (allrows) {
- if (isVector) {
- QString* error=0;
- ret.setTree(Analitza::Operations::reduceUnary(Analitza::Operator::transpose, \
matrix, &error));
- delete matrix;
-
- Q_ASSERT(error == 0);
- } else
- ret.setTree(matrix);
-
- return ret;
- } else {
- ret.addError("Every argument must be a matrixrow element");
- delete matrix;
- }
- } break;
+ Q_ASSERT(error == 0);
+ } else
+ ret.setTree(matrix);
+ } else {
+ ret.addError("Every argument must be a matrixrow element");
+ delete matrix;
}
} else
ret.addError("we dont allow empty vector or rows"); // TODO better message
} else
ret.addError("to build a matrix use vector or rows as args"); // TODO better \
message
- //END commom
return ret;
}
@@ -357,31 +195,6 @@ Expression MatrixCommand::operator()(const QList< \
Analitza::Expression >& args) //END FillMatrixConstructor
-//BEGIN ZeroMatrixConstructor
-
-const QString ZeroMatrixCommand::id = QString("zeromatrix");
-const ExpressionType ZeroMatrixCommand::type = \
ExpressionType(ExpressionType::Lambda)
-.addParameter(ExpressionType(ExpressionType::Any, \
ExpressionType(ExpressionType::Value)))
-// .addParameter(ExpressionType(ExpressionType::Any))
-.addParameter(MatrixType);
-
-Expression ZeroMatrixCommand::operator()(const QList< Analitza::Expression >& args)
-{
-// if (args.size() != 2) {
-// Expression ret;
-// ret.addError(QCoreApplication::tr("Invalid parameter count for '%2'. Should \
have %1 parameters.").arg(2).arg(ZeroMatrixCommand::id));
-//
-// return ret;
-// }
-
- MatrixCommand fillMatrix;
-
- return fillMatrix(args);
-}
-
-//END ZeroMatrixConstructor
-
-
//BEGIN IdentityMatrixConstructor
const QString IdentityMatrixCommand::id = QString("identitymatrix");
diff --git a/analitza/commands/matrixcommands.h b/analitza/commands/matrixcommands.h
index 10aa20c..18f3612 100644
--- a/analitza/commands/matrixcommands.h
+++ b/analitza/commands/matrixcommands.h
@@ -25,7 +25,6 @@ namespace Analitza {
class Expression;
};
-//TODO split this class in matrix and blockmatrix ...
class MatrixCommand: public Analitza::FunctionDefinition
{
public:
@@ -35,15 +34,6 @@ public:
static const Analitza::ExpressionType type;
};
-class ZeroMatrixCommand: public Analitza::FunctionDefinition
-{
-public:
- virtual Analitza::Expression operator()(const QList< Analitza::Expression >& args);
-
- static const QString id;
- static const Analitza::ExpressionType type;
-};
-
class IdentityMatrixCommand: public Analitza::FunctionDefinition
{
public:
@@ -53,7 +43,6 @@ public:
static const Analitza::ExpressionType type;
};
-//TODO split this class in diag and blockdiag ...
class DiagonalMatrixCommand: public Analitza::FunctionDefinition
{
public:
diff --git a/analitza/commands/matrixqueries.cpp \
b/analitza/commands/matrixqueries.cpp index 1397d4e..48a8af3 100644
--- a/analitza/commands/matrixqueries.cpp
+++ b/analitza/commands/matrixqueries.cpp
@@ -81,17 +81,11 @@ static const QString MATRIX_SIZE_ERROR_MESSAGE = \
QCoreApplication::tr("Matrix di //BEGIN IsZeroMatrix
const QString IsZeroMatrixCommand::id = QString("iszeromatrix");
-const ExpressionType IsZeroMatrixCommand::type = variadicFunctionType(VectorType);
+const ExpressionType IsZeroMatrixCommand::type = functionType(MatrixType, \
ValueType);
Expression IsZeroMatrixCommand::operator()(const QList< Analitza::Expression >& \
args) {
- if (args.size() != 1) {
- Expression ret;
- ret.addError(QCoreApplication::tr("Invalid parameter count for '%2'. Should have \
%1 parameters.").arg(1).arg(IsZeroMatrixCommand::id));
-
- return ret;
- }
-
+ qDebug() << "bam bam " << args.first().toString(); //TODO last test
return Expression(new Analitza::Cn(static_cast<const \
Analitza::Matrix*>(args.first().tree())->isZero())); }
@@ -99,17 +93,10 @@ Expression IsZeroMatrixCommand::operator()(const QList< \
Analitza::Expression >&
const QString IsIdentityMatrixCommand::id = QString("isidentitymatrix");
-const ExpressionType IsIdentityMatrixCommand::type = \
variadicFunctionType(VectorType); +const ExpressionType IsIdentityMatrixCommand::type \
= functionType(MatrixType, ValueType);
Expression IsIdentityMatrixCommand::operator()(const QList< Analitza::Expression >& \
args) {
- if (args.size() != 1) {
- Expression ret;
- ret.addError(QCoreApplication::tr("Invalid parameter count for '%2'. Should have \
%1 parameters.").arg(1).arg(IsIdentityMatrixCommand::id));
-
- return ret;
- }
-
return Expression(new \
Analitza::Cn(AnalitzaUtils::isIdentityMatrix(static_cast<const \
Analitza::Matrix*>(args.first().tree())))); }
diff --git a/analitza/commands/vectorcommands.h b/analitza/commands/vectorcommands.h
index 7fa52ac..5845428 100644
--- a/analitza/commands/vectorcommands.h
+++ b/analitza/commands/vectorcommands.h
@@ -33,7 +33,8 @@ class Expression;
*
* VectorCommand constructs a vector by 2 ways:
*
- * The first way creates a vector from a list, with the same list elements.
+ * The first way creates a vector from a list, with the same list elements.
+ * For example:
* \code vector(range(5)) \endcode
* constructs
* \code vector { 1, 2, 3, 4, 5 } \endcode
diff --git a/analitza/tests/matrixtest.cpp b/analitza/tests/matrixtest.cpp
index 1645f72..a86b163 100644
--- a/analitza/tests/matrixtest.cpp
+++ b/analitza/tests/matrixtest.cpp
@@ -115,7 +115,7 @@ void MatrixTest::testCorrect_data()
script.clear();
script << "A := matrix{matrixrow{2, 3}, matrixrow{-5, 1}}";
script << "B := matrix{matrixrow{12, 13}, matrixrow{-15, 11}}";
- script << "matrix(matrixrow{A, B})";
+ script << "blockmatrix(matrixrow{A, B})";
QTest::newRow("simple block matrix") << script << "matrix { matrixrow { 2, 3, 12, \
13 }, matrixrow { -5, 1, -15, 11 } }";
const QString blockmatrix = "matrix { matrixrow { 1, 8, 7, 6 }, matrixrow { 3, 5, \
0, 2 }, matrixrow { 1, 4, 9, 3 } }"; @@ -125,7 +125,7 @@ void \
MatrixTest::testCorrect_data() script << "B := matrix{matrixrow{7,6}, \
matrixrow{0,2}}"; script << "C := matrix(matrixrow{1,4})";
script << "D := matrix(vector{9}, vector{3})";
- script << "matrix(matrixrow{A, B}, matrixrow{C, D})";
+ script << "blockmatrix(matrixrow{A, B}, matrixrow{C, D})";
QTest::newRow("block matrix 4 blocks conf 1") << script << blockmatrix;
script.clear();
@@ -133,7 +133,7 @@ void MatrixTest::testCorrect_data()
script << "B := matrix(vector{6,2})";
script << "C := matrix(matrixrow{1,4,9})";
script << "D := matrix(1,1,3)";
- script << "matrix(matrixrow{A, B}, matrixrow{C, D})";
+ script << "blockmatrix(matrixrow{A, B}, matrixrow{C, D})";
QTest::newRow("block matrix by rows, conf 2") << script << blockmatrix;
script.clear();
@@ -143,7 +143,7 @@ void MatrixTest::testCorrect_data()
script << "D := matrix(matrixrow{0,2})";
script << "E := matrix(vector{1},vector{4})";
script << "F := matrix{matrixrow{9,3}}";
- script << "matrix(matrixrow{A, B}, matrixrow{C, D}, matrixrow{E, F})";
+ script << "blockmatrix(matrixrow{A, B}, matrixrow{C, D}, matrixrow{E, F})";
QTest::newRow("block matrix by rows, conf 3") << script << blockmatrix;
script.clear();
@@ -151,7 +151,7 @@ void MatrixTest::testCorrect_data()
script << "B := matrix(matrixrow{1,4})";
script << "C := matrix{matrixrow{7,6}, matrixrow{0,2}}";
script << "D := matrix(vector{9}, vector{3})";
- script << "matrix(vector{A, B}, vector{C, D})";
+ script << "blockmatrix(vector{A, B}, vector{C, D})";
QTest::newRow("block matrix by cols, conf 1") << script << blockmatrix;
script.clear();
@@ -161,11 +161,11 @@ void MatrixTest::testCorrect_data()
script << "D := matrix{matrixrow{7,6}}";
script << "E := matrix(matrixrow{0,2})";
script << "F := matrix(matrixrow{9,3})";
- script << "matrix(vector{A, B, C}, vector{D, E, F})";
+ script << "blockmatrix(vector{A, B, C}, vector{D, E, F})";
QTest::newRow("block matrix by cols, conf 2") << script << blockmatrix;
script.clear();
- script << "zeromatrix(2,5)";
+ script << "matrix(2,5)";
QTest::newRow("simple 0") << script << "matrix { matrixrow { 0, 0, 0, 0, 0 }, \
matrixrow { 0, 0, 0, 0, 0 } }";
script.clear();
@@ -181,7 +181,7 @@ void MatrixTest::testCorrect_data()
QTest::newRow("0*I") << script << "matrix { matrixrow { 0, 0, 0 }, matrixrow { 0, \
0, 0 }, matrixrow { 0, 0, 0 } }";
script.clear();
- script << "zeromatrix(3,3) + identitymatrix(3)";
+ script << "matrix(3,3) + identitymatrix(3)";
QTest::newRow("0 + I") << script << "matrix { matrixrow { 1, 0, 0 }, matrixrow { 0, \
1, 0 }, matrixrow { 0, 0, 1 } }";
script.clear();
@@ -210,14 +210,14 @@ void MatrixTest::testCorrect_data()
QTest::newRow("selector diag by vector") << script << "-0.6";
script.clear();
- script << "diag(matrix{matrixrow{1, 3}, matrixrow{-6, 8}}, matrix{matrixrow{5, 6}, \
matrixrow{14, -1.2}})"; + script << "blockdiag(matrix{matrixrow{1, 3}, matrixrow{-6, \
8}}, matrix{matrixrow{5, 6}, matrixrow{14, -1.2}})"; QTest::newRow("simple block \
diagonal") << script << "matrix { matrixrow { 1, 3, 0, 0 }, matrixrow { -6, 8, 0, 0 \
}, matrixrow { 0, 0, 5, 6 }, matrixrow { 0, 0, 14, -1.2 } }";
script.clear();
script << "I := identitymatrix(3)";
script << "A := matrix(2,3, -6)";
script << "B := 3*I";
- script << "diag(I, A, B)";
+ script << "blockdiag(I, A, B)";
QTest::newRow("block diagonal") << script << "matrix { matrixrow { 1, 0, 0, 0, 0, \
0, 0, 0, 0 }, matrixrow { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, matrixrow { 0, 0, 1, 0, 0, 0, \
0, 0, 0 }, matrixrow { 0, 0, 0, -6, -6, -6, 0, 0, 0 }, matrixrow { 0, 0, 0, -6, -6, \
-6, 0, 0, 0 }, matrixrow { 0, 0, 0, 0, 0, 0, 3, 0, 0 }, matrixrow { 0, 0, 0, 0, 0, 0, \
0, 3, 0 }, matrixrow { 0, 0, 0, 0, 0, 0, 0, 0, 3 } }";
script.clear();
@@ -255,7 +255,7 @@ void MatrixTest::testCorrect_data()
script << "B := matrix(3,3, 4.5)";
script << "D := diag(v)";
script << "I := identitymatrix(3)";
- script << "O := zeromatrix(3,3)";
+ script << "O := matrix(3,3)";
script << "T := tridiag(2,1,8,3)";
script << "A + B + D - cos(pi)*I + O + T";
QTest::newRow("complex exp") << script << "matrix { matrixrow { 11.5, 11.5, 6.5 }, \
matrixrow { 10.5, 11.5, 9.3 }, matrixrow { 10.3, -8.5, 6.5 } }"; @@ -367,7 +367,7 @@ \
void MatrixTest::testCorrect_data() QTest::newRow("is not diag") << script << \
"false";
script.clear();
- script << "iszeromatrix(zeromatrix(8,5))";
+ script << "iszeromatrix(matrix(8,5))";
QTest::newRow("is zero matrix") << script << "true";
script.clear();
@@ -421,31 +421,33 @@ void MatrixTest::testIncorrect_data()
QTest::newRow("matrix: fill empty matrix result") << "matrix(0, 0, sin(1))";
QTest::newRow("matrix: not all vectors") << "matrix(vector{1}, 3)";
QTest::newRow("matrix: not all matrixrow elements") << "matrix(matrixrow{1}, \
list{2})"; + QTest::newRow("matrix: not all matrixrow elements 2") << \
"matrix(matrixrow{1}, vector{2})"; QTest::newRow("matrix: neg square") << \
"matrix(-9)";
- QTest::newRow("matrix: bad block matrix size") << "matrix(vector{zeromatrix(1,2), \
zeromatrix(32,13)})";
- QTest::newRow("matrix: bad block matrix args size") << \
"matrix(vector{zeromatrix(1,2), zeromatrix(32,13)}, \
vector{zeromatrix(7,13)})";
- QTest::newRow("matrix: bad block matrix args type 1") << \
"matrix(vector{zeromatrix(32,13), list{23}}, vector{zeromatrix(7,13), \
zeromatrix(32,1)})"; + QTest::newRow("matrix: bad block matrix size") << \
"blockmatrix(vector{matrix(1,2), matrix(32,13)})"; + QTest::newRow("matrix: bad block \
matrix args size") << "blockmatrix(vector{matrix(1,2), matrix(32,13)}, \
vector{matrix(7,13)})"; + QTest::newRow("matrix: bad block matrix args type 1") << \
"blockmatrix(vector{matrix(32,13), list{23}}, vector{matrix(7,13), matrix(32,1)})";
//TODO split block constructor
//TODO and refine type system for variadic functios with same type as args ... can \
compare can reduce check is Any and Any,Type is NOT a error // \
QTest::newRow("matrix: bad block matrix args type 2") << "matrix(vector{list{23}, \
zeromatrix(32,13)}, vector{zeromatrix(7,13), zeromatrix(32,1)})";
- QTest::newRow("zero matrix: empty matrix result") << "zeromatrix(0, 0)";
- QTest::newRow("zero matrix: bad number of args") << "zeromatrix()";
- QTest::newRow("zero matrix: bad dim") << "zeromatrix(23, -3.5)";
+ QTest::newRow("zero matrix: empty matrix result") << "matrix(0, 0)";
+ QTest::newRow("zero matrix: bad number of args") << "matrix()";
+ QTest::newRow("zero matrix: bad dim") << "matrix(23, -3.5)";
+ QTest::newRow("zero matrix: bad dim2") << "matrix(-23, 5)";
QTest::newRow("identity matrix: matrix result") << "identitymatrix(0)";
QTest::newRow("diag: 0 args") << "diag()";
QTest::newRow("diag: bad arg, one empty matrix") << "diag(identitymatrix(0))";
QTest::newRow("diag: bad diag index") << "diag(matrix(4,6,3.2), -98)";
QTest::newRow("diag: bad diag index type") << "diag(matrix(4,6,3.2), list{-98})";
- QTest::newRow("diag: bad block diag, empty matrix 1") << "diag(zeromatrix(0,0), \
matrix(2,2,1))"; + QTest::newRow("diag: bad block diag, empty matrix 1") << \
"diag(matrix(0,0), matrix(2,2,1))"; QTest::newRow("diag: bad block diag, empty \
matrix 2") << "diag(matrix{matrixrow{1}}, tridiag(1,2,3,0))"; \
QTest::newRow("tridiag: empty matrix result") << "tridiag(1,2,3,0)"; \
QTest::newRow("tridiag: bad number of args") << "tridiag(1,2,2)"; \
QTest::newRow("iszeromatrix: bad number of args") << "iszeromatrix()"; \
QTest::newRow("isdiag: bad number of args") << "isdiag(matrix{matrixrow{1}}, 2)"; \
QTest::newRow("isdiag: bad number of args2") << "isdiag()";
- QTest::newRow("bad dimensions:2x2identitymatrix and 2x1zeromatrix") << \
"2*(identitymatrix(2) + zeromatrix(2,1))"; + QTest::newRow("bad \
dimensions:2x2identitymatrix and 2x1zeromatrix") << "2*(identitymatrix(2) + \
matrix(2,1))"; QTest::newRow("bad dimensions:2x2identitymatrix and -2x2matrix") << \
"2*(identitymatrix(2) + matrix(-2, 2,1))";
//TODO incorect block matrix
@@ -453,13 +455,14 @@ void MatrixTest::testIncorrect_data()
//TODO better names
QTest::newRow("bad") << "vector(list{23},4)";
QTest::newRow("bad2") << "vector(4, list{23}, 44)";
- QTest::newRow("bad0") << "zeromatrix(list{3}, 31)";
+ QTest::newRow("bad0") << "matrix(list{3}, 31)";
QTest::newRow("badlist") << "range(list{3}, 31, true)";
QTest::newRow("badlist") << "range(list{3}, 31)";
QTest::newRow("badlist") << "range(2,3,vector{3}, list{2}, 4)";
QTest::newRow("badlist") << "range(2,3,9, list{2}, 4)";
QTest::newRow("badlist") << "range(2,3,9, 9, vector{4})";
QTest::newRow("badlist4") << "range(2,3,9, 9, 3,4,5,5)";
+ QTest::newRow("zero bad") << "iszeromatrix(matrix(-8,5))"; //TODO last test
}
void MatrixTest::testIncorrect()
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic