#include <vector>
#include <cmath>
#include <concepts>
#include <print>
#include <format>
/**
* @brief Concept to ensure T is a floating-point type.
*/
template<typename T>
concept FloatingPoint = std::floating_point<T>;
/**
* @brief LogisticMap simulates chaotic behavior using the logistic equation.
*
* @tparam T A floating-point type like float or double
*/
template<FloatingPoint T>
class LogisticMap
{
public:
constexpr LogisticMap(T r, T x0, std::size_t steps)
: m_r(r), m_x0(x0), m_steps(steps)
{
m_values.reserve(steps);
}
void simulate()
{
T x = m_x0;
for (std::size_t i = 0; i < m_steps; ++i)
{
x = m_r * x * (static_cast<T>(1.0) - x);
m_values.push_back(x);
}
}
[[nodiscard]]
const std::vector<T>& values() const noexcept
{
return m_values;
}
void compareWith(const LogisticMap<T>& other) const
{
std::print("{:<5} {:>15} {:>15} {:>15}\n", "Step", "Map1", "Map2", "Δ Difference");
for (std::size_t i = 0; i < m_steps; ++i)
{
const T diff = std::abs(m_values[i] - other.m_values[i]);
std::print("{:<5} {:>15.10f} {:>15.10f} {:>15.10f}\n",
i, m_values[i], other.m_values[i], diff);
}
}
private:
T m_r;
T m_x0;
std::size_t m_steps;
std::vector<T> m_values;
};
auto main() ->int
{
constexpr double r = 3.99;
constexpr double x0 = 0.500000;
constexpr double x0_perturbed = 0.500005;
constexpr std::size_t steps = 16;
LogisticMap<double> map1(r, x0, steps);
LogisticMap<double> map2(r, x0_perturbed, steps);
map1.simulate();
map2.simulate();
std::println("=== Chaos Simulation with Logistic Map ===");
std::println("Initial x0: {:.6f}\n", x0);
std::println("Perturbed x0: {:.6f} (~0.001% difference)\n", x0_perturbed);
std::println("Growth factor r: {:.2f}\n", r);
std::println("Simulation steps: {}\n\n", steps);
map1.compareWith(map2);
return 0;
}
این هم کدش با سی++ ۲۳ 😎😉
June 23, 2025 220 3