pythonnumpybigfloat

Convert type from BigFloat to Float in python


I'm computing exp using BigFloat library in python. But then I have to compute the inverse of a matrix of BigFloats. I use function numpy.linalg.inv but I'm getting the following error:

No loop matching the specified signature and casting was found for ufunc inv

So I'd say I need to convert BigFloat to some other type. How can I do it?


Solution

  • Considering a 2D list of Bigfloat values (with exp):

    from bigfloat import *
    import numpy as np
    
    row = 4
    col = 4
    mat = []
    with precision(1000):
        for i in range(0,row):
            row = []
            for j in range(0,col):
                row.append(exp(1./(i+j+1)))
            mat.append(row)
    print "A single element in bigfloat: \n",mat[0][0]
    mat_numpivized = np.array([[np.float(col) for col in row] for row in mat])
    print "\nNumpived matrix of bigfloat: \n",mat_numpivized
    inversed_mat = np.linalg.inv(mat_numpivized)
    print "\nInverse of numpivized bigfloat matrix: \n", inversed_mat
    

    result:

    A single element in bigfloat: 
        2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408914993488416750924476146066808226480016847741185374234544243710753907774499206950
    
    Numpived matrix of bigfloat: 
    [[ 2.71828183  1.64872127  1.39561243  1.28402542]
     [ 1.64872127  1.39561243  1.28402542  1.22140276]
     [ 1.39561243  1.28402542  1.22140276  1.18136041]
     [ 1.28402542  1.22140276  1.18136041  1.15356499]]
    
    Inverse of numpivized bigfloat matrix: 
    [[    3.82106765   -30.68951855    61.37113204   -34.60880961]
     [  -30.68951855   385.05574805  -890.19682475   538.10687764]
     [   61.37113204  -890.19682475  2211.11911647 -1390.16166038]
     [  -34.60880961   538.10687764 -1390.16166038   893.29628089]]