1 /*
2     Copyright (C) 2010 William Hart
3 
4     This file is part of FLINT.
5 
6     FLINT is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
10 */
11 
12 #include "fmpz_mat.h"
13 
14 /*
15     The macros xxx_putc, xxx_flint_printf, and xxx_fmpz_print are provided
16     as wrappers to handle return values and error conditions.  While
17     this is not exactly pretty, it improves the readability of the
18     functions fmpz_mat_fprint and fmpz_mat_fprint_pretty.  Moreover,
19     if we later want to improve the handling of returns values, e.g.
20     to return the number of characters printed, this will be easier.
21 
22     The macros are undef'd at the end of the file.
23  */
24 
25 #define xxx_putc(c)        \
26 do {                       \
27     z = fputc((c), file);  \
28     if (z <= 0)            \
29         return z;          \
30 } while (0)
31 
32 #define xxx_flint_printf()                       \
33 do {                                       \
34     z = flint_fprintf(file, "%li %li  ", r, c);  \
35     if (z <= 0)                            \
36         return z;                          \
37 } while (0)
38 
39 #define xxx_fmpz_print(f)        \
40 do {                             \
41     z = fmpz_fprint(file, (f));  \
42     if (z <= 0)                  \
43         return z;                \
44 } while(0)
45 
fmpz_mat_fprint(FILE * file,const fmpz_mat_t mat)46 int fmpz_mat_fprint(FILE * file, const fmpz_mat_t mat)
47 {
48     int z;
49     slong i, j;
50     slong r = mat->r;
51     slong c = mat->c;
52 
53     xxx_flint_printf();
54     for (i = 0; (i < r); i++)
55     {
56         for (j = 0; j < c; j++)
57         {
58             xxx_fmpz_print(mat->rows[i] + j);
59             if (j != c - 1)
60                 xxx_putc(' ');
61         }
62         if (i != r - 1)
63             xxx_putc(' ');
64     }
65 
66     return z;
67 }
68 
fmpz_mat_fprint_pretty(FILE * file,const fmpz_mat_t mat)69 int fmpz_mat_fprint_pretty(FILE * file, const fmpz_mat_t mat)
70 {
71     int z;
72     slong i, j;
73     slong r = mat->r;
74     slong c = mat->c;
75 
76     xxx_putc('[');
77     for (i = 0; i < r; i++)
78     {
79         xxx_putc('[');
80         for (j = 0; j < c; j++)
81         {
82             xxx_fmpz_print(mat->rows[i] + j);
83             if (j != c - 1)
84                 xxx_putc(' ');
85         }
86         xxx_putc(']');
87         xxx_putc('\n');
88     }
89     xxx_putc(']');
90 
91     return z;
92 }
93 
94 #undef xxx_putc
95 #undef xxx_flint_printf
96 #undef xxx_fmpz_print
97 
98