gpt4 book ai didi

python - 将字符串中的所有冗余 float 转换为整数

转载 作者:行者123 更新时间:2023-12-01 13:12:02 25 4
gpt4 key购买 nike

我正在使用 Sympy 创建一个自定义函数,将复数平方根转换为复数。当我输入 -sqrt(-2 + 2*sqrt(3)*I) 时,我得到了 -1 - sqrt(3)*I 的预期输出,但是,输入 -sqrt(-2.0 + 2*sqrt(3)*I)(有一个 -2.0 而不是 -2),我得到输出 -1.0 - 0.707106781186547*sqrt(6)*I

我尝试将输入表达式转换为字符串,去掉 '.0 ' 然后执行一段代码将其返回到 sympy.core 类型.add.Mul,通常与其他字符串一起使用,但变量expression仍然是一个字符串。

expression = str(input_expression).replace('.0 ', '')
exec(f'expression = {expression}')

我如何摆脱表达式中 float 的冗余使用,同时保持其 sympy.core.add.Mul 类型,以便我的函数提供良好的输出?

P.S. 0.7071067811865471/sqrt(2) 的近似值。这个数字出现在第二个输出中的事实意味着我的函数运行正常,只是没有以所需的方式输出。

编辑:无论出于何种原因,取消缩进和摆脱整个功能,将代码作为自己的程序运行会提供预期的输出。只有当代码是函数形式时,它才不起作用。

请求代码:

from IPython.display import display, Math
from sympy.abc import *
from sympy import *

def imaginary_square_root(x, y):
return(sqrt((x + sqrt(x**2 + y**2)) / (2)) + I*((y*sqrt(2)) / (2*sqrt(x + sqrt(x**2 + y**2))))) # calculates the square root of a complex number

def find_imaginary_square_root(polynomial): # 'polynomial' used because this function is meant to change expressions including variables such as 'x'
polynomial = str(polynomial).replace('.0 ', ' ')
exec(f'polynomial = {polynomial}')

list_of_square_roots = [] # list of string instances of square roots and their contents
list_of_square_root_indexes = [] # list of indexes at which the square roots can be found in the string
polynomial_string = str(polynomial)
temp_polynomial_string = polynomial_string # string used and chopped up, hence the prefix 'temp_...'
current_count = 0 # counter variable used for two seperate jobs

while 'sqrt' in temp_polynomial_string: # gets indexes of every instance of 'sqrt'
list_of_square_root_indexes.append(temp_polynomial_string.index('sqrt') + current_count)
temp_polynomial_string = temp_polynomial_string[list_of_square_root_indexes[-1] + 4:]
current_count += list_of_square_root_indexes[-1] + 4

for square_root_location in list_of_square_root_indexes:
current_count = 1 # second job for 'current_count'

for index, char in enumerate(polynomial_string[square_root_location + 5:]):
if char == '(':
current_count += 1

elif char == ')':
current_count -= 1

if not current_count: # when current_count == 0, we know that the end of the sqrt contents have been reached
list_of_square_roots.append(polynomial_string[square_root_location:square_root_location + index + 6]) # adds the square root with contents to a list
break

for individual_square_root in list_of_square_roots:
if individual_square_root in str(polynomial):
evaluate = individual_square_root[5:-1]
x = re(evaluate)
y = im(evaluate)

polynomial = polynomial.replace(eval(individual_square_root), imaginary_square_root(x, y)) # replace function used here is Sympy's replace function for polynomials

return polynomial

poly = str(-sqrt(-2.0 + 2*sqrt(3)*I))

display(Math(latex(find_imaginary_square_root(poly))))

最佳答案

你到底想完成什么?我还是不明白。你有一整 block 代码。试试这个:

from sympy import *

def parse(expr): print(simplify(expr).evalf().nsimplify())

parse(-sqrt(-2.0 + 2*sqrt(3)*I))
-1 - sqrt(3)*I

关于python - 将字符串中的所有冗余 float 转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59296496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com