]> www.ginac.de Git - ginac.git/blob - ginac/printtree.cpp
38706f6975d8c6e6de5627c987d1d0da1b4bd2f3
[ginac.git] / ginac / printtree.cpp
1 /** @file printtree.cpp
2  *
3  * print in tree- (indented-) form, so developers can have a look at the
4  * underlying structure. */
5
6 /*
7  *  GiNaC Copyright (C) 1999 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 <iostream>
25 #include <math.h>
26
27 #include "basic.h"
28 #include "ex.h"
29 #include "add.h"
30 #include "constant.h"
31 #include "expairseq.h"
32 #include "indexed.h"
33 #include "inifcns.h"
34 #include "mul.h"
35 #include "ncmul.h"
36 #include "numeric.h"
37 #include "power.h"
38 #include "relational.h"
39 #include "series.h"
40 #include "symbol.h"
41
42 void ex::printtree(ostream & os, unsigned indent) const
43 {
44     debugmsg("ex printtree",LOGLEVEL_PRINT);
45     ASSERT(bp!=0);
46     // os << "refcount=" << bp->refcount << " ";
47     bp->printtree(os,indent);
48 }
49
50 void ex::dbgprinttree(void) const
51 {
52     debugmsg("ex dbgprinttree",LOGLEVEL_PRINT);
53     ASSERT(bp!=0);
54     bp->dbgprinttree();
55 }
56
57 void basic::printtree(ostream & os, unsigned indent) const
58 {
59     debugmsg("basic printtree",LOGLEVEL_PRINT);
60     os << string(indent,' ') << "type=" << typeid(*this).name()
61        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
62        << ", flags=" << flags
63        << ", nops=" << nops() << endl;
64     for (int i=0; i<nops(); ++i) {
65         op(i).printtree(os,indent+delta_indent);
66     }
67 }
68
69 void basic::dbgprinttree(void) const
70 {
71     printtree(cerr,0);
72 }
73
74 void numeric::printtree(ostream & os, unsigned indent) const
75 {
76     debugmsg("numeric printtree", LOGLEVEL_PRINT);
77     // We are cheating here, because we don't want to include the underlying
78     // bignum package's headers again, so we use ostream::operator<<(numeric):
79     os << string(indent,' ');
80     (*this).print(os);
81     os << " (numeric): "
82        << "hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
83        << ", flags=" << flags << endl;
84 }
85
86 void symbol::printtree(ostream & os, unsigned indent) const
87 {
88     debugmsg("symbol printtree",LOGLEVEL_PRINT);
89     os << string(indent,' ') << name << " (symbol): "
90        << "serial=" << serial
91        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
92        << ", flags=" << flags << endl;
93 }
94
95 void power::printtree(ostream & os, unsigned indent) const
96 {
97     debugmsg("power printtree",LOGLEVEL_PRINT);
98
99     os << string(indent,' ') << "power: "
100        << "hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
101        << ", flags=" << flags << endl;
102     basis.printtree(os,indent+delta_indent);
103     exponent.printtree(os,indent+delta_indent);
104 }
105
106 void expairseq::printtree(ostream & os, unsigned indent) const
107 {
108     debugmsg("expairseq printtree",LOGLEVEL_PRINT);
109
110     os << string(indent,' ') << "type=" << typeid(*this).name()
111        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
112        << ", flags=" << flags
113        << ", nops=" << nops() << endl;
114     for (unsigned i=0; i<seq.size(); ++i) {
115         seq[i].rest.printtree(os,indent+delta_indent);
116         seq[i].coeff.printtree(os,indent+delta_indent);
117         if (i!=seq.size()-1) {
118             os << string(indent+delta_indent,' ') << "-----" << endl;
119         }
120     }
121     if (!overall_coeff.is_equal(default_overall_coeff())) {
122         os << string(indent+delta_indent,' ') << "-----" << endl;
123         os << string(indent+delta_indent,' ') << "overall_coeff" << endl;
124         overall_coeff.printtree(os,indent+delta_indent);
125     }
126     os << string(indent+delta_indent,' ') << "=====" << endl;
127 #ifdef EXPAIRSEQ_USE_HASHTAB
128     os << string(indent+delta_indent,' ')
129        << "hashtab size " << hashtabsize << endl;
130     if (hashtabsize==0) return;
131 #define MAXCOUNT 5
132     unsigned count[MAXCOUNT+1];
133     for (int i=0; i<MAXCOUNT+1; ++i) count[i]=0;
134     unsigned this_bin_fill;
135     unsigned cum_fill_sq=0;
136     unsigned cum_fill=0;
137     for (unsigned i=0; i<hashtabsize; ++i) {
138         this_bin_fill=0;
139         if (hashtab[i].size()>0) {
140             os << string(indent+delta_indent,' ') 
141                << "bin " << i << " with entries ";
142             for (epplist::const_iterator it=hashtab[i].begin();
143                  it!=hashtab[i].end(); ++it) {
144                 os << *it-seq.begin() << " ";
145                 this_bin_fill++;
146             }
147             os << endl;
148             cum_fill += this_bin_fill;
149             cum_fill_sq += this_bin_fill*this_bin_fill;
150         }
151         if (this_bin_fill<MAXCOUNT) {
152             ++count[this_bin_fill];
153         } else {
154             ++count[MAXCOUNT];
155         }
156     }
157     unsigned fact=1;
158     double cum_prob=0;
159     double lambda=(1.0*seq.size())/hashtabsize;
160     for (int k=0; k<MAXCOUNT; ++k) {
161         if (k>0) fact *= k;
162         double prob=pow(lambda,k)/fact*exp(-lambda);
163         cum_prob += prob;
164         os << string(indent+delta_indent,' ') << "bins with " << k << " entries: "
165            << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: "
166            << int(prob*1000)/10.0 << ")" << endl;
167     }
168     os << string(indent+delta_indent,' ') << "bins with more entries: "
169        << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: "
170        << int((1-cum_prob)*1000)/10.0 << ")" << endl;
171     
172     os << string(indent+delta_indent,' ') << "variance: "
173        << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill)
174        << endl;
175     os << string(indent+delta_indent,' ') << "average fill: "
176        << (1.0*cum_fill)/hashtabsize
177        << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << endl;
178 #endif // def EXPAIRSEQ_USE_HASHTAB
179 }
180