1 /** @file genex.cpp
2  *
3  *  Provides some routines for generating expressions that are later used as
4  *  input in the consistency checks. */
6 /*
7  *  GiNaC Copyright (C) 1999-2001 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  */
24 // For rand() and friends:
25 #include <stdlib.h>
27 #include "ginac.h"
29 #ifndef NO_NAMESPACE_GINAC
30 using namespace GiNaC;
31 #endif // ndef NO_NAMESPACE_GINAC
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;
40         for (unsigned i=0; i<=degree; ++i)
41                 unipoly += numeric((rand()-RAND_MAX/2))*pow(x,i);
43         return unipoly;
44 }
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;
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);
58         return bipoly;
59 }
61 /* Chose a randum symbol or number from the argument list. */
62 const ex
63 random_symbol(const symbol & x,
64                           const symbol & y,
65                           const symbol & z,
66                           bool rational = true,
67                           bool complex = false)
68 {
69         ex e;
70         switch (abs(rand()) % 4) {
71                 case 0:
72                         e = x;
73                         break;
74                 case 1:
75                         e = y;
76                         break;
77                 case 2:
78                         e = z;
79                         break;
80                 case 3: {
81                         int c1;
82                         do { c1 = rand()%20 - 10; } while (!c1);
83                         int c2;
84                         do { c2 = rand()%20 - 10; } while (!c2);
85                         if (!rational)
86                                 c2 = 1;
87                         e = numeric(c1, c2);
88                         if (complex && !(rand()%5))
89                                 e = e*I;
90                         break;
91                 }
92         }
93         return e;
94 }
96 /* Create a sparse random tree in three symbols. */
97 const ex
98 sparse_tree(const symbol & x,
99                         const symbol & y,
100                         const symbol & z,
101                         int level,
102                         bool trig = false,      // true includes trigonomatric functions
103                         bool rational = true, // false excludes coefficients in Q
104                         bool complex = false) // true includes complex numbers
105 {
106         if (level == 0)
107                 return random_symbol(x,y,z,rational,complex);
108         switch (abs(rand()) % 10) {
109                 case 0:
110                 case 1:
111                 case 2:
112                 case 3:
114                                            sparse_tree(x,y,z,level-1, trig, rational));
115                 case 4:
116                 case 5:
117                 case 6:
118                         return mul(sparse_tree(x,y,z,level-1, trig, rational),
119                                            sparse_tree(x,y,z,level-1, trig, rational));
120                 case 7:
121                 case 8: {
122                         ex powbase;
123                         do {
124                                 powbase = sparse_tree(x,y,z,level-1, trig, rational);
125                         } while (powbase.is_zero());
126                         return pow(powbase, abs(rand() % 4));
127                         break;
128                 }
129                 case 9:
130                         if (trig) {
131                                 switch (abs(rand()) % 4) {
132                                         case 0:
133                                                 return sin(sparse_tree(x,y,z,level-1, trig, rational));
134                                         case 1:
135                                                 return cos(sparse_tree(x,y,z,level-1, trig, rational));
136                                         case 2:
137                                                 return exp(sparse_tree(x,y,z,level-1, trig, rational));
138                                         case 3: {
139                                                 ex logex;
140                                                 do {
141                                                         ex logarg;
142                                                         do {
143                                                                 logarg = sparse_tree(x,y,z,level-1, trig, rational);
144                                                         } while (logarg.is_zero());
145                                                         // Keep the evaluator from accidentally plugging an
146                                                         // unwanted I in the tree:
147                                                         if (!complex && logarg.info(info_flags::negative))
148                                                                 logarg = -logarg;
149                                                         logex = log(logarg);
150                                                 } while (logex.is_zero());
151                                                 return logex;
152                                                 break;
153                                         }
154                                 }
155                         } else
156                                 return random_symbol(x,y,z,rational,complex);
157         }
158 }