0

I am using Multiple-precision Rationals(mpfr) object in Numpy matrix,

matrix([[ mpfr('-366998.93593422093364191959435957721088073331222596080623233278164906447646654043966366647797',300),
     mpfr('-366997.28868432286431885359868309613943011772698563764930700121744888472828510537502286003536',300),
     mpfr('-366997.28868432286431885359868309613943011772698563764930700121744888472828510537502286003536',300),
     mpfr('-366997.28868432286431885359868309613943011772698563764930700121744888472828510537502310955189',300),
     mpfr('-366997.33936304224917822062156336656390364691713762458391131405889211470102834400572590888586',300),
     mpfr('-366997.28868432286431885359868309613943011772698563764930700121744888472828510537502286003536',300)],
    [ mpfr('-40813927.104656436832435886099653290386078894027773129049451436960078610548203287954114434382',300),
     mpfr('-10418349883335.380900703935580692318458974868691020694148304775624032110383967472053357462067',300),
     mpfr('-40813927.104656436832435886099653290386078894027773129049451436960078610548203287954114434382',300),
     mpfr('-40813927.104656436832435886099653290386078894027773129049451436960078610548203287954114434382',300),
     mpfr('-40813927.104656436832435886099653290386078894027773129049451436960078610548203287954114434382',300),
     mpfr('-40813927.104656436832435886099653290386078894027773129049451436960078610548203287954114434382',300)]], dtype=object)

but when compute the inverse of the matrix, I will lose the precision.

In [10]: a.I
Out[10]:
matrix([[ -5.44966727e-07,   1.91970239e-14],
        [  1.06745086e-11,  -9.59848660e-14],
        [ -5.44964281e-07,   1.91969377e-14],
        [ -5.44964281e-07,   1.91969377e-14],
        [ -5.44964356e-07,   1.91969404e-14],
        [ -5.44964281e-07,   1.91969377e-14]])

So how to keep precision of mpfr? Any suggestion will be appreciated!

PytLab
  • 539
  • 6
  • 12

1 Answers1

0

For performance reasons, numpy uses the LAPACK libraries and has to convert the matrix elements to the standard double type. You may want to try mpmath for multiple precision matrix inversion.

casevh
  • 11,093
  • 1
  • 24
  • 35
  • Is there any way to use mpfr in Numpy? Because matrix operation in mpmath is slower than that of numpy . and I also want to keep the precision. Thanks – PytLab Apr 18 '15 at 03:01
  • @PytLab The reason numpy is faster is because it uses the floating-point types that are implemented in hardware. They have fixed precision but are very fast. Once you start using arbitrary precision arithmetic, performance will be slower. – casevh Apr 18 '15 at 04:29