Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check to see if a function in R is a constant function?

An R function is passed to me, which is defined on some interval of the Real Line and returns a numeric value. Is there a way to check to see if the function is constant?

Example functions:

f1<-function(x) {11}
f2<-function(x) {x+2}
f3<-function(x) {1+1}
f4<-function(x) {return(3)}

I am looking for a test that will say that f1,f3,f4 are constant functions but f2 isn't. Any Ideas?

Edit:

Frank's and Gregor's (edit: and Michael Lawrence's second solution)solutions below all work for all the 4 test cases given above (Marat's and Michael's don't work on all 4 cases). So there is already solutions. But extra bonus points if you can find a solution which also gives the right answer for the following 3 test functions:

f5 <- function(x) ifelse(x == 5.46512616432116, 0, 1)
f6 <- function(x) ifelse(x == 5.46512616432116, 0, 0)
f7 <- function(x) {x - x}
like image 590
patapouf_ai Avatar asked May 21 '15 16:05

patapouf_ai


People also ask

How do you know if a function is a constant function?

Mathematically speaking, a constant function is a function that has the same output value no matter what your input value is. Because of this, a constant function has the form y = b, where b is a constant (a single value that does not change). For example, y = 7 or y = 1,094 are constant functions.

What is a constant function and how do you know if a function is constant?

A constant function is a function which takes the same value for f(x) no matter what x is. When we are talking about a generic constant function, we usually write f(x) = c, where c is some unspecified constant. Examples of constant functions include f(x) = 0, f(x) = 1, f(x) = π, f(x) = −0.

What does it mean for a function to be constant?

A constant function is a linear function for which the range does not change no matter which member of the domain is used. f(x1)=f(x2) for any x1 and x2 in the domain. With a constant function, for any two points in the interval, a change in x results in a zero change in f(x) .


2 Answers

Try functionBody:

> is.numeric(functionBody(f1)[[2]])
[1] TRUE

> is.numeric(functionBody(f2)[[2]])
[1] FALSE
like image 152
Marat Talipov Avatar answered Oct 15 '22 12:10

Marat Talipov


These code-based tests are clever and fun to see, but so far I think the "try a bunch of numbers" approach may be a more powerful test, depending on the type of functions you might get and whether you care more about Type I or Type II errors in your identification.

In your question, you say

which is defined on some interval of the Real Line

So let's assume we know the domain of interest. Sample some number of points on that domain, and test your function.

n = 1e5
test = runif(n, min = 0, max = 5)
results = f(test) # sapply(test, f) if f isn't vectorized

# test for constancy
all(results == results[1]) # or all(diff(results) == 0) or however else

Any function that is truly a constant function will pass this test just fine, no matter how pathological---this will not be true for any of the other methods suggested so far. However, it is quite easy to fool the test with the example I left in the comments (or anything of this sort)

f3 = function(x) ifelse(x == 5.46512616432116, 0, 1)
like image 37
Gregor Thomas Avatar answered Oct 15 '22 13:10

Gregor Thomas