Swift matrix sum

前端 未结 3 1583
小蘑菇
小蘑菇 2021-01-16 08:18

I\'m trying to develop a func which allows to sum two matrix if they are equals to the same dimension, but I get an error \"EXC_BAD_INSTRUCTION\" on the try.

Here is

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 08:59

    swift 4 - Addition 0f 3 x 3 Matrices
    
    enum RisedError: Error {
            case DimensionNotEquals
            case Obvious(String)
        }
        func ==(lhs: Matrix, rhs: Matrix) -> Bool {
            return (lhs.rows) == (rhs.rows) && (lhs.columns) == (rhs.columns)
        }
        protocol Operation {
            mutating func sumWith(matrixB: Matrix) throws -> Matrix
    
        }
        class ViewController: UIViewController {
    
    
        //Matrix creation
            func matrixCreation(){
                var matrixA = Matrix(rows: 3, columns: 3)
                var matrixB = Matrix(rows: 3, columns: 3)
                matrixA[0,0] = 1.0
                matrixA[0, 1] = 2.0
                matrixA[0, 2] = 2.0
                matrixA[1, 0] = 3.0
                matrixA[1, 1] = 4.0
                matrixA[1, 2] = 4.0
                matrixA[2, 0] = 1.0
                matrixA[2, 1] = 1.0
                matrixA[2, 2] = 1.0
                matrixB[0,0] = 5.0
                matrixB[0, 1] = 6.0
                matrixB[0, 2] = 6.0
                matrixB[1, 0] = 7.0
                matrixB[1, 1] = 8.0
                matrixB[1, 2] = 8.0
                matrixB[2, 0] = 1.0
                matrixB[2, 1] = 1.0
                matrixB[2, 2] = 1.0
    
                var outputMartix = Matrix(rows: 3, columns: 3)
                print(matrixB , matrixA)
                do {
                    outputMartix = try! matrixA.sumWith(matrixB: matrixB)
                    print(outputMartix)
                }catch RisedError.DimensionNotEquals{
                    print("Dimensions are not equal")
    
                }
            }
    
        }
    
        //Matrix Configuration
    
        struct Matrix {
            let rows: Int, columns: Int
            var grid: [Double]
            init(rows: Int, columns: Int) {
                self.rows = rows
                self.columns = columns
                grid = Array(repeating: 0.0, count: rows * columns)
            }
            func indexIsValidForRow(row: Int, column: Int) -> Bool {
                return row >= 0 && row < rows && column >= 0 && column < columns
            }
            subscript(row: Int, column: Int) -> Double {
                get {
                    assert(indexIsValidForRow(row: row, column: column), "Index out of range")
                    return grid[(row * columns) + column]
                }
                set {
                    assert(indexIsValidForRow(row: row, column: column), "Index out of range")
                    grid[(row * columns) + column] = newValue
                }
            }
        }
        extension Matrix: Operation {
    
            mutating func sumWith(matrixB: Matrix) throws -> Matrix {
    
                guard self == matrixB else { throw RisedError.DimensionNotEquals }
    
                for row in 0...self.rows - 1 {
                    for column in 0...self.columns - 1 {
                        self[row, column] = matrixB[row, column] + self[row, column]
                    }
                }
                return self
            }
        }
    

提交回复
热议问题