Skip to content
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
284 changes: 153 additions & 131 deletions docs/api/operation/arithmetic.md

Large diffs are not rendered by default.

21 changes: 0 additions & 21 deletions docs/api/operation/combinatoric.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,27 +706,6 @@ void cartesian_product(const std::vector<T>& set1, const std::vector<T>& set2,
---

## Sequences

### Arithmetic Sequences

```c++
void arithmetic_sequence(uint_t first, uint_t diff, unsigned int terms,
std::vector<uint_t>& result);
uint_t arithmetic_sum(uint_t first, uint_t last, unsigned int terms);
uint_t nth_term_arithmetic(uint_t first, uint_t diff, unsigned int n);
```

**Examples:**
```c++
std::vector<uint_t> seq;
arithmetic_sequence(2, 3, 5, seq); // {2, 5, 8, 11, 14}

arithmetic_sum(2, 14, 5); // 40
nth_term_arithmetic(2, 3, 5); // 14
```

---

### Geometric Sequences

```c++
Expand Down
44 changes: 22 additions & 22 deletions include/imeth/operation/arithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace Arithmetic {

// Power and Roots
double power(double base, int exponent);
double squareRoot(double n);
double cubeRoot(double n);
double square_root(double n);
double cube_root(double n);

// Absolute Value and Sign
double absolute(double n);
Expand All @@ -24,13 +24,13 @@ namespace Arithmetic {

// Remainders and Divisibility
int remainder(int a, int b);
bool isDivisible(int a, int b);
bool is_divisible(int a, int b);

// Percentages
double percentOf(double percent, double total);
double whatPercent(double part, double total);
double percentIncrease(double original, double newValue);
double percentDecrease(double original, double newValue);
double percent_of(double percent, double total);
double what_percent(double part, double total);
double percent_increase(double original, double newValue);
double percent_decrease(double original, double newValue);

// Averages and Statistics
double average(const std::vector<double>& numbers);
Expand All @@ -41,30 +41,30 @@ namespace Arithmetic {
double median(std::vector<double> numbers); // Note: not const because we sort

// Fractions (simplified as doubles)
double addFractions(double num1, double den1, double num2, double den2);
double subtractFractions(double num1, double den1, double num2, double den2);
double multiplyFractions(double num1, double den1, double num2, double den2);
double divideFractions(double num1, double den1, double num2, double den2);
double add_fractions(double num1, double den1, double num2, double den2);
double subtract_fractions(double num1, double den1, double num2, double den2);
double multiply_fractions(double num1, double den1, double num2, double den2);
double divide_fractions(double num1, double den1, double num2, double den2);

// Rounding
double roundToNearest(double n);
double roundUp(double n);
double roundDown(double n);
double roundToDecimalPlaces(double n, int places);
double round_to_nearest(double n);
double round_up(double n);
double round_down(double n);
double round_to_decimalPlaces(double n, int places);

// Number Properties
bool isEven(int n);
bool isOdd(int n);
bool isPrime(int n);
int greatestCommonDivisor(int a, int b);
int leastCommonMultiple(int a, int b);
bool is_even(int n);
bool is_odd(int n);
bool is_prime(int n);
int greatest_common_divisor(int a, int b);
int least_common_multiple(int a, int b);

// Distance and Pythagorean
double distance2D(double x1, double y1, double x2, double y2);
double distance_2D(double x1, double y1, double x2, double y2);
double pythagorean(double a, double b); // Returns hypotenuse c

// Simple Interest
double simpleInterest(double principal, double rate, double time);
double simple_interest(double principal, double rate, double time);

// Sequences
void sequence(uint_t first, uint_t diff, unsigned int terms,
Expand Down
12 changes: 6 additions & 6 deletions src/geometry/shape_2d.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../include/imeth/geometry/2D.hpp"
#include "../include/imeth/operation/arithmetic.hpp"
#include "../../include/imeth/geometry/2D.hpp"
#include "../../include/imeth/operation/arithmetic.hpp"
#include <numbers>

namespace imeth {
Expand All @@ -24,7 +24,7 @@ double Triangle::area() const {
}

double Triangle::perimeter() const {
double side = imeth::Arithmetic::squareRoot((base_ / 2) * (base_ / 2) + height_ * height_);
double side = imeth::Arithmetic::square_root((base_ / 2) * (base_ / 2) + height_ * height_);
return base_ + 2 * side;
}

Expand All @@ -37,23 +37,23 @@ double Square::perimeter() const {
}

double Pentagon::area() const {
return 0.25 * imeth::Arithmetic::squareRoot(5 * (5 + 2 * imeth::Arithmetic::squareRoot(5))) * side_ * side_;
return 0.25 * imeth::Arithmetic::square_root(5 * (5 + 2 * imeth::Arithmetic::square_root(5))) * side_ * side_;
}

double Pentagon::perimeter() const {
return 5 * side_;
}

double Hexagon::area() const {
return (3 * imeth::Arithmetic::squareRoot(3) / 2) * side_ * side_;
return (3 * imeth::Arithmetic::square_root(3) / 2) * side_ * side_;
}

double Hexagon::perimeter() const {
return 6 * side_;
}

double Octagon::area() const {
return 2 * (1 + imeth::Arithmetic::squareRoot(2)) * side_ * side_;
return 2 * (1 + imeth::Arithmetic::square_root(2)) * side_ * side_;
}

double Octagon::perimeter() const {
Expand Down
2 changes: 1 addition & 1 deletion src/geometry/shape_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Cone::Cone(const double radius, const double height)
: radius_(radius), height_(height) {}

double Cone::area() const {
double slant_height = imeth::Arithmetic::squareRoot(radius_ * radius_ + height_ * height_);
double slant_height = imeth::Arithmetic::square_root(radius_ * radius_ + height_ * height_);
return std::numbers::pi * radius_ * (radius_ + slant_height);
}

Expand Down
2 changes: 1 addition & 1 deletion src/linear/algebra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ QuadraticEquation::Solution QuadraticEquation::solve(double a, double b, double
}

// Two distinct solutions
double sqrt_discriminant = imeth::Arithmetic::squareRoot(discriminant);
double sqrt_discriminant = imeth::Arithmetic::square_root(discriminant);
double x1 = (-b - sqrt_discriminant) / (2 * a);
double x2 = (-b + sqrt_discriminant) / (2 * a);

Expand Down
50 changes: 25 additions & 25 deletions src/operation/arithmetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ double power(double base, int exponent) {
return result;
}

double squareRoot(double n) {
double square_root(double n) {
if (n < 0) {
throw std::invalid_argument("Cannot take square root of negative number");
}
Expand All @@ -42,7 +42,7 @@ double squareRoot(double n) {
return z;
}

double cubeRoot(double n) {
double cube_root(double n) {
if (n == 0) return 0;
bool negative = n < 0;
if (negative) n = -n;
Expand Down Expand Up @@ -83,31 +83,31 @@ int remainder(int a, int b) {
return a % b;
}

bool isDivisible(int a, int b) {
bool is_divisible(int a, int b) {
if (b == 0) return false;
return (a % b) == 0;
}

// Percentages
double percentOf(double percent, double total) {
double percent_of(double percent, double total) {
return (percent / 100.0) * total;
}

double whatPercent(double part, double total) {
double what_percent(double part, double total) {
if (total == 0) {
throw std::invalid_argument("Total cannot be zero");
}
return (part / total) * 100.0;
}

double percentIncrease(double original, double newValue) {
double percent_increase(double original, double newValue) {
if (original == 0) {
throw std::invalid_argument("Original value cannot be zero");
}
return ((newValue - original) / original) * 100.0;
}

double percentDecrease(double original, double newValue) {
double percent_decrease(double original, double newValue) {
if (original == 0) {
throw std::invalid_argument("Original value cannot be zero");
}
Expand Down Expand Up @@ -166,7 +166,7 @@ double median(std::vector<double> numbers) {
}

// Fractions
double addFractions(const double num1, const double den1, const double num2, const double den2) {
double add_fractions(const double num1, const double den1, const double num2, const double den2) {
if (den1 == 0 || den2 == 0) {
throw std::invalid_argument("Denominator cannot be zero");
}
Expand All @@ -177,7 +177,7 @@ double addFractions(const double num1, const double den1, const double num2, con
return (newNum1 + newNum2) / commonDen;
}

double subtractFractions(const double num1, const double den1, const double num2, const double den2) {
double subtract_fractions(const double num1, const double den1, const double num2, const double den2) {
if (den1 == 0 || den2 == 0) {
throw std::invalid_argument("Denominator cannot be zero");
}
Expand All @@ -187,22 +187,22 @@ double subtractFractions(const double num1, const double den1, const double num2
return (newNum1 - newNum2) / commonDen;
}

double multiplyFractions(const double num1, const double den1, const double num2, const double den2) {
double multiply_fractions(const double num1, const double den1, const double num2, const double den2) {
if (den1 == 0 || den2 == 0) {
throw std::invalid_argument("Denominator cannot be zero");
}
return (num1 * num2) / (den1 * den2);
}

double divideFractions(const double num1, const double den1, const double num2, const double den2) {
double divide_fractions(const double num1, const double den1, const double num2, const double den2) {
if (den1 == 0 || den2 == 0 || num2 == 0) {
throw std::invalid_argument("Denominator cannot be zero and cannot divide by zero");
}
return (num1 * den2) / (den1 * num2);
}

// Rounding
double roundToNearest(const double n) {
double round_to_nearest(const double n) {
if (n >= 0) {
return static_cast<double>(static_cast<long long>(n + 0.5));
} else {
Expand All @@ -211,37 +211,37 @@ double roundToNearest(const double n) {
// this can give incorrect results... Do I have to wrap lround() from cmath here?????
}

double roundUp(const double n) {
double round_up(const double n) {
const auto intPart = static_cast<long long>(n);
if (n > 0 && n > static_cast<double>(intPart)) {
return static_cast<double>(intPart + 1);
}
return static_cast<double>(intPart);
}

double roundDown(const double n) {
double round_down(const double n) {
const auto intPart = static_cast<long long>(n);
if (n < 0 && n < static_cast<double>(intPart)) {
return static_cast<double>(intPart - 1);
}
return static_cast<double>(intPart);
}

double roundToDecimalPlaces(const double n, const int places) {
double round_to_decimalPlaces(const double n, const int places) {
const double multiplier = power(10.0, places);
return roundToNearest(n * multiplier) / multiplier;
return round_to_nearest(n * multiplier) / multiplier;
}

// Number Properties
bool isEven(const int n) {
bool is_even(const int n) {
return n % 2 == 0;
}

bool isOdd(const int n) {
return n % 2 != 0;
}

bool isPrime(const int n) {
bool is_prime(const int n) {
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
Expand All @@ -253,7 +253,7 @@ bool isPrime(const int n) {
return true;
}

int greatestCommonDivisor(int a, int b) {
int greatest_common_divisor(int a, int b) {
a = absolute(a);
b = absolute(b);

Expand All @@ -265,24 +265,24 @@ int greatestCommonDivisor(int a, int b) {
return a;
}

int leastCommonMultiple(const int a, const int b) {
int least_common_multiple(const int a, const int b) {
if (a == 0 || b == 0) return 0;
return absolute(a * b) / greatestCommonDivisor(a, b);
return absolute(a * b) / greatest_common_divisor(a, b);
}

// Distance and Pythagorean
double distance2D(const double x1, const double y1, const double x2, const double y2) {
double distance_2D(const double x1, const double y1, const double x2, const double y2) {
const double dx = x2 - x1;
const double dy = y2 - y1;
return squareRoot(dx * dx + dy * dy);
return square_root(dx * dx + dy * dy);
}

double pythagorean(const double a, const double b) {
return squareRoot(a * a + b * b);
return square_root(a * a + b * b);
}

// Simple Interest
double simpleInterest(const double principal, const double rate, const double time) {
double simple_interest(const double principal, const double rate, const double time) {
return principal * (rate / 100.0) * time;
}

Expand Down
2 changes: 1 addition & 1 deletion src/operation/combinatoric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ namespace imeth::Combinatorics {
}

double standard_deviation(const std::vector<double>& values, const std::vector<double>& probabilities) {
return Arithmetic::squareRoot(variance(values, probabilities));
return Arithmetic::square_root(variance(values, probabilities));
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ int main() {
std::cout << "10 + 5 = " << imeth::Arithmetic::add(10, 5) << "\n";
std::cout << "10 * 5 = " << imeth::Arithmetic::multiply(10, 5) << "\n";
std::cout << "2^5 = " << imeth::Arithmetic::power(2, 5) << "\n";
std::cout << "Square root of 144 = " << imeth::Arithmetic::squareRoot(144) << "\n\n";
std::cout << "Square root of 144 = " << imeth::Arithmetic::square_root(144) << "\n\n";

// Percentages
std::cout << "25% of 80 = " << imeth::Arithmetic::percentOf(25, 80) << "\n";
std::cout << "20 is what % of 50? = " << imeth::Arithmetic::whatPercent(20, 50) << "%\n\n";
std::cout << "25% of 80 = " << imeth::Arithmetic::percent_of(25, 80) << "\n";
std::cout << "20 is what % of 50? = " << imeth::Arithmetic::what_percent(20, 50) << "%\n\n";

// Statistics
const std::vector<double> grades = {85, 90, 78, 92, 88};
Expand All @@ -62,9 +62,9 @@ int main() {
std::cout << "Lowest: " << imeth::Arithmetic::minimum(grades) << "\n\n";

// Number properties
std::cout << "Is 17 prime? " << (imeth::Arithmetic::isPrime(17) ? "Yes" : "No") << "\n";
std::cout << "Is 20 even? " << (imeth::Arithmetic::isEven(20) ? "Yes" : "No") << "\n";
std::cout << "GCD of 48 and 18 = " << imeth::Arithmetic::greatestCommonDivisor(48, 18) << "\n\n";
std::cout << "Is 17 prime? " << (imeth::Arithmetic::is_prime(17) ? "Yes" : "No") << "\n";
std::cout << "Is 20 even? " << (imeth::Arithmetic::is_even(20) ? "Yes" : "No") << "\n";
std::cout << "GCD of 48 and 18 = " << imeth::Arithmetic::greatest_common_divisor(48, 18) << "\n\n";

// Quadratic equation
if (auto result = imeth::QuadraticEquation::solve(1, -5, 6); std::holds_alternative<std::pair<double, double>>(result)) {
Expand Down