Ticket #10536: trac_10536-vector-conjugation-v2.patch

File trac_10536-vector-conjugation-v2.patch, 3.8 KB (added by rbeezer, 9 years ago)
  • sage/modules/free_module_element.pyx

    # HG changeset patch
    # User Rob Beezer <beezer@ups.edu>
    # Date 1293791916 28800
    # Node ID 9e0dd913deead7c431f0966b98bd1857da8e8c77
    # Parent  b849f0558252b6778b0e45f6163a7cd09886d027
    10536: add vector conjugation
    
    diff -r b849f0558252 -r 9e0dd913deea sage/modules/free_module_element.pyx
    a b  
    18681868            if self[i] != 0:
    18691869                return (~self[i]) * self
    18701870        return self
    1871        
     1871
     1872    def conjugate(self):
     1873        r"""
     1874        Returns a vector where every entry has been replaced by its complex conjugate.
     1875
     1876        OUTPUT:
     1877
     1878        A vector of the same length, over the same ring,
     1879        but with each entry replaced by the complex conjugate, as
     1880        implemented by the ``conjugate()`` method for elements of
     1881        the base ring, which is presently always complex conjugation.
     1882
     1883        EXAMPLES::
     1884
     1885            sage: v = vector(CDF, [2.3 - 5.4*I, -1.7 + 3.6*I])
     1886            sage: w = v.conjugate(); w
     1887            (2.3 + 5.4*I, -1.7 - 3.6*I)
     1888            sage: w.parent()
     1889            Vector space of dimension 2 over Complex Double Field
     1890
     1891        Even if conjugation seems nonsensical over a certain ring, this
     1892        method for vectors cooperates silently. ::
     1893
     1894            sage: u = vector(ZZ, range(6))
     1895            sage: u.conjugate()
     1896            (0, 1, 2, 3, 4, 5)
     1897
     1898        Sage implements a few specialized subfields of the complex numbers,
     1899        such as the cyclotomic fields.  This example uses such a field
     1900        containing a primitive 7-th root of unity named ``a``. ::
     1901
     1902            sage: F.<a> = CyclotomicField(7)
     1903            sage: v = vector(F, [a^i for i in range(7)])
     1904            sage: v
     1905            (1, a, a^2, a^3, a^4, a^5, -a^5 - a^4 - a^3 - a^2 - a - 1)
     1906            sage: v.conjugate()
     1907            (1, -a^5 - a^4 - a^3 - a^2 - a - 1, a^5, a^4, a^3, a^2, a)
     1908
     1909        Sparse vectors are returned as such. ::
     1910
     1911            sage: v = vector(CC, {1: 5 - 6*I, 3: -7*I}); v
     1912            sage: v.is_sparse()
     1913            sage: vc = v.conjugate(); vc
     1914            sage: vc.conjugate()
     1915
     1916        TESTS::
     1917
     1918            sage: n = 15
     1919            sage: x = vector(CDF, [sin(i*pi/n)+cos(i*pi/n)*I for i in range(n)])
     1920            sage: x + x.conjugate() in RDF^n
     1921            True
     1922            sage: I*(x - x.conjugate()) in RDF^n
     1923            True
     1924
     1925        The parent of the conjugate is the same as that of the orginal vector.
     1926        We test this by building a specialized vector space with a non-standard
     1927        inner product, and constructing a test vector in this space. ::
     1928
     1929            sage: V = VectorSpace(CDF, 2, inner_product_matrix = [[2,1],[1,5]])
     1930            sage: v = vector(CDF, [2-3*I, 4+5*I])
     1931            sage: w = V(v)
     1932            sage: w.parent()
     1933            Ambient quadratic space of dimension 2 over Complex Double Field
     1934            Inner product matrix:
     1935            [2.0 1.0]
     1936            [1.0 5.0]
     1937            sage: w.conjugate().parent()
     1938            Ambient quadratic space of dimension 2 over Complex Double Field
     1939            Inner product matrix:
     1940            [2.0 1.0]
     1941            [1.0 5.0]
     1942        """
     1943        V = self.parent()
     1944        R = self.base_ring()
     1945        degree = self.degree()
     1946        if self.is_sparse():
     1947            # this could be a dictionary comprehension in Python 3
     1948            entries = {}
     1949            for index, entry in self.iteritems():
     1950                entries[index] = entry.conjugate()
     1951        else:
     1952            entries = [entry.conjugate() for entry in self]
     1953        return V(vector(R, degree, entries))
     1954
    18721955    def inner_product(self, right):
    18731956        """
    18741957        Returns the inner product of self and other, with respect to the