From dd8698dccf1fd44401574de5bcca1f8607c6f934 Mon Sep 17 00:00:00 2001 From: Gurkirst Singh Date: Tue, 8 Oct 2024 23:00:40 +0530 Subject: [PATCH] Add friend operator overloading --- Doxyfile | 2 +- examples/main.cpp | 4 +++ headers/firefly/vector.hpp | 69 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/Doxyfile b/Doxyfile index 3587904..cdf4746 100644 --- a/Doxyfile +++ b/Doxyfile @@ -4,7 +4,7 @@ PROJECT_NAME = "Firefly" PROJECT_BRIEF = "Standalone library for vector calculations" -PROJECT_NUMBER = "3.0.0" +PROJECT_NUMBER = "3.1.0" FILE_PATTERNS = *.cpp *.hpp diff --git a/examples/main.cpp b/examples/main.cpp index 5a860c1..034eac5 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -14,6 +14,10 @@ int main() { std::cout << v3 << std::endl; std::cout << v4 << std::endl; std::cout << v5 << std::endl; + std::cout << 1 + v1 << std::endl; + std::cout << 1.2f + v1 << std::endl; + std::cout << std::complex{1, 2} + v1 << std::endl; + std::cout << std::complex{1.3, 2.5} + v1 << std::endl; std::cout << v1.norm() << std::endl; std::cout << v1.to_normalized().norm() << std::endl; std::cout << firefly::utilities::vector::angle_between(v1, v2) << std::endl diff --git a/headers/firefly/vector.hpp b/headers/firefly/vector.hpp index cafa90e..ad7e47f 100644 --- a/headers/firefly/vector.hpp +++ b/headers/firefly/vector.hpp @@ -83,6 +83,7 @@ struct common_type, std::complex> { */ template using common_type_t = typename common_type::type; + /** * @brief Concept that ensures the type is a complex number type. * @@ -263,6 +264,23 @@ class vector : private std::array { return add(scalar); } + /** + * @brief Adds a scalar to a vector. + * + * This friend function allows adding a scalar to a vector, returning a new vector + * with the scalar added to each element of the original vector. + * + * @tparam U The type of the scalar. + * @param scalar The scalar value to add. + * @param vec The vector to add the scalar to. + * + * @return A new vector with the result of the addition. + */ + template + friend constexpr auto operator+(U const scalar, vector const &vec) { + return vec + scalar; + } + /** * @brief Performs element-wise addition of two vectors using the `+=` operator. * @@ -357,6 +375,23 @@ class vector : private std::array { return subtract(scalar); } + /** + * @brief Subtracts a scalar to a vector. + * + * This friend function allows subtracting a scalar to a vector, returning a new vector + * with the scalar subtracted from each element of the original vector. + * + * @tparam U The type of the scalar. + * @param scalar The scalar value to subtract. + * @param vec The vector to add the scalar to. + * + * @return A new vector with the result of the subtraction. + */ + template + friend constexpr auto operator-(U const scalar, vector const &vec) { + return vec - scalar; + } + /** * @brief Performs element-wise subtraction of another vector using the `-=` operator. * @@ -485,6 +520,23 @@ class vector : private std::array { return scale(scalar); } + /** + * @brief Scales a vector by a scalar. + * + * This friend function allows scaling a vector by a scalar, returning a new vector + * with each element of the original vector multiplied by the scalar. + * + * @tparam U The type of the scalar. + * @param scalar The scalar value to scale by. + * @param vec The vector to scale. + * + * @return A new vector with the result of the scaling. + */ + template + friend constexpr auto operator*(U const scalar, vector const &vec) { + return vec * scalar; + } + /** * @brief Scales the vector in place using the `*=` operator. * @@ -515,6 +567,23 @@ class vector : private std::array { return scale(1 / scalar); } + /** + * @brief Performs inverse scaling of a vector by a scalar. + * + * This friend function allows inverse scaling of a vector by a scalar, returning a new vector + * with each element of the original vector divided by the scalar. + * + * @tparam U The type of the scalar. + * @param scalar The scalar value to inversely scale by. + * @param vec The vector to scale. + * + * @return A new vector with the result of the inverse scaling. + */ + template + friend constexpr auto operator/(U const scalar, vector const &vec) { + return vec / scalar; + } + /** * @brief Scales the vector in place using the `/=` operator. *