MathLibrary
is a C++ class designed to provide a wide range of mathematical functions and utilities, including arithmetic operations, factorial and Fibonacci calculations, string manipulations, and numerical methods for derivatives and integrals. This library can be particularly useful in mathematical modeling, scientific computing, and algorithm development.
- Arithmetic Operations: Perform basic operations such as addition, subtraction, multiplication, and division.
- Mathematical Functions: Calculate ceiling, floor, maximum, minimum, power, and square.
- Factorial Calculation: Get the factorial of a non-negative integer.
- Fibonacci Sequence Generation: Compute Fibonacci numbers up to a specified index.
- String Manipulation: Reverse strings for various applications.
- Numerical Methods: Approximate derivatives and integrals for continuous functions.
-
Arithmetic Operations:
- Addition:
add(double a, double b)
: Returns the sum ofa
andb
. - Subtraction:
subtract(double a, double b)
: Returns the difference ofa
andb
. - Multiplication:
multiply(double a, double b)
: Returns the product ofa
andb
. - Division:
divide(double a, double b)
: Returns the quotient ofa
andb
. Throws an exception ifb
is zero.
- Addition:
-
Mathematical Functions:
- Ceiling:
ceil(double a)
: Returns the smallest integer greater than or equal toa
. - Floor:
floor(double a)
: Returns the largest integer less than or equal toa
. - Maximum:
maximum(double a, double b)
: Returns the larger ofa
andb
. - Minimum:
minimum(double a, double b)
: Returns the smaller ofa
andb
. - Power:
power(double base, double exp)
: Returnsbase
raised to the power ofexp
. - Square:
square(double a)
: Returns the square ofa
.
- Ceiling:
-
Special Functions:
- Factorial:
factorial(int n)
: Returns the factorial ofn
. Throws an exception ifn
is negative. - Fibonacci:
fibonacci(int n)
: Returns the nth Fibonacci number.
- Factorial:
-
String Manipulation:
- Reverse String:
reverseString(const std::string& str)
: Returns the reversed version of the input string.
- Reverse String:
-
Numerical Methods:
- Derivative:
derivative(double (*func)(double), double x, double h)
: Approximates the derivative offunc
at pointx
using central difference. - Integral:
integral(double (*func)(double), double a, double b, int n)
: Approximates the integral offunc
froma
tob
using the trapezoidal rule.
- Derivative:
-
Matrix Operations in C++:
- Matrix Multiplication: Multiplies two matrices.
- Determinant Calculation: Computes the determinant of a square matrix.
Create a file named MathLibrary.h
and include the following code:
#ifndef MATH_LIBRARY_H
#define MATH_LIBRARY_H
#include <cmath>
#include <algorithm>
#include <stdexcept>
#include <string>
class MathLibrary {
public:
static double add(double a, double b);
static double subtract(double a, double b);
static double multiply(double a, double b);
static double divide(double a, double b);
static double ceil(double a);
static double floor(double a);
static double maximum(double a, double b);
static double minimum(double a, double b);
static double power(double base, double exp);
static double square(double a);
static unsigned long long factorial(int n);
static unsigned long long fibonacci(int n);
static std::string reverseString(const std::string& str);
static double powerMToN(double m, double n);
static double derivative(double (*func)(double), double x, double h = 1e-5);
static double integral(double (*func)(double), double a, double b, int n = 1000);
static std::vector<std::vector<double>> multiply(const std::vector<std::vector<double>>& A, const std::vector<std::vector<double>>& B);
static double determinant(const std::vector<std::vector<double>>& matrix);
};
#endif // MATH_LIBRARY_H