Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add transpose.

  • Loading branch information...
commit 3d6b4cf2fb12474a4c08e13efa866e4d821ace36 1 parent 2df8c62
Mike Bostock mbostock authored
1  Makefile
View
@@ -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 \
3  d3.js
View
@@ -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;) {
4 d3.min.js
View
2 additions, 2 deletions not shown
3  src/core/transpose.js
View
@@ -0,0 +1,3 @@
+d3.transpose = function(matrix) {
+ return d3.zip.apply(d3, matrix);
+};
32 test/core/transpose-test.js
View
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.