用函数求多项式

时间:2018-11-13 12:04:32

标签: r polynomials

我正在尝试评估多项式P(x): polynomial

但是我不认为我在函数中正确输入了多项式本身。 功能代码:

<resources>

<!-- Dark application theme. -->
<style name="AppTheme.Dark" parent="Theme.MaterialComponents.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="toolbarBackground">@color/darkBackground</item>
    <item name="toolbarStyle">@style/AppTheme.Dark.Toolbar</item>
    <item name="iconColor">#fff</item>
    <item name="dividerBackground">@android:drawable/divider_horizontal_dark</item>

    <item name="android:statusBarColor">@android:color/black</item>
</style>

<style name="AppTheme.Dark.Toolbar" parent="Widget.AppCompat.Toolbar">
    <item name="android:background">@color/darkBackground</item>
    <item name="android:elevation">8dp</item>
</style>



<!-- Light application theme. -->
<style name="AppTheme.Light" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="toolbarBackground">@color/lightBackground</item>
    <item name="toolbarStyle">@style/AppTheme.Light.Toolbar</item>
    <item name="iconColor">#000</item>
    <item name="dividerBackground">@android:drawable/divider_horizontal_bright</item>

    <item name="android:datePickerDialogTheme">@style/AppTheme.Light.DatePicker</item>

    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>

<style name="AppTheme.Light.DatePicker" parent="ThemeOverlay.MaterialComponents.Dialog">
    <item name="materialButtonStyle">?android:attr/borderlessButtonStyle</item>
</style>

<style name="AppTheme.Light.Toolbar" parent="Widget.MaterialComponents.Toolbar">
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:elevation">8dp</item>
</style>

使用功能的代码:

directpoly1 <- function(x, coef, seqcoef = seq(coef) - 1) {
  sum(coef*x^seqcoef)
}
directpoly <- function(x, coef) {
  seqcoef <- seq(coef) - 1
  sapply(x, directpoly1, coef, seqcoef)
}

关于如何正确输入的任何想法?

1 个答案:

答案 0 :(得分:0)

这里有两种编写函数的方法。

  1. 具有sapply循环。使代码更具可读性,并且速度更快。
  2. 具有标准的for循环。

无需在要使用x值调用的函数之外编写单独的函数,我已将该函数放置在主要函数的主体中。
然后使用较小的输入向量测试两个函数。如您所见,for循环函数更快。

directpoly <- function(x, n = 39, coef = NULL){
  f <- function(y) sum(coef * y^seqcoef)
  if(all(is.null(coef))) coef <- rep(c(2, -1), length.out = n + 1)
  seqcoef <- rev(seq_along(coef) - 1)
  sapply(x, f)
}

directpoly2 <- function(x, n = 39, coef = NULL){
  f <- function(y) sum(coef * y^seqcoef)
  if(all(is.null(coef))) coef <- rep(c(2, -1), length.out = n + 1)
  seqcoef <- rev(seq_along(coef) - 1)
  y <- numeric(length(x))
  for(i in seq_along(y))
    y[i] <- f(x[i])
  y
}

library(microbenchmark)
library(ggplot2)

x <- seq(-10, 10, length = 50000)
mb <- microbenchmark(
  Sapply = directpoly(x),
  Forloop = directpoly2(x)
)

autoplot(mb)

enter image description here