             }
         }
         let sorted = try! topologicalSort(tree.keys.toArray(), successors: { j in tree[j] ?? [] })
-        return sorted.map{ j in (pivots[j]!, j) }
-    }
-    
-    // for debug
-    private func currentPivots() -> [(Int, Int)] {
-        pivots.map{ (j, i) in (i, j) }.sorted{ $0.0 }
-    }
-    
-    private func setPivot(_ p: (Int, Int)) {
-        setPivot(p.0, p.1)
-    }
-        
-    private func setPivot(_ i: Int, _ j: Int) {
-        pivots[j] = i
+        return sorted.map{ j in
+            let (i, a) = pivots[j]!
+            return (i, j, a)
+        }
+    }
+    
+    private func setPivot(_ c: MatrixComponent<R>) {
+        let (i, j, a) = c
+        setPivot(i, j, a)
+    }
+        
+    private func setPivot(_ i: Int, _ j: Int, _ a: R) {
+        pivots[j] = (i, a)
         pivotRows.insert(i)
     }
     

import Dispatch

public final class MatrixPivotFinder<R: Ring> {
    typealias RowEntity = RowAlignedMatrixData<R>.RowElement
    
    private let data: RowAlignedMatrixData<R>
    private let rowWeights: [Int]
    
    private var pivots: [Int : Int] // column -> pivot row
    private var pivotRows: Set<Int>
    private var debug: Bool
    
    public static func findPivots<n, m>(of A: Matrix<n, m, R>, debug: Bool = false) -> Result<n, m, R> {
        let size = A.size
        let pf = MatrixPivotFinder(A, debug: debug)
        let pivots = pf.run()
        
        func asPermutation<n>(_ order: [Int], _ n: Int) -> Permutation<n> {
            let remain = Set(0 ..< n).subtracting(order)

                queue.append(j)
            } else if a.isInvertible {
                candidates.insert(j)
                values[j] = a
            }
        }
        
