# TODO: Add comment
# 
# Author: E.Korsching Mar 15, 2012
###############################################################################


contrast.matrix <- function(categories, comparison){
	# simple contrast matrix from a given categories vector to 
	# compute e.g. all possible multiple pairwise t-tests differences
	# 
	# return a matrix like:
	#
	#		A-B		A-C		B-C
	# A		 1		 1		 0
	# B		-1		 0		 1
	# C		 0		-1		-1
	#
	# diff can be computed without using any transpose:
	#  as.matrix(x) %*% contrast.matrix
	
	uc <- unique(categories)
	p <- length(uc)
	Lmat <- matrix(0, p, p * (p-1)/2)
	for(i in 1:(p-1)) {
		Lmat[i, (i-1)*p - (i*(i+1)/2) + (i+1):p] <- 1
		Lmat[(i+1):p, (i-1)*p - (i*(i+1)/2) + (i+1):p] <- - diag(p-i)
	}
	
	#
	dimnames(Lmat) <- list(uc, comparison)
	return(Lmat)
}



