# TODO: Add comment
# 
# Author: E.Korsching 2026
###############################################################################



## test on zero rows

non.zero.rows <- function(x){
	# matrix rows does not contain zeros
	xc <- ncol(x)
	xr <- nrow(x)
	vr <- vector("logical",xr)
	vc <- vector("logical",xc)
	for(i in 1:xr){
		for(j in 1:xc){
			vc[j] <- x[i,j]==0
		}
		vr[i] <- if(sum(vc)>0){ F }else{ T }
	}
	return(vr)
}

#a <- data.frame(a=c(1,1,1,1,0), b=c(2,2,2,2,2))
#a
#non.zero.rows(a)



## count the zero containing cells in columns

count.zero.cols <- function(x){
	# count zeros in matrix columns
	xc <- ncol(x)
	xr <- nrow(x)
	zc <- vector("logical",xc)
	for(i in 1:xc){
		zc[i] <- sum(x[ ,i]==0)
	}
	return(zc)
}

#a <- data.frame(a=c(1,1,1,1,0), b=c(2,0,2,2,0), c=c(3,3,3,3,3))
#a
#count.zero.cols(a)



## first local minimum

first.loc.min <- function(x){
	# return the position of the first local minimum in search direction
	# normally a vector from start position
	xl <- length(x)
	for(i in 2:xl){
		if( x[i]>=x[i-1]){ break }
	}
	if(i==xl){
		cat("\nno local minimum found")
		return(0)
	}else{
		return(i-1)
	}
}

#a1 <- c(1,2,3,2,1,1,3,5,4)	# 1
#a1 <- c(3,2,1,2,1,1,3,5,4)	# 3
#a1 <- c(3,2,1,1,1,1,1,1,1)	# 3
#a1 <- c(1,1,2,3,1,1,1,1,1)	# 1
#a1 <- data.frame(a=c(3,2,1,2,1,1,3,5,4))
#first.loc.min(unlist(a1))


