Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/modules/vector_complex_double_dense.pyx
4036 views
1
r"""
2
Dense complex double vectors using a NumPy backend.
3
4
EXAMPLES::
5
6
sage: v = vector(CDF,[(1,-1), (2,pi), (3,5)])
7
sage: v
8
(1.0 - 1.0*I, 2.0 + 3.14159265359*I, 3.0 + 5.0*I)
9
sage: type(v)
10
<type 'sage.modules.vector_complex_double_dense.Vector_complex_double_dense'>
11
sage: parent(v)
12
Vector space of dimension 3 over Complex Double Field
13
sage: v[0] = 5
14
sage: v
15
(5.0, 2.0 + 3.14159265359*I, 3.0 + 5.0*I)
16
sage: loads(dumps(v)) == v
17
True
18
19
TESTS::
20
21
sage: v = vector(CDF, [2, 2])
22
sage: v - v
23
(0.0, 0.0)
24
sage: (v - v).norm()
25
0.0
26
27
AUTHORS:
28
29
-- Jason Grout, Oct 2008: switch to NumPy backend, factored out
30
Vector_double_dense class
31
"""
32
33
##############################################################################
34
# Copyright (C) 2008 Jason Grout <[email protected]>
35
# Distributed under the terms of the GNU General Public License (GPL)
36
# The full text of the GPL is available at:
37
# http://www.gnu.org/licenses/
38
##############################################################################
39
from sage.rings.complex_double import CDF
40
41
cimport numpy as cnumpy
42
43
numpy=None
44
45
cdef class Vector_complex_double_dense(vector_double_dense.Vector_double_dense):
46
"""
47
Vectors over the Complex Double Field. These are supposed to be
48
fast vector operations using C doubles. Most operations are
49
implemented using numpy which will call the underlying BLAS, if
50
needed, on the system.
51
52
EXAMPLES:
53
sage: v = vector(CDF,[(1,-1), (2,pi), (3,5)])
54
sage: v
55
(1.0 - 1.0*I, 2.0 + 3.14159265359*I, 3.0 + 5.0*I)
56
sage: v*v
57
-21.8696044011 + 40.5663706144*I
58
"""
59
def __cinit__(self, parent, entries, coerce=True, copy=True):
60
global numpy
61
if numpy is None:
62
import numpy
63
self._numpy_dtype = numpy.dtype('complex128')
64
self._python_dtype = complex
65
self._numpy_dtypeint = cnumpy.NPY_CDOUBLE
66
# TODO: Make ComplexDoubleElement instead of CDF for speed
67
self._sage_dtype = CDF
68
self.__create_vector__()
69
return
70
71
def __reduce__(self):
72
"""
73
Pickling
74
75
EXAMPLE:
76
sage: a = vector(CDF, range(9))
77
sage: loads(dumps(a)) == a
78
True
79
"""
80
return (unpickle_v1, (self._parent, self.list(), self._degree, self._is_mutable))
81
82
83
# For backwards compatibility, we must keep the function unpickle_v0
84
def unpickle_v0(parent, entries, degree):
85
"""
86
Create a complex double vector containing the entries.
87
88
EXAMPLE:
89
sage: v = vector(CDF, [1,2,3])
90
sage: w = sage.modules.vector_complex_double_dense.unpickle_v0(v.parent(), list(v), v.degree())
91
sage: v == w
92
True
93
"""
94
return unpickle_v1(parent, entries, degree)
95
96
def unpickle_v1(parent, entries, degree, is_mutable=None):
97
"""
98
Create a complex double vector with the given parent, entries,
99
degree, and mutability.
100
101
EXAMPLE:
102
sage: v = vector(CDF, [1,2,3])
103
sage: w = sage.modules.vector_complex_double_dense.unpickle_v1(v.parent(), list(v), v.degree(), v.is_mutable())
104
sage: v == w
105
True
106
"""
107
cdef Vector_complex_double_dense v = Vector_complex_double_dense(parent, entries)
108
if is_mutable is not None:
109
v._is_mutable = is_mutable
110
return v
111
112
113
114