1 | package symexpr |
---|
2 | |
---|
3 | type ExprParams struct { |
---|
4 | |
---|
5 | // bounds on tree |
---|
6 | MaxS, MaxD, |
---|
7 | MinS, MinD int |
---|
8 | |
---|
9 | // bounds on some operators |
---|
10 | NumDim, NumSys, NumCoeff int |
---|
11 | |
---|
12 | // usable terms at each location |
---|
13 | Roots, Nodes, Leafs, NonTrig []Expr |
---|
14 | } |
---|
15 | |
---|
16 | type ExprStats struct { |
---|
17 | depth int // layers from top (root == 1) |
---|
18 | height int // layers of subtree (leaf == 1) |
---|
19 | size int |
---|
20 | pos int |
---|
21 | numchld int |
---|
22 | } |
---|
23 | |
---|
24 | func (es *ExprStats) Depth() int { return es.depth } |
---|
25 | func (es *ExprStats) Height() int { return es.height } |
---|
26 | func (es *ExprStats) Size() int { return es.size } |
---|
27 | func (es *ExprStats) Pos() int { return es.pos } |
---|
28 | func (es *ExprStats) NumChildren() int { return es.numchld } |
---|
29 | |
---|
30 | func (t *Leaf) CalcExprStats() { |
---|
31 | t.depth = 1 |
---|
32 | t.height = 1 |
---|
33 | t.size = 1 |
---|
34 | t.pos = 0 |
---|
35 | t.numchld = 0 |
---|
36 | } |
---|
37 | func (t *Leaf) calcExprStatsR(depth int, pos *int) { |
---|
38 | t.depth = depth + 1 |
---|
39 | t.height = 1 |
---|
40 | t.size = 1 |
---|
41 | t.pos = *pos |
---|
42 | (*pos)++ |
---|
43 | t.numchld = 0 |
---|
44 | } |
---|
45 | |
---|
46 | func (u *Unary) CalcExprStats() { |
---|
47 | pos := 1 |
---|
48 | u.depth = 1 |
---|
49 | u.C.calcExprStatsR(1, &pos) |
---|
50 | u.height = 1 + u.C.Height() |
---|
51 | u.size = 1 + u.C.Size() |
---|
52 | u.pos = 0 |
---|
53 | u.numchld = 1 |
---|
54 | } |
---|
55 | func (u *Unary) calcExprStatsR(depth int, pos *int) { |
---|
56 | u.pos = *pos |
---|
57 | (*pos)++ |
---|
58 | u.depth = depth + 1 |
---|
59 | u.C.calcExprStatsR(depth+1, pos) |
---|
60 | u.height = 1 + u.C.Height() |
---|
61 | u.size = 1 + u.C.Size() |
---|
62 | u.numchld = 1 |
---|
63 | } |
---|
64 | |
---|
65 | func (n *N_ary) CalcExprStats() { |
---|
66 | c_high := 0 |
---|
67 | pos := 1 |
---|
68 | n.depth = 1 |
---|
69 | n.size = 1 |
---|
70 | n.pos = 0 |
---|
71 | n.numchld = len(n.CS) |
---|
72 | for _, C := range n.CS { |
---|
73 | if C == nil { |
---|
74 | continue |
---|
75 | } else { |
---|
76 | n.numchld++ |
---|
77 | } |
---|
78 | C.calcExprStatsR(1, &pos) |
---|
79 | n.size += C.Size() |
---|
80 | if c_high < C.Height() { |
---|
81 | c_high = C.Height() |
---|
82 | } |
---|
83 | } |
---|
84 | n.height = 1 + c_high |
---|
85 | } |
---|
86 | |
---|
87 | func (n *N_ary) calcExprStatsR(depth int, pos *int) { |
---|
88 | c_high := 0 |
---|
89 | n.depth = depth + 1 |
---|
90 | n.size = 1 |
---|
91 | n.pos = *pos |
---|
92 | (*pos)++ |
---|
93 | n.numchld = len(n.CS) |
---|
94 | for _, C := range n.CS { |
---|
95 | if C == nil { |
---|
96 | continue |
---|
97 | } else { |
---|
98 | n.numchld++ |
---|
99 | } |
---|
100 | C.calcExprStatsR(depth+1, pos) |
---|
101 | n.size += C.Size() |
---|
102 | if c_high < C.Height() { |
---|
103 | c_high = C.Height() |
---|
104 | } |
---|
105 | } |
---|
106 | n.height = 1 + c_high |
---|
107 | } |
---|
108 | |
---|
109 | func (u *PowI) CalcExprStats() { |
---|
110 | pos := 1 |
---|
111 | u.Base.calcExprStatsR(1, &pos) |
---|
112 | u.depth = 1 |
---|
113 | u.height = 1 + u.Base.Height() |
---|
114 | u.size = 1 + u.Base.Size() |
---|
115 | u.pos = 0 |
---|
116 | u.numchld = 1 |
---|
117 | } |
---|
118 | |
---|
119 | func (u *PowI) calcExprStatsR(depth int, pos *int) { |
---|
120 | u.pos = *pos |
---|
121 | (*pos)++ |
---|
122 | u.Base.calcExprStatsR(depth+1, pos) |
---|
123 | u.depth = depth + 1 |
---|
124 | u.height = 1 + u.Base.Height() |
---|
125 | u.size = 1 + u.Base.Size() |
---|
126 | u.numchld = 1 |
---|
127 | } |
---|
128 | |
---|
129 | func (u *PowF) CalcExprStats() { |
---|
130 | pos := 1 |
---|
131 | u.Base.calcExprStatsR(1, &pos) |
---|
132 | u.depth = 1 |
---|
133 | u.height = 1 + u.Base.Height() |
---|
134 | u.size = 1 + u.Base.Size() |
---|
135 | u.pos = 0 |
---|
136 | u.numchld = 1 |
---|
137 | } |
---|
138 | |
---|
139 | func (u *PowF) calcExprStatsR(depth int, pos *int) { |
---|
140 | u.pos = *pos |
---|
141 | (*pos)++ |
---|
142 | u.Base.calcExprStatsR(depth+1, pos) |
---|
143 | u.depth = depth + 1 |
---|
144 | u.height = 1 + u.Base.Height() |
---|
145 | u.size = 1 + u.Base.Size() |
---|
146 | u.numchld = 1 |
---|
147 | } |
---|
148 | |
---|
149 | func max(l, r int) int { |
---|
150 | if l > r { |
---|
151 | return l |
---|
152 | } |
---|
153 | return r |
---|
154 | } |
---|
155 | |
---|
156 | func (n *PowE) CalcExprStats() { |
---|
157 | pos := 1 |
---|
158 | n.Base.calcExprStatsR(1, &pos) |
---|
159 | n.Power.calcExprStatsR(1, &pos) |
---|
160 | n.depth = 1 |
---|
161 | n.height = 1 + max(n.Base.Height(), n.Power.Height()) |
---|
162 | n.size = 1 + n.Base.Size() + n.Power.Size() |
---|
163 | n.numchld = 2 |
---|
164 | } |
---|
165 | func (n *PowE) calcExprStatsR(depth int, pos *int) { |
---|
166 | n.pos = *pos |
---|
167 | (*pos)++ |
---|
168 | n.Base.calcExprStatsR(depth+1, pos) |
---|
169 | n.Power.calcExprStatsR(depth+1, pos) |
---|
170 | n.depth = depth + 1 |
---|
171 | n.height = 1 + max(n.Base.Height(), n.Power.Height()) |
---|
172 | n.size = 1 + n.Base.Size() + n.Power.Size() |
---|
173 | n.numchld = 2 |
---|
174 | } |
---|
175 | |
---|
176 | func (n *Div) CalcExprStats() { |
---|
177 | pos := 1 |
---|
178 | n.Numer.calcExprStatsR(1, &pos) |
---|
179 | n.Denom.calcExprStatsR(1, &pos) |
---|
180 | n.depth = 1 |
---|
181 | n.height = 1 + max(n.Numer.Height(), n.Denom.Height()) |
---|
182 | n.size = 1 + n.Numer.Size() + n.Denom.Size() |
---|
183 | n.numchld = 2 |
---|
184 | } |
---|
185 | func (n *Div) calcExprStatsR(depth int, pos *int) { |
---|
186 | n.pos = *pos |
---|
187 | (*pos)++ |
---|
188 | n.Numer.calcExprStatsR(depth+1, pos) |
---|
189 | n.Denom.calcExprStatsR(depth+1, pos) |
---|
190 | n.depth = depth + 1 |
---|
191 | n.height = 1 + max(n.Numer.Height(), n.Denom.Height()) |
---|
192 | n.size = 1 + n.Numer.Size() + n.Denom.Size() |
---|
193 | n.numchld = 2 |
---|
194 | } |
---|