- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试生成具有 11 个系数的 10 阶多项式的值。我也在尝试生成它的衍生物。我写了如下所示的三个函数。此代码生成多项式的值。a1 到 a10 是系数。
float polynm( float a0,float a1,float a2,float a3,float a4,float a5,float a6,float a7,float a8,float a9,float a10,float x)
{
float poly = a0 + a1*x + a2*pow(x,2)+a3*pow(x,3)+a4*pow(x,4)+a5*pow(x,5)+a6*pow(x,6)+a7*pow(x,7)+a8*pow(x,8)+a9*pow(x,9)+a10*pow(x,10);
return poly;
}
此代码生成它调用函数 deri 的多项式的导数
float polynm_der(float a0,float a1,float a2,float a3,float a4,float a5,float a6,float a7,float a8,float a9,float a10,float x)
{ float der = a1 + a2*deri(x,2)+a3*deri(x,3)+a4*deri(x,4)+a5*deri(x,5)+a6*deri(x,6)+a7*deri(x,7)+a8*deri(x,8)+a9*deri(x,9)+a10*deri(x,10);
return der;
}
deri is below
float deri(float x,int n)
{
float term_der = n*pow(x,n-1);
return term_der;
}
多项式的代码效率低下。如果我想生成一个 100 阶多项式,那将变得不可能。有没有一种方法可以递归地生成多项式及其导数以避免笨拙的代码。
最佳答案
一个解决方案是接受一个系数数组及其长度:
float poly(int x, float coefficients[], int order)
{
int idx;
float total;
for (idx = 0; idx < order; idx++)
total += coefficients[idx] * pow(x, idx);
return total;
}
递归解决方案会很漂亮,但这不是 Lisp。无论如何,类似的方法可以用于衍生品。请记住,在 C 语言中,函数的数组参数会变成指针,因此您不能使用 sizeof
等很酷的东西来获取它们的长度。
编辑:作为对评论的回应,您可以在构造coefficients
数组时强制执行您的要求。或者,如果您不负责该代码,则可以像这样(笨拙地)将其粘贴到函数中:
if (coefficients[0] == 0 || coefficients[1] == 0 || coefficients[order-1] == 0)
assert(0);
关于c - 在 C 中生成 10 阶多项式及其导数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5395533/
我正在做一些关于大 O 表示法的练习题,遇到了这个问题。什么是函数 𝑓(𝑛) = 𝑛^2 + 𝑛 log2(𝑛) + log2(𝑛) 的大 O 阶。展示你的作品。 我的答案是 O(n^2)
是2n吗?只是检查。 最佳答案 术语 B 树的顺序在文献中的定义并不一致。 (例如,参见 terminology section of Wikipedia's article on B-Trees )
我想使用 numpy 创建一个 3 列数组,使得该数组类似于一堆 9x9 2 列数组。这些数组中的每一个都将完全填充有 1、2、3 等。 所以,看立方体的一面,我们看到的是 1,而另一面则是 9。然后
我想将这些数据存储到顺序为 3 (10,20,30,40,50,60,70,80,90) 的 B 树中,我的结果是 并且它与我的书的结果不匹配。可以吗?谢谢:) 最佳答案 这取决于你的意思 Is it
我是 numpy 的新手。创建一个新数组并用一定范围内的随机数填充每个元素的最佳方法是什么? 例如,我想要一个 3×3 数组,其中每个元素都是 0 或 1。 最佳答案 尝试类似的东西 np.rando
我正在尝试学习设计 btree。 以下是开发 5 阶 btree 的值。 1,12,8,2,25,6,14,28,17,7,52,16,48,68,3,26,29,53,55,45,67。 当我插入
我有一个 pandas 数据框,其特征值非常小,数量级为 -322。我正在尝试标准化这些功能,但得到了 ValueError: Input contains NaN, infinity or a va
我是一名优秀的程序员,十分优秀!