Skip to content

Commit a72bacb

Browse files
committed
Introducing calculus::nInt.
1 parent c349d9e commit a72bacb

File tree

7 files changed

+226
-0
lines changed

7 files changed

+226
-0
lines changed

.idea/editor.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ SET(COMPONENTS
9595
base::baseConvert
9696
base::decimalConvert
9797
calc::abs
98+
calculus::nInt
9899
calc::add
99100
calc::atan2
100101
calc::comparison

include/fn/calculus.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 NWSOFT *
3+
* *
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy *
5+
* of this software and associated documentation files (the "Software"), to deal *
6+
* in the Software without restriction, including without limitation the rights *
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8+
* copies of the Software, and to permit persons to whom the Software is *
9+
* furnished to do so, subject to the following conditions: *
10+
* *
11+
* The above copyright notice and this permission notice shall be included in all *
12+
* copies or substantial portions of the Software. *
13+
* *
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20+
* SOFTWARE. *
21+
**************************************************************************************************/
22+
23+
#pragma once
24+
25+
#include <functional>
26+
#include <string>
27+
28+
/**
29+
* @namespace steppable::__internals::calculus
30+
* @brief Contains calculus functions.
31+
*/
32+
namespace steppable::__internals::calculus
33+
{
34+
std::string romberg(const std::function<std::string(std::string)>& f,
35+
const std::string& a,
36+
const std::string& b,
37+
int max_steps,
38+
int decimals);
39+
}

src/calculus/nInt/nInt.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 NWSOFT *
3+
* *
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy *
5+
* of this software and associated documentation files (the "Software"), to deal *
6+
* in the Software without restriction, including without limitation the rights *
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8+
* copies of the Software, and to permit persons to whom the Software is *
9+
* furnished to do so, subject to the following conditions: *
10+
* *
11+
* The above copyright notice and this permission notice shall be included in all *
12+
* copies or substantial portions of the Software. *
13+
* *
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20+
* SOFTWARE. *
21+
**************************************************************************************************/
22+
23+
#include "fn/basicArithm.hpp"
24+
#include "rounding.hpp"
25+
26+
#include <concepts>
27+
#include <functional>
28+
#include <string>
29+
#include <vector>
30+
31+
using namespace steppable::__internals::arithmetic;
32+
using namespace steppable::__internals::numUtils;
33+
using namespace std::literals;
34+
35+
namespace steppable::__internals::calculus
36+
{
37+
std::string romberg(const std::function<std::string(std::string)>& f,
38+
const std::string& a,
39+
const std::string& b,
40+
const int max_steps,
41+
const int decimals)
42+
{
43+
auto acc = "0." + std::string(decimals - 1, '0') + "1";
44+
auto Rp = std::vector(max_steps, "0"s);
45+
auto Rc = std::vector(max_steps, "0"s);
46+
47+
auto h = subtract(b, a, 0);
48+
auto fAB = add(f(a), f(b), 0);
49+
auto halfH = multiply(h, "0.5", 0);
50+
Rp.front() = multiply(halfH, fAB, 0);
51+
52+
for (int i = 1; i < max_steps; i++)
53+
{
54+
h = multiply(h, "0.5", 0);
55+
auto c = "0"s;
56+
long ep = 1 << (i - 1); // 2^(i - 1)
57+
for (long j = 1; j < (ep + 1); j++)
58+
{
59+
auto d = multiply(std::to_string((2 * j) - 1), h, 0);
60+
c = add(c, f(add(a, d, 0)), 0);
61+
}
62+
Rc.front() = add(multiply(h, c, 0), multiply("0.5", Rp.front(), 0), 0);
63+
64+
for (int j = 1; j < (i + 1); j++)
65+
{
66+
long double n_k = pow(4, j);
67+
auto one = multiply(std::to_string(n_k), Rc.at(j - 1), 0);
68+
auto top = subtract(one, Rp.at(j - 1), 0);
69+
Rc.at(j) = divide(top, std::to_string(n_k - 1), 0, decimals + 1);
70+
71+
if (i > 1 and compare(abs(subtract(Rp.at(i - 1), Rc.at(i), 0), 0), acc, 0) == "0")
72+
return roundOff(Rc.at(i), decimals);
73+
}
74+
75+
std::ranges::swap(Rp, Rc);
76+
}
77+
78+
return roundOff(Rp.at(max_steps - 1), decimals);
79+
}
80+
} // namespace steppable::__internals::calculus
81+
82+
#ifndef NO_MAIN
83+
int main() {}
84+
#endif

src/calculus/nInt/nIntReport.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 NWSOFT *
3+
* *
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy *
5+
* of this software and associated documentation files (the "Software"), to deal *
6+
* in the Software without restriction, including without limitation the rights *
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8+
* copies of the Software, and to permit persons to whom the Software is *
9+
* furnished to do so, subject to the following conditions: *
10+
* *
11+
* The above copyright notice and this permission notice shall be included in all *
12+
* copies or substantial portions of the Software. *
13+
* *
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20+
* SOFTWARE. *
21+
**************************************************************************************************/
22+
23+
#include "nIntReport.hpp"
24+
25+
#include <string>
26+
27+
std::string reportNInt()
28+
{
29+
// Intentionally not implemented.
30+
return "";
31+
}

src/calculus/nInt/nIntReport.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 NWSOFT *
3+
* *
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy *
5+
* of this software and associated documentation files (the "Software"), to deal *
6+
* in the Software without restriction, including without limitation the rights *
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8+
* copies of the Software, and to permit persons to whom the Software is *
9+
* furnished to do so, subject to the following conditions: *
10+
* *
11+
* The above copyright notice and this permission notice shall be included in all *
12+
* copies or substantial portions of the Software. *
13+
* *
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20+
* SOFTWARE. *
21+
**************************************************************************************************/
22+
23+
#pragma once
24+
25+
#include <string>
26+
27+
std::string reportNInt();

tests/testNInt.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2024 NWSOFT *
3+
* *
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy *
5+
* of this software and associated documentation files (the "Software"), to deal *
6+
* in the Software without restriction, including without limitation the rights *
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8+
* copies of the Software, and to permit persons to whom the Software is *
9+
* furnished to do so, subject to the following conditions: *
10+
* *
11+
* The above copyright notice and this permission notice shall be included in all *
12+
* copies or substantial portions of the Software. *
13+
* *
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20+
* SOFTWARE. *
21+
**************************************************************************************************/
22+
23+
#include "colors.hpp"
24+
#include "fn/basicArithm.hpp"
25+
#include "fn/calculus.hpp"
26+
#include "output.hpp"
27+
#include "testing.hpp"
28+
#include "util.hpp"
29+
30+
#include <iomanip>
31+
#include <iostream>
32+
33+
using namespace steppable::__internals::calculus;
34+
using namespace steppable::__internals::arithmetic;
35+
36+
TEST_START()
37+
38+
SECTION(Test Numerical Integration)
39+
const auto& result = romberg([](const std::string& x) { return add("1", multiply(x, "2", 0), 0); }, "0", "1", 10, 3);
40+
_.assertIsEqual(result, "2.000");
41+
SECTION_END()
42+
43+
TEST_END()

0 commit comments

Comments
 (0)