]> www.ginac.de Git - ginac.git/blobdiff - check/exam_matrices.cpp
- Complete revamp of methods in class matrix. Some redundant (and poor)
[ginac.git] / check / exam_matrices.cpp
index 81fed94ed2265fd49d8314e10eaa0d586bc6c0c6..9f17bb3d52ccedef246b35701d0e037c7cda64dc 100644 (file)
@@ -55,7 +55,7 @@ static unsigned matrix_determinants(void)
     m3.set(0,0,a).set(0,1,b).set(0,2,c);
     m3.set(1,0,d).set(1,1,e).set(1,2,f);
     m3.set(2,0,g).set(2,1,h).set(2,2,i);
-    det = m3.determinant().expand();
+    det = m3.determinant();
     if (det != (a*e*i - a*f*h - d*b*i + d*c*h + g*b*f - g*c*e)) {
         clog << "determinant of 3x3 matrix " << m3
              << " erroneously returned " << det << endl;
@@ -74,15 +74,19 @@ static unsigned matrix_determinants(void)
     }
     
     // check dense symbolic 2x2 matrix determinant
-    m2.set(0,0,a/(a-b)).set(0,1,numeric(1));
-    m2.set(1,0,b/(a-b)).set(1,1,numeric(1));
-    det = m2.determinant(true);
+    m2.set(0,0,a/(a-b)).set(0,1,1);
+    m2.set(1,0,b/(a-b)).set(1,1,1);
+    det = m2.determinant();
     if (det != 1) {
-        clog << "determinant of 2x2 matrix " << m2
-             << " erroneously returned " << det << endl;
+        if (det.normal() == 1)  // only half wrong
+            clog << "determinant of 2x2 matrix " << m2
+                 << " was returned unnormalized as " << det << endl;
+        else  // totally wrong
+            clog << "determinant of 2x2 matrix " << m2
+                 << " erroneously returned " << det << endl;
         ++result;
     }
-
+    
     // check sparse symbolic 4x4 matrix determinant
     m4.set(0,1,a).set(1,0,b).set(3,2,c).set(2,3,d);
     det = m4.determinant();
@@ -108,28 +112,31 @@ static unsigned matrix_determinants(void)
 
 static unsigned matrix_invert1(void)
 {
+    unsigned result = 0;
     matrix m(1,1);
     symbol a("a");
-
+    
     m.set(0,0,a);
     matrix m_i = m.inverse();
     
     if (m_i(0,0) != pow(a,-1)) {
         clog << "inversion of 1x1 matrix " << m
              << " erroneously returned " << m_i << endl;
-        return 1;
+        ++result;
     }
-    return 0;
+    
+    return result;
 }
 
 static unsigned matrix_invert2(void)
 {
+    unsigned result = 0;
     matrix m(2,2);
     symbol a("a"), b("b"), c("c"), d("d");
     m.set(0,0,a).set(0,1,b);
     m.set(1,0,c).set(1,1,d);
     matrix m_i = m.inverse();
-    ex det = m.determinant().expand();
+    ex det = m.determinant();
     
     if ((normal(m_i(0,0)*det) != d) ||
         (normal(m_i(0,1)*det) != -b) ||
@@ -137,13 +144,15 @@ static unsigned matrix_invert2(void)
         (normal(m_i(1,1)*det) != a)) {
         clog << "inversion of 2x2 matrix " << m
              << " erroneously returned " << m_i << endl;
-        return 1;
+        ++result;
     }
-    return 0;
+    
+    return result;
 }
 
 static unsigned matrix_invert3(void)
 {
+    unsigned result = 0;
     matrix m(3,3);
     symbol a("a"), b("b"), c("c");
     symbol d("d"), e("e"), f("f");
@@ -152,7 +161,7 @@ static unsigned matrix_invert3(void)
     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().normal().expand();
+    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)) ||
@@ -165,9 +174,46 @@ static unsigned matrix_invert3(void)
         (normal(m_i(2,2)*det) != (a*e-b*d))) {
         clog << "inversion of 3x3 matrix " << m
              << " erroneously returned " << m_i << endl;
-        return 1;
+        ++result;
     }
-    return 0;
+    
+    return result;
+}
+
+static unsigned matrix_solve2(void)
+{
+    // 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_misc(void)
@@ -206,11 +252,11 @@ static unsigned matrix_misc(void)
     // produce a runtime-error by inverting a singular matrix and catch it
     matrix m4(2,2);
     matrix m5;
-    bool caught=false;
+    bool caught = false;
     try {
         m5 = inverse(m4);
     } catch (std::runtime_error err) {
-        caught=true;
+        caught = true;
     }
     if (!caught) {
         cerr << "singular 2x2 matrix " << m4
@@ -227,11 +273,12 @@ unsigned exam_matrices(void)
     
     cout << "examining symbolic matrix manipulations" << flush;
     clog << "----------symbolic matrix manipulations:" << endl;
-
+    
     result += matrix_determinants();  cout << '.' << flush;
     result += matrix_invert1();  cout << '.' << flush;
     result += matrix_invert2();  cout << '.' << flush;
     result += matrix_invert3();  cout << '.' << flush;
+    result += matrix_solve2();  cout << '.' << flush;
     result += matrix_misc();  cout << '.' << flush;
     
     if (!result) {