Ticket #7723: trac_7723-fixround1.patch
File trac_7723-fixround1.patch, 13.7 KB (added by , 11 years ago) |
---|
-
sage/matrix/docs.py
# HG changeset patch # User Dag Sverre Seljebotn <dagss@student.matnat.uio.no> # Date 1261394254 -3600 # Node ID 7f7c7a6fe810047035fd2bb9184801068296ee31 # Parent 2815a797fade5d3fdc25baec8130015e3f142909 [mq]: trac_7723-fixround1 diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/docs.py
a b 184 184 * _generic_matrix_times_matrix_ -- multiply two matrices with compatible dimensions but 185 185 different base rings (for optimized code paths 186 186 that works between different base rings or 187 sparse-dense-multiplication) 187 sparse-dense-multiplication). NOTE: One must also 188 implement _matrix_times_matrix_! 188 189 * cdef _cmp_c_impl -- compare two matrices with identical parents 189 190 * cdef _lmul_ -- multiply this matrix on the right by a scalar, i.e., self * scalar 190 191 * cdef _rmul_ -- multiply this matrix on the left by a scalar, i.e., scalar * self -
sage/matrix/matrix0.pyx
diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix0.pyx
a b 3447 3447 sage.structure.element.Matrix right, 3448 3448 Parent codomain): 3449 3449 r""" 3450 Return the product of two matrices. 3450 Return the product of two matrices with different parents. 3451 3452 NOTE that even if this method is overrided, one should likely 3453 also override ``_matrix_times_matrix`` for the case where 3454 parents are identical. 3451 3455 3452 3456 The matrices may have different parents, but are guaranteed to 3453 3457 have a multiplication codomain (e.g. they have conforming … … 3461 3465 3462 3466 Specific matrix implementations might override this to provide faster 3463 3467 operations (in particular, perform sparse-dense multiplication). 3468 3464 3469 """ 3465 3470 cdef sage.structure.element.Matrix A = self, B = right 3466 3471 cdef bint left_sparse = self.is_sparse_c() … … 3482 3487 r""" 3483 3488 Return the product of two matrices. 3484 3489 3485 The matrices should both the same base ring and either both3490 The matrices should both have the same base ring and either both 3486 3491 should be dense or both sparse. 3487 3492 3488 3493 EXAMPLE of matrix times matrix over same base ring: We multiply -
sage/matrix/matrix_double_dense.pyx
diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix_double_dense.pyx
a b 437 437 [ 38.0 44.0 50.0 56.0] 438 438 [ 83.0 98.0 113.0 128.0] 439 439 [128.0 152.0 176.0 200.0] 440 """ 440 441 sage: x=matrix(RDF, 3, 0) * matrix(RDF, 0, 4); x; parent(x) 442 [0.0 0.0 0.0 0.0] 443 [0.0 0.0 0.0 0.0] 444 [0.0 0.0 0.0 0.0] 445 Full MatrixSpace of 3 by 4 dense matrices over Real Double Field 446 sage: x=matrix(RDF, 0, 3) * matrix(RDF, 3, 4); x; parent(x) 447 [] 448 Full MatrixSpace of 0 by 4 dense matrices over Real Double Field 449 sage: x=matrix(RDF, 2, 3) * matrix(RDF, 3, 0); x; parent(x) 450 [] 451 Full MatrixSpace of 2 by 0 dense matrices over Real Double Field 452 453 sage: x=matrix(RDF, 3, 0) * matrix(RDF, 0, 4, sparse=True); x; parent(x) 454 [0.0 0.0 0.0 0.0] 455 [0.0 0.0 0.0 0.0] 456 [0.0 0.0 0.0 0.0] 457 Full MatrixSpace of 3 by 4 dense matrices over Real Double Field 458 sage: x=matrix(RDF, 0, 3) * matrix(RDF, 3, 4, sparse=True); x; parent(x) 459 [] 460 Full MatrixSpace of 0 by 4 dense matrices over Real Double Field 461 sage: x=matrix(RDF, 2, 3) * matrix(RDF, 3, 0, sparse=True); x; parent(x) 462 [] 463 Full MatrixSpace of 2 by 0 dense matrices over Real Double Field 464 465 """ 441 466 if self._nrows == 0 or self._ncols == 0 or right._nrows == 0 or right._ncols == 0: 442 467 return codomain.zero_matrix() 443 468 444 469 cdef Matrix_double_dense left, M 445 470 446 471 # There's no real advantage to have a mixed complex/real code path here, 447 # change both operands to same base ring 472 # change both operands to same base ring (avoiding change_ring could 473 # reduce constant overhead though) 448 474 left = self 449 475 base = codomain._base 450 476 if left._parent._base is not base: 477 # can only result in RDF->CDF or CDF->RDF 451 478 left = left.change_ring(base) 452 479 if right._parent._base is not base: 453 480 right = right.change_ring(base) 454 481 455 if numpy is None:456 import numpy457 M = create_uninitialized_Matrix_double_dense(codomain, left.__class__)458 482 if right.is_dense_c(): 459 M._matrix_numpy = numpy.dot(left._matrix_numpy, 460 (<Matrix_double_dense?>right)._matrix_numpy) 483 return left._matrix_times_matrix_(right) 461 484 else: 485 M = create_uninitialized_Matrix_double_dense(codomain, left.__class__) 462 486 # Do a transposed multiplication -- scipy.sparse only defines right-mul. 463 487 # Avoid using Sage matrices to avoid some data copying -- transposing 464 488 # a scipy.sparse CSC matrix results in a CSR matrix of the same data, … … 469 493 M._matrix_numpy = numpy.ascontiguousarray((right_T * left_T).T) 470 494 return M 471 495 496 cdef sage.structure.element.Matrix _matrix_times_matrix_(self, 497 sage.structure.element.Matrix right): 498 """ 499 Multiply self*right as matrices in the case where parents are identical. 500 501 TESTS:: 502 503 sage: A = matrix(RDF,3,range(1,10)) 504 sage: B = matrix(RDF,3,range(1,13)) 505 sage: A*B 506 [ 38.0 44.0 50.0 56.0] 507 [ 83.0 98.0 113.0 128.0] 508 [128.0 152.0 176.0 200.0] 509 510 """ 511 global numpy 512 if self._nrows == 0 or self._ncols == 0 or right._nrows == 0 or right._ncols == 0: 513 return self.matrix_space(self._nrows, right._ncols).zero_matrix() 514 cdef Matrix_double_dense M = self._new(self._nrows, right._ncols) 515 if numpy is None: 516 import numpy 517 M._matrix_numpy = numpy.dot(self._matrix_numpy, 518 (<Matrix_double_dense?>right)._matrix_numpy) 519 return M 520 472 521 # cdef int _cmp_c_impl(self, Matrix right) except -2: 473 522 def __invert__(self): 474 523 """ -
sage/matrix/matrix_double_sparse.pyx
diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix_double_sparse.pyx
a b 566 566 # - _add_ 567 567 # - __neg__ 568 568 # - _generic_matrix_times_matrix_ 569 # - _matrix_times_matrix_ 570 # - _matrix_times_vector_ 569 571 ######################################################################## 570 572 571 573 cpdef ModuleElement _lmul_(self, RingElement right): … … 652 654 """ 653 655 Multiply ``self`` with ``right``. 654 656 655 Overrides generic version to provide: 656 657 - faster code paths for sparse times dense 658 - complex times real uses less memory (indices not copied) 657 Overrides generic version to provide faster code path for sparse times dense. 659 658 660 659 TESTS: 661 660 … … 697 696 [ 38.0 44.0 50.0 56.0] 698 697 [ 83.0 98.0 113.0 128.0] 699 698 [128.0 152.0 176.0 200.0] 699 700 Test zero-size matrices:: 701 702 sage: x=matrix(RDF, 3, 0, sparse=True) * matrix(RDF, 0, 4, sparse=False); x; parent(x) 703 [0.0 0.0 0.0 0.0] 704 [0.0 0.0 0.0 0.0] 705 [0.0 0.0 0.0 0.0] 706 Full MatrixSpace of 3 by 4 dense matrices over Real Double Field 707 sage: x=matrix(RDF, 0, 3, sparse=True) * matrix(RDF, 3, 4, sparse=False); x; parent(x) 708 [] 709 Full MatrixSpace of 0 by 4 dense matrices over Real Double Field 710 sage: x=matrix(RDF, 2, 3, sparse=True) * matrix(RDF, 3, 0, sparse=False); x; parent(x) 711 [] 712 Full MatrixSpace of 2 by 0 dense matrices over Real Double Field 700 713 701 714 A.dense_matrix()*B is handled in ``Matrix_double_dense``. 702 715 """ 703 716 cdef sage.structure.element.Matrix left 704 cdef Matrix_double_sparse result_sparse, right_sparse705 717 cdef Matrix_double_dense result_dense, right_dense 706 718 707 719 if self._nrows == 0 or self._ncols == 0 or right._nrows == 0 or right._ncols == 0: … … 713 725 # forward call. 714 726 left = self.change_ring(codomain._base) 715 727 return left._generic_matrix_times_matrix(right, codomain) 728 if right._parent._base is not base: 729 right = right.change_ring(codomain._base) 716 730 717 # Note: Codomain base ring may be either of RDF or CDF.718 # Codomain is dense if and only if right.is_dense().719 731 if right.is_dense_c(): 720 732 if VERBOSE: print 'Sparse times dense' 721 733 result_dense = create_uninitialized_Matrix_double_dense(codomain) 722 734 result_dense._matrix_numpy = (self._entries_csc * 723 735 (<Matrix_double_dense?>right)._matrix_numpy) 724 assert result_dense._matrix_numpy.dtype == result_dense._numpy_dtype725 736 return result_dense 726 737 else: 727 if VERBOSE: print 'Sparse times sparse' 728 if right._parent._base is not base: 729 right = right.change_ring(codomain._base) 730 result_sparse = create_uninitialized_Matrix_double_sparse(codomain) 731 result_sparse._entries_csc = (self._entries_csc * 732 (<Matrix_double_sparse?>right)._entries_csc).tocsc() 733 assert result_sparse._entries_csc.dtype == result_sparse._numpy_dtype 734 return result_sparse 738 return self._matrix_times_matrix_(right) 739 740 cdef sage.structure.element.Matrix _matrix_times_matrix_(self, 741 sage.structure.element.Matrix right): 742 """ 743 Multiply ``self`` with ``right`` with the same parents. 744 745 TESTS: 746 747 sage: from sage.matrix.matrix_double_sparse import _verbose 748 sage: A = matrix(RDF,3,range(1,10),sparse=True) 749 sage: B = matrix(RDF,3,range(1,13),sparse=True) 750 sage: with _verbose(): A*B 751 Sparse times sparse 752 [ 38.0 44.0 50.0 56.0] 753 [ 83.0 98.0 113.0 128.0] 754 [128.0 152.0 176.0 200.0] 755 756 Test zero-size matrices:: 757 758 sage: x=matrix(RDF, 3, 0, sparse=True) * matrix(RDF, 0, 4, sparse=True); x; parent(x) 759 [0.0 0.0 0.0 0.0] 760 [0.0 0.0 0.0 0.0] 761 [0.0 0.0 0.0 0.0] 762 Full MatrixSpace of 3 by 4 sparse matrices over Real Double Field 763 sage: x=matrix(RDF, 0, 3, sparse=True) * matrix(RDF, 3, 4, sparse=True); x; parent(x) 764 [] 765 Full MatrixSpace of 0 by 4 sparse matrices over Real Double Field 766 sage: x=matrix(RDF, 2, 3, sparse=True) * matrix(RDF, 3, 0, sparse=True); x; parent(x) 767 [] 768 Full MatrixSpace of 2 by 0 sparse matrices over Real Double Field 769 770 """ 771 cdef sage.structure.element.Matrix left 772 cdef Matrix_double_sparse result_sparse 773 if VERBOSE: print 'Sparse times sparse' 774 775 codomain = self.matrix_space(self._nrows, right._ncols) 776 if self._nrows == 0 or self._ncols == 0 or right._nrows == 0 or right._ncols == 0: 777 return codomain.zero_matrix() 778 result_sparse = create_uninitialized_Matrix_double_sparse(codomain) 779 result_sparse._entries_csc = (self._entries_csc * 780 (<Matrix_double_sparse?>right)._entries_csc).tocsc() 781 return result_sparse 735 782 736 783 cdef Vector _matrix_times_vector_(self, Vector v): 737 784 """ -
sage/matrix/matrix_integer_2x2.pyx
diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix_integer_2x2.pyx
a b 60 60 """ 61 61 return "Space of 2x2 integer matrices" 62 62 63 def _get_matrix_c lass(self):63 def _get_matrix_constructor_info(self): 64 64 """ 65 65 EXAMPLES: 66 66 sage: A = sage.matrix.matrix_integer_2x2.MatrixSpace_ZZ_2x2() 67 sage: A._get_matrix_c lass()68 <type 'sage.matrix.matrix_integer_2x2.Matrix_integer_2x2'>67 sage: A._get_matrix_constructor_info() 68 (<type 'sage.matrix.matrix_integer_2x2.Matrix_integer_2x2'>, ()) 69 69 """ 70 return Matrix_integer_2x270 return (Matrix_integer_2x2, ()) 71 71 72 72 ZZ_2x2_parent = MatrixSpace_ZZ_2x2_class() 73 73 def MatrixSpace_ZZ_2x2(): -
sage/matrix/matrix_space.py
diff -r 2815a797fade -r 7f7c7a6fe810 sage/matrix/matrix_space.py
a b 1059 1059 def matrix(self, x=0, coerce=True, copy=True, rows=True, format=None): 1060 1060 """ 1061 1061 Create a matrix in self. The entries ``x`` can be specified 1062 either as a single list of length ``nrows \*ncols``, or as a1062 either as a single list of length ``nrows*ncols``, or as a 1063 1063 list of lists. If the matrix space is sparse the entries can 1064 1064 also be given as a dict mapping entry coordinates to 1065 1065 values. If ``format=='coo'``, ``x`` should specify the entries