Evaluating polynomial with function
Im trying to evaluate a polynomial P(x):
However i dont think im entering the polynomial itself correctly in my function.
Code for function:
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)
Code for using function:
directpoly(x=seq(-10,10, length=5000000), rep(c(2,-1),20))
Any ideas on how to enter it correctly?
r polynomials
add a comment |
Im trying to evaluate a polynomial P(x):
However i dont think im entering the polynomial itself correctly in my function.
Code for function:
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)
Code for using function:
directpoly(x=seq(-10,10, length=5000000), rep(c(2,-1),20))
Any ideas on how to enter it correctly?
r polynomials
1
Tryseqcoef = seq_along(coef)
. This means you will need to include the zero coefficients in the vectorcoef
.
– Rui Barradas
Nov 13 '18 at 12:22
Thx! Can you see if im using the function correctly, so when evaluating P(x) with my function, is P(x) then correctly typed in?
– torvin
Nov 13 '18 at 12:27
add a comment |
Im trying to evaluate a polynomial P(x):
However i dont think im entering the polynomial itself correctly in my function.
Code for function:
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)
Code for using function:
directpoly(x=seq(-10,10, length=5000000), rep(c(2,-1),20))
Any ideas on how to enter it correctly?
r polynomials
Im trying to evaluate a polynomial P(x):
However i dont think im entering the polynomial itself correctly in my function.
Code for function:
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)
Code for using function:
directpoly(x=seq(-10,10, length=5000000), rep(c(2,-1),20))
Any ideas on how to enter it correctly?
r polynomials
r polynomials
asked Nov 13 '18 at 12:04
torvintorvin
284
284
1
Tryseqcoef = seq_along(coef)
. This means you will need to include the zero coefficients in the vectorcoef
.
– Rui Barradas
Nov 13 '18 at 12:22
Thx! Can you see if im using the function correctly, so when evaluating P(x) with my function, is P(x) then correctly typed in?
– torvin
Nov 13 '18 at 12:27
add a comment |
1
Tryseqcoef = seq_along(coef)
. This means you will need to include the zero coefficients in the vectorcoef
.
– Rui Barradas
Nov 13 '18 at 12:22
Thx! Can you see if im using the function correctly, so when evaluating P(x) with my function, is P(x) then correctly typed in?
– torvin
Nov 13 '18 at 12:27
1
1
Try
seqcoef = seq_along(coef)
. This means you will need to include the zero coefficients in the vector coef
.– Rui Barradas
Nov 13 '18 at 12:22
Try
seqcoef = seq_along(coef)
. This means you will need to include the zero coefficients in the vector coef
.– Rui Barradas
Nov 13 '18 at 12:22
Thx! Can you see if im using the function correctly, so when evaluating P(x) with my function, is P(x) then correctly typed in?
– torvin
Nov 13 '18 at 12:27
Thx! Can you see if im using the function correctly, so when evaluating P(x) with my function, is P(x) then correctly typed in?
– torvin
Nov 13 '18 at 12:27
add a comment |
1 Answer
1
active
oldest
votes
Here are two ways of writing your function.
- With a
sapply
loop. Makes the code more readable and some times faster. - With a standard
for
loop.
There is no need to write a separate function outside the function to be called with the x
values, I have placed that function in the body of the main functions.
Then both functions are tested, with a smaller input vector. As you can see, the for
loop function is faster.
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)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280664%2fevaluating-polynomial-with-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here are two ways of writing your function.
- With a
sapply
loop. Makes the code more readable and some times faster. - With a standard
for
loop.
There is no need to write a separate function outside the function to be called with the x
values, I have placed that function in the body of the main functions.
Then both functions are tested, with a smaller input vector. As you can see, the for
loop function is faster.
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)
add a comment |
Here are two ways of writing your function.
- With a
sapply
loop. Makes the code more readable and some times faster. - With a standard
for
loop.
There is no need to write a separate function outside the function to be called with the x
values, I have placed that function in the body of the main functions.
Then both functions are tested, with a smaller input vector. As you can see, the for
loop function is faster.
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)
add a comment |
Here are two ways of writing your function.
- With a
sapply
loop. Makes the code more readable and some times faster. - With a standard
for
loop.
There is no need to write a separate function outside the function to be called with the x
values, I have placed that function in the body of the main functions.
Then both functions are tested, with a smaller input vector. As you can see, the for
loop function is faster.
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)
Here are two ways of writing your function.
- With a
sapply
loop. Makes the code more readable and some times faster. - With a standard
for
loop.
There is no need to write a separate function outside the function to be called with the x
values, I have placed that function in the body of the main functions.
Then both functions are tested, with a smaller input vector. As you can see, the for
loop function is faster.
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)
answered Nov 13 '18 at 14:10
Rui BarradasRui Barradas
16.4k51730
16.4k51730
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280664%2fevaluating-polynomial-with-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Try
seqcoef = seq_along(coef)
. This means you will need to include the zero coefficients in the vectorcoef
.– Rui Barradas
Nov 13 '18 at 12:22
Thx! Can you see if im using the function correctly, so when evaluating P(x) with my function, is P(x) then correctly typed in?
– torvin
Nov 13 '18 at 12:27