下面是一个示例脚本,用于计算多项式的导数和根。我没有包括csv读/写,因为我不确定您使用的确切格式。
from sympy import Symbol, Poly
# Define symbols
x = Symbol("x")
# Add csv reading here
input_rows = [
[2.234, 0, 0.523, 2.3123, 4.123],
[2, 2, 2, 2, 2]]
output_rows = []
# Iterate over each row
for r in input_rows:
# Create polynomial from coefficients
y = Poly(r, x)
print(y)
# 1st derivative and its roots
y_dx = y.diff(x)
y_dx_roots = y_dx.nroots()
# 2nd derivative and its roots
y_ddx = y_dx.diff(x)
y_ddx_roots = y_ddx.nroots()
# Write results to list of dicts
output_rows.append({
"1st deriv": y_dx.all_coeffs(),
"2nd deriv": y_ddx.all_coeffs(),
"1st deriv roots": y_dx_roots,
"2nd deriv roots": y_ddx_roots})
print(*output_rows, sep="\n")