Skip to content

Commit

Permalink
Introducing calculus::nInt.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCG-coder committed Aug 10, 2024
1 parent c349d9e commit a72bacb
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/editor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ SET(COMPONENTS
base::baseConvert
base::decimalConvert
calc::abs
calculus::nInt
calc::add
calc::atan2
calc::comparison
Expand Down
39 changes: 39 additions & 0 deletions include/fn/calculus.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**************************************************************************************************
* Copyright (c) 2023-2024 NWSOFT *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**************************************************************************************************/

#pragma once

#include <functional>
#include <string>

/**
* @namespace steppable::__internals::calculus
* @brief Contains calculus functions.
*/
namespace steppable::__internals::calculus
{
std::string romberg(const std::function<std::string(std::string)>& f,
const std::string& a,
const std::string& b,
int max_steps,
int decimals);
}
84 changes: 84 additions & 0 deletions src/calculus/nInt/nInt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**************************************************************************************************
* Copyright (c) 2023-2024 NWSOFT *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**************************************************************************************************/

#include "fn/basicArithm.hpp"
#include "rounding.hpp"

#include <concepts>
#include <functional>
#include <string>
#include <vector>

using namespace steppable::__internals::arithmetic;
using namespace steppable::__internals::numUtils;
using namespace std::literals;

namespace steppable::__internals::calculus
{
std::string romberg(const std::function<std::string(std::string)>& f,
const std::string& a,
const std::string& b,
const int max_steps,
const int decimals)
{
auto acc = "0." + std::string(decimals - 1, '0') + "1";
auto Rp = std::vector(max_steps, "0"s);
auto Rc = std::vector(max_steps, "0"s);

auto h = subtract(b, a, 0);
auto fAB = add(f(a), f(b), 0);
auto halfH = multiply(h, "0.5", 0);
Rp.front() = multiply(halfH, fAB, 0);

for (int i = 1; i < max_steps; i++)
{
h = multiply(h, "0.5", 0);
auto c = "0"s;
long ep = 1 << (i - 1); // 2^(i - 1)
for (long j = 1; j < (ep + 1); j++)
{
auto d = multiply(std::to_string((2 * j) - 1), h, 0);
c = add(c, f(add(a, d, 0)), 0);
}
Rc.front() = add(multiply(h, c, 0), multiply("0.5", Rp.front(), 0), 0);

for (int j = 1; j < (i + 1); j++)
{
long double n_k = pow(4, j);
auto one = multiply(std::to_string(n_k), Rc.at(j - 1), 0);
auto top = subtract(one, Rp.at(j - 1), 0);
Rc.at(j) = divide(top, std::to_string(n_k - 1), 0, decimals + 1);

if (i > 1 and compare(abs(subtract(Rp.at(i - 1), Rc.at(i), 0), 0), acc, 0) == "0")
return roundOff(Rc.at(i), decimals);
}

std::ranges::swap(Rp, Rc);
}

return roundOff(Rp.at(max_steps - 1), decimals);
}
} // namespace steppable::__internals::calculus

#ifndef NO_MAIN
int main() {}
#endif
31 changes: 31 additions & 0 deletions src/calculus/nInt/nIntReport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**************************************************************************************************
* Copyright (c) 2023-2024 NWSOFT *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**************************************************************************************************/

#include "nIntReport.hpp"

#include <string>

std::string reportNInt()
{
// Intentionally not implemented.
return "";
}
27 changes: 27 additions & 0 deletions src/calculus/nInt/nIntReport.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**************************************************************************************************
* Copyright (c) 2023-2024 NWSOFT *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**************************************************************************************************/

#pragma once

#include <string>

std::string reportNInt();
43 changes: 43 additions & 0 deletions tests/testNInt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**************************************************************************************************
* Copyright (c) 2023-2024 NWSOFT *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**************************************************************************************************/

#include "colors.hpp"
#include "fn/basicArithm.hpp"
#include "fn/calculus.hpp"
#include "output.hpp"
#include "testing.hpp"
#include "util.hpp"

#include <iomanip>
#include <iostream>

using namespace steppable::__internals::calculus;
using namespace steppable::__internals::arithmetic;

TEST_START()

SECTION(Test Numerical Integration)
const auto& result = romberg([](const std::string& x) { return add("1", multiply(x, "2", 0), 0); }, "0", "1", 10, 3);
_.assertIsEqual(result, "2.000");
SECTION_END()

TEST_END()

0 comments on commit a72bacb

Please sign in to comment.