Dim mul_dim(const Dim& l, const Dim& r) { if (l.dim1!=1 || r.dim1!=1) throw DimException("cannot multiply a matrix array"); if (l.type()==Dim::SCALAR) // scalar multiplication. return r; else { if (l.dim3!=r.dim2) { if (l.dim2==r.dim2) { if (r.dim3==1) // dot product return Dim::scalar(); else // vector-matrix product return Dim::row_vec(r.dim3); } throw DimException("mismatched dimensions in matrix multiplication"); } else { if (l.dim2==1) if (r.dim3==1) return Dim::scalar(); else return Dim::row_vec(r.dim3); else if (r.dim3==1) return Dim::col_vec(l.dim2); else return Dim::matrix(l.dim2,r.dim3); } } }
void TestDim::test01() { Dim d; TEST_ASSERT(d.dim1==1); TEST_ASSERT(d.dim2==1); TEST_ASSERT(d.dim3==1); TEST_ASSERT(d==Dim::scalar()); TEST_ASSERT(d.is_scalar()); TEST_ASSERT(d.type()==Dim::SCALAR); TEST_ASSERT(Dim(d)==d); TEST_ASSERT((Dim::scalar()=d)==d); }
void TestDim::scalar() { Dim d; CPPUNIT_ASSERT(d.nb_rows()==1); CPPUNIT_ASSERT(d.nb_cols()==1); CPPUNIT_ASSERT(d.size()==1); CPPUNIT_ASSERT(d==Dim::scalar()); CPPUNIT_ASSERT(d.is_scalar()); CPPUNIT_ASSERT(!d.is_vector()); CPPUNIT_ASSERT(!d.is_matrix()); CPPUNIT_ASSERT(d.type()==Dim::SCALAR); CPPUNIT_ASSERT(Dim(d)==d); CPPUNIT_ASSERT((Dim::scalar()=d)==d); }