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 | (0, 5.00... - 6.00...*I, 0, -7.00...*I) |
| 1913 | sage: v.is_sparse() |
| 1914 | True |
| 1915 | sage: vc = v.conjugate(); vc |
| 1916 | (0, 5.00... + 6.00...*I, 0, 7.00...*I) |
| 1917 | sage: vc.conjugate() |
| 1918 | (0, 5.00... - 6.00...*I, 0, -7.00...*I) |
| 1919 | |
| 1920 | TESTS:: |
| 1921 | |
| 1922 | sage: n = 15 |
| 1923 | sage: x = vector(CDF, [sin(i*pi/n)+cos(i*pi/n)*I for i in range(n)]) |
| 1924 | sage: x + x.conjugate() in RDF^n |
| 1925 | True |
| 1926 | sage: I*(x - x.conjugate()) in RDF^n |
| 1927 | True |
| 1928 | |
| 1929 | The parent of the conjugate is the same as that of the orginal vector. |
| 1930 | We test this by building a specialized vector space with a non-standard |
| 1931 | inner product, and constructing a test vector in this space. :: |
| 1932 | |
| 1933 | sage: V = VectorSpace(CDF, 2, inner_product_matrix = [[2,1],[1,5]]) |
| 1934 | sage: v = vector(CDF, [2-3*I, 4+5*I]) |
| 1935 | sage: w = V(v) |
| 1936 | sage: w.parent() |
| 1937 | Ambient quadratic space of dimension 2 over Complex Double Field |
| 1938 | Inner product matrix: |
| 1939 | [2.0 1.0] |
| 1940 | [1.0 5.0] |
| 1941 | sage: w.conjugate().parent() |
| 1942 | Ambient quadratic space of dimension 2 over Complex Double Field |
| 1943 | Inner product matrix: |
| 1944 | [2.0 1.0] |
| 1945 | [1.0 5.0] |
| 1946 | """ |
| 1947 | V = self.parent() |
| 1948 | R = self.base_ring() |
| 1949 | degree = self.degree() |
| 1950 | if self.is_sparse(): |
| 1951 | # this could be a dictionary comprehension in Python 3 |
| 1952 | entries = {} |
| 1953 | for index, entry in self.iteritems(): |
| 1954 | entries[index] = entry.conjugate() |
| 1955 | else: |
| 1956 | entries = [entry.conjugate() for entry in self] |
| 1957 | return V(vector(R, degree, entries)) |
| 1958 | |