多维数组的笛卡尔乘积
Cartesian product of multidimensional array
我用js组合数学代码生成了这个:
(function(global) {
'use strict';
if (global.Combinatorics) return;
/* common methods */
var addProperties = function(dst, src) {
Object.keys(src).forEach(function(p) {
Object.defineProperty(dst, p, {
value: src[p]
});
});
};
var hideProperty = function(o, p) {
Object.defineProperty(o, p, {
writable: true
});
};
var toArray = function(f) {
var e, result = [];
this.init();
while (e = this.next()) result.push(f ? f(e) : e);
this.init();
return result;
};
var common = {
toArray: toArray,
map: toArray,
forEach: function(f) {
var e;
this.init();
while (e = this.next()) f(e);
this.init();
},
filter: function(f) {
var e, result = [];
this.init();
while (e = this.next()) if (f(e)) result.push(e);
this.init();
return result;
}
};
/* Cartesian Product */
var arraySlice = Array.prototype.slice;
var cartesianProduct = function() {
if (!arguments.length) throw new RangeError;
var args = arraySlice.call(arguments);
args = args[0];
console.log(args);
var
size = args.reduce(function(p, a) {
return p * a.length;
}, 1),
sizeOf = function() {
return size;
},
dim = args.length,
that = Object.create(args, {
length: {
get: sizeOf
}
});
if (!size) throw new RangeError;
hideProperty(that, 'index');
addProperties(that, {
valueOf: sizeOf,
dim: dim,
init: function() {
this.index = 0;
},
get: function() {
if (arguments.length !== this.length) return;
var result = [];
arguments.forEach(function(element,index,array) {
var i = arguments[index];
if(i >= this[index].length) return;
result.push(this[index][i]);
});
return result;
},
nth: function(n) {
var result = [];
arguments.forEach(function(element,index,array) {
var l = this[index].length,
i = n % l;
result.push(this[index][i]);
n -= i;
n /= l;
});
return result;
},
next: function() {
if (this.index >= size) return;
var result = this.nth(this.index);
this.index++;
return result;
}
});
addProperties(that, common);
that.init();
return that;
};
/* export */
addProperties(global.Combinatorics = Object.create(null), {
cartesianProduct: cartesianProduct
});
})(this);
var _ = [];
_[1] = [1,4];
_[7] = [2,9];
cp = Combinatorics.cartesianProduct(_);
console.log(cp.toArray());
我希望最终能得到这样的结果:
[[1,2],[1,9],[4,2],[4,9]]
但每次我在代码的这一部分使用forEach时,都要在Chrome中获得Uncaught TypeError: undefined is not a function
,在Firefox中获得TypeError: arguments.forEach is not a function
:
nth: function(n) {
var result = [];
arguments.forEach(function(element,index,array) {
var l = this[index].length,
i = n % l;
result.push(this[index][i]);
n -= i;
n /= l;
});
return result;
}
必须保留_
数组的索引。
arguments
不是Array,因此它没有forEach
方法。
您可以像在var args = arraySlice.call(arguments);
中那样将其转换为数组,也可以使用for
循环来迭代其元素。
我需要发布具有非严格索引的_
数组:
var _ = [];
_[1] = [1,4];
_[7] = [2,9];
默认的解决方案是不可行的,因为它们不处理这样的数组。所以我不得不调整Bergi在这里找到的想法:
function cartesian(arg) {
var r = [], max = arg.length-1;
function helper(arr, i) {
while(typeof arg[i] === "undefined") {
i += 1;
}
for (var j=0, l=arg[i].length; j<l; j++) {
var a = arr.slice(0); // clone arr
a.push(arg[i][j]);
if (i==max) {
r.push(a);
} else
helper(a, i+1);
}
}
helper([], 0);
return r;
}
相关文章:
- 多维数组的笛卡尔乘积
- 循环遍历JQueryUI选项卡控件中的每个选项卡,并构建一个对象数组
- 没有重复的笛卡尔乘积
- 笛卡尔乘积的反转
- 计算 1 个数组中元素的笛卡尔乘积
- 通过循环访问未知数量的数组来创建JavaScript对象的笛卡尔乘积(powerset?)
- 将选项卡中的 URL 与数组中的 URL 进行比较,并向数组添加更多内容
- Angular Js 在排序后卡在插入数组值
- 2 个数组的笛卡尔积
- javascript中对象的笛卡尔乘积
- 如何将两个数组组合为笛卡尔积
- 打开基于逗号分隔数组的新选项卡
- 如何使用科学记数法使(笛卡尔)图表轴按比例缩放
- 无法在卡数组中获取唯一值
- [Javascript]获取所有数组组合(笛卡尔乘积)
- 如何在javascript中实现不同长度n数组的笛卡尔乘积
- 在javascript中获取最近的笛卡尔轴对齐矢量
- 为什么我的摩卡/应该数组抛出测试失败
- 极坐标到笛卡尔坐标的函数没有输出正确的数据
- 笛卡尔坐标与球坐标的转换