快速矩阵求和 [英] Swift matrix sum

查看:15
本文介绍了快速矩阵求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个函数,如果它们等于相同的维度,它允许对两个矩阵求和,但我在尝试时收到错误EXC_BAD_INSTRUCTION".

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.

这是我的游乐场:

import Foundation

enum RisedError: ErrorType {
    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
}

struct Matrix {
    let rows: Int, columns: Int
    var grid: [Double]
    init(rows: Int, columns: Int) {
        self.rows = rows
        self.columns = columns
        grid = Array(count: rows * columns, repeatedValue: 0.0)
    }
    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, column: column), "Index out of range")
            return grid[(row * columns) + column]
        }
        set {
            assert(indexIsValidForRow(row, column: column), "Index out of range")
            grid[(row * columns) + column] = newValue
        }
    }
}

var matrixA = Matrix(rows: 2, columns: 2)
matrixA[0,0] = 1.0
matrixA[0,1] = 2.0
matrixA[1,0] = 3.0
matrixA[1,1] = 4.0
var matrixB = Matrix(rows: 2, columns: 2)
matrixB[0,0] = 5.0
matrixB[0,1] = 6.0
matrixB[1,0] = 7.0
matrixB[1,1] = 8.0

print(matrixA)
print(matrixB)

extension Matrix: Operation {

    mutating func sumWith(matrixB: Matrix) throws -> Matrix {

        guard self == matrixB else { throw RisedError.DimensionNotEquals }

        for row in 0...self.rows {
            for column in 0...self.columns {
                self[row, column] = matrixB[row, column] + self[row, column]
            }
        }
        return self
    }
}

do {
    try matrixA.sumWith(matrixB)
} catch RisedError.DimensionNotEquals {
    print("The two matrix's dimensions aren't equals")
} catch {
    print("Something very bad happens")
}

这是错误日志:

推荐答案

问题是你使用的是 封闭范围运算符在你的 for 循环 0...self.rows.这将包括迭代中范围的上限,在您的情况下超出范围,因此会崩溃.

The problem is you're using the closed range operator in your for loop 0...self.rows. This will include the upper bound of the range in the iteration, which in your case is out of bounds and will therefore crash.

您想改用半开范围运算符 ..<:

You want to use the half-open range operator ..< instead:

for row in 0..<self.rows {
    for column in 0..<self.columns {
        self[row, column] = matrixB[row, column] + self[row, column]
    }
}

这将迭代到但不包括上限.

This will iterate up to but not including the upper bound.

我还要注意 @MartinR 上面的评论 - 将矩阵的相等性定义为基于尺寸相同似乎不合逻辑.请记住,相等意味着可替代性(即,如果 a == bab 可以互换).

I would also note @MartinR's comment above – defining equality for the matrices to be solely based on the dimensions being the same seems illogical. Remember that equality implies substitutability (i.e if a == b, a and b are interchangeable).

我会考虑更改您的 == 以检查维度 值,然后在您的 sumWith 方法中实现您自己的维度检查(或创建一个新方法来比较尺寸).

I would consider changing your == to check both dimensions and values, and then implement your own dimension check in your sumWith method (or create a new method to compare dimensions).

这篇关于快速矩阵求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆