/usr/lib/python3/dist-packages/optlang/symbolics.py is in python3-optlang 1.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright 2017 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module contains a common interface to symbolic operations in Sympy and Symengine respectively.
All symbolic operations in the optlang codebase should use these functions.
"""
from __future__ import division
import os
import six
import uuid
import logging
import optlang
logger = logging.getLogger(__name__)
# Read environment variable
SYMENGINE_PREFERENCE = os.environ.get("OPTLANG_USE_SYMENGINE", "")
if SYMENGINE_PREFERENCE.lower() in ("false", "no", "off"):
USE_SYMENGINE = False
else: # pragma: no cover
try:
import symengine
import symengine.sympy_compat
from symengine.sympy_compat import Symbol as symengine_Symbol
except ImportError as e:
if SYMENGINE_PREFERENCE.lower() in ("true", "yes", "on"):
logger.warn("Symengine could not be imported: " + str(e))
if os.getenv('TRAVIS', None) is not None: # Travis should error here # pragma: no cover
raise RuntimeError("Symengine should be used but could not be!")
USE_SYMENGINE = False
else:
USE_SYMENGINE = True
if USE_SYMENGINE: # pragma: no cover # noqa: C901
import operator
from six.moves import reduce
optlang._USING_SYMENGINE = True
Integer = symengine.Integer
Real = symengine.RealDouble
Basic = symengine.Basic
Number = symengine.Number
Zero = Integer(0)
One = Integer(1)
NegativeOne = Integer(-1)
sympify = symengine.sympy_compat.sympify
Add = symengine.Add
Mul = symengine.Mul
Pow = symengine.sympy_compat.Pow
class Symbol(symengine_Symbol):
def __new__(cls, name, *args, **kwargs):
if not isinstance(name, six.string_types):
raise TypeError("name should be a string, not %s" % repr(type(name)))
return symengine_Symbol.__new__(cls, name)
def __init__(self, name, *args, **kwargs):
super(Symbol, self).__init__(name)
self._name = name
def __repr__(self):
return self._name
def __str__(self):
return self._name
def __getnewargs__(self):
return (self._name, {})
def add(*args):
if len(args) == 1:
args = args[0]
elif len(args) == 0:
return Zero
return Add(*args)
def mul(*args):
if len(args) == 1:
args = args[0]
elif len(args) == 0:
return One # if you multiply nothing the result should be zero
return Mul(*args)
else: # Use sympy
import sympy
from sympy.core.assumptions import _assume_rules
from sympy.core.facts import FactKB
from sympy.core.expr import Expr
optlang._USING_SYMENGINE = False
Integer = sympy.Integer
Real = sympy.RealNumber
Basic = sympy.Basic
Number = sympy.Number
Zero = Integer(0)
One = Integer(1)
NegativeOne = Integer(-1)
sympify = sympy.sympify
Add = sympy.Add
Mul = sympy.Mul
Pow = sympy.Pow
class Symbol(sympy.Symbol):
def __new__(cls, name, **kwargs):
if not isinstance(name, six.string_types):
raise TypeError("name should be a string, not %s" % repr(type(name)))
obj = sympy.Symbol.__new__(cls, str(uuid.uuid1()))
obj.name = name
obj._assumptions = FactKB(_assume_rules)
obj._assumptions._tell('commutative', True)
obj._assumptions._tell('uuid', uuid.uuid1())
return obj
def __init__(self, *args, **kwargs):
super(Symbol, self).__init__()
def add(*args):
if len(args) == 1:
args = args[0]
elif len(args) == 0:
return Zero
return sympy.Add._from_args(args)
def mul(*args):
if len(args) == 1:
args = args[0]
elif len(args) == 0:
return One
return sympy.Mul._from_args(args)
|