Path: blob/develop/src/sage/matrix/matrix_complex_ball_dense.pyx
4081 views
# distutils: libraries = flint1r"""2Arbitrary precision complex ball matrices34AUTHORS:56- Clemens Heuberger (2014-10-25): Initial version.78This is an incomplete interface to the `acb_mat module9<https://flintlib.org/doc/acb_mat.html>`_ of FLINT; it may be useful to refer10to its documentation for more details.1112TESTS::1314sage: mat = matrix(CBF, 2, 2, range(4))15sage: x = polygen(QQ)16sage: pol = x^3 + 217sage: pol(mat)18[8.000000000000000 11.00000000000000]19[22.00000000000000 41.00000000000000]2021sage: mat = matrix(ComplexBallField(20), 2, 2, list(range(4)))*i/3 # needs sage.symbolic22sage: loads(dumps(mat)).identical(mat) # needs sage.symbolic23True24"""25# ****************************************************************************26# Copyright (C) 2014 Clemens Heuberger <[email protected]>27#28# This program is free software: you can redistribute it and/or modify29# it under the terms of the GNU General Public License as published by30# the Free Software Foundation, either version 2 of the License, or31# (at your option) any later version.32# http://www.gnu.org/licenses/33# ****************************************************************************34from cpython.object cimport Py_EQ, Py_NE35from cysignals.signals cimport sig_on, sig_str, sig_off3637from sage.arith.power cimport generic_power_pos38from sage.libs.flint.acb cimport *39from sage.libs.flint.acb_mat cimport *40from sage.libs.gmp.mpz cimport mpz_fits_ulong_p, mpz_get_ui41from sage.matrix.constructor import matrix42from sage.matrix.args cimport SparseEntry, MatrixArgs_init43from sage.rings.complex_interval cimport ComplexIntervalFieldElement44from sage.rings.complex_arb cimport (45ComplexBall,46ComplexIntervalFieldElement_to_acb,47acb_to_ComplexIntervalFieldElement)48from sage.rings.integer cimport Integer49from sage.rings.polynomial.polynomial_complex_arb cimport Polynomial_complex_arb50from sage.structure.element cimport Element, Matrix51from sage.structure.parent cimport Parent52from sage.structure.sequence import Sequence5354from sage.misc.superseded import experimental55from sage.rings.polynomial import polynomial_ring_constructor565758cdef void matrix_to_acb_mat(acb_mat_t target, source) noexcept:59"""60Convert a matrix containing :class:`ComplexIntervalFieldElement` to an ``acb_mat_t``.6162INPUT:6364- ``target`` -- an ``acb_mat_t``6566- ``source`` -- a matrix consisting of :class:`ComplexIntervalFieldElement`6768OUTPUT:6970None.71"""72cdef unsigned long nrows, ncols, r, c7374nrows = acb_mat_nrows(target)75ncols = acb_mat_ncols(target)7677for r in range(nrows):78for c in range(ncols):79ComplexIntervalFieldElement_to_acb(acb_mat_entry(target, r, c),80source[r][c])8182cdef ComplexIntervalFieldElement _to_CIF(acb_t source, ComplexIntervalFieldElement template):83cdef ComplexIntervalFieldElement result84result = template._new()85acb_to_ComplexIntervalFieldElement(86result, source)87return result8889cdef Matrix_generic_dense acb_mat_to_matrix(acb_mat_t source, Parent CIF):90"""91Convert an ``acb_mat_t`` to a matrix containing :class:`ComplexIntervalFieldElement`.9293INPUT:9495- ``source`` -- an ``acb_mat_t``9697- ``precision`` -- positive integer9899OUTPUT:100101A :class:`~sage.matrix.matrix_generic_dense.Matrix_generic_dense`102containing :class:`ComplexIntervalFieldElement`.103"""104cdef unsigned long nrows, ncols, r, c105cdef ComplexIntervalFieldElement template106107nrows = acb_mat_nrows(source)108ncols = acb_mat_ncols(source)109template = CIF(0)110111return matrix(112[[_to_CIF(acb_mat_entry(source, r, c), template)113for c in range(ncols)]114for r in range(nrows)])115116cdef inline long prec(Matrix_complex_ball_dense mat) noexcept:117return mat._base_ring._prec118119cdef class Matrix_complex_ball_dense(Matrix_dense):120"""121Matrix over a complex ball field. Implemented using the122``acb_mat`` type of the FLINT library.123124EXAMPLES::125126sage: MatrixSpace(CBF, 3)(2)127[2.000000000000000 0 0]128[ 0 2.000000000000000 0]129[ 0 0 2.000000000000000]130sage: matrix(CBF, 1, 3, [1, 2, -3])131[ 1.000000000000000 2.000000000000000 -3.000000000000000]132"""133def __cinit__(self):134"""135Create and allocate memory for the matrix.136137EXAMPLES::138139sage: from sage.matrix.matrix_complex_ball_dense import Matrix_complex_ball_dense140sage: a = Matrix_complex_ball_dense.__new__( # indirect doctest141....: Matrix_complex_ball_dense, Mat(CBF, 2), 0, 0, 0)142sage: type(a)143<class 'sage.matrix.matrix_complex_ball_dense.Matrix_complex_ball_dense'>144"""145sig_str("FLINT exception")146acb_mat_init(self.value, self._nrows, self._ncols)147sig_off()148149def __dealloc__(self):150"""151Free all the memory allocated for this matrix.152153EXAMPLES::154155sage: a = Matrix(CBF, 2, [1, 2, 3, 4]) # indirect doctest156sage: del a157"""158acb_mat_clear(self.value)159160cdef Matrix_complex_ball_dense _new(self, Py_ssize_t nrows, Py_ssize_t ncols):161r"""162Return a new matrix over the same base ring.163"""164cdef Parent P165if nrows == self._nrows and ncols == self._ncols:166P = self._parent167else:168P = self.matrix_space(nrows, ncols)169return Matrix_complex_ball_dense.__new__(Matrix_complex_ball_dense, P, None, None, None)170171def __init__(self, parent, entries=None, copy=None, bint coerce=True):172r"""173Initialize a dense matrix over the complex ball field.174175INPUT:176177- ``parent`` -- a matrix space over a complex ball field178179- ``entries`` -- see :func:`matrix`180181- ``copy`` -- ignored (for backwards compatibility)182183- ``coerce`` -- if ``False``, assume without checking that the184entries lie in the base ring185186EXAMPLES:187188The ``__init__`` function is called implicitly in each of the189examples below to actually fill in the values of the matrix.190191We create a `2 \times 2` and a `1\times 4` matrix::192193sage: matrix(CBF, 2, 2, range(4))194[ 0 1.000000000000000]195[2.000000000000000 3.000000000000000]196sage: Matrix(CBF, 1, 4, range(4))197[ 0 1.000000000000000 2.000000000000000 3.000000000000000]198199If the number of columns isn't given, it is determined from the200number of elements in the list. ::201202sage: matrix(CBF, 2, range(4))203[ 0 1.000000000000000]204[2.000000000000000 3.000000000000000]205sage: matrix(CBF, 2, range(6))206[ 0 1.000000000000000 2.000000000000000]207[3.000000000000000 4.000000000000000 5.000000000000000]208209Another way to make a matrix is to create the space of matrices and210convert lists into it. ::211212sage: A = Mat(CBF, 2); A213Full MatrixSpace of 2 by 2 dense matrices over214Complex ball field with 53 bits of precision215sage: A(range(4))216[ 0 1.000000000000000]217[2.000000000000000 3.000000000000000]218219Actually it is only necessary that the input can be converted to a220list, so the following also works::221222sage: v = reversed(range(4)); type(v)223<...iterator'>224sage: A(v)225[3.000000000000000 2.000000000000000]226[1.000000000000000 0]227228Matrices can have many rows or columns (in fact, on a 64-bit229machine they could have up to `2^{63}-1` rows or columns)::230231sage: v = matrix(CBF, 1, 10^5, range(10^5))232sage: v.parent()233Full MatrixSpace of 1 by 100000 dense matrices over234Complex ball field with 53 bits of precision235236TESTS::237238sage: MatrixSpace(CBF, 0, 0).one()239[]240sage: Matrix(CBF, 0, 100)2410 x 100 dense matrix over Complex ball field with 53 bits242of precision (use the '.str()' method to see the entries)243sage: Matrix(CBF, 100, 0)244100 x 0 dense matrix over Complex ball field with 53 bits245of precision (use the '.str()' method to see the entries)246"""247ma = MatrixArgs_init(parent, entries)248cdef ComplexBall z249for t in ma.iter(coerce, True):250se = <SparseEntry>t251z = <ComplexBall>se.entry252acb_set(acb_mat_entry(self.value, se.i, se.j), z.value)253254cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, object x):255"""256Set position ``i``, ``j`` of this matrix to ``x``.257258The object ``x`` must be of type ``ComplexBall``.259260INPUT:261262- ``i`` -- row263264- ``j`` -- column265266- ``x`` -- must be ComplexBall! The value to set ``self[i,j]`` to.267268EXAMPLES::269270sage: a = matrix(CBF, 2, 3, range(6)); a271[ 0 1.000000000000000 2.000000000000000]272[3.000000000000000 4.000000000000000 5.000000000000000]273sage: a[0, 0] = 10274sage: a275[10.00000000000000 1.000000000000000 2.000000000000000]276[3.000000000000000 4.000000000000000 5.000000000000000]277"""278acb_set(acb_mat_entry(self.value, i, j), (<ComplexBall> x).value)279280cdef get_unsafe(self, Py_ssize_t i, Py_ssize_t j):281"""282Return ``(i, j)`` entry of this matrix as a new ComplexBall.283284.. warning::285286This is very unsafe; it assumes ``i`` and ``j`` are in the right287range.288289EXAMPLES::290291sage: a = MatrixSpace(CBF, 3)(range(9)); a292[ 0 1.000000000000000 2.000000000000000]293[3.000000000000000 4.000000000000000 5.000000000000000]294[6.000000000000000 7.000000000000000 8.000000000000000]295sage: a[1, 2]2965.000000000000000297sage: a[4, 7]298Traceback (most recent call last):299...300IndexError: matrix index out of range301sage: a[-1, 0]3026.000000000000000303"""304cdef ComplexBall z = ComplexBall.__new__(ComplexBall)305z._parent = self._base_ring306acb_set(z.value, acb_mat_entry(self.value, i, j))307return z308309cdef copy_from_unsafe(self, Py_ssize_t iDst, Py_ssize_t jDst, src, Py_ssize_t iSrc, Py_ssize_t jSrc):310"""311Copy the ``(iSrc, jSrc)`` entry of ``src`` into the ``(iDst, jDst)``312entry of ``self``.313314.. warning::315316This is very unsafe; it assumes ``iSrc``, ``jSrc``, ``iDst`` and317``jDst`` are in the right range, and that ``src`` is a318Matrix_complex_ball_dense with the same base ring as ``self``.319320INPUT:321322- ``iDst`` - the row to be copied to in ``self``.323- ``jDst`` - the column to be copied to in ``self``.324- ``src`` - the matrix to copy from. Should be a325Matrix_complex_ball_dense with the same base ring as326``self``.327- ``iSrc`` - the row to be copied from in ``src``.328- ``jSrc`` - the column to be copied from in ``src``.329330TESTS::331332sage: m = MatrixSpace(CBF,3,4)(range(12))333sage: m334[ 0 1.000000000000000 2.000000000000000 3.000000000000000]335[4.000000000000000 5.000000000000000 6.000000000000000 7.000000000000000]336[8.000000000000000 9.000000000000000 10.00000000000000 11.00000000000000]337sage: m.transpose()338[ 0 4.000000000000000 8.000000000000000]339[1.000000000000000 5.000000000000000 9.000000000000000]340[2.000000000000000 6.000000000000000 10.00000000000000]341[3.000000000000000 7.000000000000000 11.00000000000000]342sage: m.matrix_from_rows([0,2])343[ 0 1.000000000000000 2.000000000000000 3.000000000000000]344[8.000000000000000 9.000000000000000 10.00000000000000 11.00000000000000]345sage: m.matrix_from_columns([1,3])346[1.000000000000000 3.000000000000000]347[5.000000000000000 7.000000000000000]348[9.000000000000000 11.00000000000000]349sage: m.matrix_from_rows_and_columns([1,2],[0,3])350[4.000000000000000 7.000000000000000]351[8.000000000000000 11.00000000000000]352"""353cdef Matrix_complex_ball_dense _src = <Matrix_complex_ball_dense> src354acb_set(acb_mat_entry(self.value, iDst, jDst), acb_mat_entry(_src.value, iSrc, jSrc))355356cpdef _richcmp_(left, right, int op):357r"""358EXAMPLES::359360sage: a = matrix(CBF, [[1,2],[3,4]])361sage: b = matrix(CBF, [[1,2],[3,4]])362sage: a == b363True364sage: a + 1/3 == b + 1/3365False366sage: a < b367Traceback (most recent call last):368...369TypeError: no order is defined on complex ball matrices370371TESTS::372373sage: a = matrix(CBF, [1/3])374sage: b = matrix(CBF, [1/3])375sage: a == a or b == b or a[0,0] == a[0,0] or a[0,0] == b[0,0]376False377"""378cdef Matrix_complex_ball_dense lt = <Matrix_complex_ball_dense> left379cdef Matrix_complex_ball_dense rt = <Matrix_complex_ball_dense> right380if op == Py_EQ:381return acb_mat_eq(lt.value, rt.value)382elif op == Py_NE:383return acb_mat_ne(lt.value, rt.value)384else:385raise TypeError("no order is defined on complex ball matrices")386387def identical(self, Matrix_complex_ball_dense other):388r"""389Test if the corresponding entries of two complex ball matrices390represent the same balls.391392EXAMPLES::393394sage: a = matrix(CBF, [[1/3,2],[3,4]])395sage: b = matrix(CBF, [[1/3,2],[3,4]])396sage: a == b397False398sage: a.identical(b)399True400"""401return acb_mat_equal(self.value, other.value)402403def overlaps(self, Matrix_complex_ball_dense other):404r"""405Test if two matrices with complex ball entries represent overlapping406sets of complex matrices.407408EXAMPLES::409410sage: b = CBF(0, RBF(0, rad=0.1r)); b411[+/- 0.101]*I412sage: matrix(CBF, [0, b]).overlaps(matrix(CBF, [b, 0]))413True414sage: matrix(CBF, [1, 0]).overlaps(matrix(CBF, [b, 0]))415False416"""417return acb_mat_overlaps(self.value, other.value)418419def contains(self, Matrix_complex_ball_dense other):420r"""421Test if the set of complex matrices represented by ``self`` is422contained in that represented by ``other``.423424EXAMPLES::425426sage: b = CBF(0, RBF(0, rad=.1r)); b427[+/- 0.101]*I428sage: matrix(CBF, [0, b]).contains(matrix(CBF, [0, 0]))429True430sage: matrix(CBF, [0, b]).contains(matrix(CBF, [b, 0]))431False432sage: matrix(CBF, [b, b]).contains(matrix(CBF, [b, 0]))433True434"""435return acb_mat_contains(self.value, other.value)436437def __neg__(self):438r"""439TESTS::440441sage: -matrix(CBF, [[1,2]])442[-1.000000000000000 -2.000000000000000]443"""444cdef Matrix_complex_ball_dense res = self._new(self._nrows, self._ncols)445sig_on()446acb_mat_neg(res.value, self.value)447sig_off()448return res449450cpdef _add_(self, other):451r"""452TESTS::453454sage: matrix(CBF, [[1,2]])._add_(matrix(CBF, [3,4]))455[4.000000000000000 6.000000000000000]456"""457cdef Matrix_complex_ball_dense res = self._new(self._nrows, self._ncols)458sig_on()459acb_mat_add(res.value, self.value, (<Matrix_complex_ball_dense> other).value, prec(self))460sig_off()461return res462463cpdef _sub_(self, other):464r"""465TESTS::466467sage: matrix(CBF, [[1,2]])._sub_(matrix(CBF, [3,4]))468[-2.000000000000000 -2.000000000000000]469"""470cdef Matrix_complex_ball_dense res = self._new(self._nrows, self._ncols)471sig_on()472acb_mat_sub(res.value, self.value, (<Matrix_complex_ball_dense> other).value, prec(self))473sig_off()474return res475476cpdef _lmul_(self, Element a):477r"""478TESTS::479480sage: matrix(CBF, [[1,2]])._lmul_(CBF(I))481[1.000000000000000*I 2.000000000000000*I]482"""483cdef Matrix_complex_ball_dense res = self._new(self._nrows, self._ncols)484sig_on()485acb_mat_scalar_mul_acb(res.value, self.value, (<ComplexBall> a).value, prec(self))486sig_off()487return res488489cpdef _rmul_(self, Element a):490r"""491TESTS::492493sage: matrix(CBF, [[1,2]])._rmul_(CBF(I))494[1.000000000000000*I 2.000000000000000*I]495"""496return self._lmul_(a)497498cdef _matrix_times_matrix_(self, Matrix other):499r"""500TESTS::501502sage: matrix(CBF, [[1,2]])*matrix([[3], [4]]) # indirect doctest503[11.00000000000000]504"""505cdef Matrix_complex_ball_dense res = self._new(self._nrows, other._ncols)506sig_on()507acb_mat_mul(res.value, self.value, (<Matrix_complex_ball_dense> other).value, prec(self))508sig_off()509return res510511cpdef _pow_int(self, n):512r"""513Return the ``n``-th power of this matrix.514515EXAMPLES::516517sage: mat = matrix(CBF, [[1/2, 1/3], [1, 1]])518sage: mat**2519[[0.5833333333333...] [0.500000000000000 +/- ...e-16]]520[ 1.500000000000000 [1.333333333333333 +/- ...e-16]]521sage: mat**(-2)522[ [48.00000000000...] [-18.00000000000...]]523[[-54.0000000000...] [21.000000000000...]]524525TESTS::526527sage: mat**(0r)528[1.000000000000000 0]529[ 0 1.000000000000000]530531sage: mat**(1/2)532Traceback (most recent call last):533...534NotImplementedError: non-integral exponents not supported535536sage: (-(matrix(CBF, [2])**(-2**100))[0,0].log(2)).log(2)537[100.000000000000 +/- ...e-14]538sage: (-(matrix(CBF, [2])**(-2**64+1))[0,0].log(2)).log(2)539[64.0000000000000 +/- ...e-14]540"""541cdef Matrix_complex_ball_dense res = self._new(self._nrows, self._ncols)542cdef Matrix_complex_ball_dense tmp543cdef unsigned long expo544n = Integer(n)545if self._nrows != self._ncols:546raise ArithmeticError("self must be a square matrix")547548neg = (n < 0)549if neg:550n = -n551if mpz_fits_ulong_p((<Integer>n).value):552expo = mpz_get_ui((<Integer>n).value)553sig_on()554acb_mat_pow_ui(res.value, self.value, expo, prec(self))555sig_off()556else:557tmp = generic_power_pos(self, n)558acb_mat_set(res.value, tmp.value)559if neg:560sig_on()561acb_mat_inv(res.value, res.value, prec(self))562sig_off()563564return res565566def __invert__(self):567r"""568TESTS::569570sage: ~matrix(CBF, [[1/2, 1/3], [1, 1]])571[ [6.00000000000000 +/- ...e-15] [-2.00000000000000 +/- ...e-15]]572[[-6.00000000000000 +/- ...e-15] [3.00000000000000 +/- ...e-15]]573sage: ~matrix(CBF, [[1/2, 1/3]])574Traceback (most recent call last):575...576ArithmeticError: self must be a square matrix577sage: mat = matrix(CBF, [[1/3, 1/2], [0, 1]]) - 1/3578sage: ~mat579Traceback (most recent call last):580...581ZeroDivisionError: unable to compute the inverse, is the matrix singular?582"""583if not self.is_square():584raise ArithmeticError("self must be a square matrix")585cdef Matrix_complex_ball_dense res = self._new(self._nrows, self._ncols)586sig_on()587cdef bint success = acb_mat_inv(res.value, self.value, prec(self))588sig_off()589if success:590return res591else:592raise ZeroDivisionError("unable to compute the inverse, is the matrix singular?")593594def transpose(self):595r"""596Return the transpose of ``self``.597598EXAMPLES::599600sage: m = matrix(CBF, 2, 3, [1, 2, 3, 4, 5, 6])601sage: m.transpose()602[1.000000000000000 4.000000000000000]603[2.000000000000000 5.000000000000000]604[3.000000000000000 6.000000000000000]605sage: m.transpose().parent()606Full MatrixSpace of 3 by 2 dense matrices over Complex ball field with 53 bits of precision607608TESTS::609610sage: m = matrix(CBF,2,3,range(6))611sage: m.subdivide([1],[2])612sage: m613[ 0 1.000000000000000|2.000000000000000]614[-----------------------------------+-----------------]615[3.000000000000000 4.000000000000000|5.000000000000000]616sage: m.transpose()617[ 0|3.000000000000000]618[1.000000000000000|4.000000000000000]619[-----------------+-----------------]620[2.000000000000000|5.000000000000000]621622sage: m = matrix(CBF,0,2)623sage: m.subdivide([],[1])624sage: m.subdivisions()625([], [1])626sage: m.transpose().subdivisions()627([1], [])628629sage: m = matrix(CBF,2,0)630sage: m.subdivide([1],[])631sage: m.subdivisions()632([1], [])633sage: m.transpose().subdivisions()634([], [1])635"""636cdef Py_ssize_t nc = self._ncols637cdef Py_ssize_t nr = self._nrows638cdef Matrix_complex_ball_dense trans = self._new(nc, nr)639acb_mat_transpose(trans.value, self.value)640if self._subdivisions is not None:641row_divs, col_divs = self.subdivisions()642trans.subdivide(col_divs, row_divs)643return trans644645def _solve_right_nonsingular_square(self, Matrix_complex_ball_dense rhs, check_rank=None):646r"""647TESTS::648649sage: matrix(CBF, [[1/2, 1/3], [1, 1]]).solve_right(vector([-1, 1]))650([-8.00000000000000 +/- ...], [9.00000000000000 +/- ...])651sage: matrix(CBF, 2, 2, 0).solve_right(vector([-1, 1]))652Traceback (most recent call last):653...654ValueError: unable to invert this matrix655sage: b = CBF(0, RBF(0, rad=.1r))656sage: matrix(CBF, [[1, 1], [0, b]]).solve_right(vector([-1, 1]))657Traceback (most recent call last):658...659ValueError: unable to invert this matrix660"""661cdef Matrix_complex_ball_dense res = self._new(self._nrows, rhs._ncols)662sig_on()663success = acb_mat_solve(res.value, self.value, rhs.value, min(prec(self), prec(rhs)))664sig_off()665if success:666return res667else:668raise ValueError("unable to invert this matrix")669670def determinant(self):671r"""672Compute the determinant of this matrix.673674EXAMPLES::675676sage: matrix(CBF, [[1/2, 1/3], [1, 1]]).determinant()677[0.1666666666666667 +/- ...e-17]678sage: matrix(CBF, [[1/2, 1/3], [1, 1]]).det()679[0.1666666666666667 +/- ...e-17]680sage: matrix(CBF, [[1/2, 1/3]]).determinant()681Traceback (most recent call last):682...683ValueError: self must be a square matrix684"""685cdef ComplexBall res = ComplexBall.__new__(ComplexBall)686res._parent = self._base_ring687if self._nrows != self._ncols:688raise ValueError("self must be a square matrix")689sig_on()690acb_mat_det(res.value, self.value, prec(self))691sig_off()692return res693694def trace(self):695r"""696Compute the trace of this matrix.697698EXAMPLES::699700sage: matrix(CBF, [[1/3, 1/3], [1, 1]]).trace()701[1.333333333333333 +/- ...e-16]702sage: matrix(CBF, [[1/2, 1/3]]).trace()703Traceback (most recent call last):704...705ValueError: self must be a square matrix706"""707cdef ComplexBall res = ComplexBall.__new__(ComplexBall)708res._parent = self._base_ring709if self._nrows != self._ncols:710raise ValueError("self must be a square matrix")711sig_on()712acb_mat_trace(res.value, self.value, prec(self))713sig_off()714return res715716def charpoly(self, var='x', algorithm=None):717r"""718Compute the characteristic polynomial of this matrix.719720EXAMPLES::721722sage: from sage.matrix.benchmark import hilbert_matrix723sage: mat = hilbert_matrix(5).change_ring(ComplexBallField(10))724sage: mat.charpoly()725x^5 + ([-1.8 +/- 0.0258])*x^4 + ([0.3 +/- 0.05...)*x^3 +726([+/- 0.0...])*x^2 + ([+/- 0.0...])*x + [+/- 0.0...]727728TESTS::729730sage: mat.charpoly(algorithm='hessenberg')731x^5 + ([-1.8 +/- 0.04...])*x^4 + ([0.3 +/- 0.08...])*x^3732+ ([+/- 0.0...])*x^2 + ([+/- ...e-4])*x + [+/- ...e-6]733sage: mat.charpoly('y')734y^5 + ([-1.8 +/- 0.02...])*y^4 + ([0.3 +/- 0.05...])*y^3 +735([+/- 0.0...])*y^2 + ([+/- 0.0...])*y + [+/- 0.0...]736"""737if self._nrows != self._ncols:738raise ValueError("self must be a square matrix")739if algorithm is not None:740return super(Matrix_dense, self).charpoly(var=var, algorithm=algorithm)741Pol = polynomial_ring_constructor._single_variate(self.base_ring(), var)742cdef Polynomial_complex_arb res = Polynomial_complex_arb(Pol)743sig_on()744acb_mat_charpoly(res._poly, self.value, prec(self))745sig_off()746return res747748@experimental(issue_number=30393)749def eigenvalues(self, other=None, *, extend=None):750r"""751(Experimental.) Compute rigorous enclosures of the eigenvalues of this matrix.752753INPUT:754755- ``self`` -- an `n \times n` matrix756- ``other`` -- unsupported (generalized eigenvalue problem), should be ``None``757- ``extend`` -- ignored758759OUTPUT:760761A :class:`~sage.structure.sequence.Sequence` of complex balls of762length equal to the size of the matrix.763764Each element represents one eigenvalue with the correct multiplicities765in case of overlap. The output intervals are either disjoint or766identical, and identical intervals are guaranteed to be grouped767consecutively. Each complete run of `k` identical balls thus represents768a cluster of exactly `k` eigenvalues which could not be separated from769each other at the current precision, but which could be isolated from770the other eigenvalues.771772There is currently no guarantee that the algorithm converges as the773working precision is increased.774775See the `FLINT documentation <https://flintlib.org/doc/acb_mat.html#c.acb_mat_eig_multiple>`__776for more information.777778EXAMPLES::779780sage: from sage.matrix.benchmark import hilbert_matrix781sage: mat = hilbert_matrix(5).change_ring(CBF)782sage: mat.eigenvalues()783doctest:...: FutureWarning: This class/method/function is marked as experimental.784...785[[1.567050691098...] + [+/- ...]*I, [0.208534218611...] + [+/- ...]*I,786[3.287928...e-6...] + [+/- ...]*I, [0.000305898040...] + [+/- ...]*I,787[0.011407491623...] + [+/- ...]*I]788789sage: mat = Permutation([2, 1, 4, 5, 3]).to_matrix().dense_matrix().change_ring(CBF)790sage: mat.eigenvalues()791Traceback (most recent call last):792...793ValueError: unable to certify the eigenvalues794sage: precond = matrix(ZZ, [[-1, -2, 2, 2, -2], [2, -2, -2, -2, 2],795....: [-2, 2, -1, 2, 1], [2, 1, -1, 0, 2], [-2, 0, 1, -1, 1]])796sage: (~precond*mat*precond).eigenvalues()797[[-0.5000000000000...] + [-0.8660254037844...]*I, [-1.000000000000...] + [+/- ...]*I,798[-0.5000000000000...] + [0.8660254037844...]*I,799[1.000000000000...] + [+/- ...]*I, [1.000000000000...] + [+/- ...]*I]800801.. SEEALSO:: :meth:`eigenvectors_right`802"""803if self._nrows != self._ncols:804raise ValueError("self must be a square matrix")805cdef long n = self._ncols806cdef acb_ptr eigval_approx, eigval807cdef acb_mat_t eigvec_approx808if other is not None:809raise NotImplementedError810try:811eigval_approx = _acb_vec_init(n)812acb_mat_init(eigvec_approx, n, n)813acb_mat_approx_eig_qr(eigval_approx, NULL, eigvec_approx, self.value, NULL, 0, prec(self))814eigval = _acb_vec_init(n)815if not acb_mat_eig_multiple(eigval, self.value, eigval_approx, eigvec_approx, prec(self)):816raise ValueError("unable to certify the eigenvalues")817res = _acb_vec_to_list(eigval, n, self._parent._base)818finally:819acb_mat_clear(eigvec_approx)820_acb_vec_clear(eigval, n)821_acb_vec_clear(eigval_approx, n)822return Sequence(res)823824@experimental(issue_number=30393)825def eigenvectors_right_approx(self, other=None, *, extend=None):826r"""827(Experimental.) Compute *non-rigorous* approximations of the828eigenvalues and eigenvectors of this matrix.829830INPUT:831832- ``self`` -- an `n \times n` matrix833- ``other`` -- unsupported (generalized eigenvalue problem), should be ``None``834- ``extend`` -- ignored835836OUTPUT:837838A list of triples of the form ``(eigenvalue, [eigenvector], 1)``. The839eigenvalue and the entries of the eigenvector are complex balls with840zero radius.841842No guarantees are made about the accuracy of the output.843844See the `FLINT documentation <https://flintlib.org/doc/acb_mat.html#c.acb_mat_approx_eig_qr>`__845for more information.846847EXAMPLES::848849sage: from sage.matrix.benchmark import hilbert_matrix850sage: mat = hilbert_matrix(3).change_ring(CBF)851sage: eigval, eigvec, _ = mat.eigenvectors_right_approx()[0]852doctest:...: FutureWarning: This class/method/function is marked as experimental.853...854sage: eigval855[1.40831892712...]856sage: eigval.rad()8570.00000000858sage: eigvec859[([0.8270449269720...], [0.4598639043655...], [0.3232984352444...])]860sage: (mat - eigval)*eigvec[0]861([1e-15 +/- ...], [2e-15 +/- ...], [+/- ...])862863.. SEEALSO:: :meth:`eigenvectors_right`864"""865if self._nrows != self._ncols:866raise ValueError("self must be a square matrix")867cdef long n = self._ncols868cdef Matrix_complex_ball_dense eigvec = self._new(n, n)869cdef acb_ptr _eigval870if other is not None:871raise NotImplementedError872try:873_eigval = _acb_vec_init(n)874acb_mat_approx_eig_qr(_eigval, NULL, eigvec.value, self.value, NULL, 0, prec(self))875eigval = _acb_vec_to_list(_eigval, n, self._parent._base)876finally:877_acb_vec_clear(_eigval, n)878return [(val, [vec], 1) for val, vec in zip(eigval, eigvec.columns())]879880@experimental(issue_number=30393)881def eigenvectors_right(self, other=None, *, extend=None):882r"""883(Experimental.) Compute rigorous enclosures of the eigenvalues and884eigenvectors of this matrix.885886INPUT:887888- ``self`` -- an `n \times n` matrix889- ``other`` -- unsupported (generalized eigenvalue problem), should be ``None``890- ``extend`` -- ignored891892OUTPUT:893894A list of triples of the form ``(eigenvalue, [eigenvector], 1)``.895896Unlike :meth:`eigenvalues` and :meth:`eigenvectors_right_approx`, this897method currently fails in the presence of multiple eigenvalues.898899Additionally, there is currently no guarantee that the algorithm900converges as the working precision is increased.901902See the `FLINT documentation <https://flintlib.org/doc/acb_mat.html#c.acb_mat_eig_simple>`__903for more information.904905EXAMPLES::906907sage: from sage.matrix.benchmark import hilbert_matrix908sage: mat = hilbert_matrix(3).change_ring(CBF)909sage: eigval, eigvec, _ = mat.eigenvectors_right()[0]910doctest:...: FutureWarning: This class/method/function is marked as experimental.911...912sage: eigval913[1.40831892712...] + [+/- ...]*I914sage: eigvec915[([0.82704492697...] + [+/- ...]*I, [0.45986390436...] + [+/- ...]*I, [0.32329843524...] + [+/- ...]*I)]916sage: (mat - eigval)*eigvec[0]917([+/- ...] + [+/- ...]*I, [+/- ...] + [+/- ...]*I, [+/- ...] + [+/- ...]*I)918919.. SEEALSO:: :meth:`eigenvectors_right_approx`, :meth:`eigenvalues`920"""921if self._nrows != self._ncols:922raise ValueError("self must be a square matrix")923cdef long n = self._ncols924cdef acb_ptr eigval_approx, _eigval925cdef acb_mat_t eigvec_approx926cdef Matrix_complex_ball_dense eigvec = self._new(n, n)927if other is not None:928raise NotImplementedError929try:930_eigval = _acb_vec_init(n)931eigval_approx = _acb_vec_init(n)932acb_mat_init(eigvec_approx, n, n)933acb_mat_approx_eig_qr(eigval_approx, NULL, eigvec_approx, self.value, NULL, 0, prec(self))934if not acb_mat_eig_simple(_eigval, NULL, eigvec.value, self.value, eigval_approx, eigvec_approx, prec(self)):935raise ValueError("unable to isolate the eigenvalues (multiple eigenvalues?)")936eigval = _acb_vec_to_list(_eigval, n, self._parent._base)937finally:938acb_mat_clear(eigvec_approx)939_acb_vec_clear(_eigval, n)940_acb_vec_clear(eigval_approx, n)941return [(val, [vec], 1) for val, vec in zip(eigval, eigvec.columns())]942943def eigenvectors_left_approx(self, other=None, *, extend=None):944r"""945(Experimental.) Compute *non-rigorous* approximations of the946left eigenvalues and eigenvectors of this matrix.947948INPUT:949950- ``self`` -- an `n \times n` matrix951- ``other`` -- unsupported (generalized eigenvalue problem), should be ``None``952- ``extend`` -- ignored953954OUTPUT:955956A list of triples of the form ``(eigenvalue, [eigenvector], 1)``. The957eigenvalue and the entries of the eigenvector are complex balls with958zero radius.959960No guarantees are made about the accuracy of the output.961962See the `FLINT documentation <https://flintlib.org/doc/acb_mat.html#c.acb_mat_approx_eig_qr>`__963for more information.964965EXAMPLES::966967sage: mat = matrix(CBF, 3, [2, 3, 5, 7, 11, 13, 17, 19, 23])968sage: eigval, eigvec, _ = mat.eigenvectors_left_approx()[0]969sage: eigval970[1.1052996349... +/- ...]971sage: eigvec[0]972([0.69817246751...], [-0.67419514369...], [0.240865343781...])973sage: eigvec[0] * (mat - eigval)974([+/- ...], [+/- ...], [+/- ...])975976.. SEEALSO:: :meth:`eigenvectors_left`977"""978return self.transpose().eigenvectors_right_approx(other=None, extend=extend)979980def eigenvectors_left(self, other=None, *, extend=True):981r"""982(Experimental.) Compute rigorous enclosures of the eigenvalues and983left eigenvectors of this matrix.984985INPUT:986987- ``self`` -- an `n \times n` matrix988- ``other`` -- unsupported (generalized eigenvalue problem), should be ``None``989- ``extend`` -- ignored990991OUTPUT:992993A list of triples of the form ``(eigenvalue, [eigenvector], 1)``.994995Unlike :meth:`eigenvalues` and :meth:`eigenvectors_left_approx`, this996method currently fails in the presence of multiple eigenvalues.997998Additionally, there is currently no guarantee that the algorithm999converges as the working precision is increased.10001001See the `FLINT documentation <https://flintlib.org/doc/acb_mat.html#c.acb_mat_eig_simple>`__1002for more information.10031004EXAMPLES::10051006sage: mat = matrix(CBF, 3, [2, 3, 5, 7, 11, 13, 17, 19, 23])1007sage: eigval, eigvec, _ = mat.eigenvectors_left()[0]1008sage: eigval1009[1.1052996349...] + [+/- ...]*I1010sage: eigvec[0]1011([0.69817246751...] + [+/- ...]*I, [-0.67419514369...] + [+/- ...]*I, [0.240865343781...] + [+/- ...]*I)1012sage: eigvec[0] * (mat - eigval)1013([+/- ...] + [+/- ...]*I, [+/- ...] + [+/- ...]*I, [+/- ...] + [+/- ...]*I)10141015.. SEEALSO:: :meth:`eigenvectors_right`, :meth:`eigenvalues`, :meth:`eigenvectors_left_approx`1016"""1017return self.transpose().eigenvectors_right(other=other, extend=extend)10181019def exp(self):1020r"""1021Compute the exponential of this matrix.10221023EXAMPLES::10241025sage: matrix(CBF, [[i*pi, 1], [0, i*pi]]).exp() # needs sage.symbolic1026[[-1.00000000000000 +/- ...e-16] + [+/- ...e-16]*I [-1.00000000000000 +/- ...e-16] + [+/- ...e-16]*I]1027[ 0 [-1.00000000000000 +/- ...e-16] + [+/- ...e-16]*I]1028sage: matrix(CBF, [[1/2, 1/3]]).exp()1029Traceback (most recent call last):1030...1031ValueError: self must be a square matrix1032"""1033cdef Matrix_complex_ball_dense res = self._new(self._nrows, self._ncols)1034if self._nrows != self._ncols:1035raise ValueError("self must be a square matrix")1036sig_on()1037acb_mat_exp(res.value, self.value, prec(self))1038sig_off()1039return res10401041cdef _acb_vec_to_list(acb_ptr vec, long n, Parent parent):1042cdef ComplexBall b1043res = []1044for i in range(n):1045b = ComplexBall.__new__(ComplexBall)1046b._parent = parent1047acb_set(b.value, &vec[i])1048res.append(b)1049return res105010511052