サブ permutation.js Last modified: Mar 25, 2004
var array0, nr, nc, um, t, ntrue, denom, stat_val, nc1, nr1, array1, array2
var EPSILON = 1e-14
var MAXLOOP = 2000
var CRITERION = 5000
function com(n, m)
{
var k, i
var retv = 1
k = Math.min(n-m, m)
for (i = 1; i <= k; i++) {
retv *= (n -i+1)/i
}
return retv
}
function decomp(data, a)
{
var i, j, n = 0
for (i = 0; i < data.length; i++) {
for (j = 0; j < data[i].length; j++) {
a[n++] = data[i][j]
}
}
return n
}
function found()
{
var i, n1, n2
n1 = n2 = 0
for (i = 0; i < nc; i++) {
if (um[0][i]) {
array1[n1++] = array0[i]
}
else {
array2[n2++] = array0[i]
}
}
if (stat(array1, n1, array2, n2) >= stat_val-EPSILON) {
ntrue++
}
}
function search(x, y)
{
var t
if (y == 0) {
found()
}
else if (x == 0) {
if ((t = um[0][0]-um[y][0]) < 0) {
return
}
um[0][0] = t
search(nc1, y-1)
um[0][0] += um[y][0]
}
else {
search(x-1, y)
while (um[y][0] && um[0][x]) {
++um[y][x]
--um[y][0]
--um[0][x]
search(x-1, y)
}
um[y][0] += um[y][x]
um[0][x] +=um[y][x]
um[y][x] = 0
}
}
function exact(n1, n2)
{
var i
ntrue = 0
denom = com(n1+n2, n1)
um = new Array(nr)
for (i = 0; i < nr; i++) {
um[i] = new Array(nc)
}
for (i = 0; i < nc; i++) {
um[0][i] = 1
um[1][i] = 0
}
um[1][0] = n2
nc1 = nc-1
nr1 = nr-1
search(nc1, nr1)
}
function approx(n1, n2)
{
var i, j, k, n, temp, a
ntrue = 0
denom = MAXLOOP
n = n1+n2
a = new Array(n)
for (i = 0; i < n; i++) {
a[i] = i
}
for (i = 0; i < MAXLOOP; i++) {
for (j = 0; j < n; j++) {
for (;;) {
k = Math.floor(Math.random()*n)
if (k >= 0 && k < n) break
}
temp = a[k]
a[k] = a[j]
a[j] = temp
}
for (j = 0; j < n1; j++) {
array1[j] = array0[a[j]]
}
for (j = 0; j < n2; j++) {
array2[j] = array0[a[j+n1]]
}
if (stat(array1, n1, array2, n2) >= stat_val-EPSILON) {
ntrue++
}
}
}
直前のページへ戻る E-mail to Shigenobu AOKI