la.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00040 #ifndef LINEAR_H
00041 #define LINEAR_H
00042 
00043 #include "fastlib/la/matrix.h"
00044 #include "fastlib/la/uselapack.h"
00045 //#include "matrix.h"
00046 //#include "uselapack.h"
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     // trace has meaning only for square matrices
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
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3