// Copyright (C) 2026 Kiyotsugu Arai
// SPDX-License-Identifier: LGPL-3.0-or-later

// example_complex.cpp
// Demo of basic Complex<T> operations
//
// Build:
//   cl /std:c++latest /EHsc /O2 /utf-8 /I<sangi>/include examples\example_complex.cpp

#include <cmath>
#include <numbers>
#include <math/core/Complex.hpp>
#include <iostream>

using namespace sangi;

int main() {
    std::cout << "=== sangi: Complex Numbers ===" << std::endl;

    // --- Basic operations ---
    std::cout << "\n--- Basic Arithmetic ---" << std::endl;
    Complex<double> z1(3.0, 4.0);   // 3 + 4i
    Complex<double> z2(1.0, -2.0);  // 1 - 2i

    std::cout << "z1 = " << z1 << std::endl;
    std::cout << "z2 = " << z2 << std::endl;
    std::cout << "z1 + z2 = " << z1 + z2 << std::endl;
    std::cout << "z1 * z2 = " << z1 * z2 << std::endl;
    std::cout << "z1 / z2 = " << z1 / z2 << std::endl;

    // --- Absolute value & argument ---
    std::cout << "\n--- Absolute Value & Argument ---" << std::endl;
    std::cout << "|z1| = " << abs(z1) << std::endl;
    std::cout << "arg(z1) = " << arg(z1) << " rad" << std::endl;
    std::cout << "conj(z1) = " << conj(z1) << std::endl;

    // --- Euler's formula: e^(iπ) + 1 = 0 ---
    std::cout << "\n--- Euler's Formula: e^(i*pi) + 1 = 0 ---" << std::endl;
    Complex<double> ipie = exp(Complex<double>(0.0, std::numbers::pi));
    Complex<double> euler_result = ipie + Complex<double>(1.0, 0.0);
    std::cout << "e^(i*pi) = " << ipie << std::endl;
    std::cout << "e^(i*pi) + 1 = " << euler_result
              << "  (|error| = " << abs(euler_result) << ")" << std::endl;

    // --- Mathematical functions ---
    std::cout << "\n--- Mathematical Functions ---" << std::endl;
    Complex<double> z(1.0, 1.0);
    std::cout << "z = " << z << std::endl;
    std::cout << "exp(z) = " << exp(z) << std::endl;
    std::cout << "log(z) = " << log(z) << std::endl;
    std::cout << "sqrt(z) = " << sqrt(z) << std::endl;
    std::cout << "sin(z) = " << sin(z) << std::endl;
    std::cout << "cos(z) = " << cos(z) << std::endl;

    // --- Polar form ---
    std::cout << "\n--- Polar Form ---" << std::endl;
    auto p = polar(2.0, std::numbers::pi / 6);  // r=2, θ=30°
    std::cout << "polar(2, pi/6) = " << p << std::endl;
    std::cout << "  |z| = " << abs(p) << ",  arg = " << arg(p) * 180.0 / std::numbers::pi << " deg" << std::endl;

    // --- Powers ---
    std::cout << "\n--- Powers ---" << std::endl;
    Complex<double> i(0.0, 1.0);
    std::cout << "i^2 = " << pow(i, 2) << std::endl;
    std::cout << "i^3 = " << pow(i, 3) << std::endl;
    std::cout << "i^4 = " << pow(i, 4) << std::endl;

    return 0;
}
