Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add friend operator overloading #17

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>{1, 2} + v1 << std::endl;
std::cout << std::complex<float>{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
Expand Down
69 changes: 69 additions & 0 deletions headers/firefly/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct common_type<std::complex<T1>, std::complex<T2>> {
*/
template <typename T1, typename T2>
using common_type_t = typename common_type<T1, T2>::type;

/**
* @brief Concept that ensures the type is a complex number type.
*
Expand Down Expand Up @@ -263,6 +264,23 @@ class vector : private std::array<T, Length> {
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 <vector_type U>
friend constexpr auto operator+(U const scalar, vector<T, Length> const &vec) {
return vec + scalar;
}

/**
* @brief Performs element-wise addition of two vectors using the `+=` operator.
*
Expand Down Expand Up @@ -357,6 +375,23 @@ class vector : private std::array<T, Length> {
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 <vector_type U>
friend constexpr auto operator-(U const scalar, vector<T, Length> const &vec) {
return vec - scalar;
}

/**
* @brief Performs element-wise subtraction of another vector using the `-=` operator.
*
Expand Down Expand Up @@ -485,6 +520,23 @@ class vector : private std::array<T, Length> {
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 <vector_type U>
friend constexpr auto operator*(U const scalar, vector<T, Length> const &vec) {
return vec * scalar;
}

/**
* @brief Scales the vector in place using the `*=` operator.
*
Expand Down Expand Up @@ -515,6 +567,23 @@ class vector : private std::array<T, Length> {
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 <vector_type U>
friend constexpr auto operator/(U const scalar, vector<T, Length> const &vec) {
return vec / scalar;
}

/**
* @brief Scales the vector in place using the `/=` operator.
*
Expand Down
Loading