Changeset 7830:3d3fa7c5bb39


Ignore:
Timestamp:
12/21/07 14:28:10 (5 years ago)
Author:
Robert Miller <rlmillster@…>
Branch:
default
Parents:
7825:2c4862993e0e (diff), 7829:39f62d653a62 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

local merge

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sage/modules/free_module_element.pyx

    r7729 r7830  
    266266 
    267267    def _vector_(self, R): 
     268        r"""Return self as a vector. 
     269 
     270        EXAMPLES: 
     271            sage: v = vector(ZZ, [2, 12, 22]) 
     272            sage: vector(v) 
     273            (2, 12, 22) 
     274            sage: vector(GF(7), v) 
     275            (2, 5, 1) 
     276            sage: vector(v, ZZ['x', 'y']) 
     277            (2, 12, 22) 
     278        """ 
    268279        return self.change_ring(R) 
     280 
     281    def _matrix_(self, R=None): 
     282        r"""Return self as a row matrix. 
     283 
     284        EXAMPLES: 
     285            sage: v = vector(ZZ, [2, 12, 22]) 
     286            sage: vector(v) 
     287            (2, 12, 22) 
     288            sage: vector(GF(7), v) 
     289            (2, 5, 1) 
     290            sage: vector(v, ZZ['x', 'y']) 
     291            (2, 12, 22) 
     292        """ 
     293        if R is None: 
     294            R = self.base_ring() 
     295        from sage.matrix.constructor import matrix 
     296        return matrix(R, [list(self)]) 
     297 
     298    def transpose(self): 
     299        r"""Return self as a column matrix. 
     300 
     301        EXAMPLES: 
     302            sage: v = vector(ZZ, [2, 12, 22]) 
     303            sage: transpose(vector(v)) 
     304            [ 2] 
     305            [12] 
     306            [22] 
     307 
     308            sage: transpose(vector(GF(7), v)) 
     309            [2] 
     310            [5] 
     311            [1] 
     312 
     313            sage: transpose(vector(v, ZZ['x', 'y'])) 
     314            [ 2] 
     315            [12] 
     316            [22] 
     317        """ 
     318        return self._matrix_().transpose() 
    269319 
    270320    def _hash(self): 
  • sage/modules/free_module_element.pyx

    r7829 r7830  
    321321        return hash(tuple(list(self))) 
    322322 
     323    def copy(self): 
     324        """ 
     325        Make a copy of this vector. 
     326         
     327        EXAMPLES: 
     328            sage: v = vector([1..5]); v 
     329            (1, 2, 3, 4, 5) 
     330            sage: w = v.copy() 
     331            sage: v == w 
     332            True 
     333            sage: v is w 
     334            False 
     335             
     336            sage: v = vector([1..5], sparse=True); v 
     337            (1, 2, 3, 4, 5) 
     338            sage: v.copy() 
     339            (1, 2, 3, 4, 5) 
     340        """ 
     341        return self.__copy__() 
     342 
     343    def __copy__(self): 
     344        if self.is_sparse(): 
     345            return self.parent()(self.dict()) 
     346        else: 
     347            return self.parent()(self.list()) 
     348 
     349    def set_immutable(self): 
     350        """ 
     351        Make this vector immutable.  This operation can't be undone. 
     352         
     353        EXAMPLES: 
     354            sage: v = vector([1..5]); v 
     355            (1, 2, 3, 4, 5) 
     356            sage: v[1] = 10 
     357            sage: v.set_immutable() 
     358            sage: v[1] = 10 
     359            Traceback (most recent call last): 
     360            ... 
     361            ValueError: vector is immutable; please change a copy instead (use self.copy()) 
     362        """ 
     363        self._is_mutable = 0 
     364 
     365    def is_mutable(self): 
     366        """ 
     367        Return True if this vector is mutable, i.e., the entries can be changed. 
     368 
     369        EXAMPLES: 
     370            sage: v = vector(QQ['x,y'], [1..5]); v.is_mutable() 
     371            True 
     372            sage: v.set_immutable() 
     373            sage: v.is_mutable() 
     374            False 
     375        """ 
     376        return self._is_mutable 
     377 
     378    def is_immutable(self): 
     379        """ 
     380        Return True if this vector is immutable, i.e., the entries cannot be changed. 
     381 
     382        EXAMPLES: 
     383            sage: v = vector(QQ['x,y'], [1..5]); v.is_immutable() 
     384            False 
     385            sage: v.set_immutable() 
     386            sage: v.is_immutable() 
     387            True 
     388        """ 
     389        return not self._is_mutable 
     390 
    323391    def change_ring(self, R): 
     392        """ 
     393        Change the base ring of this vector, by coercing each element 
     394        of this vector into R. 
     395 
     396        EXAMPLES: 
     397            sage: v = vector(QQ['x,y'], [1..5]); v.change_ring(GF(3)) 
     398            (1, 2, 0, 1, 2) 
     399        """ 
    324400        P = self.parent() 
    325401        if P.base_ring() is R: 
     
    357433 
    358434    def __abs__(self): 
    359         return self.norm() 
     435        """ 
     436        Return the square root of the sum of the squares of the entries of this vector. 
     437 
     438        EXAMPLES: 
     439            sage: v = vector([1..5]); abs(v) 
     440            sqrt(15) 
     441            sage: v = vector(RDF, [1..5]); abs(v) 
     442            3.87298334621 
     443        """ 
     444        return sum(self.list()).sqrt() 
    360445     
    361446    cdef int _cmp_c_impl(left, Element right) except -2: 
     
    460545         
    461546    def _repr_(self): 
     547        """ 
     548        String representation of a vector. 
     549         
     550        EXAMPLES: 
     551            sage: vector(QQ, [])._repr_() 
     552            '()' 
     553            sage: vector(QQ, range(5))._repr_() 
     554            '(0, 1, 2, 3, 4)' 
     555 
     556        Symbolic are not displayed using ASCII art.  
     557            sage: x = var('x') 
     558            sage: v = vector([x/(2*x)+sqrt(2)+var('theta')^3,x/(2*x)]); v 
     559            (theta^3 + sqrt(2) + 1/2, 1/2) 
     560            sage: v._repr_() 
     561            '(theta^3 + sqrt(2) + 1/2, 1/2)' 
     562        """ 
    462563        d = self.degree() 
    463564        if d == 0: return "()" 
    464565        # compute column widths 
    465         S = [str(x) for x in self.list(copy=False)] 
     566        S = [repr(x) for x in self.list(copy=False)] 
    466567        #width = max([len(x) for x in S]) 
    467568        s = "(" 
     
    507608            (1.0, 2.0, 3.0 + 4.0*I) 
    508609            sage: v[1:] = (1,3); v 
    509             (1.0, 1.0, 3.0)         
    510         """ 
     610            (1.0, 1.0, 3.0) 
     611 
     612            sage: v.set_immutable() 
     613            sage: v[1:2] = [3,5] 
     614            Traceback (most recent call last): 
     615            ... 
     616            ValueError: vector is immutable; please change a copy instead (use self.copy()) 
     617        """ 
     618        if not self._is_mutable: 
     619            raise ValueError, "vector is immutable; please change a copy instead (use self.copy())" 
    511620        cdef Py_ssize_t k, d, n 
    512621        d = self.degree() 
     
    9311040        cdef FreeModuleElement_generic_dense x 
    9321041        x = PY_NEW(FreeModuleElement_generic_dense) 
     1042        x._is_mutable = 1 
    9331043        x._parent = self._parent 
    9341044        x._entries = v 
     
    9591069             
    9601070            if len(entries) != self.degree(): 
    961                 raise ArithmeticError, "entries must be a list of length %s"%\ 
     1071                raise TypeError, "entries must be a list of length %s"%\ 
    9621072                            self.degree() 
    9631073            if coerce: 
     
    10791189        Set entry i of self to value. 
    10801190        """ 
     1191        if not self._is_mutable: 
     1192            raise ValueError, "vector is immutable; please change a copy instead (use self.copy())" 
    10811193        i = int(i) 
    10821194        #if not isinstance(i, int): 
     
    11021214            (5, 3) 
    11031215        """ 
     1216        if not self._is_mutable: 
     1217            raise ValueError, "vector is immutable; please change a copy instead (use self.copy())" 
    11041218        cdef Py_ssize_t k, n, d 
    11051219        d = self.degree() 
     
    11751289        cdef FreeModuleElement_generic_sparse x 
    11761290        x = PY_NEW(FreeModuleElement_generic_sparse) 
     1291        x._is_mutable = 1 
    11771292        x._parent = self._parent 
    11781293        x._entries = v 
     
    13361451        Like __setitem__ but with no type or bounds checking. 
    13371452        """ 
     1453        if not self._is_mutable: 
     1454            raise ValueError, "vector is immutable; please change a copy instead (use self.copy())" 
    13381455        i = int(i) 
    13391456        if x == 0: 
     
    13461463        """ 
    13471464        """ 
     1465        if not self._is_mutable: 
     1466            raise ValueError, "vector is immutable; please change a copy instead (use self.copy())" 
    13481467        i = int(i) 
    13491468        #if not isinstance(i, int): 
Note: See TracChangeset for help on using the changeset viewer.