         )
     }
     
-    private func mapNonZeroEntries(_ f: (Int, Int, R) -> R) -> Self {
+    public func mapNonZeroEntries(_ f: (Int, Int, R) -> R) -> Self {
         .init(size: size) { setEntry in
-            nonZeroEntries.forEach { (i, j, a) in setEntry(i, j, f(i, j, a)) }
+            nonZeroEntries.forEach { (i, j, a) in
+                let b = f(i, j, a)
+                if !b.isZero {
+                    setEntry(i, j, b)
+                }
+            }
         }
     }
     
-    fileprivate struct Index: Hashable {
-        let row, col: Int
-        init(_ row: Int, _ col: Int) {
+    public struct Index: Hashable {
+        public let row, col: Int
+        public init(_ row: Int, _ col: Int) {
             self.row = row
             self.col = col
         }

        AnySequence(NonZeroEntryIterator(data))
    }
    
    public var isZero: Bool {
        data.isEmpty
    }
    
    public var isIdentity: Bool {
        data.allSatisfy { (e, a) in
            e.row == e.col && a.isIdentity
        }
    }
    
    public static func ==(a: Self, b: Self) -> Bool {
        a.data == b.data
    }

//  Created by Taketo Sano on 2021/05/14.
//

//  Implemented with DOK (dictionary of keys) format.
//  https://en.wikipedia.org/wiki/Sparse_matrix#Dictionary_of_keys_(DOK)
//  Not intended for fast computation.

public struct DefaultMatrixImpl<R: Ring>: SparseMatrixImpl {
    public typealias BaseRing = R
    public typealias Data = [Index : R]
    
    public var size: (rows: Int, cols: Int)
    public private(set) var data: Data
    
    private init(size: MatrixSize, data: Data) {
        assert(size.0 >= 0)
