如何提取浮点数的分子和分母和根?

sha*_*dow 3 python math numpy python-3.x

FLOAT_NUM = 2*math.sqrt(3)/5
>>> 0.23094010767585033
Run Code Online (Sandbox Code Playgroud)

考虑到我从 FLOAT_NUM 的值开始,如何提取 numerator==2 和 denominator==5 以及 uder root==3 的值?例如:

input >> 0.23094010767585033
output>> 2,5,3
Run Code Online (Sandbox Code Playgroud)

lej*_*lot 5

虽然不可能找到反向操作,因为可以从多个操作中获得相同的值:

def f(a,b,c):
  return a*np.sqrt(b)/c

f(1,1,1) == f(2,1,2) 
Run Code Online (Sandbox Code Playgroud)

您绝对可以找到一组产生输出的值(通过更改初始猜测 x0,您可以控制它应该搜索的“值的哪个区域”,您可以添加约束等,只需查看scipy 的文档

import numpy as np
from scipy.optimize import minimize

def f(a,b,c):
  return a*np.sqrt(np.abs(b))/c # for safety we remove sign from b

def loss(x, value):
  return (f(*x)-value)**2


print(minimize(loss, x0=(1,1,1), args=( 0.23094010767585033,)))
Run Code Online (Sandbox Code Playgroud)

输出

      fun: 1.407352885154195e-12
 hess_inv: array([[2.19894411, 0.10853698, 0.53500981],
                  [0.10853698, 0.97322067, 0.16951733],
                  [0.53500981, 0.16951733, 0.81315655]])
      jac: array([ 1.15720094e-06,  4.00176469e-07, -3.21558937e-07])
  message: 'Optimization terminated successfully.'
     nfev: 20
      nit: 4
     njev: 5
   status: 0
  success: True
        x: array([0.47495072, 0.68534754, 1.70255982])
Run Code Online (Sandbox Code Playgroud)

因此,对于 a=0.47495072, b=0.68534754, c=1.70255982,您将获得几乎感兴趣的值(高达 1e-12 误差)。

现在,如果 a、b 和 c 应该是整数,事情会变得更加棘手。您仍然可以通过优化路径 - 只需将 scipy 替换为任何用于遗传算法或其他无梯度优化器的库。

使用盆地跳跃的非常粗略的整数版本:

import numpy as np
from scipy.optimize import basinhopping

def f(a,b,c):
  return int(a)*np.sqrt(int(np.abs(b)))/int(c)

def loss(x, value=0.23094010767585033):
  return (f(*x)-value)**2

r=basinhopping(loss, stepsize=1, niter=10000, x0=(1,1,1), )
print(r)
print(list(map(int, r['x'])))
Run Code Online (Sandbox Code Playgroud)

和示例性输出

                        fun: 7.703719777548943e-34
 lowest_optimization_result:       fun: 7.703719777548943e-34
 hess_inv: array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]])
      jac: array([0., 0., 0.])
  message: 'Optimization terminated successfully.'
     nfev: 4
      nit: 0
     njev: 1
   status: 0
  success: True
        x: array([ 2.69160446,  3.44627592, 15.48606124])
                    message: ['requested number of basinhopping iterations completed successfully']
      minimization_failures: 5
                       nfev: 40004
                        nit: 10000
                       njev: 10001
                          x: array([ 2.69160446,  3.44627592, 15.48606124])
[2, 3, 15]
Run Code Online (Sandbox Code Playgroud)

显示 2*sqrt(3)/15 给出了您想要的值,最高可达 1e-34 错误。