Skip to content

Commit

Permalink
Add transpose.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 23, 2011
1 parent 2df8c62 commit 3d6b4cf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -53,6 +53,7 @@ d3.core.js: \
src/core/number.js \
src/core/sum.js \
src/core/quantile.js \
src/core/transpose.js \
src/core/zip.js \
src/core/bisect.js \
src/core/first.js \
Expand Down
3 changes: 3 additions & 0 deletions d3.js
Expand Up @@ -175,6 +175,9 @@ d3.quantile = function(values, p) {
e = H - h;
return e ? v + e * (values[h] - v) : v;
};
d3.transpose = function(matrix) {
return d3.zip.apply(d3, matrix);
};
d3.zip = function() {
if (!(n = arguments.length)) return [];
for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m;) {
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/core/transpose.js
@@ -0,0 +1,3 @@
d3.transpose = function(matrix) {
return d3.zip.apply(d3, matrix);
};
32 changes: 32 additions & 0 deletions test/core/transpose-test.js
@@ -0,0 +1,32 @@
require("../env");
require("../../d3");

var vows = require("vows"),
assert = require("assert");

var suite = vows.describe("d3.transpose");

suite.addBatch({
"transpose": {
topic: function() {
return d3.transpose;
},
"transposes a square matrix": function(transpose) {
assert.deepEqual(d3.transpose([[1, 2], [3, 4]]), [[1, 3], [2, 4]]);
},
"transposes a non-square matrix": function(transpose) {
assert.deepEqual(d3.transpose([[1, 2, 3, 4, 5], [2, 4, 6, 8, 10]]), [[1, 2], [2, 4], [3, 6], [4, 8], [5, 10]]);
},
"transposes a single-row matrix": function(transpose) {
assert.deepEqual(d3.transpose([[1, 2, 3, 4, 5]]), [[1], [2], [3], [4], [5]]);
},
"transposes an empty matrix": function(transpose) {
assert.deepEqual(d3.transpose([]), []);
},
"ignores extra elements given an irregular matrix": function(transpose) {
assert.deepEqual(d3.transpose([[1, 2], [3, 4], [5, 6, 7]]), [[1, 3, 5], [2, 4, 6]]);
}
}
});

suite.export(module);

0 comments on commit 3d6b4cf

Please sign in to comment.