+ unsigned result = 0;
+ matrix m(3,3);
+ symbol a("a"), b("b"), c("c");
+ symbol d("d"), e("e"), f("f");
+ symbol g("g"), h("h"), i("i");
+ m.set(0,0,a).set(0,1,b).set(0,2,c);
+ m.set(1,0,d).set(1,1,e).set(1,2,f);
+ m.set(2,0,g).set(2,1,h).set(2,2,i);
+ matrix m_i = m.inverse();
+ ex det = m.determinant();
+
+ if ((normal(m_i(0,0)*det) != (e*i-f*h)) ||
+ (normal(m_i(0,1)*det) != (c*h-b*i)) ||
+ (normal(m_i(0,2)*det) != (b*f-c*e)) ||
+ (normal(m_i(1,0)*det) != (f*g-d*i)) ||
+ (normal(m_i(1,1)*det) != (a*i-c*g)) ||
+ (normal(m_i(1,2)*det) != (c*d-a*f)) ||
+ (normal(m_i(2,0)*det) != (d*h-e*g)) ||
+ (normal(m_i(2,1)*det) != (b*g-a*h)) ||
+ (normal(m_i(2,2)*det) != (a*e-b*d))) {
+ clog << "inversion of 3x3 matrix " << m
+ << " erroneously returned " << m_i << endl;
+ ++result;
+ }
+
+ return result;
+}
+
+static unsigned matrix_solve2()
+{
+ // check the solution of the multiple system A*X = B:
+ // [ 1 2 -1 ] [ x0 y0 ] [ 4 0 ]
+ // [ 1 4 -2 ]*[ x1 y1 ] = [ 7 0 ]
+ // [ a -2 2 ] [ x2 y2 ] [ a 4 ]
+ unsigned result = 0;
+ symbol a("a");
+ symbol x0("x0"), x1("x1"), x2("x2");
+ symbol y0("y0"), y1("y1"), y2("y2");
+ matrix A(3,3);
+ A.set(0,0,1).set(0,1,2).set(0,2,-1);
+ A.set(1,0,1).set(1,1,4).set(1,2,-2);
+ A.set(2,0,a).set(2,1,-2).set(2,2,2);
+ matrix B(3,2);
+ B.set(0,0,4).set(1,0,7).set(2,0,a);
+ B.set(0,1,0).set(1,1,0).set(2,1,4);
+ matrix X(3,2);
+ X.set(0,0,x0).set(1,0,x1).set(2,0,x2);
+ X.set(0,1,y0).set(1,1,y1).set(2,1,y2);
+ matrix cmp(3,2);
+ cmp.set(0,0,1).set(1,0,3).set(2,0,3);
+ cmp.set(0,1,0).set(1,1,2).set(2,1,4);
+ matrix sol(A.solve(X, B));
+ for (unsigned ro=0; ro<3; ++ro)
+ for (unsigned co=0; co<2; ++co)
+ if (cmp(ro,co) != sol(ro,co))
+ result = 1;
+ if (result) {
+ clog << "Solving " << A << " * " << X << " == " << B << endl
+ << "erroneously returned " << sol << endl;
+ }
+
+ return result;
+}
+
+static unsigned matrix_evalm()
+{
+ unsigned result = 0;
+
+ matrix S(2, 2, lst(
+ 1, 2,
+ 3, 4
+ )), T(2, 2, lst(
+ 1, 1,
+ 2, -1
+ )), R(2, 2, lst(
+ 27, 14,
+ 36, 26
+ ));
+
+ ex e = ((S + T) * (S + 2*T));
+ ex f = e.evalm();
+ if (!f.is_equal(R)) {
+ clog << "Evaluating " << e << " erroneously returned " << f << " instead of " << R << endl;
+ result++;
+ }
+
+ return result;
+}
+
+static unsigned matrix_rank()
+{
+ unsigned result = 0;
+ symbol x("x"), y("y");
+ matrix m(3,3);
+
+ // the zero matrix always has rank 0
+ if (m.rank() != 0) {
+ clog << "The rank of " << m << " was not computed correctly." << endl;
+ ++result;
+ }
+
+ // a trivial rank one example
+ m = 1, 0, 0,
+ 2, 0, 0,
+ 3, 0, 0;
+ if (m.rank() != 1) {
+ clog << "The rank of " << m << " was not computed correctly." << endl;
+ ++result;
+ }
+
+ // an example from Maple's help with rank two
+ m = x, 1, 0,
+ 0, 0, 1,
+ x*y, y, 1;
+ if (m.rank() != 2) {
+ clog << "The rank of " << m << " was not computed correctly." << endl;
+ ++result;
+ }
+
+ // the 3x3 unit matrix has rank 3
+ m = ex_to<matrix>(unit_matrix(3,3));
+ if (m.rank() != 3) {
+ clog << "The rank of " << m << " was not computed correctly." << endl;
+ ++result;
+ }
+
+ return result;