/*=============================================================================

    This file is part of FLINT.

    FLINT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    FLINT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2010 William Hart

******************************************************************************/

*******************************************************************************

    Memory management

*******************************************************************************

fmpz * _fmpz_vec_init(slong len)

    Returns an initialised vector of \code{fmpz}'s of given length.

void _fmpz_vec_clear(fmpz * vec, slong len)

    Clears the entries of \code{(vec, len)} and frees the space allocated 
    for \code{vec}.

*******************************************************************************

    Randomisation

*******************************************************************************

void _fmpz_vec_randtest(fmpz * f, flint_rand_t state, 
                        slong len, mp_bitcnt_t bits)

    Sets the entries of a vector of the given length to random integers with 
    up to the given number of bits per entry.

void _fmpz_vec_randtest_unsigned(fmpz * f, flint_rand_t state, 
                                 slong len, mp_bitcnt_t bits)

    Sets the entries of a vector of the given length to random unsigned 
    integers with up to the given number of bits per entry.

*******************************************************************************

    Bit sizes and norms

*******************************************************************************

slong _fmpz_vec_max_bits(const fmpz * vec, slong len)

    If $b$ is the maximum number of bits of the absolute value of any 
    coefficient of \code{vec}, then if any coefficient of \code{vec} is 
    negative, $-b$ is returned, else $b$ is returned.

slong _fmpz_vec_max_bits_ref(const fmpz * vec, slong len)

    If $b$ is the maximum number of bits of the absolute value of any 
    coefficient of \code{vec}, then if any coefficient of \code{vec} is 
    negative, $-b$ is returned, else $b$ is returned.
    This is a slower reference implementation of \code{_fmpz_vec_max_bits}.

ulong _fmpz_vec_max_limbs(const fmpz * vec, slong len)

    Returns the maximum number of limbs needed to store the absolute value 
    of any entry in \code{(vec, len)}.  If all entries are zero, returns 
    zero.

void _fmpz_vec_height(fmpz_t height, const fmpz * vec, slong len)

    Computes the height of \code{(vec, len)}, defined as the largest of the
    absolute values the coefficients. Equivalently, this gives the infinity
    norm of the vector. If \code{len} is zero, the height is $0$.

slong _fmpz_vec_height_index(const fmpz * vec, slong len)

    Returns the index of an entry of maximum absolute value in the vector.
    The the length must be at least 1.

*******************************************************************************

    Input and output

*******************************************************************************

int _fmpz_vec_fread(FILE * file, fmpz ** vec, slong * len)

    Reads a vector from the stream \code{file} and stores it at 
    \code{*vec}.  The format is the same as the output format of 
    \code{_fmpz_vec_fprint()}, followed by either any character 
    or the end of the file.

    The interpretation of the various input arguments depends on whether 
    or not \code{*vec} is \code{NULL}:

    If \code{*vec == NULL}, the value of \code{*len} on input is ignored.  
    Once the length has been read from \code{file}, \code{*len} is set 
    to that value and a vector of this length is allocated at \code{*vec}. 
    Finally, \code{*len} coefficients are read from the input stream.  In 
    case of a file or parsing error, clears the vector and sets \code{*vec} 
    and \code{*len} to \code{NULL} and \code{0}, respectively.

    Otherwise, if \code{*vec != NULL}, it is assumed that \code{(*vec, *len)} 
    is a properly initialised vector.  If the length on the input stream 
    does not match \code{*len}, a parsing error is raised.  Attempts to read 
    the right number of coefficients from the input stream.  In case of a 
    file or parsing error, leaves the vector \code{(*vec, *len)} in its 
    current state.

    In case of success, returns a positive value.  In case of failure, 
    returns a non-positive value.

int _fmpz_vec_read(fmpz ** vec, slong * len)

    Reads a vector from \code{stdin} and stores it at \code{*vec}.

    For further details, see \code{_fmpz_vec_fread()}.

int _fmpz_vec_fprint(FILE * file, const fmpz * vec, slong len)

    Prints the vector of given length to the stream \code{file}. The 
    format is the length followed by two spaces, then a space separated 
    list of coefficients. If the length is zero, only $0$ is printed.

    In case of success, returns a positive value.  In case of failure, 
    returns a non-positive value.

int _fmpz_vec_print(const fmpz * vec, slong len)

    Prints the vector of given length to \code{stdout}.

    For further details, see \code{_fmpz_vec_fprint()}.

*******************************************************************************

    Conversions

*******************************************************************************

void _fmpz_vec_get_nmod_vec(mp_ptr res, 
                            const fmpz * poly, slong len, nmod_t mod)

    Reduce the coefficients of \code{(poly, len)} modulo the given
    modulus and set \code{(res, len)} to the result.

void _fmpz_vec_set_nmod_vec(fmpz * res, 
                               mp_srcptr poly, slong len, nmod_t mod)

    Set the coefficients of \code{(res, len)} to the symmetric modulus
    of the coefficients of \code{(poly, len)}, i.e. convert the given
    coefficients modulo the given modulus $n$ to their signed integer
    representatives in the range $[-n/2, n/2)$.

slong _fmpz_vec_get_fft(mp_limb_t ** coeffs_f, 
                         const fmpz * coeffs_m, slong l, slong length)

    Convert the vector of coeffs \code{coeffs_m} to an fft vector 
    \code{coeffs_f} of the given \code{length} with \code{l} limbs per
    coefficient with an additional limb for overflow. 

void _fmpz_vec_set_fft(fmpz * coeffs_m, slong length,
                      const mp_ptr * coeffs_f, slong limbs, slong sign)

    Convert an fft vector \code{coeffs_f} of the given \code{length} 
    to a vector of \code{fmpz}'s. Each is assumed to be the given 
    number of limbs in length with an additional limb for overflow. If the 
    output coefficients are to be signed then set \code{sign}, 
    otherwise clear it. 

slong _fmpz_vec_get_d_vec_2exp(double * appv, const fmpz * vec, slong len)

    Export the array of \code{len} entries starting at the pointer \code{vec}
    to an array of doubles \code{appv}, each entry of which is notionally
    multiplied by a single returned exponent to give the original entry. The
    returned exponent is set to be the maximum exponent of all the original
    entries so that all the doubles in \code{appv} have a maximum absolute
    value of 1.0.

void _fmpz_vec_get_mpf_vec(mpf * appv, const fmpz * vec, slong len)

    Export the array of \code{len} entries starting at the pointer \code{vec}
    to an array of mpfs \code{appv}.

*******************************************************************************

    Assignment and basic manipulation

*******************************************************************************

void _fmpz_vec_set(fmpz * vec1, const fmpz * vec2, slong len2)

    Makes a copy of \code{(vec2, len2)} into \code{vec1}.

void _fmpz_vec_swap(fmpz * vec1, fmpz * vec2, slong len2)

    Swaps the integers in \code{(vec1, len2)} and \code{(vec2, len2)}.

void _fmpz_vec_zero(fmpz * vec, slong len)

    Zeros the entries of \code{(vec, len)}.

void _fmpz_vec_neg(fmpz * vec1, const fmpz * vec2, slong len2)

    Negates \code{(vec2, len2)} and places it into \code{vec1}.

*******************************************************************************

    Comparison

*******************************************************************************

int _fmpz_vec_equal(const fmpz * vec1, const fmpz * vec2, slong len)

    Compares two vectors of the given length and returns $1$ if they are 
    equal, otherwise returns $0$.

int _fmpz_vec_is_zero(const fmpz * vec, slong len)

    Returns $1$ if \code{(vec, len)} is zero, and $0$ otherwise.

*******************************************************************************

    Sorting

*******************************************************************************

void _fmpz_vec_sort(fmpz * vec, slong len)

    Sorts the coefficients of \code{vec} in ascending order.

*******************************************************************************

    Addition and subtraction

*******************************************************************************

void _fmpz_vec_add(fmpz * res, const fmpz * vec1, 
				               const fmpz * vec2, slong len2)

    Sets \code{(res, len2)} to the sum of \code{(vec1, len2)} 
    and \code{(vec2, len2)}.

void _fmpz_vec_sub(fmpz * res, const fmpz * vec1, 
				               const fmpz * vec2, slong len2)

    Sets \code{(res, len2)} to \code{(vec1, len2)} minus \code{(vec2, len2)}.

*******************************************************************************

    Scalar multiplication and division

*******************************************************************************

void _fmpz_vec_scalar_mul_fmpz(fmpz * vec1, 
                                const fmpz * vec2, slong len2, const fmpz_t x)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} multiplied by $c$, 
    where $c$ is an \code{fmpz_t}.

id _fmpz_vec_scalar_mul_si(fmpz * vec1, const fmpz * vec2, slong len2, slong c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} multiplied by $c$, 
    where $c$ is a \code{slong}.

void _fmpz_vec_scalar_mul_ui(fmpz * vec1,
                                       const fmpz * vec2, slong len2, ulong c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} multiplied by $c$, 
    where $c$ is an \code{ulong}.

void _fmpz_vec_scalar_mul_2exp(fmpz * vec1, 
                                     const fmpz * vec2, slong len2, ulong exp)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} multiplied by \code{2^exp}.

void _fmpz_vec_scalar_divexact_fmpz(fmpz * vec1, 
                                const fmpz * vec2, slong len2, const fmpz_t x)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by $x$, where the 
    division is assumed to be exact for every entry in \code{vec2}.

void _fmpz_vec_scalar_divexact_si(fmpz * vec1, 
                                        const fmpz * vec2, slong len2, slong c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by $x$, where the 
    division is assumed to be exact for every entry in \code{vec2}.

void _fmpz_vec_scalar_divexact_ui(fmpz * vec1, 
                                        const fmpz * vec2, ulong len2, ulong c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by $x$, where the 
    division is assumed to be exact for every entry in \code{vec2}.

void _fmpz_vec_scalar_fdiv_q_fmpz(fmpz * vec1, 
                                const fmpz * vec2, slong len2, const fmpz_t c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by $c$, rounding 
    down towards minus infinity whenever the division is not exact.

void _fmpz_vec_scalar_fdiv_q_si(fmpz * vec1, 
                                        const fmpz * vec2, slong len2, slong c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by $c$, rounding 
    down towards minus infinity whenever the division is not exact.

void _fmpz_vec_scalar_fdiv_q_ui(fmpz * vec1, 
                                       const fmpz * vec2, slong len2, ulong c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by $c$, rounding 
    down towards minus infinity whenever the division is not exact.

void _fmpz_vec_scalar_fdiv_q_2exp(fmpz * vec1, 
                                     const fmpz * vec2, slong len2, ulong exp)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by \code{2^exp}, 
    rounding down towards minus infinity whenever the division is not exact.

void _fmpz_vec_scalar_fdiv_r_2exp(fmpz * vec1, 
                                     const fmpz * vec2, slong len2, ulong exp)

    Sets \code{(vec1, len2)} to the remainder of \code{(vec2, len2)} 
    divided by \code{2^exp}, rounding down the quotient towards minus 
    infinity whenever the division is not exact.

void _fmpz_vec_scalar_tdiv_q_fmpz(fmpz * vec1, 
                                const fmpz * vec2, slong len2, const fmpz_t c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by $c$, rounding 
    towards zero whenever the division is not exact.

void _fmpz_vec_scalar_tdiv_q_si(fmpz * vec1, 
                                        const fmpz * vec2, slong len2, slong c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by $c$, rounding 
    towards zero whenever the division is not exact.

void _fmpz_vec_scalar_tdiv_q_ui(fmpz * vec1, 
                                       const fmpz * vec2, slong len2, ulong c)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by $c$, rounding 
    towards zero whenever the division is not exact.

void _fmpz_vec_scalar_tdiv_q_2exp(fmpz * vec1, 
                                     const fmpz * vec2, slong len2, ulong exp)

    Sets \code{(vec1, len2)} to \code{(vec2, len2)} divided by \code{2^exp}, 
    rounding down towards zero whenever the division is not exact.

void _fmpz_vec_scalar_addmul_fmpz(fmpz * vec1, 
                                const fmpz * vec2, slong len2, const fmpz_t c)

    Adds \code{(vec2, len2)} times $c$ to \code{(vec1, len2)}, where $c$ is a 
    \code{fmpz_t}.

void _fmpz_vec_scalar_addmul_si(fmpz * vec1, 
                                        const fmpz * vec2, slong len2, slong c)

    Adds \code{(vec2, len2)} times $c$ to \code{(vec1, len2)}, where $c$ is a 
    \code{slong}.

void _fmpz_vec_scalar_addmul_si_2exp(fmpz * vec1, 
                             const fmpz * vec2, slong len2, slong c, ulong exp)

    Adds \code{(vec2, len2)} times \code{c * 2^exp} to \code{(vec1, len2)}, 
    where $c$ is a \code{slong}.

void _fmpz_vec_scalar_submul_fmpz(fmpz * vec1, 
                                const fmpz * vec2, slong len2, const fmpz_t x)

    Subtracts \code{(vec2, len2)} times $c$ from \code{(vec1, len2)}, 
    where $c$ is a \code{fmpz_t}.

void _fmpz_vec_scalar_submul_si(fmpz * vec1, 
                                        const fmpz * vec2, slong len2, slong c)

    Subtracts \code{(vec2, len2)} times $c$ from \code{(vec1, len2)}, 
    where $c$ is a \code{slong}.

void _fmpz_vec_scalar_submul_si_2exp(fmpz * vec1, 
                               const fmpz * vec2, slong len2, slong c, ulong e)

    Subtracts \code{(vec2, len2)} times $c \times 2^e$ 
    from \code{(vec1, len2)}, where $c$ is a \code{slong}.

*******************************************************************************

    Sums and products

*******************************************************************************

void _fmpz_vec_sum(fmpz_t res, const fmpz * vec, slong len)

    Sets \code{res} to the sum of the entries in \code{(vec, len)}.
    Aliasing of \code{res} with the entries in \code{vec} is not permitted.

void _fmpz_vec_prod(fmpz_t res, const fmpz * vec, slong len)

    Sets \code{res} to the product of the entries in \code{(vec, len)}.
    Aliasing of \code{res} with the entries in \code{vec} is not permitted.
    Uses binary splitting.

*******************************************************************************

    Reduction mod $p$

*******************************************************************************

void _fmpz_vec_scalar_mod_fmpz(fmpz *res, 
                               const fmpz *vec, slong len, const fmpz_t p)

    Reduces all entries in \code{(vec, len)} modulo $p > 0$.

void _fmpz_vec_scalar_smod_fmpz(fmpz *res, 
                                const fmpz *vec, slong len, const fmpz_t p)

    Reduces all entries in \code{(vec, len)} modulo $p > 0$, choosing 
    the unique representative in $(-p/2, p/2]$.

*******************************************************************************

    Gaussian content

*******************************************************************************

void _fmpz_vec_content(fmpz_t res, const fmpz * vec, slong len)

    Sets \code{res} to the non-negative content of the entries in \code{vec}.  
    The content of a zero vector, including the case when the length is zero, 
    is defined to be zero.

void _fmpz_vec_lcm(fmpz_t res, const fmpz * vec, slong len)

    Sets \code{res} to the nonnegative least common multiple of the entries
    in \code{vec}. The least common multiple is zero if any entry in
    the vector is zero. The least common multiple of a length zero vector is
    defined to be one.

*******************************************************************************

    Dot product

*******************************************************************************

void _fmpz_vec_dot(fmpz_t res, const fmpz * vec1, const fmpz * vec2,
                   slong len2)

    Sets \code{res} to the dot product of \code{(vec1, len2)} and
    \code{(vec2, len2)}.
