summaryrefslogtreecommitdiff
path: root/src/Expr/Expr_NamedFunction.cxx
blob: dec5580e094864634fa02bae05ffee177c35d146 (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
//static const char* sccsid = "@(#)Expr_NamedFunction.cxx	3.4 95/02/02"; // Do not delete this line. Used by sccs.
// Copyright: 	Matra-Datavision 1991
// File:	Expr_NamedFunction.cxx
// Created:	Wed Jun 26 11:44:52 1991
// Author:	Arnaud BOUZY
//		<adn>

#include <Expr_NamedFunction.ixx>
#include <Expr_NamedConstant.hxx>
#include <Standard_OutOfRange.hxx>
#include <Expr_FunctionDerivative.hxx>
#include <Expr_NumericValue.hxx>
#include <Expr.hxx>

Expr_NamedFunction::Expr_NamedFunction (const TCollection_AsciiString& name, const Handle(Expr_GeneralExpression)& exp, const Expr_Array1OfNamedUnknown& vars) : 
                                 myVariables(vars.Lower(),vars.Upper())
{
  myVariables=vars;
  myName = name;
  myExp = exp;
}

void Expr_NamedFunction::SetName(const TCollection_AsciiString& newname)
{
  myName = newname;
}

TCollection_AsciiString Expr_NamedFunction::GetName () const
{
  return myName;
}

Standard_Integer Expr_NamedFunction::NbOfVariables () const
{
  return myVariables.Length();
}

Handle(Expr_NamedUnknown) Expr_NamedFunction::Variable (const Standard_Integer index) const
{
  return myVariables(index);
}

Standard_Real Expr_NamedFunction::Evaluate (const Expr_Array1OfNamedUnknown& vars, const TColStd_Array1OfReal& values) const
{
  if (vars.Length() != values.Length()) {
    Standard_OutOfRange::Raise();
  }
  return myExp->Evaluate(vars,values);
}


Handle(Expr_GeneralFunction) Expr_NamedFunction::Copy () const
{
  return new Expr_NamedFunction(myName,Expr::CopyShare(Expression()),myVariables);
}

Handle(Expr_GeneralFunction) Expr_NamedFunction::Derivative(const Handle(Expr_NamedUnknown)& var) const
{
  Handle(Expr_NamedFunction) me = this;
  return new Expr_FunctionDerivative(me,var,1);
}

Handle(Expr_GeneralFunction) Expr_NamedFunction::Derivative(const Handle(Expr_NamedUnknown)& var, const Standard_Integer deg) const
{
  Handle(Expr_NamedFunction) me = this;
  return new Expr_FunctionDerivative(me,var,deg);
}

Standard_Boolean Expr_NamedFunction::IsIdentical (const Handle(Expr_GeneralFunction)& func) const
{
  if (!func->IsKind(STANDARD_TYPE(Expr_NamedFunction))) {
    return Standard_False;
  }
  if (myName != Handle(Expr_NamedFunction)::DownCast(func)->GetName()) {       
    return Standard_False;
  }
  Standard_Integer nbvars = NbOfVariables();
  if (nbvars != func->NbOfVariables()) {
    return Standard_False;
  }
  Handle(Expr_NamedUnknown) thisvar;
  for (Standard_Integer i =1; i<=nbvars; i++) {
    thisvar = Variable(i);
    if (!thisvar->IsIdentical(func->Variable(i))) {
      return Standard_False;
    }
  }
  if (!Expression()->IsIdentical(Handle(Expr_NamedFunction)::DownCast(func)->Expression())) {
    return Standard_False;
  }
  return Standard_True;
}

Standard_Boolean Expr_NamedFunction::IsLinearOnVariable(const Standard_Integer) const
{
  // bad implementation, should be improved
  return myExp->IsLinear();
}

TCollection_AsciiString Expr_NamedFunction::GetStringName() const
{
  return myName;
}

Handle(Expr_GeneralExpression) Expr_NamedFunction::Expression() const
{
  return myExp;
}

void Expr_NamedFunction::SetExpression(const Handle(Expr_GeneralExpression)& anexp)
{
  myExp = anexp;
}