#include <fesa/fem/line2_shape.hpp>

#include <cmath>
#include <stdexcept>

namespace fesa {

std::array<double, 2> line2_shape(const double xi) {
    return {0.5 * (1.0 - xi), 0.5 * (1.0 + xi)};
}

std::array<double, 2> line2_shape_derivative(const double) {
    return {-0.5, 0.5};
}

double line2_jacobian(const double length) {
    if (!std::isfinite(length) || length <= 0.0) {
        throw std::invalid_argument{
            "Line2 Jacobian requires a positive finite length."};
    }

    const double jacobian = length / 2.0;
    if (jacobian == 0.0) {
        throw std::invalid_argument{
            "Line2 Jacobian must be representable as a positive double."};
    }

    return jacobian;
}

}  // namespace fesa
