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
""" C Int Lists
This is a class for fast basic operations with lists of C ints. It is similar to the double precision TimeSeries class. It has all the standard C int semantics, of course, including overflow. It is also similar to the Python list class, except all elements are C ints, which makes some operations much, much faster. For example, concatenating two IntLists can be over 10 times faster than concatenating the corresponding Python lists of ints, and taking slices is also much faster.
AUTHOR:
- William Stein, 2010-03 """
#***************************************************************************** # Copyright (C) 2010 William Stein <wstein@gmail.com> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
# Global parameter that sets the maximum number of entries of an IntList to print.
from libc.string cimport memcpy from cysignals.memory cimport sig_malloc, sig_free from cysignals.signals cimport sig_on, sig_off
from sage.finance.time_series cimport TimeSeries from cpython.bytes cimport PyBytes_FromStringAndSize, PyBytes_AsString from sage.structure.richcmp cimport rich_to_bool
cdef class IntList: """ A list of C int's. """ def __cinit__(self): """ Create new empty uninitialized IntList.
EXAMPLES::
sage: stats.IntList(5) # indirect test [0, 0, 0, 0, 0]
"""
def __init__(self, values): """ Create an initialized list of C ints.
INPUT:
- values -- int, long, Integer, list of integers, or a TimeSeries
If the input is a time series or list of floats, then the integer parts of the intries are taken (not the floor).
EXAMPLES::
sage: stats.IntList(8) [0, 0, 0, 0, 0, 0, 0, 0]
sage: stats.IntList([1,5,-39392]) [1, 5, -39392]
We check for overflow when creating the IntList::
sage: stats.IntList([1, 3, 2^32]) Traceback (most recent call last): ... OverflowError: ... too large to convert to C long # 32-bit OverflowError: ... too large to convert to int # 64-bit
Printing omits entries::
sage: stats.IntList(1000) [0, 0, 0, 0, 0 ... 0, 0, 0, 0, 0]
Floats are truncated to their integer parts::
sage: stats.IntList([1.1, -2.6]) [1, -2] sage: stats.IntList(stats.TimeSeries([1.1, -2.6])) [1, -2] """ cdef TimeSeries T else:
raise MemoryError cdef Py_ssize_t i else:
def __richcmp__(IntList self, other, int op): """ Compare self and other. This has the same semantics as list comparison.
EXAMPLES::
sage: v = stats.IntList([1,2,3]); w = stats.IntList([1,2]) sage: v < w False sage: w < v True sage: v == v True sage: w == w True """ cdef IntList _other cdef Py_ssize_t c, i _other = IntList(other) else: return rich_to_bool(op, -1 if d < 0 else 1)
def __dealloc__(self): """ Deallocate memory used by the IntList, if it was allocated. """
def __repr__(self): """ Return string representation of this IntList.
EXAMPLES::
sage: a = stats.IntList([1..15]); a.__repr__() '[1, 2, 3, 4, 5 ... 11, 12, 13, 14, 15]' sage: sage.stats.intlist.max_print = 20 sage: a.__repr__() '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]' sage: sage.stats.intlist.max_print = 10 sage: a.__repr__() '[1, 2, 3, 4, 5 ... 11, 12, 13, 14, 15]' """ else:
def __getitem__(self, i): """ Return i-th entry or slice of self, following standard Python semantics. The returned slice is an intlist, and the returned entry is a Python int.
INPUT:
- i -- integer or slice
EXAMPLES::
sage: a = stats.IntList([0..9]); a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] sage: a[5] 5 sage: a[-2] 8 sage: a[5:-2] [5, 6, 7] sage: type(a[5:-2]) <... 'sage.stats.intlist.IntList'> sage: type(a[5]) <... 'int'> """ cdef Py_ssize_t start, stop, step, j cdef IntList t start = self._length - 1 stop = self._length return new_int_list(0) step = -step t = new_int_list((stop-start)/step) for j from 0 <= j < (stop-start)/step: t._values[j] = self._values[stop-1 - j*step] t = new_int_list((stop-start)/step) for j from 0 <= j < (stop-start)/step: t._values[j] = self._values[j*step+start] else: else:
def __setitem__(self, Py_ssize_t i, int x): """ Set the i-th entry of self, following standard Python semantics.
INPUT:
- i -- an integer - x -- an int
EXAMPLES::
sage: a = stats.IntList([-2,3,7,-4]) sage: a[1] = 10393; a [-2, 10393, 7, -4] sage: a[-1] = -10; a [-2, 10393, 7, -10] sage: a[100] Traceback (most recent call last): ... IndexError: IntList index out of range sage: a[-100] Traceback (most recent call last): ... IndexError: IntList index out of range """ raise IndexError("index out of range") raise IndexError("index out of range")
def __reduce__(self): """ Used in pickling int lists.
EXAMPLES::
sage: a = stats.IntList([-2,3,7,-4]) sage: loads(dumps(a)) == a True
sage: v = stats.IntList([1,-3]) sage: v.__reduce__() # py2 (<built-in function unpickle_intlist_v1>, ('...', 2)) sage: v.__reduce__() # py3 (<built-in function unpickle_intlist_v1>, (b'...', 2)) sage: loads(dumps(v)) == v True
Note that dumping and loading with compress False is much faster, though dumping with compress True can save a lot of space::
sage: v = stats.IntList([1..10^5]) sage: loads(dumps(v, compress=False),compress=False) == v True
"""
def list(self): """ Return Python list version of self with Python ints as entries.
EXAMPLES::
sage: a = stats.IntList([1..15]); a [1, 2, 3, 4, 5 ... 11, 12, 13, 14, 15] sage: a.list() [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] sage: list(a) == a.list() True sage: type(a.list()[0]) <... 'int'> """ cdef Py_ssize_t i
cpdef int sum(self): """ Return the sum of the entries of self.
EXAMPLES::
sage: stats.IntList([1..100]).sum() 5050
Note that there can be overflow, since the entries are C ints::
sage: a = stats.IntList([2^30,2^30]); a [1073741824, 1073741824] sage: a.sum() -2147483648 """ cdef Py_ssize_t i
cpdef int prod(self): """ Return the product of the entries of self.
EXAMPLES::
sage: a = stats.IntList([1..10]); a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sage: a.prod() 3628800 sage: factorial(10) 3628800
Note that there can be overflow::
sage: a = stats.IntList([2^30, 2]); a [1073741824, 2] sage: a.prod() -2147483648 """ cdef Py_ssize_t i
def __len__(self): """ Return the number of entries in this time series.
OUTPUT:
Python integer
EXAMPLES::
sage: len(stats.IntList([1..15])) 15 sage: len(stats.IntList([])) 0 sage: len(stats.IntList(10^6)) 1000000 """
def __add__(left, right): """ Concatenate the integer lists self and right.
EXAMPLES::
sage: stats.IntList([-2,3,5]) + stats.IntList([1,1,17]) [-2, 3, 5, 1, 1, 17] """ raise TypeError("right operand must be an int list") raise TypeError("left operand must be an int list")
def min(self, bint index=False): """ Return the smallest value in this integer list. If this series has length 0 we raise a ValueError.
INPUT:
- index -- bool (default: False); if True, also return index of minimal entry.
OUTPUT:
- float -- smallest value - integer -- index of smallest value; only returned if index=True
EXAMPLES::
sage: v = stats.IntList([1,-4,3,-2,-4]) sage: v.min() -4 sage: v.min(index=True) (-4, 1) """ raise ValueError("min() arg is an empty sequence") cdef Py_ssize_t i, j else:
def max(self, bint index=False): """ Return the largest value in this time series. If this series has length 0 we raise a ValueError
INPUT:
- index -- bool (default: False); if True, also return index of maximum entry.
OUTPUT:
- int -- largest value - int -- index of largest value; only returned if index=True
EXAMPLES::
sage: v = stats.IntList([1,-4,3,-2,-4,3]) sage: v.max() 3 sage: v.max(index=True) (3, 2) """ raise ValueError("max() arg is an empty sequence") else:
def time_series(self): """ Return TimeSeries version of self, which involves changing each entry to a double.
EXAMPLES::
sage: T = stats.IntList([-2,3,5]).time_series(); T [-2.0000, 3.0000, 5.0000] sage: type(T) <... 'sage.finance.time_series.TimeSeries'> """ # We just reach into the data structure underlying T, since we # want this function to be *very* fast. cdef Py_ssize_t i
def plot(self, *args, **kwds): """ Return a plot of this IntList. This just constructs the corresponding double-precision floating point TimeSeries object, passing on all arguments.
EXAMPLES::
sage: stats.IntList([3,7,19,-2]).plot() Graphics object consisting of 1 graphics primitive sage: stats.IntList([3,7,19,-2]).plot(color='red',pointsize=50,points=True) Graphics object consisting of 1 graphics primitive """
def plot_histogram(self, *args, **kwds): """ Return a histogram plot of this IntList. This just constructs the corresponding double-precision floating point TimeSeries object, and plots it, passing on all arguments.
EXAMPLES::
sage: stats.IntList([1..15]).plot_histogram() Graphics object consisting of 50 graphics primitives """
cdef IntList new_int_list(Py_ssize_t length): """ Function that is used internally to quickly create a new intlist without initializing any of the allocated memory.
INPUT:
- length -- a nonnegative integer
OUTPUT:
- an IntList. """ raise ValueError("length must be nonnegative")
""" Version 1 unpickle method.
INPUT:
- ``v`` -- a raw char buffer
EXAMPLES::
sage: v = stats.IntList([1,2,3]) sage: s = v.__reduce__()[1][0] sage: type(s) <... 'str'> sage: sage.stats.intlist.unpickle_intlist_v1(s, 3) [1, 2, 3] sage: sage.stats.intlist.unpickle_intlist_v1(s+s,6) [1, 2, 3, 1, 2, 3] sage: sage.stats.intlist.unpickle_intlist_v1('',0) [] """ |