【问题标题】:Solving system of linear equations with square roots求解具有平方根的线性方程组
【发布时间】:2018-09-23 13:31:19
【问题描述】:

假设我有一个带有平方根的线性方程组

  1  1  |  1  
  (1/2 + sqrt(5) / 2)  (1/2 - sqrt(5) / 2)  | 1

使用 np.linalg.solve 来求解我通常会做的这个方程组

vars = [[1, 1], [1/2 + sqrt(5)/2, -sqrt(5)/2 + 1/2]]
outcomes = [1, 1]
solution = np.linalg.solve(vars, outcomes) 
#solution has to be only whole numbers, no crazy decimals. Preferably in the following form
[ sqrt(x), sqrt(y) ]

但是这会返回错误,因为它不知道如何处理 sqrt()。我怎样才能用平方根求解这个方程组并得到完整的数字,所以没有小数?

【问题讨论】:

  • 如果你想使用sqrt你必须从math导入。您想如何将解表示为整数的平方根?尝试先手动解决。
  • Numpy 是用于进行数值计算的包,不支持符号计算。你应该看看Sympy

标签: python numpy matrix linear-algebra gaussian


【解决方案1】:

我不确定是否理解您的问题,但是您可以让脚本像这样工作:

import math
import numpy as np

vars = [[1, 1], [1/2 + math.sqrt(5)/2, -math.sqrt(5)/2 + 1/2]]
outcomes = [1, 1]
solution = np.linalg.solve(vars, outcomes) 

print("Solutions:", solution)

【讨论】:

  • “解决方案没有小数”?嗯?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 2021-12-13
相关资源
最近更新 更多