This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
/
args.pxd
122 lines (100 loc) · 4.23 KB
/
args.pxd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from cpython.object cimport PyObject
from sage.structure.element cimport Element, Matrix
from sage.structure.parent cimport Parent
cdef enum entries_type:
# flags appearing in the types below
MA_FLAG_ASSUME_SQUARE = 0x01_00 # Assume a square matrix if only 1 dimension given
MA_FLAG_DIM_REQUIRED = 0x02_00 # Dimensions must be given
MA_FLAG_BASE_REQUIRED = 0x04_00 # Base must be given or is trivial to determine
MA_FLAG_FINAL = 0x10_00 # Can occur after finalize()
MA_FLAG_SPARSE = 0x20_00 # Sparse by default
# types of input entries
MA_ENTRIES_UNKNOWN = 0 # anything
MA_ENTRIES_ZERO = 0x17_01 # zero matrix
MA_ENTRIES_SCALAR = 0x17_02 # single scalar value
MA_ENTRIES_SEQ_SEQ = 0x10_03 # list of lists
MA_ENTRIES_SEQ_FLAT = 0x10_04 # flat list
MA_ENTRIES_SEQ_SPARSE = 0x37_05 # list of SparseEntry instances
MA_ENTRIES_CALLABLE = 0x13_06 # function for the entries
MA_ENTRIES_MATRIX = 0x14_07 # Sage matrix
MA_ENTRIES_MAPPING = 0x01_08 # dict
MA_ENTRIES_METHOD = 0x00_09 # function returning the matrix
MA_ENTRIES_NDARRAY = 0x00_0A # numpy array
# value for exception raised
MA_EXCEPT = -1
cdef class SparseEntry:
cdef public long i, j
cdef public object entry
cdef inline SparseEntry make_SparseEntry(long i, long j, entry):
e = <SparseEntry>SparseEntry.__new__(SparseEntry)
e.i = i
e.j = j
e.entry = entry
return e
cdef class MatrixArgs:
# For integers, -1 means unknown
# For Python objects, None means unknown
cdef public Parent space # parent of matrix
cdef public Parent base # parent of entries
cdef public long nrows, ncols
cdef public object entries
cdef entries_type typ
cdef public bint sparse
cdef public dict kwds # **kwds for MatrixSpace()
cdef bint is_finalized
cpdef Matrix matrix(self, bint convert=?)
cpdef list list(self, bint convert=?)
cpdef dict dict(self, bint convert=?)
cdef inline bint ref_safe(self):
"""
Can we safely return self.entries without making a copy?
A refcount of 1 means that self.entries is the only reference.
"""
return (<PyObject*>self.entries).ob_refcnt == 1
cdef inline bint need_to_convert(self, x):
"""Is ``x`` not an element of ``self.base``?"""
if not isinstance(x, Element):
return True
return (<Element>x)._parent is not self.base
cdef int set_base_from_entries(self, entries) except -1
cdef inline int setdefault_base(self, B) except -1:
"""
Set the base ring if not previously set.
"""
if self.base is not None:
return 0
self.base = <Parent?>B
cdef inline int set_ncols(self, long n) except -1:
"""
Set the number of columns with consistency checking: if the
value was previously set, it must remain the same.
"""
if n < 0:
raise ArithmeticError("number of columns must be non-negative")
cdef long p = self.ncols
if p != -1 and p != n:
raise ValueError(f"inconsistent number of columns: should be {p} but got {n}")
self.ncols = n
cdef inline int set_nrows(self, long n) except -1:
"""
Set the number of rows with consistency checking: if the
value was previously set, it must remain the same.
"""
if n < 0:
raise ArithmeticError("number of rows must be non-negative")
cdef long p = self.nrows
if p != -1 and p != n:
raise ValueError(f"inconsistent number of rows: should be {p} but got {n}")
self.nrows = n
cpdef int set_space(self, space) except -1
cdef int finalize(self) except -1
cdef int process_mapping(self) except -1
cdef int process_method(self) except -1
cdef int process_ndarray(self) except -1
cdef int finalize_seq_seq(self) except -1
cdef int finalize_seq_scalar(self) except -1
cdef int finalize_callable(self) except -1
cdef entries_type get_type(self) except MA_EXCEPT
cdef entries_type sequence_type(self) except MA_EXCEPT
cdef int set_seq_flat(self, entries) except -1
cpdef MatrixArgs MatrixArgs_init(space, entries)