Converting recursive formulate into pseudocode using memoization - recursion

i've been trying to turn the recurrence formula underneath into a pseudocode that uses memoization, however currently all i know is my below attempt is incorrect, is anyone able to point me in the right direction?
My recurrence formula:
N(C,i) =
1 if C = 0
0 if i=0 or C<0}
N(C-p_i, i-1) + N(C, i-1) otherwise
My current attempt:
MEM-N(C, i, r)
if r[i] >= 0 then
return r[i]
if i = 0 and r[i] >= 0 or C < 0 and r[i] >= 0 then
return 0
else if C = 0 and r[i] >= 0 then
return 1
else
q = -$\infty$
q = MEM-N(C - $p_i$, i-1) + MEM-N(C,i - x, r)
r[i] = q
return q

Following on from the comments:
MEM-N(C, i, r)
if C = 0 then
return 1
else if i = 0 or C < 0 then
return 0
else if r[i] >= 0 then
return r[i] # move here
else
q = MEM-N(C - p_i, i - 1, r) + MEM-N(C, i - 1, r) # fix
r[i] = q
return q

Related

If-Statements and integrate() function in R

Do if statements not work for integrate? I have to do something much more complicated than this, but I am supplying this example because it isolated the problem.
Kernel = function(x){
if(abs(x)<1){
w = 1 - abs(x)
} else{
w = 0
}
return(w)
}
integrate(Kernel,
0,
1)
The error message:
the condition has length > 1 and only the first element will be used
Kernel = function(x){
pmax(1-abs(x), 0)
}
integrate(Kernel, 0, 1)
0.5 with absolute error < 5.6e-15
or even:
Kernel1 = function(x){
ifelse(abs(x)<1, 1-abs(x), 0)
}
integrate(Kernel1, 0, 1)
0.5 with absolute error < 5.6e-15
If you want to maintain the way you have written your function, you have to vectorize it:
Kernel2 = function(x){
ifelse(abs(x)< 1, 1-abs(x), 0)
if(abs(x)<1){
w = 1 - abs(x)
} else{
w = 0
}
return(w)
}
integrate(Vectorize(Kernel2), 0, 1)
0.5 with absolute error < 5.6e-15

Translate matrix algebra code from VB to R

For a project, I am trying to replicate/rewrite the following Visual Basic code from a case study so I can run it in R.
For j=1 to N
For I=1 to M
While I>j
If b(I,j) c=P then
a(I,j)=l else
if b(1, j)<= [P+ (l-P ) /21 then
a(I,j)=3 else
a(1, j)=O
end if
end if
wend
while Icj
if a(I,j)=l then
a(j,i)=l else
if a(I,j)=3 then
a(j,I)=O else
a( j ,I) =3
end if
end if
wend
next
next
The goal is to compare the values b(j,I) from matrix B(M,N) and fill matrix A(M,N) with either 0,1 or 3. It should represent the results of a football game.
This is what I got so far:
M <- 3
N <- 3
A <- matrix(0,M,N)
B <- matrix(sample(0:4,25, replace=TRUE),nrow=M, ncol=N)
for (i in 1:nrow(B)) {
for (j in 1:ncol(B)) {
while (i > j) {
if (B[i,j] > B[j,i]) {
A[i,j] = 3 & A[j,i] = 0
}
if (B[i,j] < B[j,i]) {
A[i,j] = 0 & A[j,i] = 3
}
else {
A[i,j] = 1 & A[j,i] = 1
}
}
}
}
I would really appreciate any help or tips!

How to iterate over 1 <= i < j <= n elements in R?

So I am trying to do a for loop over the upper triangle part of a matrix, so I only want the elements 1 <= i < j <= n. And I tried it out in R as follows:
for(i in 1:n-1) {
for(j in i+1:n) {
...
}
}
But instead of iterating over 1 <= i < j <= n these for loops go over the elements i + 1 <= j <= i + n, 1 <= i < n.
I'm new to R, so I don't understand what is happening. Could someone give me a hint how to do it correctly?
Thanks.
for(i in seq(1, n - 1)) {
for(j in seq(i + 1, n)) {
...
}
}
alternatively
for(i in 1:(n - 1)) {
for(j in (i + 1):n) {
...
}
}
The issue is that R understands i+1:n as i + (1:n)

Getting node stack overflow error in R for Quicksort

quicksort <- function(x, s, e) {
p = s
i = 0
j = 0
for (k in 1:length(x)) {
if (x[p] < x[k])
i = k
}
if (!i)
i = e
for (k in length(x):1) {
if (x[p] > x[k])
j = k
}
if (!j)
j = s
if (i < j) {
t = x[i]
x[i] = x[j]
x[j] = t
} else {
t = x[j]
x[j] = x[p]
x[p] = t
quicksort(x, s, j - 1)
quicksort(x, j + 1, e)
}
x
}
quick = function(x) {
quicksort(x, 1, length(x))
}
When I run this i R console with a Vector I getting Error
> x<-c(4,47,480,15,0,147,1,56862,12)
> quick(x)
Error: node stack overflow
Its working when testing each command in R console but complete code doesn't work perfectly is the logic correct or wrong with code
Modified the Code as
quicksort<-function(x){
if(length(x)<=1)return(x)
p<-x[1]
therest<-x[-1]
i<-therest[therest<p]
j<-therest[therest>p]
i<-quicksort(i)
j<-quicksort(j)
return(c(i,p,j))
}
Now it Works The Console is responded as
> x<-c(4,47,480,15,0,147,1,56862,12)
> quicksort(x)
[1] 0 1 4 12 15 47 147 480 56862
The Code in the Question is implementation from C++ but code there in the answer is direct R

MATLAB to R Conversion: Append values to an existing empty array through for loop

I have the below code with me. This code was written originally in MATLAB. I have two questions here:
1) What would be the corresponding command in R for the below command in MATLAB:
duet(i).p = [];
2) In the below code I am getting all the correct 6 values for duet$n, but I am not getting correct values for duet$p. My question is how to append the values to an empty existing array duet$p[i] in R through the for loop iterations.
This line is not working in the below code:
duet$p[i] <- c(duet$p[i],j)
I might also have declared duet$p[i] <- array() incorrectly.
The values for duet.n and duet.p from MATLAB are:
duet.n
2 0 2 0 1 3
duet.p
[] [3,6] [] [1,3,5,6] [1,6] []
In R, I am getting duet$n values correctly, but I am not able to get the array kind of results for duet$p.
Any help to get the duet$p values would be appreciated.
x <- matrix(c(-1,2,4,1,7,4.2,3,0,1.2,-1.2,5.1,4,2,3.1,1.1,1,1,9,0,1,2,2,8,1,2,2,2,2,2,2),nrow=6,ncol=5,byrow=T)
fro=1;N=6;M=2;V=3;
F <- list(f=c())
duet = list()
for (i = 1 : N){
duet$n[i] = 0
duet$p[i] = array() ## Create an empty array
for (j in 1 : N){
dl = 0
de = 0
dm = 0
for (k = 1 : M){
if (x[i,V + k] < x[j,V + k]){
dl = dl + 1
} else if (x[i,V + k] == x[j,V + k]){
de = de + 1
} else{
dm = dm + 1
}
}
if (dl == 0 & de != M){
duet$n[i] = duet$n[i] + 1
} else if (dm == 0 & de != M){
duet$p[i] = c(duet$p[i],j)
}
}
if (duet$n[i] == 0){
x[i,6] = 1
F$f = c(F$f,i)
}
}
This appears to get the output you want:
x <- matrix(c(-1,2,4,1,7,4.2,3,0,1.2,-1.2,5.1,4,2,3.1,1.1,1,1,9,0,1,2,2,8,1,2,2,2,2,2,2),nrow=6,ncol=5,byrow=T)
fro=1;N=6;M=2;V=3;
F <- list(f=c())
duet = list(n=rep(0,N), p=lapply(1:N, function(x)c()))
for (i in 1 : N){
duet$n[i] = 0
#duet$p[[i]] = c() ## Create an empty array
#if(i==2) browser()
for (j in 1 : N){
k=1:M
dl <- sum(x[i,V + k] < x[j,V + k])
de <- sum(x[i,V + k] == x[j,V + k])
dm <- sum(x[i,V + k] > x[j,V + k])
if (dl == 0 & de != M){
duet$n[i] = duet$n[i] + 1
} else if (dm == 0 & de != M){
duet$p[[i]] = c(duet$p[[i]],j)
}
}
if (duet$n[i] == 0){
#x[i,6] = 1
F$f = c(F$f,i)
}
}
What have I done?
commented out the line x[i,6] =1, because there isn't an x[i,6], and I'm not sure what you meant it to be. You will need to sort this out.
Initialised duet$n as a vector
Initialised duet$p as a list of n empty vectors
removed the k loop as conditional counting in R can be done as the sum of elements where the condition is TRUE.
corrected the syntax of for loops: = became in
I think you're trying to do duet[i]$p instead of what you're doing. Also you need to initialize each cell as a list

Resources