From d4a4137c9bfb37922ef053443ae1055fccaf2205 Mon Sep 17 00:00:00 2001 From: TianKai Ma Date: Fri, 15 Dec 2023 16:29:43 +0800 Subject: [PATCH] docs: `Vector.h` --- CustomMath_lib/Matrix/Matrix.h | 4 ++-- CustomMath_lib/Vector/Vector.cpp | 4 ++++ CustomMath_lib/Vector/Vector.h | 26 ++++++++++++++++++++++---- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/CustomMath_lib/Matrix/Matrix.h b/CustomMath_lib/Matrix/Matrix.h index a302015..3f5ff0c 100644 --- a/CustomMath_lib/Matrix/Matrix.h +++ b/CustomMath_lib/Matrix/Matrix.h @@ -87,7 +87,7 @@ class Matrix { [[nodiscard]] Matrix sub_lowerTriangle() const; /// Set the sub-matrix of the matrix [start, end) x [start, end), both use index that start from 0 - /// \warning this is often used in conjunction with sub_matrix() to set a sub-matrix to another matrix + /// \warning this is often used in conjunction with sub_matrix() void set(ull start_row, ull end_row, ull start_col, ull end_col, const Matrix &other); /* @@ -112,7 +112,7 @@ class Matrix { /// A.rows == A.cols [[nodiscard]] bool isSquare() const; - /// (a_{ij}^2)^(1/2) + /// 2-norm [[nodiscard]] lld norm() const; /// min{a_{ij}} diff --git a/CustomMath_lib/Vector/Vector.cpp b/CustomMath_lib/Vector/Vector.cpp index 50cafe8..d1382bd 100644 --- a/CustomMath_lib/Vector/Vector.cpp +++ b/CustomMath_lib/Vector/Vector.cpp @@ -157,6 +157,10 @@ lld Vector::operator*(const Vector &other) const { return result; } +Vector operator*(lld scalar, const Vector &vector) { + return vector * scalar; +} + bool Vector::operator==(const Vector &other) const { if (this->size != other.size) { return false; diff --git a/CustomMath_lib/Vector/Vector.h b/CustomMath_lib/Vector/Vector.h index beab20d..dae5b5e 100644 --- a/CustomMath_lib/Vector/Vector.h +++ b/CustomMath_lib/Vector/Vector.h @@ -18,39 +18,57 @@ class Vector { /// Copy constructor Vector(const Vector &other); + /// size, 0 explicit Vector(ull size); + /// size, default_value explicit Vector(ull size, lld default_value); + /// size, [lower_bound, upper_bound] explicit Vector(ull size, double lower_bound, double upper_bound); + /// 1d vector explicit Vector(const std::vector &array); + /// \warning this constructor is not robust / time-efficient explicit Vector(std::string matlab_array); + /// Return a sub-vector of this vector [start, end) [[nodiscard]] Vector sub_vector(ull start, ull end) const; + /// Set a sub-vector of this vector [start, end) + /// \warning this is often used in conjunction with sub_vector() void set(ull start, ull end, const Vector &other); + /// Print the vector void print(); + /// u == v + [[nodiscard]] bool operator==(const Vector &other) const; + + /// 2-norm + [[nodiscard]] lld norm() const; + + /// u + v [[nodiscard]] Vector operator+(const Vector &other) const; + /// u - v [[nodiscard]] Vector operator-(const Vector &other) const; + /// u * v -> Real [[nodiscard]] lld operator*(const Vector &other) const; + /// u * scalar [[nodiscard]] Vector operator*(lld scalar) const; + /// u / scalar [[nodiscard]] Vector operator/(lld scalar) const; - - [[nodiscard]] bool operator==(const Vector &other) const; - - [[nodiscard]] lld norm() const; }; +/// [1,2,3,0,2] -> [1,1,1,0,1] Vector sign(const Vector &vector); +/// scalar * u Vector operator*(lld scalar, const Vector &vector); #endif //NUMERICAL_ALGEBRA_VECTOR_H