查找多项式的导数 [英] finding the derivative of a polynomial

查看:103
本文介绍了查找多项式的导数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我象征性地想知道如何将多项式解析为一个函数并返回导数.我将使用哪种数据结构或方法来解析多项式?最好不要使用任何库,因为这个问题可能会在技术面试中弹出.

I wondering symbolically how you would parse a polynomial into a function and return the derivative. What data structure would I use or method to parse the polynomial? Preferably without using any libraries, as this question could pop up in a technical interview.

polynomial-> of nth degree

def derivative(polynomial):
    return derivative

Example:

f(x)  = 2x^2+3x+1
f'(x) = 4x+3

我不想要解决方案,这不是功课,而是关于我将从何处开始的提示.

I don't want a solution, this is not homework but a hint of where I would start.

推荐答案

单个变量中的多项式可以简单地表示为包含系数的数组.因此,例如1 + 5x 3 -29x 5 可以表示为[1, 0, 0, 5, 0, -29].以这种形式表示的导数易于计算.

A polynomial in a single variable can be represented simply as an array containing the coefficients. So for example 1 + 5x3 - 29x5 can be expressed as [1, 0, 0, 5, 0, -29]. Expressed in this form the derivative is easy to compute.

假设poly是上面的python列表.然后

suppose poly is a python list as above. Then

deriv_poly = [poly[i] * i for i in range(1, len(poly))]

对于稀疏多项式,其他表示形式相对容易一些,例如成对的列表(coefficient, exponent)或字典将指数映射到系数.

For sparse polynomial other representations are relatively easy, such as a list of pairs (coefficient, exponent) or dictionary mapping exponents to coefficients.

解析表达式更加复杂,但是使用各种解析框架应该很容易,因为语法相对简单.

Parsing the expression is more complicated but using various parsing frameworks should be easy because the grammar is comparatively simple.

这篇关于查找多项式的导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆