Dense vectors using a NumPy backend. - Modules

Skip to content
Back to top
View this page
Edit this page

# Dense vectors using a NumPy backend.

AUTHORS:

- Jason Grout, Oct 2008: switch to numpy backend, factored out `Vector_double_dense` class

- Josh Kantor

- William Stein

class sage.modules.vector_numpy_dense.Vector_numpy_dense

  Bases: `FreeModuleElement`

  Base class for vectors implemented using numpy arrays.

  This class cannot be instantiated on its own. The numpy vector creation depends on several variables that are set in the subclasses.

  EXAMPLES:

  Sage

      sage: v = vector(RDF, [1,2,3,4]); v
      (1.0, 2.0, 3.0, 4.0)
      sage: v*v
      30.0

  Python

      >>> from sage.all import *
      >>> v = vector(RDF, [Integer(1),Integer(2),Integer(3),Integer(4)]); v
      (1.0, 2.0, 3.0, 4.0)
      >>> v*v
      30.0

  numpy(dtype=None)

    Return numpy array corresponding to this vector.

    INPUT:

    - `dtype` – if specified, the numpy dtype of the returned array

    EXAMPLES:

    Sage

        sage: v = vector(CDF,4,range(4))
        sage: v.numpy()
        array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j])
        sage: v = vector(CDF,0)
        sage: v.numpy()
        array([], dtype=complex128)
        sage: v = vector(RDF,4,range(4))
        sage: v.numpy()
        array([0., 1., 2., 3.])
        sage: v = vector(RDF,0)
        sage: v.numpy()
        array([], dtype=float64)

    Python

        >>> from sage.all import *
        >>> v = vector(CDF,Integer(4),range(Integer(4)))
        >>> v.numpy()
        array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j])
        >>> v = vector(CDF,Integer(0))
        >>> v.numpy()
        array([], dtype=complex128)
        >>> v = vector(RDF,Integer(4),range(Integer(4)))
        >>> v.numpy()
        array([0., 1., 2., 3.])
        >>> v = vector(RDF,Integer(0))
        >>> v.numpy()
        array([], dtype=float64)

    A numpy dtype may be requested manually:

    Sage

        sage: import numpy
        sage: v = vector(CDF, 3, range(3))
        sage: v.numpy()
        array([0.+0.j, 1.+0.j, 2.+0.j])
        sage: v.numpy(dtype=numpy.float64)
        array([0., 1., 2.])
        sage: v.numpy(dtype=numpy.float32)
        array([0., 1., 2.], dtype=float32)

    [Python]

        >>> from sage.all import *
        >>> import numpy
        >>> v = vector(CDF, Integer(3), range(Integer(3)))
        >>> v.numpy()
        array([0.+0.j, 1.+0.j, 2.+0.j])
        >>> v.numpy(dtype=numpy.float64)
        array([0., 1., 2.])
        >>> v.numpy(dtype=numpy.float32)
        array([0., 1., 2.], dtype=float32)