la.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00040 #ifndef LINEAR_H
00041 #define LINEAR_H
00042
00043 #include "fastlib/la/matrix.h"
00044 #include "fastlib/la/uselapack.h"
00045
00046
00047
00048 #include "fastlib/math/math_lib.h"
00049
00050 #include <math.h>
00051
00052 namespace la {
00056 inline double DistanceSqEuclidean(
00057 index_t length, const double *va, const double *vb) {
00058 double s = 0;
00059 do {
00060 double d = *va++ - *vb++;
00061 s += d * d;
00062 } while (--length);
00063 return s;
00064 }
00068 inline double DistanceSqEuclidean(const Vector& x, const Vector& y) {
00069 DEBUG_SAME_SIZE(x.length(), y.length());
00070 return DistanceSqEuclidean(x.length(), x.ptr(), y.ptr());
00071 }
00081 template<int t_pow>
00082 inline double RawLMetric(
00083 index_t length, const double *va, const double *vb) {
00084 double s = 0;
00085 do {
00086 double d = *va++ - *vb++;
00087 s += math::PowAbs<t_pow, 1>(d);
00088 } while (--length);
00089 return s;
00090 }
00100 template<int t_pow>
00101 inline double LMetric(
00102 index_t length, const double *va, const double *vb) {
00103 return math::Pow<1, t_pow>(RawLMetric<t_pow>(length, va, vb));
00104 }
00108 inline double Trace(Matrix &a) {
00109
00110 DEBUG_SAME_SIZE(a.n_cols(), a.n_rows());
00111 double trace=0;
00112 for(index_t i=0; i<a.n_cols(); i++) {
00113 trace+=a.get(i, i);
00114 }
00115 return trace;
00116 }
00117
00125 inline success_t LeastSquareFit(Vector &y, Matrix &x, Vector *a) {
00126 DEBUG_SAME_SIZE(y.length(), x.n_rows());
00127 DEBUG_ASSERT(x.n_rows() >= x.n_cols());
00128 Vector r_xy_vec;
00129 Matrix r_xx_mat;
00130 la::MulTransAInit(x, x, &r_xx_mat);
00131 la::MulInit(y, x, &r_xy_vec);
00132 success_t status = la::SolveInit(r_xx_mat, r_xy_vec, a);
00133 if unlikely(status != SUCCESS_PASS) {
00134 if (status==SUCCESS_FAIL) {
00135 FATAL("Least square fit failed \n");
00136 } else {
00137 NONFATAL("Least square fit returned a warning \n");
00138 }
00139 }
00140 return status;
00141 }
00142
00150 inline success_t LeastSquareFit(Matrix &y, Matrix &x, Matrix *a) {
00151 DEBUG_SAME_SIZE(y.n_rows(), x.n_rows());
00152 DEBUG_ASSERT(x.n_rows() >= x.n_cols());
00153 Matrix r_xy_mat;
00154 Matrix r_xx_mat;
00155 la::MulTransAInit(x, x, &r_xx_mat);
00156 la::MulTransAInit(x, y, &r_xy_mat);
00157 success_t status = la::SolveInit(r_xx_mat, r_xy_mat, a);
00158 if unlikely(status != SUCCESS_PASS) {
00159 if (status==SUCCESS_FAIL) {
00160 FATAL("Least square fit failed \n");
00161 } else {
00162 NONFATAL("Least square fit returned a warning \n");
00163 }
00164 }
00165 return status;
00166 }
00167
00175 inline success_t LeastSquareFitTrans(Matrix &y, Matrix &x, Matrix *a) {
00176 DEBUG_SAME_SIZE(y.n_rows(), x.n_cols());
00177 DEBUG_ASSERT(x.n_cols() >= x.n_rows());
00178 Matrix r_xy_mat;
00179 Matrix r_xx_mat;
00180 la::MulTransBInit(x, x, &r_xx_mat);
00181 la::MulInit(x, y, &r_xy_mat);
00182 success_t status = la::SolveInit(r_xx_mat, r_xy_mat, a);
00183 if unlikely(status != SUCCESS_PASS) {
00184 if (status==SUCCESS_FAIL) {
00185 FATAL("Least square fit failed \n");
00186 } else {
00187 NONFATAL("Least square fit returned a warning \n");
00188 }
00189 }
00190 return status;
00191 }
00192 };
00193
00194 #endif