# HG changeset patch
# User David Loeffler <d.loeffler.01@cantab.net>
# Date 1274190776 -3600
# Node ID e6f6fcb4f0acd42adcd2590484fa177dce3e70b2
# Parent 221804dc9284b171fcdfb1b1ee03a860772dfe8c
[mq]: trac_7492_review.patch
diff -r 221804dc9284 -r e6f6fcb4f0ac sage/combinat/permutation.py
a
|
b
|
|
3563 | 3563 | Returns the positive sum of permutations corresponding to |
3564 | 3564 | the bistochastic matrix. |
3565 | 3565 | |
3566 | | A stochastic matrix is a matrix such that the sum of the elements |
3567 | | of any row is equal to 1. A bistochastic matrix is a stochastic matrix |
3568 | | whose transpose matrix is also stochastic ( there are conditions |
3569 | | both on the rows and on the columns ). |
| 3566 | A stochastic matrix is a matrix with nonnegative real entries such that the |
| 3567 | sum of the elements of any row is equal to 1. A bistochastic matrix is a |
| 3568 | stochastic matrix whose transpose matrix is also stochastic ( there are |
| 3569 | conditions both on the rows and on the columns ). |
3570 | 3570 | |
3571 | 3571 | According to the Birkhoff-von Neumann Theorem, any bistochastic matrix |
3572 | 3572 | can be written as a positive sum of permutation matrices, which also |
… |
… |
|
3582 | 3582 | |
3583 | 3583 | - ``M`` -- A bistochastic matrix |
3584 | 3584 | |
3585 | | - ``check`` (boolean) -- set to ``True`` (default) to checl |
| 3585 | - ``check`` (boolean) -- set to ``True`` (default) to check |
3586 | 3586 | that the matrix is indeed bistochastic |
3587 | 3587 | |
3588 | 3588 | OUTPUT: |
… |
… |
|
3628 | 3628 | sage: print decomp |
3629 | 3629 | 2*B[[1, 4, 2, 3, 5]] + 3*B[[3, 1, 4, 2, 5]] + 9*B[[4, 1, 3, 5, 2]] + 6*B[[5, 3, 4, 1, 2]] |
3630 | 3630 | |
3631 | | An exception is raised when the matrix is not bistochastic:: |
| 3631 | An exception is raised when the matrix is not positive and bistochastic:: |
3632 | 3632 | |
3633 | 3633 | sage: M = Matrix([[2,3],[2,2]]) |
3634 | 3634 | sage: decomp = bistochastic_as_sum_of_permutations(M) |
3635 | 3635 | Traceback (most recent call last): |
3636 | 3636 | ... |
3637 | 3637 | ValueError: The matrix is not bistochastic |
| 3638 | |
| 3639 | sage: bistochastic_as_sum_of_permutations(Matrix(GF(7), 2, [2,1,1,2])) |
| 3640 | Traceback (most recent call last): |
| 3641 | ... |
| 3642 | ValueError: The base ring of the matrix must have a coercion map to RR |
| 3643 | |
| 3644 | sage: bistochastic_as_sum_of_permutations(Matrix(ZZ, 2, [2,-1,-1,2])) |
| 3645 | Traceback (most recent call last): |
| 3646 | ... |
| 3647 | ValueError: The matrix should have nonnegative entries |
3638 | 3648 | """ |
3639 | 3649 | |
3640 | 3650 | from sage.graphs.bipartite_graph import BipartiteGraph |
3641 | 3651 | from sage.combinat.free_module import CombinatorialFreeModule |
3642 | | from sage.rings.real_mpfr import RR |
| 3652 | from sage.rings.all import RR |
3643 | 3653 | |
3644 | 3654 | n=M.nrows() |
3645 | 3655 | |
… |
… |
|
3652 | 3662 | if not RR.has_coerce_map_from(M.base_ring()): |
3653 | 3663 | raise ValueError("The base ring of the matrix must have a coercion map to RR") |
3654 | 3664 | |
| 3665 | if not all([x >= 0 for x in M.list()]): |
| 3666 | raise ValueError, "The matrix should have nonnegative entries" |
| 3667 | |
3655 | 3668 | CFM=CombinatorialFreeModule(M.base_ring(),Permutations(n)) |
3656 | 3669 | value=0 |
3657 | 3670 | |