Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)))
def legendre(n, x):
e = Integer(1)/(Integer(2)**n * fact(Integer(n))) * diff((x**2-1)**n, x, n)
return e.expand()
def legendre(n, x):
e = Integer(1)/(Integer(2)**n * fact(Integer(n))) * diff((x**2-1)**n, x, n)
return e.expand()
def _canonicalize(self, expression):
if isinstance(expression, float):
return sympy.RealNumber(expression)
elif isinstance(expression, int):
return sympy.Integer(expression)
else:
#expression = expression.expand() This would be a good way to canonicalize, but is quite slow
return expression
Args:
lmax (int): highest order angular momentum quantum number
scaled (bool): if scaled, includes factor of 1 / (2 * np.pi ** 0.5)
"""
def _top_sh(lp, kr, sp, sm):
return ((2 ** kr * (2 * lp + 1) / (2 * lp + 2)) ** 0.5 *
(_x * sp - (1 - kr) * _y * sm))
def _mid_sh(lp, m, sm, smm):
return (((2 * lp + 1) * _z * sm - ((lp + m) * (lp - m)) ** 0.5 *
(_x*_x + _y*_y + _z*_z) * smm) /
(((lp + m + 1) * (lp - m + 1)) ** 0.5))
def _bot_sh(lp, kr, sp, sm):
return ((2 ** kr * (2 * lp + 1) / (2 * lp + 2)) ** 0.5 *
(_y * sp + (1 - kr) * _x * sm))
sh = OrderedDict([(l, OrderedDict([])) for l in range(lmax + 1)])
sh[0][0] = Integer(1)
for L in range(1, lmax + 1):
lp = L - 1
kr = int(not lp)
mls = list(range(-L, L + 1))
sh[L][mls[0]] = _bot_sh(lp, kr, sh[lp][lp], sh[lp][-lp])
for ml in mls[1:-1]:
try: rec = sh[lp - 1][ml]
except KeyError: rec = sh[lp][ml]
sh[L][ml] = _mid_sh(lp, ml, sh[lp][ml], rec)
sh[L][mls[-1]] = _top_sh(lp, kr, sh[lp][lp], sh[lp][-lp])
if scaled:
for L in range(lmax + 1):
for ml in range(-L, L + 1):
sh[L][ml] /= (2 * np.pi ** 0.5)
return sh