]> www.ginac.de Git - ginac.git/blob - check/check_matrices.cpp
- Fixed the bug that broke xloop's po_redux.
[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 matrices with coefficients in
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<20; ++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 is zero:
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 /* determinants of some sparse symbolic matrices with multivariate rational
55  * function coefficients. */
56 static unsigned rational_matrix_determinants(void)
57 {
58     unsigned result = 0;
59     symbol a("a"), b("b"), c("c");
60
61     for (int size=3; size<8; ++size) {
62         matrix A(size,size);
63         for (int r=0; r<size-1; ++r) {
64             // populate one element in each row:
65             ex numer = sparse_tree(a, b, c, 4, false, false, false);
66             ex denom;
67             do {
68                 denom = sparse_tree(a, b, c, 1, false, false, false);
69             } while (denom.is_zero());
70             A.set(r,unsigned(rand()%size),numer/denom);
71         }
72         for (int c=0; c<size; ++c) {
73             // set the last line to a linear combination of two other lines
74             // to guarantee that the determinant is zero:
75             A.set(size-1,c,A(0,c)-A(size-2,c));
76         }
77         if (!A.determinant().is_zero()) {
78             clog << "Determinant of " << size << "x" << size << " matrix "
79                  << endl << A << endl
80                  << "was not found to vanish!" << endl;
81             ++result;
82         }
83     }
84     
85     return result;
86 }
87
88 /* Some quite wild determinants with functions and stuff like that. */
89 static unsigned wild_matrix_determinants(void)
90 {
91     unsigned result = 0;
92     symbol a("a"), b("b"), c("c");
93     
94     for (int size=3; size<6; ++size) {
95         matrix A(size,size);
96         for (int r=0; r<size-1; ++r) {
97             // populate one element in each row:
98             ex numer = sparse_tree(a, b, c, 3, true, true, false);
99             ex denom;
100             do {
101                 denom = sparse_tree(a, b, c, 1, false, true, false);
102             } while (denom.is_zero());
103             A.set(r,unsigned(rand()%size),numer/denom);
104         }
105         for (int c=0; c<size; ++c) {
106             // set the last line to a linear combination of two other lines
107             // to guarantee that the determinant is zero:
108             A.set(size-1,c,A(0,c)-A(size-2,c));
109         }
110         if (!A.determinant().is_zero()) {
111             clog << "Determinant of " << size << "x" << size << " matrix "
112                  << endl << A << endl
113                  << "was not found to vanish!" << endl;
114             ++result;
115         }
116     }
117     
118     return result;
119 }
120
121 unsigned check_matrices(void)
122 {
123     unsigned result = 0;
124     
125     cout << "checking symbolic matrix manipulations" << flush;
126     clog << "---------symbolic matrix manipulations:" << endl;
127     
128     result += integdom_matrix_determinants();  cout << '.' << flush;
129     result += rational_matrix_determinants();  cout << '.' << flush;
130     result += wild_matrix_determinants();  cout << '.' << flush;
131     
132     if (!result) {
133         cout << " passed " << endl;
134         clog << "(no output)" << endl;
135     } else {
136         cout << " failed " << endl;
137     }
138     
139     return result;
140 }