]> www.ginac.de Git - ginac.git/blob - check/exam_matrices.cpp
- Fixed a logic error in numeric::info().
[ginac.git] / check / exam_matrices.cpp
1 /** @file exam_matrices.cpp
2  *
3  *  Here we examine 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 <stdexcept>
24 #include "exams.h"
25
26 static unsigned matrix_determinants(void)
27 {
28     unsigned result = 0;
29     ex det;
30     matrix m1(1,1), m2(2,2), m3(3,3), m4(4,4);
31     symbol a("a"), b("b"), c("c");
32     symbol d("d"), e("e"), f("f");
33     symbol g("g"), h("h"), i("i");
34     
35     // check symbolic trivial matrix determinant
36     m1.set(0,0,a);
37     det = m1.determinant();
38     if (det != a) {
39         clog << "determinant of 1x1 matrix " << m1
40              << " erroneously returned " << det << endl;
41         ++result;
42     }
43     
44     // check generic dense symbolic 2x2 matrix determinant
45     m2.set(0,0,a).set(0,1,b);
46     m2.set(1,0,c).set(1,1,d);
47     det = m2.determinant();
48     if (det != (a*d-b*c)) {
49         clog << "determinant of 2x2 matrix " << m2
50              << " erroneously returned " << det << endl;
51         ++result;
52     }
53     
54     // check generic dense symbolic 3x3 matrix determinant
55     m3.set(0,0,a).set(0,1,b).set(0,2,c);
56     m3.set(1,0,d).set(1,1,e).set(1,2,f);
57     m3.set(2,0,g).set(2,1,h).set(2,2,i);
58     det = m3.determinant().expand();
59     if (det != (a*e*i - a*f*h - d*b*i + d*c*h + g*b*f - g*c*e)) {
60         clog << "determinant of 3x3 matrix " << m3
61              << " erroneously returned " << det << endl;
62         ++result;
63     }
64     
65     // check dense numeric 3x3 matrix determinant
66     m3.set(0,0,numeric(0)).set(0,1,numeric(-1)).set(0,2,numeric(3));
67     m3.set(1,0,numeric(3)).set(1,1,numeric(-2)).set(1,2,numeric(2));
68     m3.set(2,0,numeric(3)).set(2,1,numeric(4)).set(2,2,numeric(-2));
69     det = m3.determinant();
70     if (det != 42) {
71         clog << "determinant of 3x3 matrix " << m3
72              << " erroneously returned " << det << endl;
73         ++result;
74     }
75     
76     // check dense symbolic 2x2 matrix determinant
77     m2.set(0,0,a/(a-b)).set(0,1,numeric(1));
78     m2.set(1,0,b/(a-b)).set(1,1,numeric(1));
79     det = m2.determinant(true);
80     if (det != 1) {
81         clog << "determinant of 2x2 matrix " << m2
82              << " erroneously returned " << det << endl;
83         ++result;
84     }
85
86     // check sparse symbolic 4x4 matrix determinant
87     m4.set(0,1,a).set(1,0,b).set(3,2,c).set(2,3,d);
88     det = m4.determinant();
89     if (det != a*b*c*d) {
90         clog << "determinant of 4x4 matrix " << m4
91              << " erroneously returned " << det << endl;
92         ++result;
93     }
94     
95     // check characteristic polynomial
96     m3.set(0,0,a).set(0,1,-2).set(0,2,2);
97     m3.set(1,0,3).set(1,1,a-1).set(1,2,2);
98     m3.set(2,0,3).set(2,1,4).set(2,2,a-3);
99     ex p = m3.charpoly(a);
100     if (p != 0) {
101         clog << "charpoly of 3x3 matrix " << m3
102              << " erroneously returned " << p << endl;
103         ++result;
104     }
105     
106     return result;
107 }
108
109 static unsigned matrix_invert1(void)
110 {
111     matrix m(1,1);
112     symbol a("a");
113
114     m.set(0,0,a);
115     matrix m_i = m.inverse();
116     
117     if (m_i(0,0) != pow(a,-1)) {
118         clog << "inversion of 1x1 matrix " << m
119              << " erroneously returned " << m_i << endl;
120         return 1;
121     }
122     return 0;
123 }
124
125 static unsigned matrix_invert2(void)
126 {
127     matrix m(2,2);
128     symbol a("a"), b("b"), c("c"), d("d");
129     m.set(0,0,a).set(0,1,b);
130     m.set(1,0,c).set(1,1,d);
131     matrix m_i = m.inverse();
132     ex det = m.determinant().expand();
133     
134     if ((normal(m_i(0,0)*det) != d) ||
135         (normal(m_i(0,1)*det) != -b) ||
136         (normal(m_i(1,0)*det) != -c) ||
137         (normal(m_i(1,1)*det) != a)) {
138         clog << "inversion of 2x2 matrix " << m
139              << " erroneously returned " << m_i << endl;
140         return 1;
141     }
142     return 0;
143 }
144
145 static unsigned matrix_invert3(void)
146 {
147     matrix m(3,3);
148     symbol a("a"), b("b"), c("c");
149     symbol d("d"), e("e"), f("f");
150     symbol g("g"), h("h"), i("i");
151     m.set(0,0,a).set(0,1,b).set(0,2,c);
152     m.set(1,0,d).set(1,1,e).set(1,2,f);
153     m.set(2,0,g).set(2,1,h).set(2,2,i);
154     matrix m_i = m.inverse();
155     ex det = m.determinant().normal().expand();
156     
157     if ((normal(m_i(0,0)*det) != (e*i-f*h)) ||
158         (normal(m_i(0,1)*det) != (c*h-b*i)) ||
159         (normal(m_i(0,2)*det) != (b*f-c*e)) ||
160         (normal(m_i(1,0)*det) != (f*g-d*i)) ||
161         (normal(m_i(1,1)*det) != (a*i-c*g)) ||
162         (normal(m_i(1,2)*det) != (c*d-a*f)) ||
163         (normal(m_i(2,0)*det) != (d*h-e*g)) ||
164         (normal(m_i(2,1)*det) != (b*g-a*h)) ||
165         (normal(m_i(2,2)*det) != (a*e-b*d))) {
166         clog << "inversion of 3x3 matrix " << m
167              << " erroneously returned " << m_i << endl;
168         return 1;
169     }
170     return 0;
171 }
172
173 static unsigned matrix_misc(void)
174 {
175     unsigned result = 0;
176     matrix m1(2,2);
177     symbol a("a"), b("b"), c("c"), d("d"), e("e"), f("f");
178     m1.set(0,0,a).set(0,1,b);
179     m1.set(1,0,c).set(1,1,d);
180     ex tr = trace(m1);
181     
182     // check a simple trace
183     if (tr.compare(a+d)) {
184         clog << "trace of 2x2 matrix " << m1
185              << " erroneously returned " << tr << endl;
186         ++result;
187     }
188     
189     // and two simple transpositions
190     matrix m2 = transpose(m1);
191     if (m2(0,0) != a || m2(0,1) != c || m2(1,0) != b || m2(1,1) != d) {
192         clog << "transpose of 2x2 matrix " << m1
193              << " erroneously returned " << m2 << endl;
194         ++result;
195     }
196     matrix m3(3,2);
197     m3.set(0,0,a).set(0,1,b);
198     m3.set(1,0,c).set(1,1,d);
199     m3.set(2,0,e).set(2,1,f);
200     if (transpose(transpose(m3)) != m3) {
201         clog << "transposing 3x2 matrix " << m3 << " twice"
202              << " erroneously returned " << transpose(transpose(m3)) << endl;
203         ++result;
204     }
205     
206     // produce a runtime-error by inverting a singular matrix and catch it
207     matrix m4(2,2);
208     matrix m5;
209     bool caught=false;
210     try {
211         m5 = inverse(m4);
212     } catch (std::runtime_error err) {
213         caught=true;
214     }
215     if (!caught) {
216         cerr << "singular 2x2 matrix " << m4
217              << " erroneously inverted to " << m5 << endl;
218         ++result;
219     }
220     
221     return result;
222 }
223
224 unsigned exam_matrices(void)
225 {
226     unsigned result = 0;
227     
228     cout << "examining symbolic matrix manipulations" << flush;
229     clog << "----------symbolic matrix manipulations:" << endl;
230
231     result += matrix_determinants();  cout << '.' << flush;
232     result += matrix_invert1();  cout << '.' << flush;
233     result += matrix_invert2();  cout << '.' << flush;
234     result += matrix_invert3();  cout << '.' << flush;
235     result += matrix_misc();  cout << '.' << flush;
236     
237     if (!result) {
238         cout << " passed " << endl;
239         clog << "(no output)" << endl;
240     } else {
241         cout << " failed " << endl;
242     }
243     
244     return result;
245 }