summaryrefslogtreecommitdiff
path: root/src/Expr/Expr_Sum.cxx
blob: d742a5723fcabe13ddfd1d27fa40c69891699f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//static const char* sccsid = "@(#)Expr_Sum.cxx	3.2 95/01/10"; // Do not delete this line. Used by sccs.
// Copyright: 	Matra-Datavision 1991
// File:	Expr_Sum.cxx
// Created:	Fri Apr 19 09:34:35 1991
// Author:	Arnaud BOUZY
//		<adn>


#ifndef DEB
#define No_Standard_RangeError
#define No_Standard_OutOfRange
#endif

#include <Expr_Sum.ixx>
#include <Standard_OutOfRange.hxx>
#include <TColStd_Array1OfInteger.hxx>
#include <Expr_NumericValue.hxx>
#include <Expr.hxx>

Expr_Sum::Expr_Sum (const Expr_SequenceOfGeneralExpression& exps)
{
  Standard_Integer i;
  Standard_Integer max = exps.Length();
  for (i=1; i<= max; i++) {
    AddOperand(exps(i));
  }
}

Expr_Sum::Expr_Sum (const Handle(Expr_GeneralExpression)& exp1, const Handle(Expr_GeneralExpression)& exp2)
{
  AddOperand(exp1);
  AddOperand(exp2);
}

Handle(Expr_GeneralExpression) Expr_Sum::Copy () const
{
  Expr_SequenceOfGeneralExpression ops;
  Standard_Integer i;
  Standard_Integer max = NbOperands();
  for (i=1; i <= max; i++) {
    ops.Append(Expr::CopyShare(Operand(i)));
  }
  return new Expr_Sum(ops);
}

Standard_Boolean Expr_Sum::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
{
  if (!Other->IsKind(STANDARD_TYPE(Expr_Sum))) {
    return Standard_False;
  }
  Handle(Expr_Sum) me = this;
  Handle(Expr_Sum) SOther = Handle(Expr_Sum)::DownCast(Other);
  Standard_Integer max = NbOperands();
  if (SOther->NbOperands() != max) {
    return Standard_False;
  }
  Handle(Expr_GeneralExpression) myop;
  Handle(Expr_GeneralExpression) hisop;
  Standard_Integer i=1;
  TColStd_Array1OfInteger tab(1,max);
  for (Standard_Integer k=1; k<=max;k++) {
    tab(k)=0;
  }
  Standard_Boolean ident = Standard_True;
  while ((i<=max) && (ident)) {
    Standard_Integer j=1;
    Standard_Boolean found = Standard_False;
    myop = Operand(i);
    while ((j<=max) && (!found)) {
      hisop = SOther->Operand(j);
      found = myop->IsIdentical(hisop);
      if (found) {
	found = (tab(j) == 0);
	tab(j)=i;
      }
      j++;
    }
    ident = found;
    i++;
  }
  return ident;
}

Standard_Boolean Expr_Sum::IsLinear () const
{
  Standard_Boolean result = Standard_True;
  Standard_Integer i=1;
  Standard_Integer max = NbOperands();
  while ((i <= max) && result) {
    result = Operand(i)->IsLinear();
    i++;
  }
  return result;
}

Handle(Expr_GeneralExpression) Expr_Sum::Derivative (const Handle(Expr_NamedUnknown)& X) const
{
  Expr_SequenceOfGeneralExpression opsder;
  Standard_Integer i;
  Standard_Integer max = NbOperands();
  for (i=1; i<= max; i++) {
    opsder.Append(Operand(i)->Derivative(X));
  }
  Handle(Expr_Sum) deriv = new Expr_Sum(opsder);
  return deriv->ShallowSimplified();
}

Handle(Expr_GeneralExpression) Expr_Sum::NDerivative (const Handle(Expr_NamedUnknown)& X, const Standard_Integer N) const
{
  if (N <= 0) {
    Standard_OutOfRange::Raise();
  }
  Expr_SequenceOfGeneralExpression opsder;
  Standard_Integer i;
  Standard_Integer max = NbOperands();
  for (i=1; i<= max; i++) {
    opsder.Append(Operand(i)->NDerivative(X,N));
  }
  Handle(Expr_Sum) deriv = new Expr_Sum(opsder);
  return deriv->ShallowSimplified();
}

Handle(Expr_GeneralExpression) Expr_Sum::ShallowSimplified () const
{
  Standard_Integer i;
  Standard_Integer max = NbOperands();
  Standard_Integer nbvals =0;
  Handle(Expr_GeneralExpression) op;
  Expr_SequenceOfGeneralExpression newops;
  Standard_Boolean subsum = Standard_False;
  for (i=1; (i<= max) && !subsum; i++) {
    op = Operand(i);
    subsum = op->IsKind(STANDARD_TYPE(Expr_Sum));
  }
  if (subsum) {
    Handle(Expr_GeneralExpression) other;
    Handle(Expr_Sum) sumop;
    Standard_Integer nbssumop;
    for (i=1; i<= max; i++) {
      op = Operand(i);
      if (op->IsKind(STANDARD_TYPE(Expr_Sum))) {
	sumop = Handle(Expr_Sum)::DownCast(op);
	nbssumop = sumop->NbOperands();
	for (Standard_Integer j=1; j<= nbssumop; j++) {
	  other = sumop->Operand(j);
	  newops.Append(other);
	}
      }
      else {
	newops.Append(op);
      }
    }
    sumop = new Expr_Sum(newops);
    return sumop->ShallowSimplified();
  }
#ifndef DEB
  Standard_Real vals = 0.;
#else
  Standard_Real vals;
#endif
  Standard_Boolean noone = Standard_True;
  for (i = 1; i <= max ; i++) {
    op = Operand(i);
    if (op->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
      Handle(Expr_NumericValue) NVop = Handle(Expr_NumericValue)::DownCast(op);
      if (nbvals == 0) {
	noone = Standard_False;
	vals = NVop->GetValue();
	nbvals = 1;
      }
      else {
	vals = vals + NVop->GetValue();
	nbvals++;
      }
    }
    else {
      newops.Append(op);
    }
  }
  if (!noone) {
    if (newops.IsEmpty()) {         // result is only numericvalue (even zero)
      return new Expr_NumericValue(vals); 
    }
    if (vals != 0.0) {
      if (nbvals == 1) {
	Handle(Expr_Sum) me = this;
	return me;
      }
      Handle(Expr_NumericValue) thevals = new Expr_NumericValue(vals);
      newops.Append(thevals);  // non-zero value added
      return new Expr_Sum(newops);
    }
    if (newops.Length() == 1) {
      // case X + 0
      return newops(1);
    }
    return new Expr_Sum(newops);
  }
  Handle(Expr_Sum) me = this;
  return me;
}

Standard_Real Expr_Sum::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
{
  Standard_Integer max = NbOperands();
  Standard_Real res = 0.0;
  for (Standard_Integer i=1; i<= max; i++) {
    res = res + Operand(i)->Evaluate(vars,vals);
  }
  return res;
}

TCollection_AsciiString Expr_Sum::String() const
{
  Handle(Expr_GeneralExpression) op;
  Standard_Integer nbop = NbOperands();
  op = Operand(1);
  TCollection_AsciiString str;
  if (op->NbSubExpressions() > 1) {
    str = "(";
    str += op->String();
    str += ")";;
  }
  else {
    str = op->String();
  }
  for (Standard_Integer i=2; i<=nbop; i++) {
    str += "+";
    op = Operand(i);
    if (op->NbSubExpressions() > 1) {
      str += "(";
      str += op->String();
      str += ")";;
    }
    else {
      str += op->String();
    }
  }
  return str;
}