summaryrefslogtreecommitdiff
path: root/src/Expr/Expr_UnaryFunction.cxx
blob: 6b59d79be093fce9e6f43913dd88318f7f5f8d94 (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
//static const char* sccsid = "@(#)Expr_UnaryFunction.cxx	3.2 95/01/10"; // Do not delete this line. Used by sccs.
// Copyright: 	Matra-Datavision 1991
// File:	Expr_UnaryFunction.cxx
// Created:	Tue May 28 14:40:33 1991
// Author:	Arnaud BOUZY
//		<adn>

#ifndef DEB
#define No_Standard_RangeError
#define No_Standard_OutOfRange
#endif

#include <Expr_UnaryFunction.ixx>
#include <Expr_InvalidFunction.hxx>
#include <Expr_Array1OfNamedUnknown.hxx>
#include <Expr_Array1OfGeneralExpression.hxx>
#include <Expr_FunctionDerivative.hxx>
#include <Expr_Product.hxx>
#include <Expr_Operators.hxx>
#include <Expr.hxx>

Expr_UnaryFunction::Expr_UnaryFunction (const Handle(Expr_GeneralFunction)& func, const Handle(Expr_GeneralExpression)& exp)
{
  if (func->NbOfVariables() != 1) {
    Expr_InvalidFunction::Raise();
  }
  myFunction = func;
  CreateOperand(exp);
}

Handle(Expr_GeneralFunction) Expr_UnaryFunction::Function () const
{
  return myFunction;
}

Handle(Expr_GeneralExpression) Expr_UnaryFunction::ShallowSimplified () const
{
  Handle(Expr_GeneralExpression) op = Operand();
  if (op->IsKind(STANDARD_TYPE(Expr_NumericValue))) {
    Handle(Expr_NumericValue) nval = Handle(Expr_NumericValue)::DownCast(op);
    TColStd_Array1OfReal tabval(1,1);
    tabval(1) = nval->GetValue();
    Expr_Array1OfNamedUnknown vars(1,1);
    vars(1) = myFunction->Variable(1);
    Standard_Real res = myFunction->Evaluate(vars,tabval);
    return new Expr_NumericValue(res);
  }
  Handle(Expr_UnaryFunction) me = this;
  return me;
}

Handle(Expr_GeneralExpression) Expr_UnaryFunction::Copy () const
{
  return new Expr_UnaryFunction(myFunction,Expr::CopyShare(Operand()));
}

Standard_Boolean Expr_UnaryFunction::IsIdentical (const Handle(Expr_GeneralExpression)& Other) const
{
  if (!Other->IsKind(STANDARD_TYPE(Expr_UnaryFunction))) {
    return Standard_False;
  }
  Handle(Expr_UnaryFunction) fother = Handle(Expr_UnaryFunction)::DownCast(Other);
  Handle(Expr_GeneralExpression) otherexp = fother->Operand();
  if (otherexp->IsIdentical(Operand())) {
    if (myFunction->IsIdentical(fother->Function())) {
      return Standard_True;
    }
  }
  return Standard_False;
}

Standard_Boolean Expr_UnaryFunction::IsLinear () const
{
  if (!ContainsUnknowns()) {
    return Standard_True;
  }
  if (!Operand()->IsLinear()) {
    return Standard_False;
  }
  return myFunction->IsLinearOnVariable(1);
}

Handle(Expr_GeneralExpression) Expr_UnaryFunction::Derivative (const Handle(Expr_NamedUnknown)& X) const
{
  Handle(Expr_NamedUnknown) myvar = myFunction->Variable(1);
  Handle(Expr_GeneralExpression) myop = Operand();
  Handle(Expr_GeneralExpression) myexpder = myop->Derivative(X);
  Handle(Expr_GeneralFunction) myfuncder = myFunction->Derivative(myvar);
  Handle(Expr_UnaryFunction) firstpart = new Expr_UnaryFunction(myfuncder,Expr::CopyShare(myop));
  Handle(Expr_Product) resu = firstpart->ShallowSimplified() * myexpder;
  return resu->ShallowSimplified();
}

Standard_Real Expr_UnaryFunction::Evaluate(const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& vals) const
{
  Expr_Array1OfNamedUnknown varsfunc(1,1);
  varsfunc(1) = myFunction->Variable(1);
  TColStd_Array1OfReal valsfunc(1,1);
  valsfunc(1) = Operand()->Evaluate(vars,vals);
  return myFunction->Evaluate(varsfunc,valsfunc);
}

TCollection_AsciiString Expr_UnaryFunction::String() const
{
  TCollection_AsciiString res = myFunction->GetStringName();
  res += "(";
  res += Operand()->String();
  res += ")";;
  return res;
}