Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
############################################################################# # Copyright (C) 2004, 2007 William Stein <wstein@gmail.com> # Distributed under the terms of the GNU General Public License (GPL) # The full text of the GPL is available at: # http://www.gnu.org/licenses/ #############################################################################
from cysignals.memory cimport sig_malloc, sig_free
from sage.modules.vector_modn_sparse cimport c_vector_modint
cdef int allocate_c_vector_modint(c_vector_modint* v, Py_ssize_t num_nonzero) except -1: """ Allocate memory for a c_vector_modint -- most user code won't call this. """ raise MemoryError("Error allocating memory") sig_free(v.entries) raise MemoryError("Error allocating memory")
cdef int init_c_vector_modint(c_vector_modint* v, int p, Py_ssize_t degree, Py_ssize_t num_nonzero) except -1: """ Initialize a c_vector_modint. """ raise MemoryError("Error allocating memory for sparse vector.") clear_c_vector_modint(v) raise OverflowError("The prime must be <= 46340.")
cdef void clear_c_vector_modint(c_vector_modint* v):
cdef Py_ssize_t binary_search0_modn(Py_ssize_t* v, Py_ssize_t n, int x): """ Find the position of the int x in the array v, which has length n. Returns -1 if x is not in the array v. """
cdef Py_ssize_t i, j, k else: # only possibility is that v[k] == x
cdef Py_ssize_t binary_search_modn(Py_ssize_t* v, Py_ssize_t n, int x, Py_ssize_t* ins): """ Find the position of the integer x in the array v, which has length n. Returns -1 if x is not in the array v, and in this case ins is set equal to the position where x should be inserted in order to obtain an ordered array. """
cdef Py_ssize_t i, j, k else: else: # only possibility is that v[k] == x # end while
cdef int get_entry(c_vector_modint* v, Py_ssize_t n) except -1: """ Returns the n-th entry of the sparse vector v. This would be v[n] in Python syntax. """ raise IndexError("Index must be between 0 and the degree minus 1.") cdef Py_ssize_t m
cdef object to_list(c_vector_modint* v): """ Returns a Python list of 2-tuples (i,x), where x=v[i] runs through the nonzero elements of x, in order. """ cdef object X cdef Py_ssize_t i X = [] for i from 0 <= i < v.num_nonzero: X.append( (v.positions[i], v.entries[i]) ) return X
cdef int set_entry(c_vector_modint* v, Py_ssize_t n, int x) except -1: """ Set the n-th component of the sparse vector v equal to x. This would be v[n] = x in Python syntax. """ raise IndexError("Index (=%s) must be between 0 and %s."%(n, v.degree-1)) cdef Py_ssize_t i, m, ins cdef Py_ssize_t m2, ins2 cdef Py_ssize_t *pos cdef int *e
# The position n was found in the array of positions. # Now there are two cases: # 1. x =/= 0, which is easy, and # 2. x = 0, in which case we have to recopy # positions and entries, without the m-th # element, and change num_nonzero. else: # case 2 else: # Allocate new memory and copy over elements from the # old array. This is similar to case 2 above, # except we are inserting a new entry rather than # deleting an old one. The new entry should be inserted # at position ins, which was computed using binary search. # # There is one exception -- if the new entry is 0, we # do nothing and return.
cdef int add_c_vector_modint_init(c_vector_modint* sum, c_vector_modint* v, c_vector_modint* w, int multiple) except -1: """ Set sum = v + multiple*w. """ raise ArithmeticError("The vectors must be modulo the same prime.") raise ArithmeticError("The vectors must have the same degree.")
cdef int s cdef Py_ssize_t nz, i, j, k cdef c_vector_modint* z
multiple = multiple + v.p
# ALGORITHM: # 1. Allocate enough memory to hold the union of the two # lists of positions. We allocate the sum of the number # of positions of both (up to the degree), to avoid # having to make two passes. This might be slightly wasteful of # memory, but is faster. # 2. Move along the entries of v and w, copying them into the # new position / entry array. When position are the same, # add modulo p. # 3. Set num_nonzero and return success code.
# 1. Allocate memory: # 2. Merge entries else: # equal, so add and copy #end if # end while
cdef int scale_c_vector_modint(c_vector_modint* v, int scalar) except -1: clear_c_vector_modint(v) init_c_vector_modint(v, v.p, v.degree, 0) return 0 scalar = scalar + v.p cdef Py_ssize_t i
|