Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit cfb09b6

Browse files
author
Lennart
committed
Merge v0.2.0 to stable
* feat: add sketch for Vector2 class * test: add assertTrue function * fix: typo error * feat: replace Vector2 with vector templates * feat: replace vector templates with dynamic vectors * test: add vector test to build * test: add assertError function * chore: replace GNU++11 with standard C++11 * feat: optimize and add vector operations * test: extend vector tests * doc: add equal function * chore: fix build status url * chore: update license to 2016 * chore: fix build status image * chore: fix formatting * doc: add constant and typedef comments * fix: remove unused includes * doc: vector operation doc * feat: add vector unary minus operator * test: add scalar multiplication and inversion tests * test: extend assertError message * test: add vector dot product tests * test: fix message format * test: add vector cross product tests * test: add vector sum operation tests * feat: add vector norm and length operations * fix: remove unnessecary includes * feat: add vector to_string operation * fix: block operations for null-sized tuples * test: add vector norm operation tests * fix: minimize function overhead * fix: root bug * fix: add error message * test: add vector length tests * feature: reduce copying of constants * doc: add vector guide to README
1 parent e9dae2b commit cfb09b6

File tree

8 files changed

+495
-130
lines changed

8 files changed

+495
-130
lines changed

LICENSE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Mooxmirror
3+
Copyright (c) 2016 Lennart Espe
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22-

Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Compiler flags
2-
CFLAGS=-I./include -std=gnu++11 -Wall
3-
CFLAGS_LIB=-I./include -std=gnu++11 -c
4-
CFLAGS_TEST=test/tmath_test.cpp build/libtmath.a
2+
CFLAGS=-I./include -std=c++11 -Wall
3+
CFLAGS_LIB=-I./include -std=c++11 -c
4+
CFLAGS_TEST=./build/libtmath.a test/tmath_test.cpp
55

66
all: lib test
77
@echo Done.
88

99
lib: build_folder tmath.o
1010
ar rcs build/libtmath.a build/tmath.o
1111

12-
test: test_sine test_cosine test_tangent test_cosecant test_cotangent test_secant test_rad_deg test_abs test_factorial test_roots test_power test_exp_log
12+
test: test_sine test_cosine test_tangent test_cosecant test_cotangent test_secant test_rad_deg test_abs test_factorial test_roots test_power test_exp_log test_vectors
1313
@echo all tests passed
1414

1515
build_folder:
@@ -66,6 +66,10 @@ test_exp_log: test_folder
6666
$(CC) $(CFLAGS) test/exp-log/test.cpp -o build/test/exp-log $(CFLAGS_TEST)
6767
@build/test/exp-log
6868

69+
test_vectors: test_folder
70+
$(CC) $(CFLAGS) test/vectors/test.cpp -o build/test/vectors $(CFLAGS_TEST)
71+
@build/test/vectors
72+
6973
tmath.o: build_folder
7074
$(CC) $(CFLAGS_LIB) src/tmath.cpp -o build/tmath.o
7175

README.md

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# TMath [![Build status](https://travis-ci.org/mooxmirror/tmath.svg?branch=stable)](https://travis-ci.org/mooxmirror/tmath)
1+
# TMath [![Build status](https://travis-ci.org/lnsp/tmath.svg?branch=stable)](https://travis-ci.org/lnsp/tmath)
22
A small math function collection based on the Taylor expansion series.
33

44
## Building the project
@@ -12,50 +12,71 @@ A small math function collection based on the Taylor expansion series.
1212
Just build it as described above, include the header files and link the library. The library uses the types `TMath::DOUBLE`(`long double`) and `TMath::LONG`(`long long`) for parameters and return values.
1313

1414
## What is included?
15+
### Mathematical functions
1516

16-
Function | Description
17-
:------------------------: | ---------------------------------------
18-
`sin(DOUBLE x)` | sine of x
19-
`asin(DOUBLE x)` | arcsine of x
20-
`sinh(DOUBLE x)` | hyperbolic sine of x
21-
`cos(DOUBLE x)` | cosine of x
22-
`acos(DOUBLE x)` | arccosine of x
23-
`cosh(DOUBLE x)` | hyperbolic cosine of x
24-
`tan(DOUBLE x)` | tangent of x
25-
`atan(DOUBLE x)` | arctangent of x
26-
`cot(DOUBLE x)` | cotangent of x
27-
`acot(DOUBLE x)` | arccotangent of x
28-
`coth(DOUBLE x)` | hyperbolic cotangent of x
29-
`sec(DOUBLE x)` | secant of x
30-
`asec(DOUBLE x)` | arcsecant of x
31-
`sech(DOUBLE x)` | hyperbolic secant of x
32-
`cosec(DOUBLE x)` | cosecant of x
33-
`acsc(DOUBLE x)` | arccosecant of x
34-
`csch(DOUBLE x)` | hyperbolic cosecant of x
35-
`floor(DOUBLE x)` | next lower integer of x
36-
`ceil(DOUBLE x)` | next higher integer of x
37-
`mod(LONG x, LONG y)` | the remainder of the division x / y
38-
`exp(DOUBLE x)` | natural exponential function
39-
`sqrt(DOUBLE x)` | squareroot of x
40-
`root(DOUBLE x, DOUBLE n)` | n-th root of x
41-
`ln(DOUBLE x)` | natural logarithm of x
42-
`lg(DOUBLE x)` | common logarithm of x
43-
`lb(DOUBLE x)` | binary logarithm of x
44-
`log(DOUBLE x, DOUBLE n)` | logarithm with base n of x
45-
`pow(DOUBLE x, DOUBLE n)` | x to the power of n
46-
`pow(LONG x, LONG n)` | x to the power of n
47-
`pow(DOUBLE x, LONG n)` | x to the power of n
48-
`fac(LONG n)` | factorial of n
49-
`facd(LONG n)` | factorial of n using floating point
50-
`oddfac(LONG n)` | odd-factorial of n
51-
`oddfacd(LONG n)` | odd-factorial of n using floating point
52-
`rad(DOUBLE x)` | degrees to radiant
53-
`deg(DOUBLE x)` | radiant to degrees
54-
`abs(DOUBLE x)` | absolute value of x
17+
Function | Description
18+
:-------------------------------------: | ---------------------------------------------------------
19+
`sin(DOUBLE x)` | sine of x
20+
`asin(DOUBLE x)` | arcsine of x
21+
`sinh(DOUBLE x)` | hyperbolic sine of x
22+
`cos(DOUBLE x)` | cosine of x
23+
`acos(DOUBLE x)` | arccosine of x
24+
`cosh(DOUBLE x)` | hyperbolic cosine of x
25+
`tan(DOUBLE x)` | tangent of x
26+
`atan(DOUBLE x)` | arctangent of x
27+
`cot(DOUBLE x)` | cotangent of x
28+
`acot(DOUBLE x)` | arccotangent of x
29+
`coth(DOUBLE x)` | hyperbolic cotangent of x
30+
`sec(DOUBLE x)` | secant of x
31+
`asec(DOUBLE x)` | arcsecant of x
32+
`sech(DOUBLE x)` | hyperbolic secant of x
33+
`cosec(DOUBLE x)` | cosecant of x
34+
`acsc(DOUBLE x)` | arccosecant of x
35+
`csch(DOUBLE x)` | hyperbolic cosecant of x
36+
`floor(DOUBLE x)` | next lower integer of x
37+
`ceil(DOUBLE x)` | next higher integer of x
38+
`mod(LONG x, LONG y)` | the remainder of the division x / y
39+
`exp(DOUBLE x)` | natural exponential function
40+
`sqrt(DOUBLE x)` | squareroot of x
41+
`root(DOUBLE x, DOUBLE n)` | n-th root of x
42+
`ln(DOUBLE x)` | natural logarithm of x
43+
`lg(DOUBLE x)` | common logarithm of x
44+
`lb(DOUBLE x)` | binary logarithm of x
45+
`log(DOUBLE x, DOUBLE n)` | logarithm with base n of x
46+
`pow(DOUBLE x, DOUBLE n)` | x to the power of n
47+
`pow(LONG x, LONG n)` | x to the power of n
48+
`pow(DOUBLE x, LONG n)` | x to the power of n
49+
`fac(LONG n)` | factorial of n
50+
`facd(LONG n)` | factorial of n using floating point
51+
`oddfac(LONG n)` | odd-factorial of n
52+
`oddfacd(LONG n)` | odd-factorial of n using floating point
53+
`rad(DOUBLE x)` | degrees to radiant
54+
`deg(DOUBLE x)` | radiant to degrees
55+
`abs(DOUBLE x)` | absolute value of x
56+
`equal(DOUBLE x, DOUBLE y)` | floating-point number equality
57+
`equal(DOUBLE x, DOUBLE y, DOUBLE eps)` | floating-point number equality with a variance of epsilon
58+
59+
### Vector class
60+
To initialize a new vector just use initializer lists (like `Vector {1, 2, 3}`) or create a null-vector using `Vector(n)` (where n is the number of dimensions).
61+
62+
Operation | Description
63+
--------------- | -------------------------------------------------------------
64+
`a[n]` | Access the n-th element of the vector `a`
65+
`a + b` | Adds vector `a` and `b`
66+
`a - b` | Subtracts vector `a` from `b`
67+
`a * s` | Scales the vector by the factor `s`
68+
`a / s` | Scales the vector by the factor `1 / s`
69+
`a == b` | Tests if vector `a` is equal to `b`
70+
`a != b` | Tests if vector `a` is not equal to `b`
71+
`a.equal(b, e)` | Tests if the vector `a` is equal to `b` with the accuracy `e`
72+
`a.dot(b)` | Dot product of `a` and `b`.
73+
`a.cross(b)` | Cross product of `a` and `b`
74+
`a.norm()` | Normalized copy of the vector `a`
75+
`a.length()` | Length of vector `a`
76+
`a.dim()` | Dimensions of vector `a`
77+
`a.to_string()` | Generates a string representation of the vector `a`
5578

5679
## What is planned?
5780
- Statistics (planned for v0.3)
58-
- Vectors (planned for v0.2)
59-
- Matrices (planned for v0.2)
60-
- Vector operations (planned for v0.2)
61-
- Matrix operations (planned for v0.2)
81+
- Matrices (planned for v0.3)
82+
- Matrix operations (planned for v0.3)

include/tmath.hpp

Lines changed: 82 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,105 @@
11
#ifndef _TMATH_HPP
22
#define _TMATH_HPP
33

4+
#include <vector>
5+
#include <initializer_list>
6+
#include <string>
7+
48
namespace TMath {
9+
// Shortform for long long
510
typedef long long LONG;
11+
// Shortform for long double
612
typedef long double DOUBLE;
13+
// A estimation of Pi
714
const DOUBLE PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286;
15+
// A estimation of e
816
const DOUBLE E = 2.718281828459045235360287471352662497757247093699959574966967627724076630354;
17+
// Constant for equality comparison for floating point numbers
18+
const DOUBLE EQUAL_EPSILON = 1e-7;
19+
// Mismatched dimensions error for vectors and matrices
20+
const std::string DIMENSION_ERROR = "Mismatched dimensions";
21+
// Error if the operation is not applicable
22+
const std::string BAD_OPERATION = "Operation is not applicable";
23+
// Vector length is equal to zero
24+
const std::string ZERO_LENGTH = "Vector has length 0";
25+
26+
LONG floor(const DOUBLE&);
27+
LONG ceil(const DOUBLE&);
28+
DOUBLE mod(const DOUBLE&, const DOUBLE&);
929

10-
LONG floor(DOUBLE x);
11-
LONG ceil(DOUBLE x);
12-
DOUBLE mod(DOUBLE x, DOUBLE y);
30+
DOUBLE sin(const DOUBLE&); // sine
31+
DOUBLE asin(const DOUBLE&); // arcsine
32+
DOUBLE sinh(const DOUBLE&); // hyperbolic sine
1333

14-
DOUBLE sin(DOUBLE x); // sine
15-
DOUBLE asin(DOUBLE x); // arcsine
16-
DOUBLE sinh(DOUBLE x); // hyperbolic sine
34+
DOUBLE cos(const DOUBLE&); // cosine
35+
DOUBLE acos(const DOUBLE&); // arccosine
36+
DOUBLE cosh(const DOUBLE&); //hyperbolic cosine
1737

18-
DOUBLE cos(DOUBLE x); // cosine
19-
DOUBLE acos(DOUBLE x); // arccosine
20-
DOUBLE cosh(DOUBLE x); //hyperbolic cosine
38+
DOUBLE tan(const DOUBLE&); // tangent
39+
DOUBLE atan(const DOUBLE&); // arctangent
40+
DOUBLE tanh(const DOUBLE&); // hyperbolic tangent
2141

22-
DOUBLE tan(DOUBLE x); // tangent
23-
DOUBLE atan(DOUBLE x); // arctangent
24-
DOUBLE tanh(DOUBLE x); // hyperbolic tangent
42+
DOUBLE cot(const DOUBLE&); // cotangent
43+
DOUBLE acot(const DOUBLE&); // arccotangent
44+
DOUBLE coth(const DOUBLE&); // hyperbolic cotangent
2545

26-
DOUBLE cot(DOUBLE x); // cotangent
27-
DOUBLE acot(DOUBLE x); // arccotangent
28-
DOUBLE coth(DOUBLE x); // hyperbolic cotangent
46+
DOUBLE sec(const DOUBLE&); // secant
47+
DOUBLE asec(const DOUBLE&); // arcsecant
48+
DOUBLE sech(const DOUBLE&); // hyperbolic secant
2949

30-
DOUBLE sec(DOUBLE x); // secant
31-
DOUBLE asec(DOUBLE x); // arcsecant
32-
DOUBLE sech(DOUBLE x); // hyperbolic secant
50+
DOUBLE csc(const DOUBLE&); // cosecant
51+
DOUBLE acsc(const DOUBLE&); // arccosecant
52+
DOUBLE csch(const DOUBLE&); // hyperbolic cosecant
3353

34-
DOUBLE csc(DOUBLE x); // cosecant
35-
DOUBLE acsc(DOUBLE x); // arccosecant
36-
DOUBLE csch(DOUBLE x); // hyperbolic cosecant
54+
DOUBLE rad(const DOUBLE&); // degrees to radians
55+
DOUBLE deg(const DOUBLE&); // radians to degrees
3756

38-
DOUBLE rad(DOUBLE deg); // degrees to radians
39-
DOUBLE deg(DOUBLE rad); // radians to degrees
57+
DOUBLE exp(const DOUBLE&);
58+
DOUBLE sqrt(const DOUBLE&);
59+
DOUBLE root(const DOUBLE&, const DOUBLE&);
60+
DOUBLE ln(const DOUBLE&);
61+
DOUBLE lg(const DOUBLE&);
62+
DOUBLE lb(const DOUBLE&);
63+
DOUBLE log(const DOUBLE&, const DOUBLE&);
4064

41-
DOUBLE exp(DOUBLE x);
42-
DOUBLE sqrt(DOUBLE x);
43-
DOUBLE root(DOUBLE x, DOUBLE n);
44-
DOUBLE ln(DOUBLE x);
45-
DOUBLE lg(DOUBLE x);
46-
DOUBLE lb(DOUBLE x);
47-
DOUBLE log(DOUBLE x, DOUBLE n);
65+
DOUBLE pow(const DOUBLE&, const DOUBLE&);
66+
DOUBLE pow(const DOUBLE&, const LONG&);
67+
LONG pow(const LONG&, const LONG&);
4868

49-
DOUBLE pow(DOUBLE x, DOUBLE n);
50-
DOUBLE pow(DOUBLE x, LONG n);
51-
LONG pow(LONG x, LONG n);
69+
LONG fac(const LONG&);
70+
DOUBLE facd(const LONG&);
71+
LONG oddfac(const LONG&);
72+
DOUBLE oddfacd(const LONG&);
5273

53-
LONG fac(LONG n);
54-
DOUBLE facd(LONG n);
55-
LONG oddfac(LONG n);
56-
DOUBLE oddfacd(LONG n);
74+
DOUBLE abs(const DOUBLE&);
75+
DOUBLE equal(const DOUBLE&, const DOUBLE&);
76+
DOUBLE equal(const DOUBLE&, const DOUBLE&, const DOUBLE&);
5777

58-
DOUBLE abs(DOUBLE x);
78+
class Vector {
79+
private:
80+
std::vector<DOUBLE> elements;
81+
int checkDimensions(const Vector&) const;
82+
public:
83+
Vector(std::initializer_list<DOUBLE> list) : elements(list) {}
84+
Vector(const int& d) : elements(d) {}
85+
Vector(const Vector& v) : elements(v.elements) {};
86+
DOUBLE& operator[](const int&);
87+
Vector operator+(const Vector&) const;
88+
Vector operator-() const;
89+
Vector operator-(const Vector&) const;
90+
Vector operator*(const DOUBLE&) const;
91+
Vector operator/(const DOUBLE&) const;
92+
bool equal(const Vector&, const DOUBLE&) const;
93+
bool operator==(const Vector&) const;
94+
bool operator!=(const Vector&) const;
95+
DOUBLE dot(const Vector&) const;
96+
Vector cross(const Vector&) const;
97+
DOUBLE sum() const;
98+
Vector norm() const;
99+
DOUBLE length() const;
100+
int dim() const;
101+
std::string to_string() const;
102+
};
59103
}
60104

61105
#endif

include/tmath_test.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
#define _TMATH_TEST_HPP
33

44
#include <string>
5+
#include <functional>
56
#include "tmath.hpp"
67

78
namespace TMathTest {
89
const TMath::DOUBLE DEFAULT_TOLERANCE = 0.001;
910
bool equal(TMath::DOUBLE x, TMath::DOUBLE y, TMath::DOUBLE tolerance);
1011
void assert(TMath::DOUBLE value, TMath::DOUBLE correct, std::string expression);
1112
void assert(TMath::DOUBLE value, TMath::DOUBLE correct, std::string expression, TMath::DOUBLE tolerance);
13+
void assertTrue(bool b, std::string expression);
14+
void assertError(std::function<void ()>, std::string expression);
1215
}
1316

1417
#endif

0 commit comments

Comments
 (0)