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
"Operands"
#***************************************************************************** # Copyright (C) 2011 Burcin Erocal <burcin@erocal.org> # # 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/ #*****************************************************************************
from sage.libs.pynac.pynac cimport GEx from sage.symbolic.expression cimport new_Expression_from_GEx
cdef inline int normalize_index(object arg, int nops, object err_msg) except -1: """ Given an index ``arg`` and the number of operands ``nops`` return an integer between 0 and ``nops`` which will be used as the index to fetch the data from the underlying vector.
TESTS::
sage: from sage.symbolic.getitem import normalize_index_for_doctests as normalize_index sage: normalize_index(-1, 4) 3 sage: normalize_index(1, 4) 1 sage: normalize_index(1.5, 4) Traceback (most recent call last): ... TypeError: some error message sage: normalize_index(-5, 4) Traceback (most recent call last): ... IndexError: operand index out of range, got -5, expect between -4 and 3 sage: normalize_index(5, 4) Traceback (most recent call last): ... IndexError: operand index out of range, got 5, expect between -4 and 3 """ cdef int i
def normalize_index_for_doctests(arg, nops): """ Wrapper function to test ``normalize_index``.
TESTS::
sage: from sage.symbolic.getitem import normalize_index_for_doctests sage: normalize_index_for_doctests(-1, 4) 3 """
cdef class OperandsWrapper(SageObject): """ Operands wrapper for symbolic expressions.
EXAMPLES::
sage: x,y,z = var('x,y,z') sage: e = x + x*y + z^y + 3*y*z; e x*y + 3*y*z + x + z^y sage: e.op[1] 3*y*z sage: e.op[1,1] z sage: e.op[-1] z^y sage: e.op[1:] [3*y*z, x, z^y] sage: e.op[:2] [x*y, 3*y*z] sage: e.op[-2:] [x, z^y] sage: e.op[:-2] [x*y, 3*y*z] sage: e.op[-5] Traceback (most recent call last): ... IndexError: operand index out of range, got -5, expect between -4 and 3 sage: e.op[5] Traceback (most recent call last): ... IndexError: operand index out of range, got 5, expect between -4 and 3 sage: e.op[1,1,0] Traceback (most recent call last): ... TypeError: expressions containing only a numeric coefficient, constant or symbol have no operands sage: e.op[:1.5] Traceback (most recent call last): ... TypeError: slice indices must be integers or None or have an __index__ method sage: e.op[:2:1.5] Traceback (most recent call last): ... ValueError: step value must be an integer """ def __getitem__(self, arg): """ TESTS::
sage: t = 1+x+x^2 sage: t.op[1:] [x, 1] """ cdef int ind, nops cdef int bind, eind, step cdef GEx cur_ex
else: else: else:
# handle nested index raise TypeError(ind_err_msg)
def _repr_(self): """ TESTS::
sage: (x^2).op Operands of x^2 """
def _latex_(self): r""" TESTS::
sage: latex((x^2).op) \text{Operands wrapper for expression }x^{2} """
def __reduce__(self): """ TESTS::
sage: (x^2).op.__reduce__() (<built-in function restore_op_wrapper>, (x^2,)) sage: loads(dumps((x^2).op)) Operands of x^2 """
def restore_op_wrapper(expr): """ TESTS::
sage: from sage.symbolic.getitem import restore_op_wrapper sage: restore_op_wrapper(x^2) Operands of x^2 """ |