Changeset 7830:3d3fa7c5bb39
- Timestamp:
- 12/21/07 14:28:10 (5 years ago)
- 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. - Files:
-
- 2 edited
-
sage/modules/free_module_element.pyx (modified) (1 diff)
-
sage/modules/free_module_element.pyx (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/modules/free_module_element.pyx
r7729 r7830 266 266 267 267 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 """ 268 279 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() 269 319 270 320 def _hash(self): -
sage/modules/free_module_element.pyx
r7829 r7830 321 321 return hash(tuple(list(self))) 322 322 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 323 391 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 """ 324 400 P = self.parent() 325 401 if P.base_ring() is R: … … 357 433 358 434 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() 360 445 361 446 cdef int _cmp_c_impl(left, Element right) except -2: … … 460 545 461 546 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 """ 462 563 d = self.degree() 463 564 if d == 0: return "()" 464 565 # 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)] 466 567 #width = max([len(x) for x in S]) 467 568 s = "(" … … 507 608 (1.0, 2.0, 3.0 + 4.0*I) 508 609 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())" 511 620 cdef Py_ssize_t k, d, n 512 621 d = self.degree() … … 931 1040 cdef FreeModuleElement_generic_dense x 932 1041 x = PY_NEW(FreeModuleElement_generic_dense) 1042 x._is_mutable = 1 933 1043 x._parent = self._parent 934 1044 x._entries = v … … 959 1069 960 1070 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"%\ 962 1072 self.degree() 963 1073 if coerce: … … 1079 1189 Set entry i of self to value. 1080 1190 """ 1191 if not self._is_mutable: 1192 raise ValueError, "vector is immutable; please change a copy instead (use self.copy())" 1081 1193 i = int(i) 1082 1194 #if not isinstance(i, int): … … 1102 1214 (5, 3) 1103 1215 """ 1216 if not self._is_mutable: 1217 raise ValueError, "vector is immutable; please change a copy instead (use self.copy())" 1104 1218 cdef Py_ssize_t k, n, d 1105 1219 d = self.degree() … … 1175 1289 cdef FreeModuleElement_generic_sparse x 1176 1290 x = PY_NEW(FreeModuleElement_generic_sparse) 1291 x._is_mutable = 1 1177 1292 x._parent = self._parent 1178 1293 x._entries = v … … 1336 1451 Like __setitem__ but with no type or bounds checking. 1337 1452 """ 1453 if not self._is_mutable: 1454 raise ValueError, "vector is immutable; please change a copy instead (use self.copy())" 1338 1455 i = int(i) 1339 1456 if x == 0: … … 1346 1463 """ 1347 1464 """ 1465 if not self._is_mutable: 1466 raise ValueError, "vector is immutable; please change a copy instead (use self.copy())" 1348 1467 i = int(i) 1349 1468 #if not isinstance(i, int):
Note: See TracChangeset
for help on using the changeset viewer.
