-
Notifications
You must be signed in to change notification settings - Fork 17
/
nml.h
183 lines (158 loc) · 6.6 KB
/
nml.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
Copyright 20201 Andrei N. Ciobanu
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifdef __cplusplus
extern "C" {
#endif
#ifndef nml_UTIL
#define nml_UTIL
#include "nml_util.h"
#define NML_MIN_COEF 0.000000000000001
// *****************************************************************************
//
// Library structures
//
// *****************************************************************************
typedef struct nml_mat_s {
unsigned int num_rows;
unsigned int num_cols;
double **data;
int is_square;
} nml_mat;
typedef struct nml_mat_lup_s {
nml_mat *L;
nml_mat *U;
nml_mat *P;
unsigned int num_permutations;
} nml_mat_lup;
typedef struct nml_mat_qr_s {
nml_mat *Q;
nml_mat *R;
} nml_mat_qr;
// *****************************************************************************
//
// Constructing and destroying a matrix struct
//
// *****************************************************************************
nml_mat *nml_mat_new(unsigned int num_rows, unsigned int num_cols);
nml_mat *nml_mat_rnd(unsigned int num_rows, unsigned int num_cols, double min, double max);
nml_mat *nml_mat_sqr(unsigned int size);
nml_mat *nml_mat_sqr_rnd(unsigned int size, double min, double max);
nml_mat *nml_mat_eye(unsigned int size);
nml_mat *nml_mat_cp(nml_mat *m);
nml_mat *nml_mat_from(unsigned int num_rows, unsigned int num_cols, unsigned int n_vals, double *vals);
nml_mat *nml_mat_fromfile(const char *file);
nml_mat *nml_mat_fromfilef(FILE *f);
void nml_mat_free(nml_mat *matrix);
// *****************************************************************************
//
// Matrix Equality
//
// *****************************************************************************
int nml_mat_eqdim(nml_mat *m1, nml_mat *m2);
int nml_mat_eq(nml_mat *m1, nml_mat *m2, double tolerance);
// *****************************************************************************
//
// Matrix printing
//
// *****************************************************************************
void nml_mat_print(nml_mat *matrix);
void nml_mat_printf(nml_mat *matrix, const char *d_fmt);
// *****************************************************************************
//
// Accessing and modifying matrix elements
//
// *****************************************************************************
double nml_mat_get(nml_mat *matrix, unsigned int i, unsigned int j);
void nml_mat_set(nml_mat *matrix, unsigned int i, unsigned int j, double value);
nml_mat *nml_mat_col_get(nml_mat *m, unsigned int col);
nml_mat *nml_mat_col_mult(nml_mat *m, unsigned int col, double num);
int nml_mat_col_mult_r(nml_mat *m, unsigned int col, double num);
nml_mat *nml_mat_row_get(nml_mat *m, unsigned int row);
nml_mat *nml_mat_row_mult(nml_mat *m, unsigned int row, double num);
int nml_mat_row_mult_r(nml_mat *m, unsigned int row, double num);
void nml_mat_all_set(nml_mat *matrix, double value);
int nml_mat_diag_set(nml_mat *matrix, double value);
nml_mat *nml_mat_row_addrow(nml_mat *m, unsigned int where, unsigned int row, double multiplier);
int nml_mat_row_addrow_r(nml_mat *m, unsigned int where, unsigned int row, double multiplier);
nml_mat *nml_mat_smult(nml_mat *m, double num);
int nml_mat_smult_r(nml_mat *m, double num);
// *****************************************************************************
//
// Modifying the matrix structure
//
// *****************************************************************************
nml_mat *nml_mat_col_rem(nml_mat *m, unsigned int column);
nml_mat *nml_mat_row_rem(nml_mat *m, unsigned int row);
nml_mat *nml_mat_row_swap(nml_mat *m, unsigned int row1, unsigned int row2);
int nml_mat_row_swap_r(nml_mat *m, unsigned int row1, unsigned int row2);
nml_mat *nml_mat_col_swap(nml_mat *m, unsigned int col1, unsigned int col2);
int nml_mat_col_swap_r(nml_mat *m, unsigned int col1, unsigned int col2);
nml_mat *nml_mat_cath(unsigned int mnun, nml_mat **matrices);
nml_mat *nml_mat_catv(unsigned int mnum, nml_mat **matrices);
// *****************************************************************************
//
// Matrix Operations
//
// *****************************************************************************
nml_mat *nml_mat_add(nml_mat *m1, nml_mat *m2);
int nml_mat_add_r(nml_mat *m1, nml_mat *m2);
nml_mat *nml_mat_sub(nml_mat *m1, nml_mat *m2);
int nml_mat_sub_r(nml_mat *m1, nml_mat *m2);
nml_mat *nml_mat_dot(nml_mat *m1, nml_mat *m2);
nml_mat *nml_mat_transp(nml_mat *m);
double nml_mat_trace(nml_mat* m);
// *****************************************************************************
//
// Row Echelon
//
// *****************************************************************************
nml_mat *nml_mat_ref(nml_mat *m);
nml_mat *nml_mat_rref(nml_mat *m);
// *****************************************************************************
//
// LUP Decomposition
//
// *****************************************************************************
nml_mat_lup *nml_mat_lup_new(nml_mat *L, nml_mat *U, nml_mat *P, unsigned int num_permutations);
nml_mat_lup *nml_mat_lup_solve(nml_mat *m);
void nml_mat_lup_free(nml_mat_lup* lu);
void nml_mat_lup_print(nml_mat_lup *lu);
void nml_mat_lup_printf(nml_mat_lup *lu, const char *fmt);
double nml_mat_det(nml_mat_lup* lup);
nml_mat *nml_mat_lu_get(nml_mat_lup* lup);
nml_mat *nml_mat_inv(nml_mat_lup *m);
// *****************************************************************************
//
// Solving linear systems of equations
//
// *****************************************************************************
nml_mat *nml_ls_solvefwd(nml_mat *low_triang, nml_mat *b);
nml_mat *nml_ls_solvebck(nml_mat *upper_triang, nml_mat *b);
nml_mat *nml_ls_solve(nml_mat_lup *lup, nml_mat* b);
// *****************************************************************************
//
// QR Decomposition
//
// *****************************************************************************
double nml_vect_dot(nml_mat *m1, unsigned int m1col, nml_mat *m2, unsigned m2col);
nml_mat *nml_mat_l2norm(nml_mat *m);
double nml_mat_col_l2norm(nml_mat *m1, unsigned int j);
nml_mat *nml_mat_normalize(nml_mat *m);
int nml_mat_normalize_r(nml_mat *m);
nml_mat_qr *nml_mat_qr_new();
void nml_mat_qr_free(nml_mat_qr *qr);
nml_mat_qr * nml_mat_qr_solve(nml_mat *m);
#endif
#ifdef __cplusplus
}
#endif