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