-
Notifications
You must be signed in to change notification settings - Fork 2
/
l1estimator.cpp
153 lines (106 loc) · 2.84 KB
/
l1estimator.cpp
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
//
// @(#)l1estimator.cpp 1.1 misha-19may103
//
// Copyright (c) 2003 of Geometrics.
// All rights reserved.
//
// Created: 19may103 by [email protected]
// Version: 19may103 by [email protected]
//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
#ifndef MAX
#define MAX(x, y) (((x) < (y)) ? (y) : (x))
#endif
//#include "magfunc.h"
#include "l1estimator.h"
extern "C" {
int l1_driver_d(int m, int n, double * ct, double *f, double *a,
double prec, double eps, int *irank, int * iter,
double *r, double *z, int *ind, int (*cancel_func)());
}
_l1_params::_l1_params() {
prec = 1.e-16;
eps = 1.e-11;
irank = 0;
iter = 0;
ind = 0;
z = 0.;
m_pCancelFunc = NULL;
}
int Estimate_L1(int n, int m, double *a_matrix, double *y, double *x, void * param,
double * synt, double * fit, double * max_fit, double *st_err, char * message) {
double prec, eps, z;
int irank, iter, ind;
int ret = 0;
int (*cancel_func)() = NULL;
prec = 1.e-16;
eps = 1.e-11;
irank = 0;
iter = 0;
ind = 0;
z = 0.;
if(param) {
L1_PARAMS * p = (L1_PARAMS *)param;
prec = p->prec;
eps = p->eps;
irank = p->irank;
iter = p->iter;
ind = p->ind;
z = p->z;
cancel_func = p->m_pCancelFunc;
}
double *r, * a_old_matrix;
r = a_old_matrix = NULL;
r = (double *)malloc(n*sizeof(double)); if(!r) goto err_return;
a_old_matrix = (double *)malloc(n*m*sizeof(double)); if(!a_old_matrix) goto err_return;
memcpy(a_old_matrix, a_matrix, n*m*sizeof(double));
l1_driver_d(m, n, a_matrix, y, x, prec, eps, &irank, &iter, r, &z, &ind, cancel_func);
ret = 1;
if(message) {
switch(ind) {
case 0:
sprintf(message,"L1: THE SOLUTION VECTOR IS UNIQUE. RANK=%d ITERATIONS=%d", irank, iter); break;
case 1:
sprintf(message,"L1: THE SOLUTION VECTOR IS MOST PROBABLY NOT UNIQUE. RANK=%d ITERATIONS=%d", irank, iter);
break;
default:
sprintf(message,"L1: PREMATURE TERMINATION OF THE CALCULATION DUE TO VERY ILL-CONDITIONING OF MATRIX");
}
}
if(param) {
L1_PARAMS * p = (L1_PARAMS *)param;
p->irank = irank;
p->iter = iter;
p->ind = ind;
p->z = z;
}
memcpy(a_matrix, a_old_matrix, n*m*sizeof(double));
if(synt) {
double d =0.;
double d_max = 0.;
for(int i=0; i<n; i++) {
d += (r[i]*r[i]);
d_max = MAX(d_max, fabs(r[i]));
synt[i] = r[i] + y[i];
}
*fit = sqrt(d/n);
*max_fit = d_max;
if(st_err) for(int i=0; i<m; i++) st_err[i] = 0;
} // end synt
err_return:
if(r) { free(r); r = NULL; }
if(a_old_matrix) { free(a_old_matrix); a_old_matrix = NULL; }
return ret;
}