在找导数的时候,如何使用过滤函数只return只导数不乘零的项,在python3中?
When finding derivatives, how do you use the filter function to return only the terms whose derivatives are not multiplied by zero, in python 3?
我写了一个函数,给定方程的项,它可以求导数。但是,当其中一项为零时,函数就会崩溃。我将如何使用过滤器来确保乘以零的项不 return?
这是我的基准代码,它可以工作但还不包括过滤器:
def find_derivative(function_terms):
return [(function_terms[0][0]*function_terms[0][1], function_terms[0][1]-1),(function_terms[1][0]*function_terms[1][1], function_terms[1][1]-1)]
function_terms[1][1]-1将导数项的幂减1。
它是这样工作的。
输入:
# Represent each polynomial term with a tuple of (coefficient, power)
# f(x) = 4 x^3 - 3 x
four_x_cubed_minus_three_x = [(4, 3), (-3, 1)]
find_derivative(four_x_cubed_minus_three_x)
输出:
[(12, 2), (-3, 0)]
这是12 x^2 - 3
的正确答案
但在这里它崩溃了:
输入:
# f(x) = 3 x^2 - 11
three_x_squared_minus_eleven = [(3, 2), (-11, 0)]
find_derivative(three_x_squared_minus_eleven)
给定方程,应该求导数。
输出:
((6, 1), (0, -1))
这有一个"ghost"项0 * x^(-1)
;我不想打印这个词。
预期输出:
[(6, 1)]
您可以使用 filter()
函数过滤元组列表,然后在过滤后的列表上应用逻辑。这样的东西应该可以工作。
filtered_terms = list(filter(lambda x: x[1]!=0, function_terms))
现在你有了没有常量的元组。因此,与其对导数进行硬编码,不如尝试循环遍历列表以获取导数。
result = []
for term in filtered_terms:
result.append((term[0]*term[1], term[1]-1))
return result
python 中有一个名为 sympy 的符号数学求解器。或许对你有用。
from sympy import *
x = symbols('x')
init_printing(use_unicode=True)
equation = 4*x**3 -3*x
diff_equation = equation.diff()
solution = diff_equation.subs({x:2})
两个变化:
- 让您的例程遍历多项式项,一次处理一项,而不是依赖于恰好有两项。
- 遇到单个术语时对其应用过滤。
我将其扩展为还消除了系数为零以及指数为零的所有内容。我添加了一个测试用例,其中 和 都是负指数,因为符号微分定理同样适用。
def find_derivative(function_terms):
return [(term[0]*term[1], term[1]-1)
for i, term in enumerate(function_terms)
if term[0] * term[1] != 0 ]
four_x_cubed_minus_three_x = [(4, 3), (-3, 1)]
print(find_derivative(four_x_cubed_minus_three_x) )
three_x_squared_minus_eleven = [(3, 2), (-11, 0)]
print(find_derivative(three_x_squared_minus_eleven) )
fifth_degree = [(1, 5), (-1, 4), (0, 3), (8, 2), (-16, 0), (1, -2)]
print(find_derivative(fifth_degree) )
输出:
[(12, 2), (-3, 0)]
[(6, 1)]
[(5, 4), (-4, 3), (16, 1), (-2, -3)]
我写了一个函数,给定方程的项,它可以求导数。但是,当其中一项为零时,函数就会崩溃。我将如何使用过滤器来确保乘以零的项不 return?
这是我的基准代码,它可以工作但还不包括过滤器:
def find_derivative(function_terms):
return [(function_terms[0][0]*function_terms[0][1], function_terms[0][1]-1),(function_terms[1][0]*function_terms[1][1], function_terms[1][1]-1)]
function_terms[1][1]-1将导数项的幂减1。
它是这样工作的。
输入:
# Represent each polynomial term with a tuple of (coefficient, power)
# f(x) = 4 x^3 - 3 x
four_x_cubed_minus_three_x = [(4, 3), (-3, 1)]
find_derivative(four_x_cubed_minus_three_x)
输出:
[(12, 2), (-3, 0)]
这是12 x^2 - 3
但在这里它崩溃了:
输入:
# f(x) = 3 x^2 - 11
three_x_squared_minus_eleven = [(3, 2), (-11, 0)]
find_derivative(three_x_squared_minus_eleven)
给定方程,应该求导数。
输出:
((6, 1), (0, -1))
这有一个"ghost"项0 * x^(-1)
;我不想打印这个词。
预期输出: [(6, 1)]
您可以使用 filter()
函数过滤元组列表,然后在过滤后的列表上应用逻辑。这样的东西应该可以工作。
filtered_terms = list(filter(lambda x: x[1]!=0, function_terms))
现在你有了没有常量的元组。因此,与其对导数进行硬编码,不如尝试循环遍历列表以获取导数。
result = []
for term in filtered_terms:
result.append((term[0]*term[1], term[1]-1))
return result
python 中有一个名为 sympy 的符号数学求解器。或许对你有用。
from sympy import *
x = symbols('x')
init_printing(use_unicode=True)
equation = 4*x**3 -3*x
diff_equation = equation.diff()
solution = diff_equation.subs({x:2})
两个变化:
- 让您的例程遍历多项式项,一次处理一项,而不是依赖于恰好有两项。
- 遇到单个术语时对其应用过滤。
我将其扩展为还消除了系数为零以及指数为零的所有内容。我添加了一个测试用例,其中 和 都是负指数,因为符号微分定理同样适用。
def find_derivative(function_terms):
return [(term[0]*term[1], term[1]-1)
for i, term in enumerate(function_terms)
if term[0] * term[1] != 0 ]
four_x_cubed_minus_three_x = [(4, 3), (-3, 1)]
print(find_derivative(four_x_cubed_minus_three_x) )
three_x_squared_minus_eleven = [(3, 2), (-11, 0)]
print(find_derivative(three_x_squared_minus_eleven) )
fifth_degree = [(1, 5), (-1, 4), (0, 3), (8, 2), (-16, 0), (1, -2)]
print(find_derivative(fifth_degree) )
输出:
[(12, 2), (-3, 0)]
[(6, 1)]
[(5, 4), (-4, 3), (16, 1), (-2, -3)]