]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/lls.c
Make av_pix_fmt_descriptors use the same pixel format names as defined
[ffmpeg] / libavutil / lls.c
index 0556d8c80fb0535b70299baef1c4dde3ea530c49..047f976c3f0e37568d20182b921463252d88e46e 100644 (file)
@@ -3,23 +3,25 @@
  *
  * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg 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
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
- * @file lls.c
+ * @file libavutil/lls.c
  * linear least squares model
  */
 
 
 #include "lls.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));
 
@@ -49,12 +47,11 @@ void av_update_lls(LLSModel *m, double *var, double decay){
     }
 }
 
-double av_solve_lls(LLSModel *m, double threshold){
+void av_solve_lls(LLSModel *m, double threshold, int min_order){
     int i,j,k;
-    double (*factor)[MAX_VARS+1]= &m->covariance[1][0];
-    double (*covar )[MAX_VARS+1]= &m->covariance[1][1];
+    double (*factor)[MAX_VARS+1]= (void*)&m->covariance[1][0];
+    double (*covar )[MAX_VARS+1]= (void*)&m->covariance[1][1];
     double  *covar_y            =  m->covariance[0];
-    double variance;
     int count= m->indep_count;
 
     for(i=0; i<count; i++){
@@ -75,33 +72,34 @@ double av_solve_lls(LLSModel *m, double threshold){
     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];
+            sum -= factor[i][k]*m->coeff[0][k];
+        m->coeff[0][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];
-    }
+    for(j=count-1; j>=min_order; j--){
+        for(i=j; i>=0; i--){
+            double sum= m->coeff[0][i];
+            for(k=i+1; k<=j; k++)
+                sum -= factor[k][i]*m->coeff[j][k];
+            m->coeff[j][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;
+        m->variance[j]= covar_y[0];
+        for(i=0; i<=j; i++){
+            double sum= m->coeff[j][i]*covar[i][i] - 2*covar_y[i+1];
+            for(k=0; k<i; k++)
+                sum += 2*m->coeff[j][k]*covar[k][i];
+            m->variance[j] += m->coeff[j][i]*sum;
+        }
     }
-    return variance;
 }
 
-double av_evaluate_lls(LLSModel *m, double *param){
+double av_evaluate_lls(LLSModel *m, double *param, int order){
     int i;
     double out= 0;
 
-    for(i=0; i<m->indep_count; i++)
-        out+= param[i]*m->coeff[i];
+    for(i=0; i<=order; i++)
+        out+= param[i]*m->coeff[order][i];
 
     return out;
 }
@@ -111,29 +109,27 @@ double av_evaluate_lls(LLSModel *m, double *param){
 #include <stdlib.h>
 #include <stdio.h>
 
-int main(){
+int main(void){
     LLSModel m;
-    int i;
+    int i, order;
 
     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);
+        double eval;
+        var[0] = (rand() / (double)RAND_MAX - 0.5)*2;
+        var[1] = var[0] + rand() / (double)RAND_MAX - 0.5;
+        var[2] = var[1] + rand() / (double)RAND_MAX - 0.5;
+        var[3] = var[2] + rand() / (double)RAND_MAX - 0.5;
         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]);
+        av_solve_lls(&m, 0.001, 0);
+        for(order=0; order<3; order++){
+            eval= av_evaluate_lls(&m, var+1, order);
+            printf("real:%9f order:%d pred:%9f var:%f coeffs:%f %9f %9f\n",
+                var[0], order, eval, sqrt(m.variance[order] / (i+1)),
+                m.coeff[order][0], m.coeff[order][1], m.coeff[order][2]);
+        }
     }
     return 0;
 }