#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Date          : 2026-02-09
# Author        : Lancelot PINCET
# GitHub        : https://github.com/LancelotPincet
# Library       : arrLP
# Module        : xp

"""
Gets numpy or cupy depending on the value of cuda argument.
"""



# %% Libraries
import numpy as np
try:
    import cupy as cp
except ImportError:
    cp = None



# %% Function
def xp(cuda:bool) :
    '''
    Gets numpy or cupy depending on the value of cuda argument.
    
    Parameters
    ----------
    cuda : bool
        Tells to take numpy or cupy.

    Returns
    -------
    _xp : np or cp
        Library to use.

    Raises
    ------
    ImportError
        If cuda is asked but unavailable.

    Examples
    --------
    >>> from arrlp import xp
    ...
    >>> _xp = xp(cuda)
    '''

    if cuda and cp is None :
        raise ImportError('Cupy is not available for xp function')
    return cp if cuda else np



# %% Test function run
if __name__ == "__main__":
    from corelp import test
    test(__file__)