diff --git a/hw1/src/include/matrix.h b/hw1/src/include/matrix.h index 479ce80d72ff4c6b2787fd29aa5c30f30da42433..621df4217151b4772e0903a29ce8ddb7b6b378ea 100644 --- a/hw1/src/include/matrix.h +++ b/hw1/src/include/matrix.h @@ -35,7 +35,16 @@ class Matrix { * @param cols The number of columns * */ - Matrix(int rows, int cols) {} + Matrix(int rows, int cols) { + + rows_=rows; + cols_=cols; + + + //创建一维数组 + linear_=new T[rows*cols]; + + } /** The number of rows in the matrix */ int rows_; @@ -51,10 +60,14 @@ class Matrix { public: /** @return The number of rows in the matrix */ - virtual auto GetRowCount() const -> int = 0; + virtual auto GetRowCount()const -> int{ + return rows_; + } /** @return The number of columns in the matrix */ - virtual auto GetColumnCount() const -> int = 0; + virtual auto GetColumnCount()const -> int { + return cols_; + } /** * Get the (i,j)th matrix element. @@ -66,7 +79,13 @@ class Matrix { * @return The (i,j)th matrix element * @throws OUT_OF_RANGE if either index is out of range */ - virtual auto GetElement(int i, int j) const -> T = 0; + virtual auto GetElement(int i, int j)const -> T{ + if(i>=rows_ || j>=cols_) + throw std::range_error("Matrix::Either index is out of range!"); + else return linear_[i*rows_+j]; + + } + /** * Set the (i,j)th matrix element. @@ -78,7 +97,13 @@ class Matrix { * @param val The value to insert * @throws OUT_OF_RANGE if either index is out of range */ - virtual void SetElement(int i, int j, T val) = 0; + virtual void SetElement(int i, int j, T val) + { + if(i>=rows_ || j>=cols_) + throw std::range_error("Matrix::Either index is out of range!"); + else linear_[i*rows_+j]=val; + + } /** * Fill the elements of the matrix from `source`. @@ -89,14 +114,32 @@ class Matrix { * @param source The source container * @throws OUT_OF_RANGE if `source` is incorrect size */ - virtual void FillFrom(const std::vector &source) = 0; + virtual void FillFrom(const std::vector &source){ + int i; + if(source.size()!=rows_*cols_) + throw std::range_error("Matrix::`source` is incorrect size!"); + + else + { + for(i=0;i { * @param rows The number of rows * @param cols The number of columns */ - RowMatrix(int rows, int cols) : Matrix(rows, cols) {} + RowMatrix(int rows, int cols) : Matrix(rows, cols) + { + + int i; + //创建一个二维数组data,并与linear_对应 + data_ = new T*[rows_]; + for(i=0;i int override { return 0; } + auto GetRowCount() const -> int override + { + return rows_; + } + /** * TODO(P0): Add implementation * @return The number of columns in the matrix */ - auto GetColumnCount() const -> int override { return 0; } + auto GetColumnCount()const -> int override{ + return cols_; + } + /** * TODO(P0): Add implementation @@ -139,7 +199,9 @@ class RowMatrix : public Matrix { * @throws OUT_OF_RANGE if either index is out of range */ auto GetElement(int i, int j) const -> T override { - throw NotImplementedException{"RowMatrix::GetElement() not implemented."}; + if(i>=rows_ || j>=cols_) + throw std::range_error("RowMatrix::Either index is out of range!"); + return data[i][j]; } /** @@ -152,7 +214,11 @@ class RowMatrix : public Matrix { * @param val The value to insert * @throws OUT_OF_RANGE if either index is out of range */ - void SetElement(int i, int j, T val) override {} + void SetElement(int i, int j, T val) override { + if(i>=rows_ || j>=cols_) + throw std::range_error("RowMatrix::Either index is out of range!"); + else data[i][j]=val; + } /** * TODO(P0): Add implementation @@ -166,7 +232,19 @@ class RowMatrix : public Matrix { * @throws OUT_OF_RANGE if `source` is incorrect size */ void FillFrom(const std::vector &source) override { - throw NotImplementedException{"RowMatrix::FillFrom() not implemented."}; + int i,j; + if(source.size()!=rows_*cols_) + throw std::range_error("Matrix::`source` is incorrect size!"); + else + { + for(i=0;i { * * Destroy a RowMatrix instance. */ - ~RowMatrix() override = default; + ~RowMatrix() override{ + int i; + for(i=0;i { T **data_; }; + + + + /** * The RowMatrixOperations class defines operations * that may be performed on instances of `RowMatrix`. */ + + template class RowMatrixOperations { public: @@ -203,8 +292,23 @@ class RowMatrixOperations { * @return The result of matrix addition */ static auto Add(const RowMatrix *matrixA, const RowMatrix *matrixB) -> std::unique_ptr> { - // TODO(P0): Add implementation - return std::unique_ptr>(nullptr); + //判断维度是否一样 + if(matrixA.GetRowCount()!= matrixB.GetRowCount() || matrixA.GetColumnCount() != matrixB.GetColumnCount()) + return std::unique_ptr>(nullptr); + //创建result类指针 + int R_rows = matrixA.GetRowCount(); + int R_cols = matrixA.GetColumnCount(); + RowMatrix *result(R_rows,R_cols); + //相加 + int i,j; + for(i=0;i *matrixA, const RowMatrix *matrixB) -> std::unique_ptr> { // TODO(P0): Add implementation - return std::unique_ptr>(nullptr); + if(matrixA.GetColumnCount() != matrixB.GetRowCount() ) + return std::unique_ptr>(nullptr); + //创建result类指针 + int R_rows = matrixA.GetRowCount(); + int R_cols = matrixB.GetColumnCount(); + RowMatrix *result(R_rows,R_cols); + //利用FillFrom()给result数组全部赋值为0 + std::vector< std::vector > vec(R_rows,std::vector(R_cols,0)); + result.FillFrom(vec); + //相乘 + int i,j,k; + for(i=0;i *matrixA, const RowMatrix *matrixB, const RowMatrix *matrixC) -> std::unique_ptr> { // TODO(P0): Add implementation + if(matrixA.GetColumnCount() != matrixB.GetRowCount() ||matrixA.GetRowCount() != matrixC.GetRowCount() || matrixB.GetColumnCount() != matrixC.GetColumnCount()) return std::unique_ptr>(nullptr); + //创建result类指针 + int R_rows = matrixA.GetRowCount(); + int R_cols = matrixB.GetColumnCount(); + RowMatrix *result(R_rows,R_cols); + //相乘 + result=Multiply(matrixA,matrixB); + //相加 + result=Add(result,matrixC); + return result; } }; -} // namespace bustub + // namespace bustub +