+#include <cstdlib> // for rand(), RAND_MAX
+#include <iostream>
+using namespace std;
+
+extern const ex
+sparse_tree(const symbol & x, const symbol & y, const symbol & z,
+ int level, bool trig = false, bool rational = true,
+ bool complex = false);
+extern const ex
+dense_univariate_poly(const symbol & x, unsigned degree);
+
+/* determinants of some sparse symbolic matrices with coefficients in
+ * an integral domain. */
+static unsigned integdom_matrix_determinants()
+{
+ unsigned result = 0;
+ symbol a("a");
+
+ for (unsigned size=3; size<22; ++size) {
+ matrix A(size,size);
+ // populate one element in each row:
+ for (unsigned r=0; r<size-1; ++r)
+ A.set(r,unsigned(rand()%size),dense_univariate_poly(a,5));
+ // set the last row to a linear combination of two other lines
+ // to guarantee that the determinant is zero:
+ for (unsigned c=0; c<size; ++c)
+ A.set(size-1,c,A(0,c)-A(size-2,c));
+ if (!A.determinant().is_zero()) {
+ clog << "Determinant of " << size << "x" << size << " matrix "
+ << endl << A << endl
+ << "was not found to vanish!" << endl;
+ ++result;
+ }
+ }
+
+ return result;
+}
+
+/* determinants of some symbolic matrices with multivariate rational function
+ * coefficients. */
+static unsigned rational_matrix_determinants()
+{
+ unsigned result = 0;
+ symbol a("a"), b("b"), c("c");
+
+ for (unsigned size=3; size<9; ++size) {
+ matrix A(size,size);
+ for (unsigned r=0; r<size-1; ++r) {
+ // populate one or two elements in each row:
+ for (unsigned ec=0; ec<2; ++ec) {
+ ex numer = sparse_tree(a, b, c, 1+rand()%4, false, false, false);
+ ex denom;
+ do {
+ denom = sparse_tree(a, b, c, rand()%2, false, false, false);
+ } while (denom.is_zero());
+ A.set(r,unsigned(rand()%size),numer/denom);
+ }
+ }
+ // set the last row to a linear combination of two other lines
+ // to guarantee that the determinant is zero:
+ for (unsigned co=0; co<size; ++co)
+ A.set(size-1,co,A(0,co)-A(size-2,co));
+ if (!A.determinant().is_zero()) {
+ clog << "Determinant of " << size << "x" << size << " matrix "
+ << endl << A << endl
+ << "was not found to vanish!" << endl;
+ ++result;
+ }
+ }
+
+ return result;
+}
+
+/* Some quite funny determinants with functions and stuff like that inside. */
+static unsigned funny_matrix_determinants()