Skip to content

Commit

Permalink
Trac #12406: solve_left and solve_right should use coercion
Browse files Browse the repository at this point in the history
This ticket uses coercion to find suitable parents for the arguments to
`Matrix.solve_right` in order to solve these problems:
{{{
sage: A = matrix(QQ, 2, [1, 2 ,3, 4])
sage: b = vector(RDF, [pi, e]); b
(3.14159265359, 2.71828182846)
sage: A.solve_right(b)   # should return (-3.564903478720541,
3.353248066155167)
(-27594871646705519/7740706532848242,
17304339474632025/5160471021898828)
sage: R.<x> = QQ[]
sage: b = vector(R, [1, x]); b
(1, x)
sage: A.solve_right(b)   # should return (t - 2, -1/2*t + 3/2)
[ugly traceback]
TypeError: not a constant polynomial
}}}

This is implemented by moving the coercion code from
`Matrix_double_dense.solve_right`, that was implemented in #17405, to
the super class. As a result, `Matrix_double_dense.solve_right` is
redundant and therefore removed.

The super method `Matrix.solve_right` is refactored a lot:

- the `check` parameter is ignored for inexact rings (see also #13932)
- the implementation is changed so that the rank of the matrix is not
computed in `solve_right`, but only in
`_solve_right_nonsingular_square`; this way, inexact rings overwriting
the latter method can avoid computing the rank (which would not work
over inexact rings)

Additionally, this ticket adds support for calling
`solve_right`/`solve_left` with non-square matrices over `RDF`/`CDF`. In
such cases, `solve_right` returns the least-squares solution of the
equation system, similar to the !Matlab/Octave backslash operator (which
our documentation already claims is implemented). This is implemented
using [https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg
.lstsq.html scipy.linalg.lstsq].
{{{
sage: A = matrix(RDF, 3, 2, [1, 3, 4, 2, 0, -3])
sage: b = vector(RDF, [5, 6, 1])
sage: x = A.solve_right(b)
sage: (A * x - b).norm()
3.2692119900020438
sage: x == A \ b
True
}}}

URL: https://trac.sagemath.org/12406
Reported by: robertwb
Ticket author(s): Markus Wageringel
Reviewer(s): Michael Orlitzky
  • Loading branch information
Release Manager committed Apr 6, 2020
2 parents f3631bb + 93cea85 commit cc04e4c
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 325 deletions.
2 changes: 1 addition & 1 deletion src/sage/geometry/polyhedron/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,7 +2388,7 @@ def generalized_permutahedron(self, coxeter_type, point=None, exact=True, regula
transf_col += [new_col]
m = matrix(AA, transf_col)
col = bf.column(i)
rhs = vector(list(col[:i+1]))
rhs = vector(AA, list(col[:i+1]))
adjusted_col = m.solve_right(rhs)
# Then scales the images so that the polytope is inscribed
c = 1 - sum(adjusted_col[j]**2 for j in range(n) if j != i)
Expand Down
Loading

0 comments on commit cc04e4c

Please sign in to comment.