Ticket #11848: trac_11848-zero-at-vectors.patch

File trac_11848-zero-at-vectors.patch, 2.2 KB (added by rbeezer, 10 years ago)
  • sage/modules/vector_double_dense.pyx

    # HG changeset patch
    # User Rob Beezer <beezer@ups.edu>
    # Date 1316993795 25200
    # Node ID 996f849bc25b2f6040c92cc5b03dcc65d70e37da
    # Parent  4ad86990d7215498658440de04560aa028dcdeeb
    11848: zero_at method for RDF/CDF vectors
    
    diff --git a/sage/modules/vector_double_dense.pyx b/sage/modules/vector_double_dense.pyx
    a b  
    571571        return self.change_ring(CDF)
    572572
    573573
     574    def zero_at(self, eps):
     575        r"""
     576        Returns a copy with small entries replaced by zeros.
     577       
     578        This is useful for modifying output from algorithms
     579        which have large relative errors when producing zero
     580        elements, e.g. to create reliable doctests.
     581
     582        INPUT:
     583
     584        - ``eps`` - cutoff value
     585
     586        OUTPUT:
     587
     588        A modified copy of the vector.  Elements smaller than
     589        or equal to ``eps`` are replaced with zeroes.  For
     590        complex matrices, the real and imaginary parts are
     591        considered individually.
     592
     593
     594        EXAMPLES::
     595       
     596            sage: v = vector(RDF, [1.0, 2.0, 10^-10, 3.0])
     597            sage: v.zero_at(1e-8)
     598            (1.0, 2.0, 0.0, 3.0)
     599            sage: v.zero_at(1e-12)
     600            (1.0, 2.0, 1e-10, 3.0)
     601           
     602        For complex numbers the real and imaginaryparts are considered
     603        separately.  ::
     604       
     605            sage: w = vector(CDF, [10^-6 + 5*I, 5 + 10^-6*I, 5 + 5*I, 10^-6 + 10^-6*I])
     606            sage: w.zero_at(1.0e-4)
     607            (5.0*I, 5.0, 5.0 + 5.0*I, 0)
     608            sage: w.zero_at(1.0e-8)
     609            (1e-06 + 5.0*I, 5.0 + 1e-06*I, 5.0 + 5.0*I, 1e-06 + 1e-06*I)
     610        """
     611        import sage.rings.complex_double
     612        global numpy
     613        cdef Vector_double_dense v       
     614        if numpy is None:
     615            import numpy
     616        eps = float(eps)
     617        out = self._vector_numpy.copy()
     618        if self._sage_dtype is sage.rings.complex_double.CDF:
     619            out.real[numpy.abs(out.real) <= eps] = 0
     620            out.imag[numpy.abs(out.imag) <= eps] = 0
     621        else:
     622            out[numpy.abs(out) <= eps] = 0
     623        v = self._new(out)
     624        return v
     625
     626
    574627    #############################
    575628    # statistics
    576629    #############################