Coverage for kwant/continuum/_common.py : 99%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Copyright 2011-2017 Kwant authors. # # This file is part of Kwant. It is subject to the license terms in the file # LICENSE.rst found in the top-level directory of this distribution and at # http://kwant-project.org/license. A list of Kwant authors can be found in # the file AUTHORS.rst at the top-level directory of this distribution and at # http://kwant-project.org/authors.
# TODO: remove when sympy correctly includes MutableDenseMatrix (lol).
'eye': sympy.eye, 'identity': sympy.eye})
# workaroud for https://github.com/sympy/sympy/issues/12060
################ Helpers to handle sympy
"""Return a callable object for computing a continuum Hamiltonian.
.. warning:: This function uses ``eval`` (because it calls ``sympy.sympify``), and thus should not be used on unsanitized input.
If necessary, the given expression is sympified using `kwant.continuum.sympify`. It is then converted into a callable object.
Parameters ---------- expr : str or SymPy expression Expression to be converted into a callable object locals : dict or ``None`` (default) Additional definitions for `~kwant.continuum.sympify`.
Examples -------- >>> f = lambdify('a + b', locals={'b': 'b + c'}) >>> f(1, 3, 5) 9
>>> ns = {'sigma_plus': [[0, 2], [0, 0]]} >>> f = lambdify('k_x**2 * sigma_plus', ns) >>> f(0.25) array([[ 0. , 0.125], [ 0. , 0. ]]) """
"""Sympify object using special rules for Hamiltonians.
If `'expr`` is already a type that SymPy understands, it will do nothing but return that value. Note that ``locals`` will not be used in this situation.
Otherwise, it is sympified by ``sympy.sympify`` with a modified namespace such that
* the position operators "x", "y" or "z" and momentum operators "k_x", "k_y", and "k_z" do not commute, * all single-letter identifiers and names of Greek letters (e.g. "pi" or "gamma") are treated as symbols, * "kron" corresponds to ``sympy.physics.quantum.TensorProduct``, and "identity" to ``sympy.eye``, * "sigma_0", "sigma_x", "sigma_y", "sigma_z" are the Pauli matrices.
In addition, Python list literals are interpreted as SymPy matrices.
.. warning:: This function uses ``eval`` (because it calls ``sympy.sympify``), and thus should not be used on unsanitized input.
Parameters ---------- expr : str or SymPy expression Expression to be converted to a SymPy object. locals : dict or ``None`` (default) Additional entries for the namespace under which `expr` is sympified. The keys must be valid Python variable names. The values may be strings, since they are all are sent through `continuum.sympify` themselves before use. (Note that this is a difference to how ``sympy.sympify`` behaves.)
.. note:: When a value of `locals` is already a SymPy object, it is used as-is, and the caller is responsible to set the commutativity of its symbols appropriately. This possible source of errors is demonstrated in the last example below.
Returns ------- result : SymPy object
Examples -------- >>> sympify('k_x * A(x) * k_x + V(x)') k_x*A(x)*k_x + V(x) # as valid sympy object
>>> sympify('k_x**2 + V', locals={'V': 'V_0 + V(x)'}) k_x**2 + V(x) + V_0
>>> ns = {'sigma_plus': [[0, 2], [0, 0]]} >>> sympify('k_x**2 * sigma_plus', ns) Matrix([ [0, 2*k_x**2], [0, 0]])
>>> sympify('k_x * A(c) * k_x', locals={'c': 'x'}) k_x*A(x)*k_x >>> sympify('k_x * A(c) * k_x', locals={'c': sympy.Symbol('x')}) A(x)*k_x**2
"""
# if ``expr`` is already a ``sympy`` object we may terminate a code path '"locals" will not be used.', RuntimeWarning, stacklevel=2)
# We assume that all present functions, like "sin", "cos", will be # provided by user during the final evaluation through "params". # Therefore we make sure they are defined as AppliedUndef, not built-in # sympy types. for r in expr.atoms(sympy.Function)}
# if ``expr`` is not a "sympy" then we proceed with sympifying process
or not k.isidentifier() or keyword.iskeyword(k)): "Invalid key in 'locals': {}\nKeys must be " "identifiers and may not be keywords".format(repr(k)))
# sympify values of locals before updating it with extra_ns # if input is for example ``[[k_x * A(x) * k_x]]`` after the first # sympify we are getting list of sympy objects, so we call sympify # second time to obtain ``sympy`` matrices. finally: converter[list] = stored_value else:
"""Make sure that specified symbols are defined as commutative.
Parameters ---------- expr: sympy.Expr or sympy.Matrix symbols: sequace of symbols Set of symbols that are requiered to be commutative. It doesn't matter of symbol is provided as commutative or not.
Returns ------- input expression with all specified symbols changed to commutative. """
"""Parse ``expr`` into monomials in the symbols in ``gens``.
Parameters ---------- expr: sympy.Expr or sympy.Matrix Input expression that will be parsed into monomials. gens: sequence of sympy.Symbol objects Generators used to separate input ``expr`` into monomials.
Returns ------- dictionary (generator: monomial)
Note ---- All generators will be substituted with its commutative version using `kwant.continuum.make_commutative`` function. """ else:
"""Parse ``expression`` into monomials in the symbols in ``gens``.
Example ------- >>> expr = A * (x**2 + y) + B * x + C >>> _expression_monomials(expr, x, y) {1: C, x**2: A, y: A, x: B} """
'"gens" as their argument.')
else:
### to not create key
################ general help functions
|