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



outer.diff <- function(x, mat=F){
	# pairwise differences of all vector elements
	# x: vector with elements
	if(anyNA(x)){ stop("input of outer.diff is NA") }
	if(!is.numeric(x)){ stop("input of outer.diff is not numeric") }
	len.x <- length(x)
	if(len.x==1){ return(x) }
	if(mat){
		erg <- matrix(0,nrow=len.x,ncol=len.x) # matrix, lower triangle filled
		for(i in 1:(len.x-1)){ #x
			for(j in (i+1):len.x){ #y
				erg[j,i] <- x[i] - x[j]
			}
		}
	}else{
		erg <- vector("numeric",length=(len.x^2-len.x)/2) # vector without diagonal values, order col wise top down, lower triangle
		k <- 1
		for(i in 1:(len.x-1)){ #x
			for(j in (i+1):len.x){ #y
				erg[k] <- x[i] - x[j]
				k <- k+1
			}
		}
	}
	return(erg)
}

#outer.diff(x=c(1,2,3), mat=T)
#outer.diff(x=c(1,2,3), mat=F)



