]> www.ginac.de Git - ginac.git/blob - check/genex.cpp
dacb40eb02380be0fc1cf40aa62c84ee823c609f
[ginac.git] / check / genex.cpp
1 /** @file genex.cpp
2  *
3  *  Provides some routines for generating expressions that are later used as 
4  *  input in the consistency checks. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
8  *
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.
13  *
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.
18  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 // For rand() and friends:
25 #include <stdlib.h>
26
27 #include "ginac.h"
28
29 #ifndef NO_NAMESPACE_GINAC
30 using namespace GiNaC;
31 #endif // ndef NO_NAMESPACE_GINAC
32
33 /* Create a dense univariate random polynomial in x.
34  * (of the form 9 - 22*a - 17*a^2 + 14*a^3 + 7*a^4 + 7a^5 if degree==5) */
35 const ex
36 dense_univariate_poly(const symbol & x, unsigned degree)
37 {
38     ex unipoly;
39     
40     for (unsigned i=0; i<=degree; ++i)
41         unipoly += numeric((rand()-RAND_MAX/2))*pow(x,i);
42     
43     return unipoly;
44 }
45
46 /* Create a dense bivariate random polynomial in x1 and x2.
47  * (of the form 9 + 52*x1 - 27*x1^2 + 84*x2 + 7*x2^2 - 12*x1*x2 if degree==2)
48  */
49 const ex
50 dense_bivariate_poly(const symbol & x1, const symbol & x2, unsigned degree)
51 {
52     ex bipoly;
53     
54     for (unsigned i1=0; i1<=degree; ++i1)
55         for (unsigned i2=0; i2<=degree-i1; ++i2)
56             bipoly += numeric((rand()-RAND_MAX/2))*pow(x1,i1)*pow(x2,i2);
57     
58     return bipoly;
59 }
60
61 const ex
62 random_symbol(const symbol & x,
63               const symbol & y,
64               const symbol & z,
65               bool rational = true)
66 {
67     ex e;
68     switch (abs(rand()) % 4) {
69         case 0:
70             e = x;
71             break;
72         case 1:
73             e = y;
74             break;
75         case 2:
76             e = z;
77             break;
78         case 3: {
79             int c1 = rand() % 20 - 10;
80             int c2 = rand() % 20 - 10;
81             if (c1 == 0) c1 = 1;
82             if (c2 == 0) c2 = 1;
83             if (!rational)
84                 c2 = 1;
85             e = numeric(c1) / numeric(c2);
86             break;
87         }
88     }
89     return e;
90 }
91
92 /* Create a sparse random tree in three symbols. */
93 const ex
94 sparse_tree(const symbol & x,
95             const symbol & y,
96             const symbol & z,
97             int level,
98             bool trig = false,    // true includes trigonomatric functions
99             bool rational = true) // false includes coefficients in Q
100 {
101     if (level == 0)
102         return random_symbol(x,y,z,rational);
103     switch (abs(rand()) % 7) {
104         case 0:
105         case 1:
106             return add(sparse_tree(x,y,z,level-1, trig, rational),
107                        sparse_tree(x,y,z,level-1, trig, rational));
108         case 2:
109         case 3:
110             return mul(sparse_tree(x,y,z,level-1, trig, rational),
111                        sparse_tree(x,y,z,level-1, trig, rational));
112         case 4:
113         case 5:
114             return power(sparse_tree(x,y,z,level-1, trig, rational),
115                          abs(rand() % 4));
116         case 6:
117             if (trig) {
118                 switch (abs(rand()) % 4) {
119                     case 0:
120                         return sin(sparse_tree(x,y,z,level-1, trig, rational));
121                     case 1:
122                         return cos(sparse_tree(x,y,z,level-1, trig, rational));
123                     case 2:
124                         return exp(sparse_tree(x,y,z,level-1, trig, rational));
125                     case 3:
126                         return log(sparse_tree(x,y,z,level-1, trig, rational));
127                 }
128             } else
129                 return random_symbol(x,y,z,rational);
130     }
131 }