我已经编写了一个解决方程组的函数,但是当我的解决方案中有一个平方根时,它确实不起作用。只要没有平方根,代码就可以用于其他方程。
我收到以下错误
TypeError: No loop matching the specified signature and casting
was found for ufunc solve1
我可以计算sqrt并得到一个十进制数,但我不想要那个。我需要用完整的数字进行计算,我宁愿让它返回sqrt(5)而不是2.236067977
我目前正在尝试解决以下重现关系
eqs :=
[
s(n) = s(n-1)+s(n-2),
s(0) = 1,
s(1) = 1
];
我已经记下了我的输出并且在这里顺着步骤。它适用于没有平方根的方程。如何让linalg与sqrt一起使用,还是应该使用不同的方法?
def solve_homogeneous_equation(init_conditions, associated):
# Write down characteristic equation for r
Returns eq_string = r^2-1*r^(2-1)-+1*r^(2-2)
# Find the roots for r
Returns r_solutions = [1/2 + sqrt(5)/2, -sqrt(5)/2 + 1/2]
# Write down general solution (for solver)
Returns two lists, one with variables and one with outcomes
general_solution_variables = [[1, 1], [1/2 + sqrt(5)/2, -sqrt(5)/2 + 1/2]]
general_solution_outcomes = [1, 1]
# Solve the system of equations
THIS IS WHERE THE ERROR OCCURS
solution = np.linalg.solve(general_solution_variables, general_solution_outcomes)
# Write the solution
This is where I rewrite the general solution with found solutions
原始函数在此定义,以防您想深入了解代码
def solve_homogeneous_equation(init_conditions, associated):
print("Starting solver")
# Write down characteristic equation for r
eq_length = len(init_conditions)
associated[0] = str('r^' + str(eq_length))
for i in range(eq_length, 0, -1):
if i in associated.keys() :
associated[i] = associated[i] + str('*r^(') + str(eq_length) + str('-') + str(i) + str(')')
print("Associated equation: " + str(associated))
eq_string = ''
for i in range(0, eq_length+1, 1):
if i in associated.keys():
if i < eq_length:
eq_string = eq_string + associated[i] + '-'
else:
eq_string = eq_string + associated[i]
print("Equation: " + eq_string)
# Find the roots for r
r_symbol = sy.Symbol('r')
r_solutions = sy.solve(eq_string, r_symbol)
r_length = len(r_solutions)
print("Solutions: " + str(r_solutions))
print("Eq length: " + str(eq_length) + " ; Amount of solutions: " + str(r_length))
# If equation length is equal to solutions
if eq_length == r_length:
# Write down general solution (for solver)
general_solution_variables = []
general_solution_outcomes = []
for i in range(0, eq_length):
general_solution_variables_single = []
for j in range(0, eq_length + 1):
if j != eq_length:
k = r_solutions[j]**i
general_solution_variables_single.append(k)
if j == eq_length:
k = init_conditions[i]
general_solution_outcomes.append(int(k))
general_solution_variables.append(general_solution_variables_single)
print("General solution variables: " + str(general_solution_variables))
print("General solution outcomes: " + str(general_solution_outcomes))
# Solve the system of equations
solution = np.linalg.solve(general_solution_variables, general_solution_outcomes)
print("Solutions: " + str(solution))
# Write the solution
solution_full = ""
for i in range(0, eq_length):
if i > 0:
solution_full = solution_full + " + "
solution_full = solution_full + str(int(solution[i])) + "*" + str(int(r_solutions[i])) + "^n"
print("Solved equation: " + solution_full)
return(solution_full)
# If equation length is not equal to solutions
elif eq_length > r_length:
print("NonEqual")
return 0
答案 0 :(得分:0)
我没有努力阅读你的代码。我注意到您可以使用sympy以符号方式解决该系统。
public class Coursenode {
private Course data;
private Coursenode next;
public Coursenode() {
this.data = null;
this.next = null;
}
public Coursenode(Course course, Coursenode next) {
this.data=course;
this.next= next;
}
public Coursenode(Coursenode x) {
this.data = x.getData();
this.next = x.getNext();
}
public Course getData() {
return data;
}
public void setData(Course data) {
this.data = data;
}
public Coursenode getNext() {
return next;
}
public void setNext(Coursenode next) {
this.next = next;
}
//Clone method
public void clone(Coursenode new_cn){
new_cn = new Coursenode (this.getData(),this.getNext());
}
}