summaryrefslogtreecommitdiff
path: root/src/Expr/Expr_Division.cxx
blob: 90152fc9466051618ed6b7314726710b49ab94f5 (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
//static const char* sccsid = "@(#)Expr_Division.cxx	3.2 95/01/10"; // Do not delete this line. Used by sccs.
// Copyright: 	Matra-Datavision 1991
// File:	Expr_Division.cxx
// Created:	Wed Apr 17 14:14:22 1991
// Author:	Arnaud BOUZY
//		<adn>

#include <Expr_Division.ixx>
#include <Expr_Product.hxx>
#include <Expr_Square.hxx>
#include <Expr_UnaryMinus.hxx>
#include <Expr_Difference.hxx>
#include <Expr_NumericValue.hxx>
#include <Expr_Operators.hxx>
#include <Expr.hxx>

Expr_Division::Expr_Division (const Handle(Expr_GeneralExpression)& exp1, const Handle(Expr_GeneralExpression)& exp2)
{
  CreateFirstOperand(exp1);
  CreateSecondOperand(exp2);
}

Handle(Expr_GeneralExpression) Expr_Division::Copy () const
{
  return Expr::CopyShare(FirstOperand()) / Expr::CopyShare(SecondOperand());
}
  
Standard_Boolean Expr_Division::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
{
  Standard_Boolean ident = Standard_False;
  if (Other->IsKind(STANDARD_TYPE(Expr_Division))) {
    Handle(Expr_GeneralExpression) myfirst = FirstOperand();
    Handle(Expr_GeneralExpression) mysecond = SecondOperand();
    Handle(Expr_Division) DOther = Handle(Expr_Division)::DownCast(Other);
    Handle(Expr_GeneralExpression) fother = DOther->FirstOperand();
    Handle(Expr_GeneralExpression) sother = DOther->SecondOperand();
    if (myfirst->IsIdentical(fother) &&
	mysecond->IsIdentical(sother)) {
      ident = Standard_True;
    }
  }
  return ident;
}

Standard_Boolean Expr_Division::IsLinear () const
{
  Handle(Expr_GeneralExpression) myfirst = FirstOperand();
  Handle(Expr_GeneralExpression) mysecond = SecondOperand();
  if (mysecond->IsKind(STANDARD_TYPE(Expr_NamedUnknown)) || mysecond->ContainsUnknowns()) {
    return Standard_False;
  }
  return (myfirst->IsLinear() && mysecond->IsLinear());
}

Handle(Expr_GeneralExpression) Expr_Division::Derivative (const Handle(Expr_NamedUnknown)& X) const
{
  if (!Contains(X)) {
    return new Expr_NumericValue(0.0);
  }
  Handle(Expr_GeneralExpression) myfirst = FirstOperand();
  Handle(Expr_GeneralExpression) mysecond = SecondOperand();
  Handle(Expr_GeneralExpression) myfder = myfirst->Derivative(X);
  Handle(Expr_GeneralExpression) mysder = mysecond->Derivative(X);

  // "u'v"
  Handle(Expr_Product) firstprod = myfder * Expr::CopyShare(mysecond);

  Handle(Expr_GeneralExpression) firstsimp = firstprod->ShallowSimplified();
  // "uv'"
  Handle(Expr_Product) secondprod = Expr::CopyShare(myfirst) * mysder;
  Handle(Expr_GeneralExpression) secondsimp = secondprod->ShallowSimplified();
  // "u'v - uv'"
  Handle(Expr_Difference) mynumer = firstsimp - secondsimp;

  // " v2"
  Handle(Expr_Square) mydenom = new Expr_Square(Expr::CopyShare(mysecond));

  // result = "u'v-uv' / v2"

  Handle(Expr_GeneralExpression) snumer = mynumer->ShallowSimplified();
  Handle(Expr_GeneralExpression) sdenom = mydenom->ShallowSimplified();
  Handle(Expr_Division) result = snumer / sdenom;

  return result->ShallowSimplified();
}

Handle(Expr_GeneralExpression) Expr_Division::ShallowSimplified () const
{
  Handle(Expr_GeneralExpression) myfirst = FirstOperand();
  Handle(Expr_GeneralExpression) mysecond = SecondOperand();

  if (myfirst->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
    Handle(Expr_NumericValue) myNVfirst = Handle(Expr_NumericValue)::DownCast(myfirst);
    if (myNVfirst->GetValue() == 0.0) {
      // case 0/X2
      return new Expr_NumericValue(0.0);
    }
    if (mysecond->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
      // case num1/num2
      Handle(Expr_NumericValue) myNVsecond = Handle(Expr_NumericValue)::DownCast(mysecond);
      return new Expr_NumericValue(myNVfirst->GetValue()/myNVsecond->GetValue());
    }
  }
  else {
    if (mysecond->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
      // case X1/num2
      Handle(Expr_NumericValue) myNVsecond = Handle(Expr_NumericValue)::DownCast(mysecond);
      if (myNVsecond->GetValue() == 1.0) {
	// case X1/1
	return myfirst;
      }
    }
  }
  Handle(Expr_Division) me = this;
  return me;
}

Standard_Real Expr_Division::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
{
  Standard_Real res = FirstOperand()->Evaluate(vars,vals);
  return res / SecondOperand()->Evaluate(vars,vals);
}

TCollection_AsciiString Expr_Division::String() const
{
  Handle(Expr_GeneralExpression) op1 = FirstOperand();
  Handle(Expr_GeneralExpression) op2 = SecondOperand();
  TCollection_AsciiString str;
  if (op1->NbSubExpressions() > 1) {
    str = "(";
    str += op1->String();
    str += ")";
  }
  else {
    str = op1->String();
  }
  str += "/";
  if (op2->NbSubExpressions() > 1) {
    str += "(";
    str += op2->String();
    str += ")";
  }
  else {
    str += op2->String();
  }
  return str;
}