// Copyright (C) 2026 EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#pragma once
#include <stan/math.hpp>
#include "StOpt/core/utils/toDouble.h"

namespace StOpt
{

template<typename S, typename V>
struct ToDouble<stan::math::var_value<S, V>>
{
    static double eval(const stan::math::var_value<S, V> &v)
    {
        return stan::math::value_of(v);
    }
};

template<typename Derived>
Eigen::ArrayXXd toDoubleArray(const Eigen::ArrayBase<Derived> &x)
{
    Eigen::ArrayXXd ret(x.rows(), x.cols());

    for (Eigen::Index i = 0; i < x.rows(); ++i)
    {
        for (Eigen::Index j = 0; j < x.cols(); ++j)
        {
            ret(i, j) = StOpt::toDouble(x.derived()(i, j));
        }
    }

    return ret;
};
}
