如何从多项式方程中求导数?
How to obtain derivative from a polynomial equation?
我用自己的方法来解决多项式问题的导数只是为了好玩。但是我想出了一个问题。我无法从 array_1 = '3*x**3 + 2*x**2 + 3*x + 5'
中删除或删除 '*x**2'
来获得这样的数组; array = [3, 2, 3, 5]
.
array_1 = '3*x**3 + 2*x**2 + 3*x + 5'
def converter(array_1):
array_1 = array_1.split(' + ')
return str(array_1)
array = [5, 3, 2] # for testing ( I reversed order of an array.)
def derivative(array):
new_array = []
for x, elem in enumerate(array):
z = x*elem
new_array.append(z)
return new_array[1:]
result = derivative(array)
print(result)
print(converter(array_1))
您的 array
只是一个字符串——它只是一个字符序列。所以每个 elem
只是一个字符。你需要做的是编写一个 parser.
对于这么简单的事情,您可以仅使用字符串操作来解析所有内容,或者 regular expressions 如果您知道如何使用它们或想阅读有关它们的教程。这是一个仅包含 str
class:
方法的示例
s = '3*x**3 + 2*x**2 + 3*x + 5'
for term in s.split('+'):
coefficient, _, factor = term.partition('*')
variable, power = factor.partition('**')
do_something_with(coefficient, variable, power)
(当然,您可能想在 do_something_with
代码中将 coefficient
和 power
转换为数字,但这很容易;只需使用 int
或 float
.)
但这会有些脆弱。它将处理您拥有的确切格式(只要 do_something_with
可以处理额外的空格,以及 variable
和 power
的空字符串——只需调用 int
或 float
可以处理前者,但不能处理后者)。但是,如果您尝试使用 3 * x**3 + 2 * x**2 + 3 * x + 5
,它将失败。
更好的选择是使用解析或 parser-generating 库。有一点学习曲线,但值得做。例如,pyparsing
is a reasonably easy one to learn, there are some great tutorials out there for it, and it comes with extensive examples,我认为其中一个与您正在做的非常接近。
最后,您的格式恰好是 Python 语法的子集。您可以通过使用 ast
,Python 附带的 Python 解析器来利用它。但是,这不完全是 novice-friendly 选项。
这是我所做的。我知道它不起作用。我会努力改进它。
array_1 = '3*x**3 + 2*x**2 + 3*x + 5'
def converter(array_1):
array_1 = array_1.split(' + ')
print(array_1)
new_array = []
for x in array_1:
new_array.append(x[-0])
return new_array
array = [5, 3, 2] # for testing
def derivative(array):
new_array = []
for x, elem in enumerate(array):
z = x*elem
new_array.append(z)
return new_array[1:]
result = derivative(array)
print(result)
print(converter(array_1))
输出:
[3, 4]
['3*x**3', '2*x**2', '3*x', '5']
['3', '2', '3', '5']
我用自己的方法来解决多项式问题的导数只是为了好玩。但是我想出了一个问题。我无法从 array_1 = '3*x**3 + 2*x**2 + 3*x + 5'
中删除或删除 '*x**2'
来获得这样的数组; array = [3, 2, 3, 5]
.
array_1 = '3*x**3 + 2*x**2 + 3*x + 5'
def converter(array_1):
array_1 = array_1.split(' + ')
return str(array_1)
array = [5, 3, 2] # for testing ( I reversed order of an array.)
def derivative(array):
new_array = []
for x, elem in enumerate(array):
z = x*elem
new_array.append(z)
return new_array[1:]
result = derivative(array)
print(result)
print(converter(array_1))
您的 array
只是一个字符串——它只是一个字符序列。所以每个 elem
只是一个字符。你需要做的是编写一个 parser.
对于这么简单的事情,您可以仅使用字符串操作来解析所有内容,或者 regular expressions 如果您知道如何使用它们或想阅读有关它们的教程。这是一个仅包含 str
class:
s = '3*x**3 + 2*x**2 + 3*x + 5'
for term in s.split('+'):
coefficient, _, factor = term.partition('*')
variable, power = factor.partition('**')
do_something_with(coefficient, variable, power)
(当然,您可能想在 do_something_with
代码中将 coefficient
和 power
转换为数字,但这很容易;只需使用 int
或 float
.)
但这会有些脆弱。它将处理您拥有的确切格式(只要 do_something_with
可以处理额外的空格,以及 variable
和 power
的空字符串——只需调用 int
或 float
可以处理前者,但不能处理后者)。但是,如果您尝试使用 3 * x**3 + 2 * x**2 + 3 * x + 5
,它将失败。
更好的选择是使用解析或 parser-generating 库。有一点学习曲线,但值得做。例如,pyparsing
is a reasonably easy one to learn, there are some great tutorials out there for it, and it comes with extensive examples,我认为其中一个与您正在做的非常接近。
最后,您的格式恰好是 Python 语法的子集。您可以通过使用 ast
,Python 附带的 Python 解析器来利用它。但是,这不完全是 novice-friendly 选项。
这是我所做的。我知道它不起作用。我会努力改进它。
array_1 = '3*x**3 + 2*x**2 + 3*x + 5'
def converter(array_1):
array_1 = array_1.split(' + ')
print(array_1)
new_array = []
for x in array_1:
new_array.append(x[-0])
return new_array
array = [5, 3, 2] # for testing
def derivative(array):
new_array = []
for x, elem in enumerate(array):
z = x*elem
new_array.append(z)
return new_array[1:]
result = derivative(array)
print(result)
print(converter(array_1))
输出:
[3, 4]
['3*x**3', '2*x**2', '3*x', '5']
['3', '2', '3', '5']