1 /*
2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef USERMAT_H_
21 #define USERMAT_H_
22 
23 #define MSEL_MAX INT_MIN
24 #define IDX_TBD  INT_MAX
25 
26 typedef enum {
27     SEL_NULL,    /* nothing supplied */
28     SEL_RANGE,   /* integer range p:q provided */
29     SEL_MATRIX,  /* selection matrix provided */
30     SEL_ALL,     /* comma-separated blank */
31     SEL_DIAG,    /* the "diag" dummy constant */
32     SEL_UPPER,   /* the "upper" dummy constant */
33     SEL_LOWER,   /* the "lower" dummy constant */
34     SEL_REAL,    /* the "real" dummy constant */
35     SEL_IMAG,    /* the "imag" dummy constant */
36     SEL_ELEMENT, /* derived: selection is a single element */
37     SEL_CONTIG,  /* derived: selection is contiguous */
38     SEL_EXCL,    /* single exclusion (negative index) */
39     SEL_SINGLE,  /* derived: degenerate range + null */
40     SEL_STR      /* for use with bundles only */
41 } SelType;
42 
43 #define is_sel_dummy(s) (s >= SEL_DIAG && s <= SEL_IMAG)
44 
45 /* Note SEL_EXCL is flagged only in the case of a single negative
46    index. SEL_MATRIX can also do exclusion, if all the elements
47    of the vector are negative.
48 */
49 
50 typedef struct matrix_subspec_ matrix_subspec;
51 
52 union msel {
53     int range[2];
54     gretl_matrix *m;
55     char *str;
56 };
57 
58 struct matrix_subspec_ {
59     int checked;
60     SelType ltype, rtype;
61     union msel lsel, rsel;
62     int *rslice;
63     int *cslice;
64 };
65 
66 #define mspec_get_row_index(m) (m->lsel.range[0])
67 #define mspec_get_col_index(m) (m->rsel.range[0])
68 
69 #define mspec_set_row_index(m,i) (m->lsel.range[0] = m->lsel.range[1] = (i))
70 #define mspec_set_col_index(m,j) (m->rsel.range[0] = m->rsel.range[1] = (j))
71 
72 #define mspec_get_element(m) (m->lsel.range[0])
73 
74 matrix_subspec *matrix_subspec_new (void);
75 
76 GList *get_named_matrix_list (void);
77 
78 gretl_matrix *get_matrix_by_name (const char *name);
79 
80 gretl_matrix *get_matrix_copy_by_name (const char *name, int *err);
81 
82 gretl_matrix *steal_matrix_by_name (const char *name);
83 
84 int assign_scalar_to_submatrix (gretl_matrix *M,
85 				const gretl_matrix *S,
86 				double x,
87 				matrix_subspec *spec);
88 
89 int matrix_replace_submatrix (gretl_matrix *M,
90 			      const gretl_matrix *S,
91 			      matrix_subspec *spec);
92 
93 int umatrix_set_names_from_string (gretl_matrix *M,
94 				   const char *s,
95 				   int byrow);
96 
97 int umatrix_set_names_from_array (gretl_matrix *M,
98 				  void *data,
99 				  int byrow);
100 
101 int umatrix_set_names_from_list (gretl_matrix *M,
102 				 const int *list,
103 				 const DATASET *dset,
104 				 int byrow);
105 
106 char *user_matrix_get_column_name (const gretl_matrix *M, int col,
107 				   int *err);
108 
109 char *user_matrix_get_row_name (const gretl_matrix *M, int row,
110 				int *err);
111 
112 double user_matrix_get_determinant (gretl_matrix *m, int tmpmat,
113 				    int f, int *err);
114 
115 gretl_matrix *user_matrix_vec (const gretl_matrix *m, int *err);
116 
117 gretl_matrix *user_matrix_vech (const gretl_matrix *m, int *err);
118 
119 gretl_matrix *user_matrix_unvech (const gretl_matrix *m, int *err);
120 
121 gretl_matrix *user_matrix_QR_decomp (const gretl_matrix *m,
122 				     gretl_matrix *R,
123 				     int *err);
124 
125 gretl_matrix *user_matrix_SVD (const gretl_matrix *m,
126 			       gretl_matrix *U,
127 			       gretl_matrix *V,
128 			       int *err);
129 
130 gretl_matrix *user_matrix_ols (const gretl_matrix *Y,
131 			       const gretl_matrix *X,
132 			       gretl_matrix *U,
133 			       gretl_matrix *V,
134 			       gretlopt opt,
135 			       int *err);
136 
137 gretl_matrix *user_matrix_rls (const gretl_matrix *Y,
138 			       const gretl_matrix *X,
139 			       const gretl_matrix *R,
140 			       const gretl_matrix *Q,
141 			       gretl_matrix *U,
142 			       gretl_matrix *V,
143 			       int *err);
144 
145 gretl_matrix *user_matrix_GHK (const gretl_matrix *C,
146 			       const gretl_matrix *A,
147 			       const gretl_matrix *B,
148 			       const gretl_matrix *U,
149 			       gretl_matrix *dP,
150 			       int *err);
151 
152 gretl_matrix *user_matrix_eigensym (const gretl_matrix *m,
153 				    gretl_matrix *R,
154 				    int *err);
155 
156 gretl_matrix *user_gensymm_eigenvals (const gretl_matrix *A,
157 				      const gretl_matrix *B,
158 				      gretl_matrix *V,
159 				      int *err);
160 
161 double matrix_get_element (const gretl_matrix *M, int i, int *err);
162 
163 gretl_matrix *matrix_get_chunk (const gretl_matrix *M,
164 				matrix_subspec *spec,
165 				int *err);
166 
167 int *mspec_make_list (int type, union msel *sel, int n,
168 		      int *err);
169 
170 int check_matrix_subspec (matrix_subspec *spec, const gretl_matrix *m);
171 
172 const char *mspec_get_string (matrix_subspec *spec, int i);
173 
174 gretl_matrix *matrix_get_submatrix (const gretl_matrix *M,
175 				    matrix_subspec *spec,
176 				    int prechecked,
177 				    int *err);
178 
179 int matrix_invert_in_place (gretl_matrix *m);
180 
181 int matrix_cholesky_in_place (gretl_matrix *m);
182 
183 int matrix_transpose_in_place (gretl_matrix *m);
184 
185 int matrix_XTX_in_place (gretl_matrix *m);
186 
187 int gretl_matrix_set_part (gretl_matrix *targ,
188 			   const gretl_matrix *src,
189 			   double x, SelType sel);
190 
191 
192 #endif /* USERMAT_H_ */
193