Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:15:11

0001 /*
0002     Copyright (C) 2011 Fredrik Johansson
0003 
0004     This file is part of FLINT.
0005 
0006     FLINT is free software: you can redistribute it and/or modify it under
0007     the terms of the GNU Lesser General Public License (LGPL) as published
0008     by the Free Software Foundation; either version 3 of the License, or
0009     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
0010 */
0011 
0012 #ifndef FMPQ_MAT_H
0013 #define FMPQ_MAT_H
0014 
0015 #ifdef FMPQ_MAT_INLINES_C
0016 #define FMPQ_MAT_INLINE
0017 #else
0018 #define FMPQ_MAT_INLINE static inline
0019 #endif
0020 
0021 #include "fmpq_types.h"
0022 
0023 #ifdef __cplusplus
0024 extern "C" {
0025 #endif
0026 
0027 FMPQ_MAT_INLINE
0028 fmpq * fmpq_mat_entry(const fmpq_mat_t mat, slong i, slong j)
0029 {
0030    return mat->entries + (i * mat->stride + j);
0031 }
0032 
0033 FMPQ_MAT_INLINE
0034 fmpz * fmpq_mat_entry_num(const fmpq_mat_t mat, slong i, slong j)
0035 {
0036    return (fmpz *)(&((*fmpq_mat_entry(mat, i, j)).num));
0037 }
0038 
0039 FMPQ_MAT_INLINE
0040 fmpz * fmpq_mat_entry_den(const fmpq_mat_t mat, slong i, slong j)
0041 {
0042    return (fmpz *)(&((*fmpq_mat_entry(mat, i, j)).den));
0043 }
0044 
0045 FMPQ_MAT_INLINE
0046 slong fmpq_mat_nrows(const fmpq_mat_t mat)
0047 {
0048    return mat->r;
0049 }
0050 
0051 FMPQ_MAT_INLINE
0052 slong fmpq_mat_ncols(const fmpq_mat_t mat)
0053 {
0054    return mat->c;
0055 }
0056 
0057 void fmpq_mat_init(fmpq_mat_t mat, slong rows, slong cols);
0058 void fmpq_mat_init_set(fmpq_mat_t mat1, const fmpq_mat_t mat2);
0059 void fmpq_mat_clear(fmpq_mat_t mat);
0060 
0061 FMPQ_MAT_INLINE
0062 void fmpq_mat_swap(fmpq_mat_t mat1, fmpq_mat_t mat2)
0063 {
0064     FLINT_SWAP(fmpq_mat_struct, *mat1, *mat2);
0065 }
0066 
0067 void fmpq_mat_swap_entrywise(fmpq_mat_t mat1, fmpq_mat_t mat2);
0068 
0069 /* Windows and concatenation */
0070 
0071 FMPQ_MAT_INLINE void
0072 fmpq_mat_window_init(fmpq_mat_t window, const fmpq_mat_t mat, slong r1,
0073                      slong c1, slong r2, slong c2)
0074 {
0075     FLINT_ASSERT(r1 >= 0 && r1 <= r2 && r2 <= mat->r);
0076     FLINT_ASSERT(c2 >= 0 && c1 <= c2 && c2 <= mat->c);
0077 
0078     window->entries = fmpq_mat_entry(mat, r1, c1);
0079     window->r = r2 - r1;
0080     window->c = c2 - c1;
0081     window->stride = mat->stride;
0082 }
0083 
0084 FMPQ_MAT_INLINE void
0085 fmpq_mat_window_clear(fmpq_mat_t FLINT_UNUSED(window))
0086 {
0087 }
0088 
0089 void fmpq_mat_concat_horizontal(fmpq_mat_t res, const fmpq_mat_t mat1,  const fmpq_mat_t mat2);
0090 void fmpq_mat_concat_vertical(fmpq_mat_t res, const fmpq_mat_t mat1,  const fmpq_mat_t mat2);
0091 
0092 /* Input and output  */
0093 
0094 void fmpq_mat_print(const fmpq_mat_t mat);
0095 
0096 /* Random matrix generation **************************************************/
0097 
0098 void fmpq_mat_randbits(fmpq_mat_t mat, flint_rand_t state, flint_bitcnt_t bits);
0099 void fmpq_mat_randtest(fmpq_mat_t mat, flint_rand_t state, flint_bitcnt_t bits);
0100 
0101 /* Special matrices **********************************************************/
0102 
0103 void fmpq_mat_hilbert_matrix(fmpq_mat_t mat);
0104 
0105 /* Basic assignment **********************************************************/
0106 
0107 void fmpq_mat_set(fmpq_mat_t dest, const fmpq_mat_t src);
0108 
0109 void fmpq_mat_zero(fmpq_mat_t mat);
0110 void fmpq_mat_one(fmpq_mat_t mat);
0111 
0112 void fmpq_mat_transpose(fmpq_mat_t rop, const fmpq_mat_t op);
0113 
0114 /* Addition, scalar multiplication  ******************************************/
0115 
0116 void fmpq_mat_add(fmpq_mat_t mat, const fmpq_mat_t mat1, const fmpq_mat_t mat2);
0117 void fmpq_mat_sub(fmpq_mat_t mat, const fmpq_mat_t mat1, const fmpq_mat_t mat2);
0118 void fmpq_mat_neg(fmpq_mat_t rop, const fmpq_mat_t op);
0119 
0120 void fmpq_mat_scalar_mul_fmpq(fmpq_mat_t rop, const fmpq_mat_t op, const fmpq_t x);
0121 void fmpq_mat_scalar_mul_fmpz(fmpq_mat_t rop, const fmpq_mat_t op, const fmpz_t x);
0122 void fmpq_mat_scalar_mul_si(fmpq_mat_t rop, const fmpq_mat_t op, const slong x);
0123 void fmpq_mat_scalar_mul_ui(fmpq_mat_t rop, const fmpq_mat_t op, const ulong x);
0124 
0125 void fmpq_mat_scalar_div_fmpz(fmpq_mat_t rop, const fmpq_mat_t op, const fmpz_t x);
0126 void fmpq_mat_scalar_div_si(fmpq_mat_t rop, const fmpq_mat_t op, const slong x);
0127 void fmpq_mat_scalar_div_ui(fmpq_mat_t rop, const fmpq_mat_t op, const ulong x);
0128 
0129 /* Basic comparison and properties *******************************************/
0130 
0131 int fmpq_mat_equal(const fmpq_mat_t mat1, const fmpq_mat_t mat2);
0132 
0133 int fmpq_mat_is_zero(const fmpq_mat_t mat);
0134 int fmpq_mat_is_one(const fmpq_mat_t mat);
0135 int fmpq_mat_is_integral(const fmpq_mat_t mat);
0136 
0137 FMPQ_MAT_INLINE
0138 int fmpq_mat_is_empty(const fmpq_mat_t mat)
0139 {
0140     return (mat->r == 0) || (mat->c == 0);
0141 }
0142 
0143 FMPQ_MAT_INLINE
0144 int fmpq_mat_is_square(const fmpq_mat_t mat)
0145 {
0146     return (mat->r == mat->c);
0147 }
0148 
0149 /* Integer matrix conversion *************************************************/
0150 
0151 int fmpq_mat_get_fmpz_mat(fmpz_mat_t dest, const fmpq_mat_t mat);
0152 void fmpq_mat_set_fmpz_mat(fmpq_mat_t dest, const fmpz_mat_t mat);
0153 
0154 void fmpq_mat_get_fmpz_mat_entrywise(fmpz_mat_t num, fmpz_mat_t den, const fmpq_mat_t mat);
0155 void fmpq_mat_get_fmpz_mat_matwise(fmpz_mat_t num, fmpz_t den, const fmpq_mat_t mat);
0156 void fmpq_mat_get_fmpz_mat_rowwise(fmpz_mat_t num, fmpz * den, const fmpq_mat_t mat);
0157 void fmpq_mat_get_fmpz_mat_colwise(fmpz_mat_t num, fmpz * den, const fmpq_mat_t mat);
0158 
0159 void fmpq_mat_get_fmpz_mat_rowwise_2(fmpz_mat_t num, fmpz_mat_t num2,
0160         fmpz * den, const fmpq_mat_t mat, const fmpq_mat_t mat2);
0161 
0162 void fmpq_mat_get_fmpz_mat_mod_fmpz(fmpz_mat_t dest, const fmpq_mat_t mat, const fmpz_t mod);
0163 int fmpq_mat_set_fmpz_mat_mod_fmpz(fmpq_mat_t X, const fmpz_mat_t Xmod, const fmpz_t mod);
0164 
0165 void fmpq_mat_set_fmpz_mat_div_fmpz(fmpq_mat_t X, const fmpz_mat_t Xmod, const fmpz_t div);
0166 
0167 /* Matrix multiplication *****************************************************/
0168 
0169 void fmpq_mat_mul_direct(fmpq_mat_t C, const fmpq_mat_t A, const fmpq_mat_t B);
0170 
0171 void fmpq_mat_mul_cleared(fmpq_mat_t C, const fmpq_mat_t A, const fmpq_mat_t B);
0172 
0173 void fmpq_mat_mul_fmpz_mat(fmpq_mat_t C, const fmpq_mat_t A, const fmpz_mat_t B);
0174 void fmpq_mat_mul(fmpq_mat_t C, const fmpq_mat_t A, const fmpq_mat_t B);
0175 
0176 void fmpq_mat_mul_r_fmpz_mat(fmpq_mat_t C, const fmpz_mat_t A, const fmpq_mat_t B);
0177 
0178 void fmpq_mat_mul_fmpz_vec(fmpq* c, const fmpq_mat_t A, const fmpz * b, slong blen);
0179 void fmpq_mat_mul_fmpq_vec(fmpq * c, const fmpq_mat_t A, const fmpq * b, slong blen);
0180 
0181 void fmpq_mat_mul_fmpz_vec_ptr(fmpq * const * c, const fmpq_mat_t A, const fmpz * const * b, slong blen);
0182 void fmpq_mat_mul_fmpq_vec_ptr(fmpq * const * c, const fmpq_mat_t A, const fmpq * const * b, slong blen);
0183 
0184 void fmpq_mat_fmpz_vec_mul(fmpq * c, const fmpz * a, slong alen, const fmpq_mat_t B);
0185 void fmpq_mat_fmpq_vec_mul(fmpq* c, const fmpq* a, slong alen, const fmpq_mat_t B);
0186 
0187 void fmpq_mat_fmpz_vec_mul_ptr(fmpq * const * c, const fmpz * const * a, slong alen, const fmpq_mat_t B);
0188 void fmpq_mat_fmpq_vec_mul_ptr(fmpq * const * c, const fmpq * const * a, slong alen, const fmpq_mat_t B);
0189 
0190 /* Kronecker product *********************************************************/
0191 
0192 void fmpq_mat_kronecker_product(fmpq_mat_t C, const fmpq_mat_t A, const fmpq_mat_t B);
0193 
0194 /* Permutations **************************************************************/
0195 
0196 void fmpq_mat_swap_rows(fmpq_mat_t mat, slong * perm, slong r, slong s);
0197 void fmpq_mat_swap_cols(fmpq_mat_t mat, slong * perm, slong r, slong s);
0198 
0199 void fmpq_mat_invert_rows(fmpq_mat_t mat, slong * perm);
0200 void fmpq_mat_invert_cols(fmpq_mat_t mat, slong * perm);
0201 
0202 /* Trace *********************************************************************/
0203 
0204 void fmpq_mat_trace(fmpq_t trace, const fmpq_mat_t mat);
0205 
0206 /* Determinant ***************************************************************/
0207 
0208 void fmpq_mat_det(fmpq_t det, const fmpq_mat_t mat);
0209 
0210 int fmpq_mat_permanent(fmpq_t res, const fmpq_mat_t A);
0211 
0212 /* Nonsingular solving *******************************************************/
0213 
0214 int fmpq_mat_solve_fmpz_mat_fraction_free(fmpq_mat_t X, const fmpz_mat_t A, const fmpz_mat_t B);
0215 int fmpq_mat_solve_fraction_free(fmpq_mat_t X, const fmpq_mat_t A, const fmpq_mat_t B);
0216 
0217 int fmpq_mat_solve_fmpz_mat_dixon(fmpq_mat_t X, const fmpz_mat_t A, const fmpz_mat_t B);
0218 int fmpq_mat_solve_dixon(fmpq_mat_t X, const fmpq_mat_t A, const fmpq_mat_t B);
0219 
0220 int fmpq_mat_solve_fmpz_mat_multi_mod(fmpq_mat_t X, const fmpz_mat_t A, const fmpz_mat_t B);
0221 int fmpq_mat_solve_multi_mod(fmpq_mat_t X, const fmpq_mat_t A, const fmpq_mat_t B);
0222 
0223 int fmpq_mat_can_solve_fmpz_mat_multi_mod(fmpq_mat_t X, const fmpz_mat_t A, const fmpz_mat_t B);
0224 int fmpq_mat_can_solve_multi_mod(fmpq_mat_t X, const fmpq_mat_t A, const fmpq_mat_t B);
0225 
0226 int fmpq_mat_can_solve_fraction_free(fmpq_mat_t X, const fmpq_mat_t A, const fmpq_mat_t B);
0227 
0228 int fmpq_mat_can_solve_fmpz_mat_dixon(fmpq_mat_t X, const fmpz_mat_t A, const fmpz_mat_t B);
0229 int fmpq_mat_can_solve_dixon(fmpq_mat_t X, const fmpq_mat_t A, const fmpq_mat_t B);
0230 
0231 int fmpq_mat_can_solve(fmpq_mat_t X, const fmpq_mat_t A, const fmpq_mat_t B);
0232 
0233 int fmpq_mat_solve_fmpz_mat(fmpq_mat_t X, const fmpz_mat_t A, const fmpz_mat_t B);
0234 int fmpq_mat_solve(fmpq_mat_t X, const fmpq_mat_t A, const fmpq_mat_t B);
0235 
0236 /* Inverse *******************************************************************/
0237 
0238 int fmpq_mat_inv(fmpq_mat_t B, const fmpq_mat_t A);
0239 
0240 /* Echelon form **************************************************************/
0241 
0242 int fmpq_mat_pivot(slong * perm, fmpq_mat_t mat, slong r, slong c);
0243 
0244 slong fmpq_mat_rref_classical(fmpq_mat_t B, const fmpq_mat_t A);
0245 slong fmpq_mat_rref_fraction_free(fmpq_mat_t B, const fmpq_mat_t A);
0246 slong fmpq_mat_rref(fmpq_mat_t B, const fmpq_mat_t A);
0247 
0248 /* Gram-Schmidt Orthogonalisation  *******************************************/
0249 
0250 void fmpq_mat_gso(fmpq_mat_t B, const fmpq_mat_t A);
0251 
0252 /* Characteristic polynomial *************************************************/
0253 
0254 void fmpq_mat_similarity(fmpq_mat_t A, slong r, fmpq_t d);
0255 
0256 /* Characteristic polynomial *************************************************/
0257 
0258 void _fmpq_mat_charpoly(fmpz * coeffs, fmpz_t den, const fmpq_mat_t mat);
0259 void fmpq_mat_charpoly(fmpq_poly_t pol, const fmpq_mat_t mat);
0260 
0261 /* Minimal polynomial ********************************************************/
0262 
0263 slong _fmpq_mat_minpoly(fmpz * coeffs, fmpz_t den, const fmpq_mat_t mat);
0264 void fmpq_mat_minpoly(fmpq_poly_t pol, const fmpq_mat_t mat);
0265 
0266 #ifdef __cplusplus
0267 }
0268 #endif
0269 
0270 #endif