# Path: torchdiffeq/_impl/rk_common.py
# class _UncheckedAssign(torch.autograd.Function):
# class RKAdaptiveStepsizeODESolver(AdaptiveStepsizeEventODESolver):
#     def forward(ctx, scratch, value, index):
#     def backward(ctx, grad_scratch):
# def _runge_kutta_step(func, y0, f0, t0, dt, t1, tableau):
# def rk4_step_func(func, t0, dt, t1, y0, f0=None, perturb=False):
# def rk4_alt_step_func(func, t0, dt, t1, y0, f0=None, perturb=False):
#     def __init__(self, func, y0, rtol, atol,
#                  first_step=None,
#                  step_t=None,
#                  jump_t=None,
#                  safety=0.9,
#                  ifactor=10.0,
#                  dfactor=0.2,
#                  max_num_steps=2 ** 31 - 1,
#                  dtype=torch.float64,
#                  **kwargs):
#     def _before_integrate(self, t):
#     def _advance(self, next_t):
#     def _advance_until_event(self, event_fn):
#     def _adaptive_step(self, rk_state):
#     def _interp_fit(self, y0, y1, k, dt):
# def _sort_tvals(tvals, t0):


import torch
from .rk_common import _ButcherTableau, RKAdaptiveStepsizeODESolver


_BOGACKI_SHAMPINE_TABLEAU = _ButcherTableau(
    alpha=torch.tensor([1 / 2, 3 / 4, 1.], dtype=torch.float64),
    beta=[
        torch.tensor([1 / 2], dtype=torch.float64),
        torch.tensor([0., 3 / 4], dtype=torch.float64),
        torch.tensor([2 / 9, 1 / 3, 4 / 9], dtype=torch.float64)
    ],
    c_sol=torch.tensor([2 / 9, 1 / 3, 4 / 9, 0.], dtype=torch.float64),
    c_error=torch.tensor([2 / 9 - 7 / 24, 1 / 3 - 1 / 4, 4 / 9 - 1 / 3, -1 / 8], dtype=torch.float64),
)

_BS_C_MID = torch.tensor([0., 0.5, 0., 0.], dtype=torch.float64)


class Bosh3Solver(RKAdaptiveStepsizeODESolver):
    order = 3

# Path: torchdiffeq/_impl/rk_common.py
# class _UncheckedAssign(torch.autograd.Function):
# class RKAdaptiveStepsizeODESolver(AdaptiveStepsizeEventODESolver):
#     def forward(ctx, scratch, value, index):
#     def backward(ctx, grad_scratch):
# def _runge_kutta_step(func, y0, f0, t0, dt, t1, tableau):
# def rk4_step_func(func, t0, dt, t1, y0, f0=None, perturb=False):
# def rk4_alt_step_func(func, t0, dt, t1, y0, f0=None, perturb=False):
#     def __init__(self, func, y0, rtol, atol,
#                  first_step=None,
#                  step_t=None,
#                  jump_t=None,
#                  safety=0.9,
#                  ifactor=10.0,
#                  dfactor=0.2,
#                  max_num_steps=2 ** 31 - 1,
#                  dtype=torch.float64,
#                  **kwargs):
#     def _before_integrate(self, t):
#     def _advance(self, next_t):
#     def _advance_until_event(self, event_fn):
#     def _adaptive_step(self, rk_state):
#     def _interp_fit(self, y0, y1, k, dt):
# def _sort_tvals(tvals, t0):


# Path: torchdiffeq/_impl/bosh3.py
import torch
from .rk_common import _ButcherTableau, RKAdaptiveStepsizeODESolver



_BOGACKI_SHAMPINE_TABLEAU = _ButcherTableau(
    alpha=torch.tensor([1 / 2, 3 / 4, 1.], dtype=torch.float64),
    beta=[
        torch.tensor([1 / 2], dtype=torch.float64),
        torch.tensor([0., 3 / 4], dtype=torch.float64),
        torch.tensor([2 / 9, 1 / 3, 4 / 9], dtype=torch.float64)
    ],
    c_sol=torch.tensor([2 / 9, 1 / 3, 4 / 9, 0.], dtype=torch.float64),
    c_error=torch.tensor([2 / 9 - 7 / 24, 1 / 3 - 1 / 4, 4 / 9 - 1 / 3, -1 / 8], dtype=torch.float64),
)

_BS_C_MID = torch.tensor([0., 0.5, 0., 0.], dtype=torch.float64)


class Bosh3Solver(RKAdaptiveStepsizeODESolver):
    order = 3

    tableau = _BOGACKI_SHAMPINE_TABLEAU