/usr/lib/python2.7/dist-packages/ufl/mathfunctions.py is in python-ufl 2017.2.0.0-2.
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | # -*- coding: utf-8 -*-
"""This module provides basic mathematical functions."""
# Copyright (C) 2008-2016 Martin Sandve Alnæs
#
# This file is part of UFL.
#
# UFL is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# UFL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with UFL. If not, see <http://www.gnu.org/licenses/>.
#
# Modified by Anders Logg, 2008
# Modified by Kristian B. Oelgaard, 2011
import math
from ufl.utils.py23 import as_native_strings
from ufl.log import warning, error
from ufl.core.operator import Operator
from ufl.core.ufl_type import ufl_type
from ufl.constantvalue import is_true_ufl_scalar, ScalarValue, Zero, FloatValue, IntValue, as_ufl
"""
TODO: Include additional functions available in <cmath> (need derivatives as well):
Exponential and logarithmic functions:
log10 Compute common logarithm (function)
TODO: Any other useful special functions?
About bessel functions:
http://en.wikipedia.org/wiki/Bessel_function
Portable implementations of bessel functions:
http://www.boost.org/doc/libs/1_47_0/libs/math/doc/sf_and_dist/html/math_toolkit/main_overview/tr1.html
Implementation in C++ std::tr1:: or boost::math::tr1::
- BesselK: cyl_bessel_k(nu, x)
- BesselI: cyl_bessel_i(nu, x)
- BesselJ: cyl_bessel_j(nu, x)
- BesselY: cyl_neumann(nu, x)
"""
# --- Function representations ---
@ufl_type(is_abstract=True, is_scalar=True, num_ops=1)
class MathFunction(Operator):
"Base class for all unary scalar math functions."
# Freeze member variables for objects in this class
__slots__ = as_native_strings(("_name",))
def __init__(self, name, argument):
Operator.__init__(self, (argument,))
if not is_true_ufl_scalar(argument):
error("Expecting scalar argument.")
self._name = name
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
try:
res = getattr(math, self._name)(a)
except ValueError:
warning('Value error in evaluation of function %s with argument %s.' % (self._name, a))
raise
return res
def __str__(self):
return "%s(%s)" % (self._name, self.ufl_operands[0])
@ufl_type()
class Sqrt(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.sqrt(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "sqrt", argument)
@ufl_type()
class Exp(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.exp(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "exp", argument)
@ufl_type()
class Ln(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.log(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "ln", argument)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
return math.log(a)
@ufl_type()
class Cos(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.cos(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "cos", argument)
@ufl_type()
class Sin(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.sin(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "sin", argument)
@ufl_type()
class Tan(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.tan(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "tan", argument)
@ufl_type()
class Cosh(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.cosh(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "cosh", argument)
@ufl_type()
class Sinh(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.sinh(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "sinh", argument)
@ufl_type()
class Tanh(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.tanh(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "tanh", argument)
@ufl_type()
class Acos(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.acos(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "acos", argument)
@ufl_type()
class Asin(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.asin(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "asin", argument)
@ufl_type()
class Atan(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
return FloatValue(math.atan(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "atan", argument)
@ufl_type(is_scalar=True, num_ops=2)
class Atan2(Operator):
__slots__ = ()
def __new__(cls, arg1, arg2):
if isinstance(arg1, (ScalarValue, Zero)) and isinstance(arg2, (ScalarValue, Zero)):
return FloatValue(math.atan2(float(arg1), float(arg2)))
return Operator.__new__(cls)
def __init__(self, arg1, arg2):
Operator.__init__(self, (arg1, arg2))
if not is_true_ufl_scalar(arg1):
error("Expecting scalar argument 1.")
if not is_true_ufl_scalar(arg2):
error("Expecting scalar argument 2.")
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
b = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
try:
res = math.atan2(a, b)
except ValueError:
warning('Value error in evaluation of function atan_2 with arguments %s, %s.' % (a, b))
raise
return res
def __str__(self):
return "atan_2(%s,%s)" % (self.ufl_operands[0], self.ufl_operands[1])
def _find_erf():
import math
if hasattr(math, 'erf'):
return math.erf
import scipy.special
if hasattr(scipy.special, 'erf'):
return scipy.special.erf
return None
@ufl_type()
class Erf(MathFunction):
__slots__ = ()
def __new__(cls, argument):
if isinstance(argument, (ScalarValue, Zero)):
erf = _find_erf()
if erf is not None:
return FloatValue(erf(float(argument)))
return MathFunction.__new__(cls)
def __init__(self, argument):
MathFunction.__init__(self, "erf", argument)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
erf = _find_erf()
if erf is None:
error("No python implementation of erf available on this system, cannot evaluate. Upgrade python or install scipy.")
return erf(a)
@ufl_type(is_abstract=True, is_scalar=True, num_ops=2)
class BesselFunction(Operator):
"Base class for all bessel functions"
__slots__ = as_native_strings(("_name", "_classname"))
def __init__(self, name, classname, nu, argument):
if not is_true_ufl_scalar(nu):
error("Expecting scalar nu.")
if not is_true_ufl_scalar(argument):
error("Expecting scalar argument.")
# Use integer representation if suitable
fnu = float(nu)
inu = int(nu)
if fnu == inu:
nu = as_ufl(inu)
else:
nu = as_ufl(fnu)
Operator.__init__(self, (nu, argument))
self._classname = classname
self._name = name
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
try:
import scipy.special
except ImportError:
error("You must have scipy installed to evaluate bessel functions in python.")
name = self._name[-1]
if isinstance(self.ufl_operands[0], IntValue):
nu = int(self.ufl_operands[0])
functype = 'n' if name != 'i' else 'v'
else:
nu = self.ufl_operands[0].evaluate(x, mapping, component,
index_values)
functype = 'v'
func = getattr(scipy.special, name + functype)
return func(nu, a)
def __str__(self):
return "%s(%s, %s)" % (self._name, self.ufl_operands[0],
self.ufl_operands[1])
@ufl_type()
class BesselJ(BesselFunction):
__slots__ = ()
def __init__(self, nu, argument):
BesselFunction.__init__(self, "cyl_bessel_j", "BesselJ", nu, argument)
@ufl_type()
class BesselY(BesselFunction):
__slots__ = ()
def __init__(self, nu, argument):
BesselFunction.__init__(self, "cyl_bessel_y", "BesselY", nu, argument)
@ufl_type()
class BesselI(BesselFunction):
__slots__ = ()
def __init__(self, nu, argument):
BesselFunction.__init__(self, "cyl_bessel_i", "BesselI", nu, argument)
@ufl_type()
class BesselK(BesselFunction):
__slots__ = ()
def __init__(self, nu, argument):
BesselFunction.__init__(self, "cyl_bessel_k", "BesselK", nu, argument)
|