sp_matrix.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include "sp_matrix.h"
3 
4 /*****************************************************************************/
5  MATRIX create_matrix (int rows, int cols)
6 
7 /*****************************************************************************
8  Creates a MATRIX of dimensions (rows, cols) and initializaes it to zeros.
9 ******************************************************************************/
10 {
11  MATRIX m;
12 
13  MROWS (m) = rows;
14  MCOLS (m) = cols;
15 
16  {
17  int i, j;
18 
19  for (i = 0; i < MROWS (m); i++)
20  for (j = 0; j < MCOLS (m); j++)
21  MDATA (m, i, j) = 0;
22  }
23 
24  return m;
25 }
26 
27 /*****************************************************************************/
28  void initialize_matrix (MATRIX *m, int rows, int cols)
29 
30 /*****************************************************************************
31  Initializes a MATRIX to dimensions (rows, cols) and content zeros.
32 ******************************************************************************/
33 {
34  MROWS (*m) = rows;
35  MCOLS (*m) = cols;
36 
37  {
38  int i, j;
39 
40  for (i = 0; i < MROWS (*m); i++)
41  for (j = 0; j < MCOLS (*m); j++)
42  MDATA (*m, i, j) = 0;
43  }
44 
45 }
46 
47 
48 /*****************************************************************************/
49  void print_matrix (char *message, MATRIX const *m)
50 
51 /*****************************************************************************
52  Print to stdout the contents of MATRIX m.
53 ******************************************************************************/
54 {
55  int i, j;
56 
57  printf ("%s\n",message);
58  printf("%d %d \n",MROWS (*m),MCOLS (*m));
59  if ((MROWS (*m) <= MAX_ROWS) && (MCOLS (*m) <= MAX_COLS))
60  for (i = 0; i < MROWS (*m); i++)
61  {
62  for (j = 0; j < MCOLS (*m); j++)
63  printf ("%10.5f ", MDATA (*m, i, j));
64  printf ("\n");
65  }
66  else printf ("Dimension incorrecta!");
67  printf ("\n");
68 }
69 
70 /*****************************************************************************/
71  VECTOR create_vector (int elements)
72 
73 /*****************************************************************************
74  Initializes a VECTOR to dimension (elements) and its contents to zeros.
75 ******************************************************************************/
76 {
77  VECTOR v;
78 
79  VELEMENTS (v) = elements;
80 
81  {
82  int i;
83 
84  for (i = 0; i < VELEMENTS (v); i++)
85  VDATA (v, i) = 0;
86  }
87 
88  return v;
89 }
90 
91 /*****************************************************************************/
92  void initialize_vector (VECTOR *v, int elements)
93 
94 /*****************************************************************************
95  Initializes a VECTOR to dimension (elements) and its contents to zeros.
96 ******************************************************************************/
97 {
98  VELEMENTS (*v) = elements;
99 
100  {
101  int i;
102 
103  for (i = 0; i < VELEMENTS (*v); i++)
104  VDATA (*v, i) = 0;
105  }
106 }
107 
108 /*****************************************************************************/
109  void print_vector (char *message, VECTOR const *v)
110 
111 /*****************************************************************************
112  Print to stdout the contents of VECTOR m.
113 ******************************************************************************/
114 {
115  int i;
116 
117  printf ("%s\n",message);
118  if (VELEMENTS (*v) <= MAX_ROWS)
119  for (i = 0; i < VELEMENTS (*v); i++)
120  {
121  printf ("%f ", VDATA (*v, i));
122  printf ("\n");
123  }
124  else printf ("Dimension incorrecta!");
125  printf ("\n");
126 }
127 
128 /*****************************************************************************/
129  float cross_product (MATRIX const *m, int f1, int c1, int f2, int c2)
130 
131 /*****************************************************************************
132 ******************************************************************************/
133 {
134  return MDATA (*m, f1, c1) * MDATA (*m, f2, c2) - MDATA (*m, f1, c2) * MDATA (*m, f2, c1);
135 }
136 
137 /*****************************************************************************/
138 int determinant (MATRIX const *m, float *result)
139 /*****************************************************************************
140 ******************************************************************************/
141 {
142  if (!M_SQUARE (*m))
143  {
144  printf ("ERROR (determinant): MATRIX must be square!\n");
145  print_matrix ("MATRIX:", m);
146  return -1;
147  }
148  else
149  {
150 
151  if (MROWS (*m) == 1)
152  *result = MDATA (*m, 0, 0);
153  else if (MROWS (*m) == 2)
154  *result = cross_product (m, 0, 0, 1, 1);
155  else
156  *result = MDATA (*m, 0, 0) * cross_product (m, 1, 1, 2, 2)
157  - MDATA (*m, 0, 1) * cross_product (m, 1, 0, 2, 2)
158  + MDATA (*m, 0, 2) * cross_product (m, 1, 0, 2, 1);
159 
160 
161  return 1;
162  }
163 }
164 
165 /*****************************************************************************/
166  int inverse_matrix (MATRIX const *m, MATRIX *n)
167 
168 /*****************************************************************************
169 ******************************************************************************/
170 {
171  if (!M_SQUARE (*m))
172  {
173  printf ("ERROR (inverse_matrix): MATRIX must be square!\n");
174  print_matrix ("MATRIX:", m);
175  n->cols=0; n->rows=0;
176  return -1;
177  }
178  else
179  {
180  float det;
181  int res;
182 
183  res = determinant (m,&det);
184 
185  if (res == -1)
186  {
187  printf ("ERROR (inverse_matrix): singular MATRIX!\n");
188  print_matrix ("MATRIX:", m);
189  return -1;
190  }
191  else
192  {
193  initialize_matrix (n, MROWS (*m), MCOLS (*m));
194  if (MROWS (*m) == 1)
195  {
196  MDATA (*n, 0, 0) = 1 / det ;
197  }
198  else if (MROWS (*m) == 2)
199  {
200  MDATA (*n, 0, 0) = MDATA (*m, 1, 1) / det ;
201  MDATA (*n, 0, 1) = -MDATA (*m, 0, 1) / det ;
202  MDATA (*n, 1, 0) = -MDATA (*m, 1, 0) / det ;
203  MDATA (*n, 1, 1) = MDATA (*m, 0, 0) / det ;
204  }
205  else
206  {
207  MDATA (*n, 0, 0) = cross_product (m, 1, 1, 2, 2) / det ;
208  MDATA (*n, 0, 1) = -cross_product (m, 0, 1, 2, 2) / det ;
209  MDATA (*n, 0, 2) = cross_product (m, 0, 1, 1, 2) / det ;
210  MDATA (*n, 1, 0) = -cross_product (m, 1, 0, 2, 2) / det ;
211  MDATA (*n, 1, 1) = cross_product (m, 0, 0, 2, 2) / det ;
212  MDATA (*n, 1, 2) = -cross_product (m, 0, 0, 1, 2) / det ;
213  MDATA (*n, 2, 0) = cross_product (m, 1, 0, 2, 1) / det ;
214  MDATA (*n, 2, 1) = -cross_product (m, 0, 0, 2, 1) / det ;
215  MDATA (*n, 2, 2) = cross_product (m, 0, 0, 1, 1) / det ;
216  }
217  }
218  }
219  return 1;
220 }
221 
222 /*****************************************************************************/
223  int multiply_matrix_vector (MATRIX const *m, VECTOR const *v, VECTOR *r)
224 
225 /*****************************************************************************
226  Returns the VECTOR-MATRIX product of m and v in r.
227 ******************************************************************************/
228 {
229  if (! (MV_COMPAT_DIM (*m, *v)))
230  {
231  printf ("ERROR (multiply_matrix_vector): MATRIX and VECTOR dimensions incompatible!\n");
232  print_matrix ("MATRIX:", m);
233  print_vector ("VECTOR:", v);
234  return -1; /*added 1996-07*/
235  }
236  else
237  {
238  int i, j;
239  float datum;
240 
241  VELEMENTS (*r) = MROWS (*m);
242 
243  for (i = 0; i < MROWS (*m); i++)
244  {
245  datum = 0;
246  for (j = 0; j < VELEMENTS (*v); j++)
247  datum = datum + MDATA (*m, i, j) * VDATA (*v, j);
248  VDATA (*r, i) = datum;
249  }
250  }
251  return 1;
252 }
253 
void initialize_matrix(MATRIX *m, int rows, int cols)
Definition: sp_matrix.c:28
MATRIX create_matrix(int rows, int cols)
Definition: sp_matrix.c:5
int cols
Definition: sp_matrix.h:23
#define MDATA(m, i, j)
Definition: sp_matrix.h:41
void print_matrix(char *message, MATRIX const *m)
Definition: sp_matrix.c:49
void print_vector(char *message, VECTOR const *v)
Definition: sp_matrix.c:109
#define MAX_ROWS
Definition: sp_matrix.h:18
#define MAX_COLS
Definition: sp_matrix.h:19
#define VDATA(v, i)
Definition: sp_matrix.h:44
#define MROWS(m)
Definition: sp_matrix.h:39
#define MV_COMPAT_DIM(m, v)
Definition: sp_matrix.h:50
VECTOR create_vector(int elements)
Definition: sp_matrix.c:71
#define M_SQUARE(m)
Definition: sp_matrix.h:46
#define m(v1, v2)
Definition: egsl_macros.h:13
int inverse_matrix(MATRIX const *m, MATRIX *n)
Definition: sp_matrix.c:166
#define VELEMENTS(v)
Definition: sp_matrix.h:43
int multiply_matrix_vector(MATRIX const *m, VECTOR const *v, VECTOR *r)
Definition: sp_matrix.c:223
void initialize_vector(VECTOR *v, int elements)
Definition: sp_matrix.c:92
float cross_product(MATRIX const *m, int f1, int c1, int f2, int c2)
Definition: sp_matrix.c:129
int rows
Definition: sp_matrix.h:22
int determinant(MATRIX const *m, float *result)
Definition: sp_matrix.c:138
#define MCOLS(m)
Definition: sp_matrix.h:40


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23