任意進数

  • 予定パッケージ名
    • RYMiscellaneous
  • 関数名
    • counterplus
    • counterplus2
  • タイトル
counterplus
  • 説明
桁ごとに異なる(かもしれない)進数に対応づけて、数値を1ずつ増やす
counterplus()では、a1xa2...xan個の異なる数値が作られる。
これは通常の「進数」
counterplus2()では、a1+a1xa2+a1xa2xa3+...+a1xa2x...xan個の数値が作られる。これは、分岐木のノードの個数
  • 使用例
counterplus(counter,t)
counterplus2(counter,t)
  • ソース
counterplus<-function(x,t){
	x[1]<-x[1]+1
	for(i in 1:(length(x))){
		if(x[i]==t[i]){
			x[i]<-0
			if(i<length(x)){
				x[i+1]<-x[i+1]+1
			}
			
		}else{
			return(x)
		}
	}
	return(x)
}
counterplus2<-function(x,t){
	x[1]<-x[1]+1
	for(i in 1:(length(x))){
		if(x[i]==(t[i]+1)){
			x[i]<-1
			if(i<length(x)){
				x[i+1]<-x[i+1]+1
			}
			
		}else{
			return(x)
		}
	}
	return(x)
}
  • 使用例
# counterplus
nchild<-c(2,3,2,4)
nstep<-length(nchild)
counter<-rep(0,nstep)
loop<-TRUE
cnt<-1
ns<-NULL
ns[[cnt]]<-counter
while(loop){
	counter<-counterplus(counter,nchild)
	#print(counter)
	cnt<-cnt+1

	if(sum(counter)==0){
		loop<-FALSE
	}else{
			ns[[cnt]]<-c(counter)

	}
}
cnt
length(ns)

# counterplus2

counter<-rep(0,nstep)
loop<-TRUE
cnt<-1
ns<-NULL
ns[[cnt]]<-counter
while(loop){
	counter<-counterplus2(counter,nchild)
	#print(counter)
	cnt<-cnt+1
	ns[[cnt]]<-c(counter)

	if(sum(counter)==sum(nchild)){
		loop<-FALSE
	}
}
cnt
ns