PIPS
matrix_io.c
Go to the documentation of this file.
1 /*
2 
3  $Id: matrix_io.c 1671 2019-06-26 19:14:11Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of Linear/C3 Library.
8 
9  Linear/C3 Library is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  Linear/C3 Library is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with Linear/C3 Library. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 
25  /* package matrice */
26 
27 #ifdef HAVE_CONFIG_H
28  #include "config.h"
29 #endif
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "linear_assert.h"
34 
35 #include "boolean.h"
36 #include "arithmetique.h"
37 
38 #include "matrix.h"
39 
40 /* void matrix_fprint(File * f, matrix a): print a rational matrix
41  *
42  * Note: the output format is compatible with matrix_fscan()
43  */
44 void matrix_fprint(FILE * f, Pmatrix a)
45 {
46  int i, j;
47  int m = MATRIX_NB_LINES(a);
48  int n = MATRIX_NB_COLUMNS(a);
50 
51  (void) fprintf(f, "%d %d\n", m, n);
52  (void) fprint_Value(f, MATRIX_DENOMINATOR(a));
53  (void) fprintf(f, "\n");
54  for(i=1; i<=m; i++) {
55  for(j=1; j<=n; j++) {
56  // matrix_pr_quot(f, MATRIX_ELEM(a,i,j), MATRIX_DENOMINATOR(a));
57  fprint_Value(f, MATRIX_ELEM(a,i,j));
58  fprintf(f, " ");
59  }
60  (void) fprintf(f, "\n");
61  }
62 }
63 
64 /* void matrix_print(matrice a): print an (nxm) rational matrix
65  *
66  * Note: the output format is incompatible with matrix_fscan()
67  * this should be implemented as a macro, but it's a function for
68  * dbx's sake
69  */
70 void matrix_print(a)
71 Pmatrix a;
72 {
73  matrix_fprint(stdout,a);
74 }
75 
76 /* void matrix_fscan(FILE * f, matrice * a, int * n, int * m):
77  * read an (nxm) rational matrix in an ASCII file accessible
78  * via file descriptor f; a is allocated as a function of its number
79  * of columns and rows, n and m.
80  *
81  * Format:
82  *
83  * n m denominator a[1,1] a[1,2] ... a[1,m] a[2,1] ... a[2,m] ... a[n,m]
84  *
85  * After the two dimensions and the global denominator, the matrix as
86  * usually displayed, line by line. Line feed can be used anywhere.
87  * Example for a (2x3) integer matrix:
88  * 2 3
89  * 1
90  * 1 2 3
91  * 4 5 6
92  */
93 void matrix_fscan(f,a,n,m)
94 FILE * f;
95 Pmatrix *a;
96 int * n; /* column size */
97 int * m; /* row size */
98 {
99  int r;
100  int c;
101 
102  /* number of read elements */
103  int n_read;
104 
105  /* read dimensions */
106  n_read = fscanf(f,"%d%d", n, m);
107  assert(n_read==2);
108  assert(1 <= *n && 1 <= *m);
109 
110  /* allocate a */
111  *a = matrix_new(*n,*m);
112 
113  /* read denominator */
114  n_read = fscan_Value(f,&(MATRIX_DENOMINATOR(*a)));
115  assert(n_read == 1);
116  /* necessaires pour eviter les divisions par zero */
118  /* pour "normaliser" un peu les representations */
120 
121  for(r = 1; r <= *n; r++)
122  for(c = 1; c <= *m; c++) {
123  n_read = fscan_Value(f, &MATRIX_ELEM(*a,r,c));
124  assert(n_read == 1);
125  }
126 }
127 
128 /* void matrix_pr_quot(FILE * f, int a, int b):
129  *
130  * print quotient a/b in a sensible way,
131  * i.e. add a space in front of positive number to compensate for the
132  * minus sign of negative number
133  *
134  * FI: a quoi sert le parametre b? A quoi sert la variable d? =>ARGSUSED
135  */
136 /*ARGSUSED*/
137 void matrix_pr_quot(FILE * f, Value a, Value b __attribute__ ((unused)))
138 {
139  if (value_pos_p(a))
140  fprintf(f, " ");
141  fprintf(f, " ");
142  fprint_Value(f, a);
143  fprintf(f, " ");
144 }
float a2sf[2] __attribute__((aligned(16)))
USER generates a user error (i.e., non fatal) by printing the given MSG according to the FMT.
Definition: 3dnow.h:3
#define value_pos_p(val)
#define VALUE_ZERO
#define value_notzero_p(val)
int Value
void fprint_Value(FILE *, Value)
Definition: io.c:42
int fscan_Value(FILE *, Value *)
Definition: io.c:58
#define MATRIX_NB_LINES(matrix)
Definition: matrix-local.h:87
#define MATRIX_NB_COLUMNS(matrix)
Definition: matrix-local.h:88
#define MATRIX_DENOMINATOR(matrix)
int MATRIX_DENONIMATOR(matrix): acces au denominateur global d'une matrice matrix
Definition: matrix-local.h:86
#define MATRIX_ELEM(matrix, i, j)
Macros d'acces aux elements d'une matrice.
Definition: matrix-local.h:80
Pmatrix matrix_new(int m, int n)
package matrix
Definition: alloc.c:41
void matrix_pr_quot(FILE *f, Value a, Value b __attribute__((unused)))
void matrix_pr_quot(FILE * f, int a, int b):
Definition: matrix_io.c:137
void matrix_fscan(FILE *f, Pmatrix *a, int *n, int *m)
void matrix_fscan(FILE * f, matrice * a, int * n, int * m): read an (nxm) rational matrix in an ASCII...
Definition: matrix_io.c:93
void matrix_fprint(FILE *f, Pmatrix a)
package matrice
Definition: matrix_io.c:44
void matrix_print(Pmatrix a)
void matrix_print(matrice a): print an (nxm) rational matrix
Definition: matrix_io.c:70
#define assert(ex)
Definition: newgen_assert.h:41
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
package matrice
Definition: matrix-local.h:63