         
         self.align = align
         
-        switch align {
-        case .Rows:
-            rowTable = [:]
-            for (j, list) in colTable {
-                for (i, a) in list {
-                    set(i, j, a)
-                }
-            }
-            colTable = nil
-        case .Cols:
-            colTable = [:]
-            for (i, list) in rowTable {
-                for (j, a) in list {
-                    set(i, j, a)
-                }
-            }
-            rowTable = nil
-        }
+        let copy = table
+        self.table = [:]
+        
+        for (i, list) in copy {
+            for (j, a) in list {
+                set(j, i, a)
+            }
+        }
+        
         sort()
     }
     
+    public func transpose() {
+        (rows, cols) = (cols, rows)
+        align = (align == .Rows) ? .Cols : .Rows
+    }
+    
+    public var isDiagonal: Bool {
+        return table.forAll { (i, list) in (list.count == 1) && list.first!.0 == i }
+    }
+    
+    public var diagonal: [R] {
+        return table.keys.sorted().flatMap { i -> R? in
+            table[i]!.first.flatMap{ (j, a) -> R? in (i == j) ? a : nil }
+        }
+    }
+    
     public static func *(a: ComputationalMatrix<R>, b: ComputationalMatrix<R>) -> ComputationalMatrix<R> {
-        assert(a.cols == b.rows)
+        assert(a.rows == b.cols)
         
         let result = ComputationalMatrix<R>(rows: a.rows, cols: b.cols, components: [])
         

        self.cols = cols
        
        self.align = align
        self.rowTable = (align == .Rows) ? [:] : nil
        self.colTable = (align == .Cols) ? [:] : nil
        
        for (i, j, a) in components {
            if a == R.zero {
                continue
            }
            set(i, j, a)
        }
        
        sort()
    }
    
    internal func set(_ i: Int, _ j: Int, _ a: R) {
        assert(a != R.zero)
        
        switch align {
        case .Rows:
            if rowTable[i] == nil {
                rowTable[i] = []
            }
            rowTable[i]!.append( (j, a) )
        case .Cols:
            if colTable[j] == nil {
                colTable[j] = []
            }
            colTable[j]!.append( (i, a) )
        }
    }

    internal func sort() {
        switch align {
        case .Rows:
            for (i, list) in rowTable {
                rowTable[i] = list.sorted{ (e1, e2) in e1.col < e2.col }
            }
        case .Cols:
            for (j, list) in colTable {
                colTable[j] = list.sorted{ (e1, e2) in e1.row < e2.row }
            }
        }
    }
    
    public static func identity(_ n: Int, align: ComputationalMatrixAlignment = .Rows) -> ComputationalMatrix<R> {
        let components = (0 ..< n).map{ i in MatrixComponent(i, i, R.identity)}
        return ComputationalMatrix(rows: n, cols: n, components: components, align: align)
    }
    
    public var toGrid: [R] {
        var grid = Array(repeating: R.zero, count: rows * cols)
        switch align {
        case .Rows:
            for (i, list) in rowTable {
                for (j, a) in list {
                    grid[i * cols + j] = a
                }
            }
        case .Cols:
            for (j, list) in colTable {
                for (i, a) in list {
                    grid[i * cols + j] = a
                }
            }
        }
        return grid
    }
    
    public var isDiagonal: Bool {
        switch align {
        case .Rows:
            return rowTable.forAll { (i, list) in (list.count == 1) && list.first!.col == i }
        case .Cols:
            return colTable.forAll { (j, list) in (list.count == 1) && list.first!.row == j }
        }
    }
    
    public var diagonal: [R] {
        switch align {
        case .Rows:
            return (0 ..< rowTable.count).map{ i in rowTable[i]!.first!.value }
        case .Cols:
            return (0 ..< colTable.count).map{ j in colTable[j]!.first!.value }
        }
    }
    

        
        while k0 < n0 || k1 < n1 {
            let b = (k0 < n0 && k1 < n1)
            if b && (p0.pointee.0 == p1.pointee.0) {
                let j0 = p0.pointee.0
                let (a0, a1) = (p0.pointee.1, p1.pointee.1)
                let value = r * a0 + a1
                
                if value != 0 {
