【发布时间】:2015-03-05 14:09:21
【问题描述】:
我的问题和我几个月前问的几乎一样:
2^N Combinaisons with Integers (Kernel), how to generate them?
基本上,我想在一个内核中包含 2^N 个组合,但我概括了我的版本,现在它变得更加复杂:
我不再想要 2 个元素的每个可能组合的总和(模 2),但我现在需要 P 元素的每个可能组合的总和(模 P):O。
N : the number of elements in kernel.
M : the length of an element in the kernel.
P : the dimension of my result.
int[][] Kernel:
....
i : 0 1 2 1 0 1 0 1 0 1 1 1 2 1 1 2 0 1 2 1 0 2 1 1 2 (length = M)
i+1 : 1 2 1 0 1 2 0 2 0 1 0 1 2 0 2 1 0 1 0 1 1 0 2 0 1 (length = M)
....
N : ....
with P = 3 (so value inside Kernel elements equals to {0,1,2}
我的目标(就像上一个有 2^N 组合的目标)是生成所有可能(所有 P^N 组合)的人:
1 * Kernel[0]
2 * Kernel[0]
....
P * kernel[0]
......
1 * Kernel[0] + 1 * Kernel[1]
1 * Kernel[0] + 2 * Kernel[1]
......
1 * kernel[0] + (P-1) * Kernel[1]
......
1 * kernel[0] + 1 * Kernel[1] ....(P elements) + 1 * Kernel[P]
我现在使用的是@pbabcdefp 给出的版本 它仅适用于 2 个元素的总和(模 2),我不知道如何使它适用于 P 元素的总和(模 P)
public static boolean[][] combinations(boolean kernel[][]) {
int n = kernel.length;
int m = kernel[0].length;
int p = 1 << n;
boolean[][] temp = new boolean[p][m];
for (int i = 0; i < p; i++)
for (int j = 0; j < n; j++)
if (((1 << j) & i) != 0)
for (int k = 0; k < m; k++)
temp[i][k] ^= kernel[j][k];
return temp;
}
和以前的版本一样,不要介意内存成本,也不要介意这种数组生成的复杂性,这只是一个理论案例。
提前感谢任何知道如何概括这种组合的人。
最好的问候,
以防万一:一个例子
int[][] Kernel :
[0] : 0 1 2 0 2 1 2 0
[1] : 1 2 2 0 1 2 2 0
so we have : N equals to 2 ; M equals to 8 and P equals to 3 (values are included inside {0,1,2}
The result should be :
0 0 0 0 0 0 0 0 (the null element is always inside the result)
0 1 2 0 2 1 2 0 (1x [0] % 3)
1 2 2 0 1 2 2 0 (1x [1] % 3)
0 2 1 0 1 2 1 0 (2x [0] % 3)
2 1 1 0 2 1 1 0 (2x [1] % 3)
0 0 0 0 0 0 0 0 (3x [0] % 3)
0 0 0 0 0 0 0 0 (3x [1] % 3)
1 0 1 0 0 0 1 0 (1x [0] + 1x [1] % 3)
1 1 0 0 2 1 0 0 (2x [0] + 1x [1] % 3)
2 2 0 0 1 2 0 0 (1x [0] + 2x [1] % 3)
我们曾经在内核中有两个元素, 我们知道新内核中有 P^2 所以 3^2 = 9 个元素,我们只是生成它们(除了计算错误:D 抱歉,但计算是写的:D)
【问题讨论】:
-
@stackoverflow.com/users/3973077/pbabcdefp 如果您对如何推广您的方法有任何想法,我召唤大师=D:D
-
我很清楚这一点,“内核”向量由用户指定,内核向量可以是任意向量,整数元素从 0 到 P-1?
-
好吧,老实说,内核向量是由其他函数计算的 :) 但是假设它是整数元素介于 0 和 (P-1) 之间的任意向量 :) 其他函数没有对组合有任何影响;)所以是的,可以是N个元素,它们都是0到(P-1)之间的随机元素
-
另外,在您的示例中,您还需要提及
2x [0] + 2x [1] %3案例,对吗?无论如何,有趣的问题。 -
是的,每个组合都带有一个系数
标签: java math complexity-theory mathematical-optimization algebra