I was working on a binomial expansion in R, I came across some issues and I feel the values do not make sense. Here is my code, I used factorial and combination from "scratch" to compute. I tried x=6, y=2 and n=4 I got 2784 as an answer. If I try 1 it gives 0. If n=i I get infinity because the denominator would equal zero
fact=1
for(i in 1:n){
fact=fact*i
}
return(fact)
}
Combi<-function(n,r){
result=f(n)/(f(r)*f(n-r))
return(result)
}
Combi(6,4)
expand.binomial<-function(x,y,n){
sumz=0
for(i in 1:n){
if(i==n){
break
}
sumz=sumz+Combi(n,i)*(x**i)*(y**(n-i))
}
return(sumz)
}
You should be aware of that, 0! is 1. In this case, f should be defined like below
f <- function(n) {
if (n == 0) {
return(1)
}
fact <- 1
for (i in 1:n) {
fact <- fact * i
}
return(fact)
}
Also, in expand.binomial, the exponents should start from 0 to n, i.e.,
expand.binomial <- function(x, y, n) {
sumz <- 0
for (i in 0:n) {
sumz <- sumz + Combi(n, i) * (x**i) * (y**(n - i))
}
return(sumz)
}
Test
> expand.binomial(6, 2, 4)
[1] 4096
> expand.binomial(6, 2, 1)
[1] 8
> expand.binomial(6, 2, 0)
[1] 1
Related
I have to functions f, and g with known initial values for f(1), and g(1) and their subsequent values are obtained trough iteration according to: f(n+1)=f(n)\cos(g(n))-g(n)\sin(g(n)) and g(n+1)=f(n)\cos(g(n))+g(n)\sin(g(n)) . I have been trying to solve this in R using:
N=100
for(n in 1:N)
{
f=function(n)
g=function(n)
if(n == 1) {
f(1)=0.8
g(1)=0.6} else {
f(n)=f(n-1)cos(g(n-1))-g(n-1)sin(g(n-1)
g(n)=f(n-1)cos(g(n-1))+g(n-1)sin(g(n-1))
}
However, this is not working. Any suggestions?
I am not sure that the following answers the question. But here are some errors in the code:
The functions are defined in the for loop.
None of the two functions is ever called.
My best guess is
f <- function(n){
if(n == 1){
0.8
}else{
f(n-1)*cos(g(n-1))-g(n-1)*sin(g(n-1))
}
}
g <- function(n){
if(n == 1) {
0.6
} else {
f(n-1)*cos(g(n-1))+g(n-1)*sin(g(n-1))
}
}
N <- 10
y <- numeric(N)
for(n in 1:N) {
y[n] <- f(n)
cat(y[n], "\n")
}
y
I want to create a function that returns its result as a vector. More specifically, a function that returns the divisors of an input value and places them inside a vector.
divisors<-function(n){
i <- 2
c<-1
x<-c()
while(i <= n) {
if(n%%i==0) {
x[c]<-i
}
i <- i + 1
c<-c+1
x
}
}
I edited a bit your code in order to return a vector and avoid NA values.
divisors <- function(n){
i <- 2
x<-vector("integer")
while(i <= n) {
if(n%%i == 0) {
x <- c(x, i)
}
i <- i + 1
}
x
}
Sorry for the inexperience, I'm a beginning coder in R
For example:
If I were to make a FOR loop by chance and I have a collection of integers 1 to 100 (1:100), what would be the proper format to ensure that would print the numbers that are divisible by another number. In this case, 5 being the number divisible. I hear that using modulo would help in this instance %%
This is what I think I should have.
For (x in 1:100) {
x%%5 == y
}
print(y)
for (x in 1:100) {
if (x%%5 == 0) {
print(x)
}
}
The modulo operator %% is used to check for divisibility. Used in the expression
x %% y, it will return 0 if y is divisible by x.
In order to make your code work, you should include an if statement to evaluate to TRUE or FALSE and replace the y with 0 inside the curly braces as mentioned above:
for (x in 1:100) {
if (x%%5 == 0) {
print(x)
}
}
For a more concise way to check for divisibility consider:
for (x in 1:100) {
if (!(x%%5)){
print(x)
}
}
Where !(x %% 5) will return TRUE for 0 and FALSE for non-zero numbers.
How's this?
x <- 1:100
div_5 <- function(x) x[x %% 5 == 0]
div_5(x)
for (i in 1:10)
{
if (i %% 2)
{
#some code
}
}
I tried to implement a simple 2D single layer perceptron and ended up with this solution:
perceptron <- function(featureVec, classVec, wStart=matrix(c(0,0,0)), eta=1, limit = 50) {
plot(x=featureVec[,1],y=featureVec[,2])
# Extending dimensions
dimension <- dim(featureVec)[1]
featureVec <- cbind(featureVec,rep(1,dimension))
# Inverting 2. class
index <- classVec == -1
featureVec[index,] <- apply(matrix(featureVec[index]),1,prod,-1)
wTemp <- wStart
y <- featureVec %*% wTemp
iteration = 0
while (T) {
y <- featureVec %*% wTemp
delta <- as.matrix(featureVec[y <= 0,])
for(i in 1:nrow(delta)) {
wTemp <- wTemp + eta*delta[i,]
}
result <- featureVec %*% wTemp
if (sum(result <= 0) == 0) {
break
}
if (iteration >= limit) {
stop("Maximum count of interations reached!")
}
iteration = iteration + 1
}
if(wTemp[2] != 0) {
abline(-wTemp[3]/wTemp[2],-wTemp[1]/wTemp[2])
} else if(wTemp[2] == 0) {
abline(v=wTemp[1])
} else if(wTemp[1] == 0) {
abline(h=wTemp[2])
}
return(wTemp)
}
The feature vector works row-wise, the class vector needs values of 1 and -1 col-wise.
For most of my tests it works correct, but when I have samples like (0,0) (0,1) with classes (1,-1) I get no result. That happens with some of my examples with two points lying on a straight line (horizontal to a coordinate axis). When I try to choose different start vectors it sometimes works correctly (I have no deterministic behaviour here right now I guess). Is that a correct behaviour or is my implementation wrong?
Thanks for your help, Meiner.
EDIT: Some changes of the inital post.
Bad Dataset:
featureTest <- matrix(c(0,0,0,1),byrow=T,nrow=2)
classTest <- matrix(c(1,-1),nrow=2)
perceptron(featureTest,classTest)
featureTest <- matrix(c(0,1,0,2),byrow=T,nrow=2)
classTest <- matrix(c(1,-1),nrow=2)
perceptron(featureTest,classTest)
Good Dataset:
featureTest <- matrix(c(0,0,0,2),byrow=T,nrow=2)
classTest <- matrix(c(1,-1),nrow=2)
perceptron(featureTest,classTest)
I want to recursively count the log cylces in my function
logCounter <- function(number) {
k <- 0
if(k>=0){
k = k+1
}
result <- log(number)
if (result > 1) {
logCounter(result)
} else {
return(k)
}
}
logCounter(123)#returns 3 because log(log(log(123))) < 1
However, my counter k does not work as I would have inspected. Therefore I really would appreciate your answer!!!
You don't need to use Recall. Try this:
logCounter <- function(number) {
if (number <1) return(0) # A minor edit.
result <- log(number)
if (result > 1) return(logCounter(result)+1)
return(1)
}
The key is to try to compose your function in a way that doesn't require storing intermediate results.
You could do this much more easily without calling the function recursively with a while loop:
logCounter <- function(number) {
k <- 0
result <- number
while(result>1){
k <- k + 1
result <- log(result)
}
return(k)
}
> logCounter(123)
[1] 3
EDIT: If you need to use recursion, consider the Recall function:
logCounter <- function(number, iter=1) {
if(log(number)>1)
out <- Recall(log(number), iter+1)
else
out <- list(log(number),iter)
return(out)
}
> logCounter(123)
[[1]]
[1] 0.4518085
[[2]]
[1] 3