[Ffmpeg-cvslog] r5740 - in trunk/libavutil: Makefile lls.c lls.h
michael
subversion
Fri Jul 14 12:03:09 CEST 2006
Author: michael
Date: Fri Jul 14 12:03:09 2006
New Revision: 5740
Added:
trunk/libavutil/lls.c
trunk/libavutil/lls.h
Modified:
trunk/libavutil/Makefile
Log:
linear least squares solver using cholesky factorization
Modified: trunk/libavutil/Makefile
==============================================================================
--- trunk/libavutil/Makefile (original)
+++ trunk/libavutil/Makefile Fri Jul 14 12:03:09 2006
@@ -12,6 +12,7 @@
intfloat_readwrite.o \
crc.o \
md5.o \
+ lls.o \
adler32.o
HEADERS = avutil.h common.h mathematics.h integer.h rational.h \
Added: trunk/libavutil/lls.c
==============================================================================
--- (empty file)
+++ trunk/libavutil/lls.c Fri Jul 14 12:03:09 2006
@@ -0,0 +1,144 @@
+/*
+ * linear least squares model
+ *
+ * Copyright (c) 2006 Michael Niedermayer <michaelni at gmx.at>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * @file lls.c
+ * linear least squares model
+ */
+
+#include <math.h>
+#include <string.h>
+
+#include "lls.h"
+
+#undef NDEBUG // allways check asserts, the speed effect is far too small to disable them
+#include <assert.h>
+
+#ifdef TEST
+#define av_log(a,b,...) printf(__VA_ARGS__)
+#endif
+
+void av_init_lls(LLSModel *m, int indep_count){
+ memset(m, 0, sizeof(LLSModel));
+
+ m->indep_count= indep_count;
+}
+
+void av_update_lls(LLSModel *m, double *var, double decay){
+ int i,j;
+
+ for(i=0; i<=m->indep_count; i++){
+ for(j=i; j<=m->indep_count; j++){
+ m->covariance[i][j] *= decay;
+ m->covariance[i][j] += var[i]*var[j];
+ }
+ }
+}
+
+double av_solve_lls(LLSModel *m, double threshold){
+ int i,j,k;
+ double (*factor)[MAX_VARS+1]= &m->covariance[1][0];
+ double (*covar )[MAX_VARS+1]= &m->covariance[1][1];
+ double *covar_y = m->covariance[0];
+ double variance;
+ int count= m->indep_count;
+
+ for(i=0; i<count; i++){
+ for(j=i; j<count; j++){
+ double sum= covar[i][j];
+
+ for(k=i-1; k>=0; k--)
+ sum -= factor[i][k]*factor[j][k];
+
+ if(i==j){
+ if(sum < threshold)
+ sum= 1.0;
+ factor[i][i]= sqrt(sum);
+ }else
+ factor[j][i]= sum / factor[i][i];
+ }
+ }
+ for(i=0; i<count; i++){
+ double sum= covar_y[i+1];
+ for(k=i-1; k>=0; k--)
+ sum -= factor[i][k]*m->coeff[k];
+ m->coeff[i]= sum / factor[i][i];
+ }
+
+ for(i=count-1; i>=0; i--){
+ double sum= m->coeff[i];
+ for(k=i+1; k<count; k++)
+ sum -= factor[k][i]*m->coeff[k];
+ m->coeff[i]= sum / factor[i][i];
+ }
+
+ variance= covar_y[0];
+ for(i=0; i<count; i++){
+ double sum= m->coeff[i]*covar[i][i] - 2*covar_y[i+1];
+ for(j=0; j<i; j++)
+ sum += 2*m->coeff[j]*covar[j][i];
+ variance += m->coeff[i]*sum;
+ }
+ return variance;
+}
+
+double av_evaluate_lls(LLSModel *m, double *param){
+ int i;
+ double out= 0;
+
+ for(i=0; i<m->indep_count; i++)
+ out+= param[i]*m->coeff[i];
+
+ return out;
+}
+
+#ifdef TEST
+
+#include <stdlib.h>
+#include <stdio.h>
+
+int main(){
+ LLSModel m;
+ int i;
+
+ av_init_lls(&m, 3);
+
+ for(i=0; i<100; i++){
+ double var[4];
+ double eval, variance;
+ var[1] = rand() / (double)RAND_MAX;
+ var[2] = rand() / (double)RAND_MAX;
+ var[3] = rand() / (double)RAND_MAX;
+
+ var[2]= var[1] + var[3];
+
+ var[0] = var[1] + var[2] + var[3] + var[1]*var[2]/100;
+
+ eval= av_evaluate_lls(&m, var+1);
+ av_update_lls(&m, var, 0.99);
+ variance= av_solve_lls(&m, 0.001);
+ av_log(NULL, AV_LOG_DEBUG, "real:%f pred:%f var:%f coeffs:%f %f %f\n",
+ var[0], eval, sqrt(variance / (i+1)),
+ m.coeff[0], m.coeff[1], m.coeff[2]);
+ }
+ return 0;
+}
+
+#endif
Added: trunk/libavutil/lls.h
==============================================================================
--- (empty file)
+++ trunk/libavutil/lls.h Fri Jul 14 12:03:09 2006
@@ -0,0 +1,42 @@
+/*
+ * linear least squares model
+ *
+ * Copyright (c) 2006 Michael Niedermayer <michaelni at gmx.at>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef LLS_H
+#define LLS_H
+
+#define MAX_VARS 32
+
+//FIXME avoid direct access to LLSModel from outside
+
+/**
+ * Linear least squares model.
+ */
+typedef struct LLSModel{
+ double covariance[MAX_VARS+1][MAX_VARS+1];
+ double coeff[MAX_VARS];
+ int indep_count;
+}LLSModel;
+
+void av_init_lls(LLSModel *m, int indep_count);
+void av_update_lls(LLSModel *m, double *param, double decay);
+double av_solve_lls(LLSModel *m, double threshold);
+double av_evaluate_lls(LLSModel *m, double *param);
+
+#endif
More information about the ffmpeg-cvslog
mailing list