summaryrefslogtreecommitdiff
path: root/src/Expr/Expr_Product.cxx
blob: 86b802bdca3698c248e40c007388282b8bc9c87f (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//static const char* sccsid = "@(#)Expr_Product.cxx	3.2 95/01/10"; // Do not delete this line. Used by sccs.
// Copyright: 	Matra-Datavision 1991
// File:	Expr_Product.cxx
// Created:	Wed Apr 17 17:10:05 1991
// Author:	Arnaud BOUZY
//		<adn>

#ifndef DEB
#define No_Standard_RangeError
#define No_Standard_OutOfRange
#endif

#include <Expr_Product.ixx>
#include <TColStd_Array1OfInteger.hxx>
#include <Expr_Sum.hxx>
#include <Expr_UnaryMinus.hxx>
#include <Expr_NumericValue.hxx>
#include <Expr_Operators.hxx>
#include <Expr.hxx>

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

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

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

Standard_Boolean Expr_Product::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
{
  if (!Other->IsKind(STANDARD_TYPE(Expr_Product))) {
    return Standard_False;
  }
  Handle(Expr_Product) me = this;
  Handle(Expr_Product) POther = Handle(Expr_Product)::DownCast(Other);
  Standard_Integer max = NbOperands();
  if (POther->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 = POther->Operand(j);
      found = myop->IsIdentical(hisop);
      if (found) {
	found = (tab(j) == 0);
	tab(j)=i;
      }
      j++;
    }
    ident = found;
    i++;
  }
  return ident;
}

Standard_Boolean Expr_Product::IsLinear () const
{
  Standard_Integer i;
  Standard_Integer max = NbOperands();
  Standard_Boolean lin = Standard_True;
  Standard_Boolean res = Standard_True;
  Handle(Expr_GeneralExpression) asimp;
  for (i=1; (i <= max) && res ; i++) {
    asimp = Operand(i);
    if (asimp->IsKind(STANDARD_TYPE(Expr_NamedUnknown)) || asimp->ContainsUnknowns()) {
      if (lin) {
	lin = Standard_False;
	if (!asimp->IsLinear()) {
	  res = Standard_False;
	}
      }
      else {
	res = Standard_False;
      }
    }
  }
  return res;
}

Handle(Expr_GeneralExpression) Expr_Product::Derivative (const Handle(Expr_NamedUnknown)& X) const
{
  if (!Contains(X)) {
    return new Expr_NumericValue(0.0);
  }
  Handle(Expr_GeneralExpression) firstop = Expr::CopyShare(Operand(1)); // U
  Handle(Expr_GeneralExpression) tailop;                               // V
  Standard_Integer nbop = NbOperands();
  if (nbop == 2) {
    tailop = Expr::CopyShare(Operand(2));
  }
  else {
    Handle(Expr_Product) prodop = Expr::CopyShare(Operand(2))*Expr::CopyShare(Operand(3));
    for (Standard_Integer i=4; i<= nbop; i++) {
      prodop->AddOperand(Expr::CopyShare(Operand(i)));
    }
    tailop = prodop;
  }
  Handle(Expr_GeneralExpression) firstder = firstop->Derivative(X); // U'
  Handle(Expr_GeneralExpression) tailder = tailop->Derivative(X);   // V'

  Handle(Expr_Product) firstmember = firstop * tailder; // U*V'

  Handle(Expr_Product) secondmember = firstder * tailop; // U'*V

  Handle(Expr_Sum) resu = firstmember->ShallowSimplified() + secondmember->ShallowSimplified();
  // U*V' + U'*V

  return resu->ShallowSimplified();
}  


Handle(Expr_GeneralExpression) Expr_Product::ShallowSimplified () const
{
  Standard_Integer i;
  Standard_Integer max = NbOperands();
  Handle(Expr_GeneralExpression) op;
  Expr_SequenceOfGeneralExpression newops;
#ifndef DEB
  Standard_Real vals = 0.;
#else
  Standard_Real vals;
#endif
  Standard_Integer nbvals = 0;
  Standard_Boolean subprod = Standard_False;
  for (i=1; (i<= max) && !subprod; i++) {
    op = Operand(i);
    subprod = op->IsKind(STANDARD_TYPE(Expr_Product));
  }
  if (subprod) {
    Handle(Expr_GeneralExpression) other;
    Handle(Expr_Product) prodop;
    Standard_Integer nbsprodop;
    for (i=1; i<= max; i++) {
      op = Operand(i);
      if (op->IsKind(STANDARD_TYPE(Expr_Product))) {
	prodop = Handle(Expr_Product)::DownCast(op);
	nbsprodop = prodop->NbOperands();
	for (Standard_Integer j=1; j<= nbsprodop; j++) {
	  other = prodop->Operand(j);
	  newops.Append(other);
	}
      }
      else {
	newops.Append(op);
      }
    }
    prodop = new Expr_Product(newops);
    return prodop->ShallowSimplified();
  }

  Standard_Boolean noone = Standard_True;
  for (i = 1; i <= max ; i++) {
    op = Operand(i);
    if (op->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
      // numeric operands are cumulated separetly
      Handle(Expr_NumericValue) NVop = Handle(Expr_NumericValue)::DownCast(op);
      if (nbvals == 0) {
	noone = Standard_False;
	vals = NVop->GetValue();
	nbvals =1;
      }
      else {
	nbvals++;
	vals = vals * NVop->GetValue();
      }
    }
    else {
      newops.Append(op);
    }
  }
  if (!noone) {
    // numeric operands encountered
    if (newops.IsEmpty()) {         // result is only numericvalue (even zero)
      // only numerics
      return new Expr_NumericValue(vals); 
    }
    if (vals != 0.0) {
      if (vals == 1.0) {
	if (newops.Length() == 1) {
	  return newops(1);
	}
	return new Expr_Product(newops);
      }
      if (vals == -1.0) {
	Handle(Expr_GeneralExpression) thefact;
	if (newops.Length() == 1) {
	  thefact = newops(1);
	}
	else {
	  thefact = new Expr_Product(newops);
	}
	return -(thefact);
      }
      if (nbvals == 1) {
	Handle(Expr_Product) me = this;
	return me;
      }
      Handle(Expr_NumericValue) thevals = new Expr_NumericValue(vals);
      newops.Append(thevals);  // non-zero value added
      return  new Expr_Product(newops);
    }
    else {
      return new Expr_NumericValue(vals); // zero absorb
    }
  }
  Handle(Expr_Product) me = this;
  return me;
}

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

TCollection_AsciiString Expr_Product::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;
}