]> www.ginac.de Git - ginac.git/blob - check/check_matrices.cpp
71893bb129ff5cbe6950308ef06b7fd7d4bf2d8b
[ginac.git] / check / check_matrices.cpp
1 /** @file check_matrices.cpp
2  *
3  *  Here we test manipulations on GiNaC's symbolic matrices. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "checks.h"
24
25 // determinants of some sparse symbolic size x size matrices over
26 // an integral domain.
27 static unsigned integdom_matrix_determinants(void)
28 {
29     unsigned result = 0;
30     symbol a("a");
31     
32     for (int size=3; size<17; ++size) {
33         matrix A(size,size);
34         for (int r=0; r<size-1; ++r) {
35             // populate one element in each row:
36             A.set(r,unsigned(rand()%size),dense_univariate_poly(a,5));
37         }
38         for (int c=0; c<size; ++c) {
39             // set the last line to a linear combination of two other lines
40             // to guarantee that the determinant vanishes:
41             A.set(size-1,c,A(0,c)-A(size-2,c));
42         }
43         if (!A.determinant().is_zero()) {
44             clog << "Determinant of " << size << "x" << size << " matrix "
45                  << endl << A << endl
46                  << "was not found to vanish!" << endl;
47             ++result;
48         }
49     }
50     
51     return result;
52 }
53
54 static unsigned rational_matrix_determinants(void)
55 {
56     unsigned result = 0;
57     symbol a("a"), b("b"), c("c");
58     
59     for (int size=3; size<13; ++size) {
60         matrix A(size,size);
61         for (int r=0; r<size-1; ++r) {
62             // populate one element in each row:
63             // FIXME: the line using sparse_tree() should be used:
64             // A.set(r,unsigned(rand()%size),sparse_tree(a, b, c, 3, true, true)/sparse_tree(a, b, c, 2, true, true));
65             A.set(r,unsigned(rand()%size),dense_univariate_poly(a,4)/dense_univariate_poly(a,2));
66         }
67         for (int c=0; c<size; ++c) {
68             // set the last line to a linear combination of two other lines
69             // to guarantee that the determinant vanishes:
70             A.set(size-1,c,A(0,c)-A(size-2,c));
71         }
72         if (!A.determinant().is_zero()) {
73             clog << "Determinant of " << size << "x" << size << " matrix "
74                  << endl << A << endl
75                  << "was not found to vanish!" << endl;
76             ++result;
77         }
78     }
79     
80     return result;
81 }
82
83 unsigned check_matrices(void)
84 {
85     unsigned result = 0;
86     
87     cout << "checking symbolic matrix manipulations" << flush;
88     clog << "---------symbolic matrix manipulations:" << endl;
89     
90     result += integdom_matrix_determinants();  cout << '.' << flush;
91     result += rational_matrix_determinants();  cout << '.' << flush;
92     
93     if (!result) {
94         cout << " passed " << endl;
95         clog << "(no output)" << endl;
96     } else {
97         cout << " failed " << endl;
98     }
99     
100     return result;
101 }