Exemplo n.º 1
0
    def _format_represent(self, result, format):
        if format == "sympy" and not isinstance(result, Matrix):
            return to_sympy(result)
        elif format == "numpy" and not isinstance(result, numpy_ndarray):
            return to_numpy(result)
        elif format == "scipy.sparse" and not isinstance(result, scipy_sparse_matrix):
            return to_scipy_sparse(result)

        return result
Exemplo n.º 2
0
Arquivo: qexpr.py Projeto: ytann/sympy
    def _format_represent(self, result, format):
        if format == 'sympy' and not isinstance(result, Matrix):
            return to_sympy(result)
        elif format == 'numpy' and not isinstance(result, numpy_ndarray):
            return to_numpy(result)
        elif format == 'scipy.sparse' and \
                not isinstance(result, scipy_sparse_matrix):
            return to_scipy_sparse(result)

        return result
Exemplo n.º 3
0
Arquivo: qexpr.py Projeto: Aang/sympy
    def _represent(self, **options):
        """Represent this object in a given basis.

        This method dispatches to the actual methods that perform the
        representation. Subclases of QExpr should define various methods to
        determine how the object will be represented in various bases. The
        format of these methods is::

            def _represent_BasisName(self, basis, **options):

        Thus to define how a quantum object is represented in the basis of
        the operator Position, you would define::

            def _represent_Position(self, basis, **options):

        Usually, basis object will be instances of Operator subclasses, but
        there is a chance we will relax this in the future to accomodate other
        types of basis sets that are not associated with an operator.

        If the ``format`` option is given it can be ("sympy", "numpy",
        "scipy.sparse"). This will ensure that any matrices that result from
        representing the object are returned in the appropriate matrix format.

        Parameters
        ==========
        basis : Operator
            The Operator whose basis functions will be used as the basis for
            representation.
        options : dict
            A dictionary of key/value pairs that give options and hints for
            the representation, such as the number of basis functions to
            be used.
        """
        basis = options.pop('basis',None)
        if basis is None:
            result = self._represent_default_basis(**options)
        else:
            result = dispatch_method(self, '_represent', basis, **options)

        # If we get a matrix representation, convert it to the right format.
        format = options.get('format', 'sympy')
        if format == 'sympy' and not isinstance(result, Matrix):
            result = to_sympy(result)
        elif format == 'numpy' and not isinstance(result, numpy_ndarray):
            result = to_numpy(result)
        elif format == 'scipy.sparse' and\
            not isinstance(result, scipy_sparse_matrix):
            result = to_scipy_sparse(result)
        return result
Exemplo n.º 4
0
    def _represent(self, **options):
        """Represent this object in a given basis.

        This method dispatches to the actual methods that perform the
        representation. Subclases of QExpr should define various methods to
        determine how the object will be represented in various bases. The
        format of these methods is::

            def _represent_BasisName(self, basis, **options):

        Thus to define how a quantum object is represented in the basis of
        the operator Position, you would define::

            def _represent_Position(self, basis, **options):

        Usually, basis object will be instances of Operator subclasses, but
        there is a chance we will relax this in the future to accomodate other
        types of basis sets that are not associated with an operator.

        If the ``format`` option is given it can be ("sympy", "numpy",
        "scipy.sparse"). This will ensure that any matrices that result from
        representing the object are returned in the appropriate matrix format.

        Parameters
        ==========
        basis : Operator
            The Operator whose basis functions will be used as the basis for
            representation.
        options : dict
            A dictionary of key/value pairs that give options and hints for
            the representation, such as the number of basis functions to
            be used.
        """
        basis = options.pop('basis',None)
        if basis is None:
            result = self._represent_default_basis(**options)
        else:
            result = dispatch_method(self, '_represent', basis, **options)

        # If we get a matrix representation, convert it to the right format.
        format = options.get('format', 'sympy')
        if format == 'sympy' and not isinstance(result, Matrix):
            result = to_sympy(result)
        elif format == 'numpy' and not isinstance(result, numpy_ndarray):
            result = to_numpy(result)
        elif format == 'scipy.sparse' and\
            not isinstance(result, scipy_sparse_matrix):
            result = to_scipy_sparse(result)
        return result
Exemplo n.º 5
0
 def _sympy_matrix(self, name, m):
     self._store_matrix(name, 'sympy', to_sympy(m))
Exemplo n.º 6
0
def test_sympy_to_sympy():
    assert to_sympy(m) == m
Exemplo n.º 7
0
def test_sympy_to_sympy():
    assert to_sympy(m) == m
Exemplo n.º 8
0
 def _sympy_matrix(self, name, m):
     self._store_matrix(name, 'sympy', to_sympy(m))
Exemplo n.º 9
0
def test_format_sympy():
    for test in _tests:
        lhs = represent(test[0], basis=A, format='sympy')
        rhs = to_sympy(test[1])
        assert lhs == rhs
Exemplo n.º 10
0
def test_format_sympy():
    for test in _tests:
        lhs = represent(test[0], basis=A, format='sympy')
        rhs = to_sympy(test[1])
        assert lhs == rhs