3 * Provides some routines for generating expressions that are later used as
4 * input in the consistency checks. */
7 * GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 using namespace GiNaC;
30 /* Create a dense univariate random polynomial in x.
31 * (of the form 9 - 22*a - 17*a^2 + 14*a^3 + 7*a^4 + 7a^5 if degree==5) */
33 dense_univariate_poly(const symbol & x, unsigned degree)
37 for (unsigned i=0; i<=degree; ++i)
38 unipoly += numeric((rand()-RAND_MAX/2))*pow(x,i);
43 /* Create a dense bivariate random polynomial in x1 and x2.
44 * (of the form 9 + 52*x1 - 27*x1^2 + 84*x2 + 7*x2^2 - 12*x1*x2 if degree==2)
47 dense_bivariate_poly(const symbol & x1, const symbol & x2, unsigned degree)
51 for (unsigned i1=0; i1<=degree; ++i1)
52 for (unsigned i2=0; i2<=degree-i1; ++i2)
53 bipoly += numeric((rand()-RAND_MAX/2))*pow(x1,i1)*pow(x2,i2);
58 /* Chose a randum symbol or number from the argument list. */
60 random_symbol(const symbol & x,
67 switch (abs(rand()) % 4) {
79 do { c1 = rand()%20 - 10; } while (!c1);
81 do { c2 = rand()%20 - 10; } while (!c2);
85 if (complex && !(rand()%5))
93 /* Create a sparse random tree in three symbols. */
95 sparse_tree(const symbol & x,
99 bool trig = false, // true includes trigonomatric functions
100 bool rational = true, // false excludes coefficients in Q
101 bool complex = false) // true includes complex numbers
104 return random_symbol(x,y,z,rational,complex);
105 switch (abs(rand()) % 10) {
110 return add(sparse_tree(x,y,z,level-1, trig, rational),
111 sparse_tree(x,y,z,level-1, trig, rational));
115 return mul(sparse_tree(x,y,z,level-1, trig, rational),
116 sparse_tree(x,y,z,level-1, trig, rational));
121 powbase = sparse_tree(x,y,z,level-1, trig, rational);
122 } while (powbase.is_zero());
123 return pow(powbase, abs(rand() % 4));
128 switch (abs(rand()) % 4) {
130 return sin(sparse_tree(x,y,z,level-1, trig, rational));
132 return cos(sparse_tree(x,y,z,level-1, trig, rational));
134 return exp(sparse_tree(x,y,z,level-1, trig, rational));
140 logarg = sparse_tree(x,y,z,level-1, trig, rational);
141 } while (logarg.is_zero());
142 // Keep the evaluator from accidentally plugging an
143 // unwanted I in the tree:
144 if (!complex && logarg.info(info_flags::negative))
147 } while (logex.is_zero());
153 return random_symbol(x,y,z,rational,complex);