Ticket #2857 (closed enhancement: fixed)

Opened 5 years ago

Last modified 5 years ago

[with patch; positive review] numerical_approx for matrices

Reported by: roed Owned by: was
Priority: major Milestone: sage-3.0
Component: linear algebra Keywords:
Cc: Work issues:
Report Upstream: Reviewers:
Authors: Merged in:
Dependencies: Stopgaps:

Description (last modified by mabshoff) (diff)

I'm running into problems with coercing to complexes or reals in matrices:

 sage: d = matrix([[3, 0],[0,sqrt(2)]])
 sage: b = matrix([[1, -1], [2, 2]])
 sage: e = b * d * b.inverse(); e

 [    1/sqrt(2) + 3/2 3/4 - 1/(2*sqrt(2))]
 [        3 - sqrt(2)     1/sqrt(2) + 3/2]

and when I try to run n() on the matrix e, I get:

 sage: e.n()  # or n(e)
 [snip]
 <type 'exceptions.TypeError'>: unable to coerce to a ComplexNumber

If you take a look at the source code for n(), you'll see that the first thing that it does is to try calling numerical_approx(prec) on the object, and then tries coercing to real or complex fields. So the solution is to write a method numerical_approx(prec) in the matrix base class that tries to numerically approximate the entries and make a new matrix out of them.

Attachments

2857.patch Download (2.8 KB) - added by dfdeshom 5 years ago.
2857.2.patch Download (2.9 KB) - added by mhansen 5 years ago.

Change History

Changed 5 years ago by dfdeshom

comment:1 Changed 5 years ago by dfdeshom

  • Summary changed from numerical_approx for matrices to [with patch; needs review] numerical_approx for matrices

Patch attached. The functionality was already there (in change_ring() and this wrapper around it works fairly well.

comment:2 Changed 5 years ago by mabshoff

  • Description modified (diff)

Changed 5 years ago by mhansen

comment:3 Changed 5 years ago by mhansen

  • Summary changed from [with patch; needs review] numerical_approx for matrices to [with patch; positive review] numerical_approx for matrices

Looks good to me.

Apply 2857.2.patch after #1763

comment:4 Changed 5 years ago by mabshoff

  • Status changed from new to closed
  • Resolution set to fixed

Merged in Sage 3.0.alpha5

Note: See TracTickets for help on using tickets.