// Copyright (c) 2017-2023, University of Tennessee. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// This program is free software: you can redistribute it and/or modify it under
// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.

#include "lapack.hh"
#include "lapack_internal.hh"
#include "lapack/fortran.h"

#include <vector>

namespace lapack {

using blas::max;
using blas::min;
using blas::real;

// -----------------------------------------------------------------------------
/// @ingroup initialize
void lacpy(
    lapack::MatrixType matrixtype, int64_t m, int64_t n,
    float const* A, int64_t lda,
    float* B, int64_t ldb )
{
    char matrixtype_ = to_char( matrixtype );
    lapack_int m_ = to_lapack_int( m );
    lapack_int n_ = to_lapack_int( n );
    lapack_int lda_ = to_lapack_int( lda );
    lapack_int ldb_ = to_lapack_int( ldb );

    LAPACK_slacpy(
        &matrixtype_, &m_, &n_,
        A, &lda_,
        B, &ldb_
    );
}

// -----------------------------------------------------------------------------
/// @ingroup initialize
void lacpy(
    lapack::MatrixType matrixtype, int64_t m, int64_t n,
    double const* A, int64_t lda,
    double* B, int64_t ldb )
{
    char matrixtype_ = to_char( matrixtype );
    lapack_int m_ = to_lapack_int( m );
    lapack_int n_ = to_lapack_int( n );
    lapack_int lda_ = to_lapack_int( lda );
    lapack_int ldb_ = to_lapack_int( ldb );

    LAPACK_dlacpy(
        &matrixtype_, &m_, &n_,
        A, &lda_,
        B, &ldb_
    );
}

// -----------------------------------------------------------------------------
/// @ingroup initialize
void lacpy(
    lapack::MatrixType matrixtype, int64_t m, int64_t n,
    std::complex<float> const* A, int64_t lda,
    std::complex<float>* B, int64_t ldb )
{
    char matrixtype_ = to_char( matrixtype );
    lapack_int m_ = to_lapack_int( m );
    lapack_int n_ = to_lapack_int( n );
    lapack_int lda_ = to_lapack_int( lda );
    lapack_int ldb_ = to_lapack_int( ldb );

    LAPACK_clacpy(
        &matrixtype_, &m_, &n_,
        (lapack_complex_float*) A, &lda_,
        (lapack_complex_float*) B, &ldb_
    );
}

// -----------------------------------------------------------------------------
/// Copies all or part of a two-dimensional matrix A to another
/// matrix B.
///
/// Overloaded versions are available for
/// `float`, `double`, `std::complex<float>`, and `std::complex<double>`.
///
/// @param[in] matrixtype
///     Specifies the part of the matrix A to be copied to B.
///     - lapack::MatrixType::Upper: Upper triangular part
///     - lapack::MatrixType::Lower: Lower triangular part
///     - lapack::MatrixType::General: All of the matrix A
///
/// @param[in] m
///     The number of rows of the matrix A. m >= 0.
///
/// @param[in] n
///     The number of columns of the matrix A. n >= 0.
///
/// @param[in] A
///     The m-by-n matrix A, stored in an lda-by-n array.
///     The m-by-n matrix A.
///     - If matrixtype = Upper, only the upper trapezium is accessed;
///     - if matrixtype = Lower, only the lower trapezium is accessed.
///
/// @param[in] lda
///     The leading dimension of the array A. lda >= max(1,m).
///
/// @param[out] B
///     The m-by-n matrix B, stored in an ldb-by-n array.
///     On exit, $B = A$ in the locations specified by matrixtype.
///
/// @param[in] ldb
///     The leading dimension of the array B. ldb >= max(1,m).
///
/// @ingroup initialize
void lacpy(
    lapack::MatrixType matrixtype, int64_t m, int64_t n,
    std::complex<double> const* A, int64_t lda,
    std::complex<double>* B, int64_t ldb )
{
    char matrixtype_ = to_char( matrixtype );
    lapack_int m_ = to_lapack_int( m );
    lapack_int n_ = to_lapack_int( n );
    lapack_int lda_ = to_lapack_int( lda );
    lapack_int ldb_ = to_lapack_int( ldb );

    LAPACK_zlacpy(
        &matrixtype_, &m_, &n_,
        (lapack_complex_double*) A, &lda_,
        (lapack_complex_double*) B, &ldb_
    );
}

}  // namespace lapack
