1#  ___________________________________________________________________________
2#
3#  Pyomo: Python Optimization Modeling Objects
4#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
5#  Under the terms of Contract DE-NA0003525 with National Technology and
6#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
7#  rights in this software.
8#  This software is distributed under the 3-clause BSD License.
9#  ___________________________________________________________________________
10
11from __future__ import division
12import math
13
14#
15# Common intrinsic functions
16#
17from pyomo.core.expr import expr_common as common
18
19#
20# Provide a global value that indicates which expression system is being used
21#
22class Mode(object):
23    pyomo5_trees = (3,)
24_mode = Mode.pyomo5_trees
25
26#
27# Pull symbols from the appropriate expression system
28#
29from pyomo.core.expr import numvalue as _numvalue
30from pyomo.core.expr import boolean_value as _logicalvalue
31
32# Pyomo5
33if _mode == Mode.pyomo5_trees:
34    from pyomo.core.expr import numeric_expr as _numeric_expr
35    from pyomo.core.expr.numeric_expr import (_add, _sub, _mul, _div, _pow,
36                                              _neg, _abs, _inplace, _unary,
37                                              NumericValue, native_types,
38                                              nonpyomo_leaf_types,
39                                              native_numeric_types,
40                                              as_numeric, value,
41                                              evaluate_expression,
42                                              expression_to_string,
43                                              polynomial_degree,
44                                              clone_expression,
45                                              sizeof_expression,
46                                              _expression_is_fixed,
47                                              clone_counter,
48                                              nonlinear_expression,
49                                              linear_expression, ExpressionBase,
50                                              NegationExpression,
51                                              NPV_NegationExpression,
52                                              ExternalFunctionExpression,
53                                              NPV_ExternalFunctionExpression,
54                                              PowExpression, NPV_PowExpression,
55                                              ProductExpression,
56                                              NPV_ProductExpression,
57                                              MonomialTermExpression,
58                                              DivisionExpression,
59                                              NPV_DivisionExpression,
60                                              ReciprocalExpression,
61                                              NPV_ReciprocalExpression,
62                                              _LinearOperatorExpression,
63                                              SumExpressionBase,
64                                              NPV_SumExpression, SumExpression,
65                                              _MutableSumExpression,
66                                              Expr_ifExpression,
67                                              UnaryFunctionExpression,
68                                              NPV_UnaryFunctionExpression,
69                                              AbsExpression, NPV_AbsExpression,
70                                              LinearExpression,
71                                              _MutableLinearExpression,
72                                              decompose_term,
73                                              LinearDecompositionError,
74                                              _decompose_linear_terms,
75                                              _process_arg,
76                                              _generate_sum_expression,
77                                              _generate_mul_expression,
78                                              _generate_other_expression,
79                                              _generate_intrinsic_function_expression,
80                                              _balanced_parens,
81                                              NPV_expression_types)
82    from pyomo.core.expr import logical_expr as _logical_expr
83    from pyomo.core.expr.logical_expr import (native_logical_types, BooleanValue,
84                                              BooleanConstant, _lt, _le, _eq,
85                                              _and, _or, _equiv, _inv, _xor,
86                                              _impl,
87                                              RangedExpression,
88                                              InequalityExpression, inequality,
89                                              EqualityExpression,
90                                              _generate_relational_expression,
91                                              _generate_logical_proposition,
92                                              BooleanExpressionBase, lnot,
93                                              equivalent, xor, implies,
94                                              _flattened, land, lor, exactly,
95                                              atmost, atleast,
96                                              UnaryBooleanExpression,
97                                              NotExpression,
98                                              BinaryBooleanExpression,
99                                              EquivalenceExpression,
100                                              XorExpression,
101                                              ImplicationExpression,
102                                              NaryBooleanExpression,
103                                              _add_to_and_or_expression,
104                                              AndExpression, OrExpression,
105                                              ExactlyExpression,
106                                              AtMostExpression,
107                                              AtLeastExpression,
108                                              special_boolean_atom_types)
109    from pyomo.core.expr.template_expr import (TemplateExpressionError,
110                                               _NotSpecified, GetItemExpression,
111                                               GetAttrExpression,
112                                               _TemplateSumExpression_argList,
113                                               TemplateSumExpression,
114                                               IndexTemplate, resolve_template,
115                                               ReplaceTemplateExpression,
116                                               substitute_template_expression,
117                                               _GetItemIndexer,
118                                               substitute_getitem_with_param,
119                                               substitute_template_with_value,
120                                               _set_iterator_template_generator,
121                                               _template_iter_context,
122                                               templatize_rule,
123                                               templatize_constraint)
124    from pyomo.core.expr import visitor as _visitor
125    from pyomo.core.expr.visitor import (SymbolMap, StreamBasedExpressionVisitor,
126                                         SimpleExpressionVisitor,
127                                         ExpressionValueVisitor,
128                                         replace_expressions,
129                                         ExpressionReplacementVisitor,
130                                         _EvaluationVisitor,
131                                         FixedExpressionError,
132                                         NonConstantExpressionError,
133                                         _EvaluateConstantExpressionVisitor,
134                                         _ComponentVisitor, identify_components,
135                                         _VariableVisitor, identify_variables,
136                                         _MutableParamVisitor,
137                                         identify_mutable_parameters,
138                                         _PolynomialDegreeVisitor,
139                                         _IsFixedVisitor, _ToStringVisitor)
140    # FIXME: we shouldn't need circular dependencies between modules
141    _visitor.LinearExpression = _numeric_expr.LinearExpression
142    _visitor.MonomialTermExpression = _numeric_expr.MonomialTermExpression
143    _visitor.NPV_expression_types = _numeric_expr.NPV_expression_types
144    _visitor.clone_counter = _numeric_expr.clone_counter
145
146    # Initialize numvalue functions
147    _numvalue._generate_sum_expression \
148        = _numeric_expr._generate_sum_expression
149    _numvalue._generate_mul_expression \
150        = _numeric_expr._generate_mul_expression
151    _numvalue._generate_other_expression \
152        = _numeric_expr._generate_other_expression
153    _numvalue._generate_relational_expression \
154        = _logical_expr._generate_relational_expression
155
156    # Initialize logicalvalue functions
157    _logicalvalue._generate_logical_proposition = _logical_expr._generate_logical_proposition
158else:
159    raise ValueError("No other expression systems are supported in Pyomo right now.")    #pragma: no cover
160
161
162def Expr_if(IF=None, THEN=None, ELSE=None):
163    """
164    Function used to construct a logical conditional expression.
165    """
166    return Expr_ifExpression(IF_=IF, THEN_=THEN, ELSE_=ELSE)
167
168#
169# NOTE: abs() and pow() are not defined here, because they are
170# Python operators.
171#
172def ceil(arg):
173    return _generate_intrinsic_function_expression(arg, 'ceil', math.ceil)
174
175def floor(arg):
176    return _generate_intrinsic_function_expression(arg, 'floor', math.floor)
177
178# e ** x
179def exp(arg):
180    return _generate_intrinsic_function_expression(arg, 'exp', math.exp)
181
182def log(arg):
183    return _generate_intrinsic_function_expression(arg, 'log', math.log)
184
185def log10(arg):
186    return _generate_intrinsic_function_expression(arg, 'log10', math.log10)
187
188# FIXME: this is nominally the same as x ** 0.5, but follows a different
189# path and produces a different NL file!
190def sqrt(arg):
191    return _generate_intrinsic_function_expression(arg, 'sqrt', math.sqrt)
192#    return _generate_expression(common._pow, arg, 0.5)
193
194
195def sin(arg):
196    return _generate_intrinsic_function_expression(arg, 'sin', math.sin)
197
198def cos(arg):
199    return _generate_intrinsic_function_expression(arg, 'cos', math.cos)
200
201def tan(arg):
202    return _generate_intrinsic_function_expression(arg, 'tan', math.tan)
203
204def sinh(arg):
205    return _generate_intrinsic_function_expression(arg, 'sinh', math.sinh)
206
207def cosh(arg):
208    return _generate_intrinsic_function_expression(arg, 'cosh', math.cosh)
209
210def tanh(arg):
211    return _generate_intrinsic_function_expression(arg, 'tanh', math.tanh)
212
213
214def asin(arg):
215    return _generate_intrinsic_function_expression(arg, 'asin', math.asin)
216
217def acos(arg):
218    return _generate_intrinsic_function_expression(arg, 'acos', math.acos)
219
220def atan(arg):
221    return _generate_intrinsic_function_expression(arg, 'atan', math.atan)
222
223def asinh(arg):
224    return _generate_intrinsic_function_expression(arg, 'asinh', math.asinh)
225
226def acosh(arg):
227    return _generate_intrinsic_function_expression(arg, 'acosh', math.acosh)
228
229def atanh(arg):
230    return _generate_intrinsic_function_expression(arg, 'atanh', math.atanh)
231