(a_gpu, overwrite=False, handle=None)
Lower triangle of a matrix.

    Return the lower triangle of a square matrix.

    Parameters
    ----------
    a_gpu : pycuda.gpuarray.GPUArray
        Input matrix of shape `(m, m)`
    overwrite : bool (default: False)
        If true, zero out the upper triangle of the matrix.
        If false, return the result in a newly allocated matrix.
    handle : int
        CUBLAS context. If no context is specified, the default handle from
        `skcuda.misc._global_cublas_handle` is used.

    Returns
    -------
    l_gpu : pycuda.gpuarray
        The lower triangle of the original matrix.

    Examples
    --------
    >>> import pycuda.autoinit
    >>> import pycuda.driver as drv
    >>> import pycuda.gpuarray as gpuarray
    >>> import numpy as np
    >>> import skcuda.linalg as linalg
    >>> linalg.init()
    >>> a = np.asarray(np.random.rand(4, 4), np.float32)
    >>> a_gpu = gpuarray.to_gpu(a)
    >>> l_gpu = linalg.tril(a_gpu, False)
    >>> np.allclose(np.tril(a), l_gpu.get())
    True
Lower triangle of a matrix.
Lower triangle of a matrix .
def tril(a_gpu, overwrite=False, handle=None):
    """
    Lower triangle of a matrix.

    Return the lower triangle of a square matrix.

    Parameters
    ----------
    a_gpu : pycuda.gpuarray.GPUArray
        Input matrix of shape `(m, m)`
    overwrite : bool (default: False)
        If true, zero out the upper triangle of the matrix.
        If false, return the result in a newly allocated matrix.
    handle : int
        CUBLAS context. If no context is specified, the default handle from
        `skcuda.misc._global_cublas_handle` is used.

    Returns
    -------
    l_gpu : pycuda.gpuarray
        The lower triangle of the original matrix.

    Examples
    --------
    >>> import pycuda.autoinit
    >>> import pycuda.driver as drv
    >>> import pycuda.gpuarray as gpuarray
    >>> import numpy as np
    >>> import skcuda.linalg as linalg
    >>> linalg.init()
    >>> a = np.asarray(np.random.rand(4, 4), np.float32)
    >>> a_gpu = gpuarray.to_gpu(a)
    >>> l_gpu = linalg.tril(a_gpu, False)
    >>> np.allclose(np.tril(a), l_gpu.get())
    True

    """

    if handle is None:
        handle = misc._global_cublas_handle

    alloc = misc._global_cublas_allocator

    if len(a_gpu.shape) != 2 or a_gpu.shape[0] != a_gpu.shape[1]:
        raise ValueError('matrix must be square')

    if a_gpu.dtype == np.float32:
        swap_func = cublas.cublasSswap
        copy_func = cublas.cublasScopy
        use_double = 0
        use_complex = 0
    elif a_gpu.dtype == np.float64:
        swap_func = cublas.cublasDswap
        copy_func = cublas.cublasDcopy
        use_double = 1
        use_complex = 0
    elif a_gpu.dtype == np.complex64:
        swap_func = cublas.cublasCswap
        copy_func = cublas.cublasCcopy
        use_double = 0
        use_complex = 1
    elif a_gpu.dtype == np.complex128:
        swap_func = cublas.cublasZswap
        copy_func = cublas.cublasZcopy
        use_double = 1
        use_complex = 1
    else:
        raise ValueError('unrecognized type')

    N = a_gpu.shape[0]

    # Get block/grid sizes:
    dev = misc.get_current_device()
    block_dim, grid_dim = misc.select_block_grid_sizes(dev, a_gpu.shape)
    tril = _get_tril_kernel(use_double, use_complex, cols=N)
    if not overwrite:
        a_orig_gpu = gpuarray.empty(a_gpu.shape, a_gpu.dtype, allocator=alloc)
        copy_func(handle, a_gpu.size, int(a_gpu.gpudata), 1, int(a_orig_gpu.gpudata), 1)

    tril(a_gpu, np.uint32(a_gpu.size),
         block=block_dim,
         grid=grid_dim)

    if overwrite:
        return a_gpu
    else:

        # Restore original contents of a_gpu:
        swap_func(handle, a_gpu.size, int(a_gpu.gpudata), 1, int(a_orig_gpu.gpudata), 1)
        return a_orig_gpu
def tril ( a_gpu , overwrite = False , handle = None ) : if handle is None : handle = misc . _global_cublas_handle alloc = misc . _global_cublas_allocator if len ( a_gpu . shape ) != 2 or a_gpu . shape [ 0 ] != a_gpu . shape [ 1 ] : raise ValueError ( 'matrix must be square' ) if a_gpu . dtype == np . float32 : swap_func = cublas . cublasSswap copy_func = cublas . cublasScopy use_double = 0 use_complex = 0 elif a_gpu . dtype == np . float64 : swap_func = cublas . cublasDswap copy_func = cublas . cublasDcopy use_double = 1 use_complex = 0 elif a_gpu . dtype == np . complex64 : swap_func = cublas . cublasCswap copy_func = cublas . cublasCcopy use_double = 0 use_complex = 1 elif a_gpu . dtype == np . complex128 : swap_func = cublas . cublasZswap copy_func = cublas . cublasZcopy use_double = 1 use_complex = 1 else : raise ValueError ( 'unrecognized type' ) N = a_gpu . shape [ 0 ] # Get block/grid sizes: dev = misc . get_current_device ( ) block_dim , grid_dim = misc . select_block_grid_sizes ( dev , a_gpu . shape ) tril = _get_tril_kernel ( use_double , use_complex , cols = N ) if not overwrite : a_orig_gpu = gpuarray . empty ( a_gpu . shape , a_gpu . dtype , allocator = alloc ) copy_func ( handle , a_gpu . size , int ( a_gpu . gpudata ) , 1 , int ( a_orig_gpu . gpudata ) , 1 ) tril ( a_gpu , np . uint32 ( a_gpu . size ) , block = block_dim , grid = grid_dim ) if overwrite : return a_gpu else : # Restore original contents of a_gpu: swap_func ( handle , a_gpu . size , int ( a_gpu . gpudata ) , 1 , int ( a_orig_gpu . gpudata ) , 1 ) return a_orig_gpu