     }
     
     public static func == (a: Self, b: Self) -> Bool {
+        assert((a.rows, a.cols) == (b.rows, b.cols), "Mismatching matrix size.")
         return a.grid == b.grid
     }
     
     public static func + (a: Self, b: Self) -> Self {
+        assert((a.rows, a.cols) == (b.rows, b.cols), "Mismatching matrix size.")
         return Self(rows: a.rows, cols: a.cols) { (i, j) -> R in
             return a[i, j] + b[i, j]
         }

    public var grid: [R]
    
    // root initializer
    public init(rows: Int, cols: Int, grid: [R]) {
        assert(!(rows == Auto && n.self == Dynamic.self) && !(cols == Auto && m.self == Dynamic.self),
               "Must specify rows/cols for DynamicMatrix.")
        
        self.rows = (rows != Auto) ? rows : n.intValue
        self.cols = (cols != Auto) ? cols : m.intValue
        self.grid = grid
    }
    
    public init(rows: Int = Auto, cols: Int = Auto, gen: (Int, Int) -> R) {
        assert(!(rows == Auto && n.self == Dynamic.self) && !(cols == Auto && m.self == Dynamic.self),
               "Must specify rows/cols for DynamicMatrix.")
        
        let _rows = (rows != Auto) ? rows : n.intValue
        let _cols = (cols != Auto) ? cols : m.intValue
        
        let grid = (0 ..< _rows * _cols).map { (index: Int) -> R in
            let (i, j) = index /% _cols
            return gen(i, j)
        }
        self.init(rows: _rows, cols: _cols, grid: grid)
    }

    public init(_ grid: R...) {
        self.init(rows: Auto, cols: Auto, grid: grid)
    }
}


        }
    }
    
    public static func * (r: R, a: Self) -> Self {
        return Self(rows: a.rows, cols: a.cols) { (i, j) -> R in
            return r * a[i, j]
