]> www.ginac.de Git - ginac.git/blob - check/genex.cpp
inlined ex::swap() and provided versions of iter_swap() for ex and expair
[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-2002 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 #include <cstdlib>
25 #include "ginac.h"
26 using namespace std;
27 using namespace GiNaC;
28
29 /* Create a dense univariate random polynomial in x.
30  * (of the form 9 - 22*a - 17*a^2 + 14*a^3 + 7*a^4 + 7a^5 if degree==5) */
31 const ex
32 dense_univariate_poly(const symbol & x, unsigned degree)
33 {
34         ex unipoly;
35         
36         for (unsigned i=0; i<=degree; ++i)
37                 unipoly += numeric((rand()-RAND_MAX/2))*pow(x,i);
38         
39         return unipoly;
40 }
41
42 /* Create a dense bivariate random polynomial in x1 and x2.
43  * (of the form 9 + 52*x1 - 27*x1^2 + 84*x2 + 7*x2^2 - 12*x1*x2 if degree==2)
44  */
45 const ex
46 dense_bivariate_poly(const symbol & x1, const symbol & x2, unsigned degree)
47 {
48         ex bipoly;
49         
50         for (unsigned i1=0; i1<=degree; ++i1)
51                 for (unsigned i2=0; i2<=degree-i1; ++i2)
52                         bipoly += numeric((rand()-RAND_MAX/2))*pow(x1,i1)*pow(x2,i2);
53         
54         return bipoly;
55 }
56
57 /* Chose a randum symbol or number from the argument list. */
58 const ex
59 random_symbol(const symbol & x,
60                           const symbol & y,
61                           const symbol & z,
62                           bool rational = true,
63                           bool complex = false)
64 {
65         ex e;
66         switch (abs(rand()) % 4) {
67                 case 0:
68                         e = x;
69                         break;
70                 case 1:
71                         e = y;
72                         break;
73                 case 2:
74                         e = z;
75                         break;
76                 case 3: {
77                         int c1;
78                         do { c1 = rand()%20 - 10; } while (!c1);
79                         int c2;
80                         do { c2 = rand()%20 - 10; } while (!c2);
81                         if (!rational)
82                                 c2 = 1;
83                         e = numeric(c1, c2);
84                         if (complex && !(rand()%5))
85                                 e = e*I;
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 excludes coefficients in Q
100                         bool complex = false) // true includes complex numbers
101 {
102         if (level == 0)
103                 return random_symbol(x,y,z,rational,complex);
104         switch (abs(rand()) % 10) {
105                 case 0:
106                 case 1:
107                 case 2:
108                 case 3:
109                         return add(sparse_tree(x,y,z,level-1, trig, rational),
110                                            sparse_tree(x,y,z,level-1, trig, rational));
111                 case 4:
112                 case 5:
113                 case 6:
114                         return mul(sparse_tree(x,y,z,level-1, trig, rational),
115                                            sparse_tree(x,y,z,level-1, trig, rational));
116                 case 7:
117                 case 8: {
118                         ex powbase;
119                         do {
120                                 powbase = sparse_tree(x,y,z,level-1, trig, rational);
121                         } while (powbase.is_zero());
122                         return pow(powbase, abs(rand() % 4));
123                         break;
124                 }
125                 case 9:
126                         if (trig) {
127                                 switch (abs(rand()) % 4) {
128                                         case 0:
129                                                 return sin(sparse_tree(x,y,z,level-1, trig, rational));
130                                         case 1:
131                                                 return cos(sparse_tree(x,y,z,level-1, trig, rational));
132                                         case 2:
133                                                 return exp(sparse_tree(x,y,z,level-1, trig, rational));
134                                         case 3: {
135                                                 ex logex;
136                                                 do {
137                                                         ex logarg;
138                                                         do {
139                                                                 logarg = sparse_tree(x,y,z,level-1, trig, rational);
140                                                         } while (logarg.is_zero());
141                                                         // Keep the evaluator from accidentally plugging an
142                                                         // unwanted I in the tree:
143                                                         if (!complex && logarg.info(info_flags::negative))
144                                                                 logarg = -logarg;
145                                                         logex = log(logarg);
146                                                 } while (logex.is_zero());
147                                                 return logex;
148                                                 break;
149                                         }
150                                 }
151                         } else
152                                 return random_symbol(x,y,z,rational,complex);
153         }
154 }