From 518e356040e265591e70acfb15c7761f3f3ce279 Mon Sep 17 00:00:00 2001 From: xhjgit <10629002+xie-huijuan@user.noreply.gitee.com> Date: Sun, 4 Sep 2022 08:53:39 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20hw1/?= =?UTF-8?q?src/include/matrix.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw1/src/include/matrix.h | 236 --------------------------------------- 1 file changed, 236 deletions(-) delete mode 100644 hw1/src/include/matrix.h diff --git a/hw1/src/include/matrix.h b/hw1/src/include/matrix.h deleted file mode 100644 index 479ce80..0000000 --- a/hw1/src/include/matrix.h +++ /dev/null @@ -1,236 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// BusTub -// -// starter_test.cpp -// -// -// Copyright (c) 2015-2020, Carnegie Mellon University Database Group -// -//===----------------------------------------------------------------------===// - -// This file is copied from bustub, thanks to CMU Database Group. - -#pragma once - -#include -#include -#include -#include "common/exception.h" - -namespace bustub { - -/** - * The Matrix type defines a common - * interface for matrix operations. - */ -template -class Matrix { - protected: - /** - * TODO(P0): Add implementation - * - * Construct a new Matrix instance. - * @param rows The number of rows - * @param cols The number of columns - * - */ - Matrix(int rows, int cols) {} - - /** The number of rows in the matrix */ - int rows_; - /** The number of columns in the matrix */ - int cols_; - - /** - * TODO(P0): Allocate the array in the constructor. - * TODO(P0): Deallocate the array in the destructor. - * A flattened array containing the elements of the matrix. - */ - T *linear_; - - public: - /** @return The number of rows in the matrix */ - virtual auto GetRowCount() const -> int = 0; - - /** @return The number of columns in the matrix */ - virtual auto GetColumnCount() const -> int = 0; - - /** - * Get the (i,j)th matrix element. - * - * Throw OUT_OF_RANGE if either index is out of range. - * - * @param i The row index - * @param j The column index - * @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; - - /** - * Set the (i,j)th matrix element. - * - * Throw OUT_OF_RANGE if either index is out of range. - * - * @param i The row index - * @param j The column index - * @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; - - /** - * Fill the elements of the matrix from `source`. - * - * Throw OUT_OF_RANGE in the event that `source` - * does not contain the required number of elements. - * - * @param source The source container - * @throws OUT_OF_RANGE if `source` is incorrect size - */ - virtual void FillFrom(const std::vector &source) = 0; - - /** - * Destroy a matrix instance. - * TODO(P0): Add implementation - */ - virtual ~Matrix() = default; -}; - -/** - * The RowMatrix type is a concrete matrix implementation. - * It implements the interface defined by the Matrix type. - */ -template -class RowMatrix : public Matrix { - public: - /** - * TODO(P0): Add implementation - * - * Construct a new RowMatrix instance. - * @param rows The number of rows - * @param cols The number of columns - */ - RowMatrix(int rows, int cols) : Matrix(rows, cols) {} - - /** - * TODO(P0): Add implementation - * @return The number of rows in the matrix - */ - auto GetRowCount() const -> int override { return 0; } - - /** - * TODO(P0): Add implementation - * @return The number of columns in the matrix - */ - auto GetColumnCount() const -> int override { return 0; } - - /** - * TODO(P0): Add implementation - * - * Get the (i,j)th matrix element. - * - * Throw OUT_OF_RANGE if either index is out of range. - * - * @param i The row index - * @param j The column index - * @return The (i,j)th matrix element - * @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."}; - } - - /** - * Set the (i,j)th matrix element. - * - * Throw OUT_OF_RANGE if either index is out of range. - * - * @param i The row index - * @param j The column index - * @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 {} - - /** - * TODO(P0): Add implementation - * - * Fill the elements of the matrix from `source`. - * - * Throw OUT_OF_RANGE in the event that `source` - * does not contain the required number of elements. - * - * @param source The source container - * @throws OUT_OF_RANGE if `source` is incorrect size - */ - void FillFrom(const std::vector &source) override { - throw NotImplementedException{"RowMatrix::FillFrom() not implemented."}; - } - - /** - * TODO(P0): Add implementation - * - * Destroy a RowMatrix instance. - */ - ~RowMatrix() override = default; - - private: - /** - * A 2D array containing the elements of the matrix in row-major format. - * - * TODO(P0): - * - Allocate the array of row pointers in the constructor. - * - Use these pointers to point to corresponding elements of the `linear` array. - * - Don't forget to deallocate the array in the destructor. - */ - T **data_; -}; - -/** - * The RowMatrixOperations class defines operations - * that may be performed on instances of `RowMatrix`. - */ -template -class RowMatrixOperations { - public: - /** - * Compute (`matrixA` + `matrixB`) and return the result. - * Return `nullptr` if dimensions mismatch for input matrices. - * @param matrixA Input matrix - * @param matrixB Input matrix - * @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); - } - - /** - * Compute the matrix multiplication (`matrixA` * `matrixB` and return the result. - * Return `nullptr` if dimensions mismatch for input matrices. - * @param matrixA Input matrix - * @param matrixB Input matrix - * @return The result of matrix multiplication - */ - static auto Multiply(const RowMatrix *matrixA, const RowMatrix *matrixB) -> std::unique_ptr> { - // TODO(P0): Add implementation - return std::unique_ptr>(nullptr); - } - - /** - * Simplified General Matrix Multiply operation. Compute (`matrixA` * `matrixB` + `matrixC`). - * Return `nullptr` if dimensions mismatch for input matrices. - * @param matrixA Input matrix - * @param matrixB Input matrix - * @param matrixC Input matrix - * @return The result of general matrix multiply - */ - static auto GEMM(const RowMatrix *matrixA, const RowMatrix *matrixB, const RowMatrix *matrixC) - -> std::unique_ptr> { - // TODO(P0): Add implementation - return std::unique_ptr>(nullptr); - } -}; -} // namespace bustub -- Gitee From 9de0d4f78ec02401bfc99050958b0ffb28e78f4b Mon Sep 17 00:00:00 2001 From: xhjgit <10629002+xie-huijuan@user.noreply.gitee.com> Date: Sun, 4 Sep 2022 10:05:41 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=A2=E6=83=A0=E5=A8=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xhjgit <10629002+xie-huijuan@user.noreply.gitee.com> --- hw1/src/include/matrix.h | 372 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 372 insertions(+) create mode 100644 hw1/src/include/matrix.h diff --git a/hw1/src/include/matrix.h b/hw1/src/include/matrix.h new file mode 100644 index 0000000..621df42 --- /dev/null +++ b/hw1/src/include/matrix.h @@ -0,0 +1,372 @@ +//===----------------------------------------------------------------------===// +// +// BusTub +// +// starter_test.cpp +// +// +// Copyright (c) 2015-2020, Carnegie Mellon University Database Group +// +//===----------------------------------------------------------------------===// + +// This file is copied from bustub, thanks to CMU Database Group. + +#pragma once + +#include +#include +#include +#include "common/exception.h" + +namespace bustub { + +/** + * The Matrix type defines a common + * interface for matrix operations. + */ +template +class Matrix { + protected: + /** + * TODO(P0): Add implementation + * + * Construct a new Matrix instance. + * @param rows The number of rows + * @param cols The number of columns + * + */ + Matrix(int rows, int cols) { + + rows_=rows; + cols_=cols; + + + //创建一维数组 + linear_=new T[rows*cols]; + + } + + /** The number of rows in the matrix */ + int rows_; + /** The number of columns in the matrix */ + int cols_; + + /** + * TODO(P0): Allocate the array in the constructor. + * TODO(P0): Deallocate the array in the destructor. + * A flattened array containing the elements of the matrix. + */ + T *linear_; + + public: + /** @return The number of rows in the matrix */ + virtual auto GetRowCount()const -> int{ + return rows_; + } + + /** @return The number of columns in the matrix */ + virtual auto GetColumnCount()const -> int { + return cols_; + } + + /** + * Get the (i,j)th matrix element. + * + * Throw OUT_OF_RANGE if either index is out of range. + * + * @param i The row index + * @param j The column index + * @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{ + 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. + * + * Throw OUT_OF_RANGE if either index is out of range. + * + * @param i The row index + * @param j The column index + * @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) + { + 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`. + * + * Throw OUT_OF_RANGE in the event that `source` + * does not contain the required number of elements. + * + * @param source The source container + * @throws OUT_OF_RANGE if `source` is incorrect size + */ + 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 +class RowMatrix : public Matrix { + public: + /** + * TODO(P0): Add implementation + * + * Construct a new RowMatrix instance. + * @param rows The number of rows + * @param cols The number of columns + */ + RowMatrix(int rows, int cols) : Matrix(rows, cols) + { + + int i; + //创建一个二维数组data,并与linear_对应 + data_ = new T*[rows_]; + for(i=0;i int override + { + return rows_; + } + + + /** + * TODO(P0): Add implementation + * @return The number of columns in the matrix + */ + auto GetColumnCount()const -> int override{ + return cols_; + } + + + /** + * TODO(P0): Add implementation + * + * Get the (i,j)th matrix element. + * + * Throw OUT_OF_RANGE if either index is out of range. + * + * @param i The row index + * @param j The column index + * @return The (i,j)th matrix element + * @throws OUT_OF_RANGE if either index is out of range + */ + auto GetElement(int i, int j) const -> T override { + if(i>=rows_ || j>=cols_) + throw std::range_error("RowMatrix::Either index is out of range!"); + return data[i][j]; + } + + /** + * Set the (i,j)th matrix element. + * + * Throw OUT_OF_RANGE if either index is out of range. + * + * @param i The row index + * @param j The column index + * @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 { + 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 + * + * Fill the elements of the matrix from `source`. + * + * Throw OUT_OF_RANGE in the event that `source` + * does not contain the required number of elements. + * + * @param source The source container + * @throws OUT_OF_RANGE if `source` is incorrect size + */ + void FillFrom(const std::vector &source) override { + int i,j; + if(source.size()!=rows_*cols_) + throw std::range_error("Matrix::`source` is incorrect size!"); + else + { + for(i=0;i +class RowMatrixOperations { + public: + /** + * Compute (`matrixA` + `matrixB`) and return the result. + * Return `nullptr` if dimensions mismatch for input matrices. + * @param matrixA Input matrix + * @param matrixB Input matrix + * @return The result of matrix addition + */ + static auto Add(const RowMatrix *matrixA, const RowMatrix *matrixB) -> std::unique_ptr> { + //判断维度是否一样 + 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 + 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 + -- Gitee