Handle dimensional errors in matrix multiplication, including undef values. Fixes #1356

This commit is contained in:
Marius Kintel 2015-06-07 13:11:27 -04:00
parent 2745b8534c
commit 87e32efc18
4 changed files with 13 additions and 10 deletions

View file

@ -501,11 +501,8 @@ Value Value::multvecnum(const Value &vecval, const Value &numval)
return Value(dstv);
}
Value Value::multmatvec(const Value &matrixval, const Value &vectorval)
Value Value::multmatvec(const VectorType &matrixvec, const VectorType &vectorvec)
{
const VectorType &matrixvec = matrixval.toVector();
const VectorType &vectorvec = vectorval.toVector();
// Matrix * Vector
VectorType dstv;
for (size_t i=0;i<matrixvec.size();i++) {
@ -525,10 +522,8 @@ Value Value::multmatvec(const Value &matrixval, const Value &vectorval)
return Value(dstv);
}
Value Value::multvecmat(const Value &vectorval, const Value &matrixval)
Value Value::multvecmat(const VectorType &vectorvec, const VectorType &matrixvec)
{
const VectorType &vectorvec = vectorval.toVector();
const VectorType &matrixvec = matrixval.toVector();
assert(vectorvec.size() == matrixvec.size());
// Vector * Matrix
VectorType dstv;
@ -583,7 +578,9 @@ Value Value::operator*(const Value &v) const
// Matrix * Matrix
VectorType dstv;
BOOST_FOREACH(const Value &srcrow, vec1) {
dstv.push_back(multvecmat(srcrow, vec2));
const VectorType &srcrowvec = srcrow.toVector();
if (srcrowvec.size() != vec2.size()) return Value::undefined;
dstv.push_back(multvecmat(srcrowvec, vec2));
}
return Value(dstv);
}

View file

@ -162,8 +162,8 @@ public:
private:
static Value multvecnum(const Value &vecval, const Value &numval);
static Value multmatvec(const Value &matrixval, const Value &vectorval);
static Value multvecmat(const Value &vectorval, const Value &matrixval);
static Value multmatvec(const VectorType &matrixvec, const VectorType &vectorvec);
static Value multvecmat(const VectorType &vectorvec, const VectorType &matrixvec);
Variant value;
};

View file

@ -37,4 +37,9 @@ echo(str("Testing alternate asymmetric matrix * matrix: ",mb5*ma5));
echo(str(" Bounds check: ",ma5*ma4));
ma6=[ [ 1, 2 ], undef ];
mb6=[ [ 4 ], [ 5 ] ];
echo(str("Testing matrix * matrix with undef elements: ",ma6*mb6));
cube(1.0);

View file

@ -8,3 +8,4 @@ ECHO: "Testing id matrix * id matrix: [[1, 0], [0, 1]]"
ECHO: "Testing asymmetric matrix * matrix: [[2, 1], [-1, 0]]"
ECHO: "Testing alternate asymmetric matrix * matrix: [[1, 0, 1], [0, 1, -1], [1, 1, 0]]"
ECHO: " Bounds check: undef"
ECHO: "Testing matrix * matrix with undef elements: undef"