[Kst] branches/work/kst/hierarchy/kst/src/plugins/linefit

Eli Fidler eli at staikos.net
Tue Dec 5 18:00:10 CET 2006


SVN commit 610808 by fidler:

manually merge trunk changes 604960:609664 into hierarchy branch


 M  +7 -7      Makefile.am  
 A             kstobject_linefit.desktop  
 M  +146 -87   linefit.cpp  
 D             linefit.xml  


--- branches/work/kst/hierarchy/kst/src/plugins/linefit/Makefile.am #610807:610808
@@ -1,11 +1,11 @@
-installdir=$(kde_moduledir)/kstplugins
-INCLUDES=-I$(srcdir)/../../../kst $(all_includes)
+INCLUDES=-I$(top_srcdir)/kst -I$(top_srcdir)/kst/src/widgets -I$(top_srcdir)/kst/src/libkst -I$(top_srcdir)/kst/src/libkstapp  -I$(top_srcdir)/kst/src/libkstmath $(all_includes)
 
-install_LTLIBRARIES=linefit.la
+kde_module_LTLIBRARIES=kstobject_linefit.la
 
-linefit_la_LDFLAGS=-module $(KDE_PLUGIN)
-linefit_la_SOURCES=linefit.cpp
+kstobject_linefit_la_LDFLAGS=$(all_libraries) -module -avoid-version
+kstobject_linefit_la_SOURCES=linefit.cpp
 
+services_DATA=kstobject_linefit.desktop
+servicesdir=$(kde_servicesdir)/kst
+
 METASOURCES=AUTO
-
-install_DATA=linefit.xml
--- branches/work/kst/hierarchy/kst/src/plugins/linefit/linefit.cpp #610807:610808
@@ -1,108 +1,167 @@
-/*
- *  Line fitting plugin demo for KST.
- *  Copyright 2003, The University of Toronto
- *  Released under the terms of the GPL.
- *
- *  Derived from Numerical Recipes in C, Press et al.
- */
+/***************************************************************************
+                   linefit.cpp
+                             -------------------
+    begin                : 09/08/06
+    copyright            : (C) 2006 The University of Toronto
+    email                :
+ ***************************************************************************/
 
-#include <stdlib.h>
-#include <math.h>
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+#include "linefit.h"
 
-#define X 0
-#define Y 1
+#include <kgenericfactory.h>
 
+//in
+static const QString& X_ARRAY = KGlobal::staticQString("X Array");
+static const QString& Y_ARRAY = KGlobal::staticQString("Y Array");
 
-extern "C" int linefit(const double *const inArrays[], const int inArrayLens[],
-		const double is[],
-		double *outArrays[], int outArrayLens[],
-		double outScalars[]);
+//out
+static const QString& X_INTERPOLATED = KGlobal::staticQString("X Interpolated");
+static const QString& Y_INTERPOLATED = KGlobal::staticQString("Y Interpolated");
 
-int linefit(const double *const inArrays[], const int inArrayLens[],
-		const double is[],
-		double *outArrays[], int outArrayLens[],
-		double outScalars[])
-{
-	int i = 0;
-	double a = 0.0, b = 0.0, sx = 0.0, sy = 0.0, sxoss = 0.0, st2 = 0.0, chi2 = 0.0;
-	double xScale;
+static const QString& A = KGlobal::staticQString("a");
+static const QString& B = KGlobal::staticQString("b");
+static const QString& CHI2 = KGlobal::staticQString("chi^2");
 
-	if (is) {} /* don't warn */
+KST_KEY_DATAOBJECT_PLUGIN( linefit )
 
-	if (inArrayLens[Y] < 1 || inArrayLens[X] < 1) {
-		return -1;
-	}
+K_EXPORT_COMPONENT_FACTORY( kstobject_linefit,
+    KGenericFactory<LineFit>( "kstobject_linefit" ) )
 
-	for (i = 0; i < 2; i++) {
-		if (outArrayLens[i] != 2) {
-			outArrays[i] = (double*)realloc(outArrays[i],
-						2*sizeof(double));
-			outArrayLens[i] = 2;
-		}
-	}
+LineFit::LineFit( QObject */*parent*/, const char */*name*/, const QStringList &/*args*/ )
+    : KstBasicPlugin() {
+}
 
-	if (inArrayLens[Y] == 1) {
-		outArrays[X][0] = inArrays[X][0];
-		outArrays[X][1] = inArrays[X][inArrayLens[X] - 1];
-		outArrays[Y][0] = inArrays[Y][0];
-		outArrays[Y][1] = inArrays[Y][0];
-		outScalars[0] = inArrays[Y][0];
-		outScalars[1] = 0;
-		outScalars[2] = chi2;
-		return 0;
-	}
 
-	xScale = inArrayLens[X]/inArrayLens[Y];
+LineFit::~LineFit() {
+}
 
-	for (i = 0; i < inArrayLens[Y]; i++) {
-		double z = xScale*i;
-		long int idx = long(rint(z));
-		double skew = z - floor(z); /* [0..1] */
-		long int idx2 = idx + 1;
-		sy += inArrays[Y][i];
-		while (idx2 >= inArrayLens[Y]) {
-			idx2--;
-		}
-		sx += inArrays[X][idx] + (inArrays[X][idx2] - inArrays[X][idx])*skew;
-	}
 
-	sxoss = sx / inArrayLens[X];
+bool LineFit::algorithm() {
+  int i = 0;
+  double a = 0.0, b = 0.0, sx = 0.0, sy = 0.0, sxoss = 0.0, st2 = 0.0, chi2 = 0.0;
+  double xScale;
 
-	for (i = 0; i < inArrayLens[X]; i++) {
-		double t = inArrays[X][i] - sxoss;
-		st2 += t * t;
-		b += t * inArrays[Y][i];
-	}
+  KstVectorPtr xIn    = inputVector(X_ARRAY);
+  KstVectorPtr yIn    = inputVector(Y_ARRAY);
 
-	b /= st2;
-	a = (sy - sx*b)/inArrayLens[X];
+  KstVectorPtr xOut   = outputVector(X_INTERPOLATED);
+  KstVectorPtr yOut   = outputVector(Y_INTERPOLATED);
 
-	/* could put goodness of fit, etc, in here */
+  KstScalarPtr _a     = outputScalar(A);
+  KstScalarPtr _b     = outputScalar(B);
+  KstScalarPtr _chi2  = outputScalar(CHI2);
 
-	outArrays[X][0] = inArrays[X][0];
-	outArrays[X][1] = inArrays[X][inArrayLens[X]-1];
-	outArrays[Y][0] = a+b*outArrays[X][0];
-	outArrays[Y][1] = a+b*outArrays[X][1];
+  if (yIn->length() < 1 || xIn->length() < 1) {
+    return false;
+  }
 
-	for (i = 0; i < inArrayLens[X]; i++) {
-		double z = xScale*i;
-		long int idx = long(rint(z));
-		double skew = z - floor(z); /* [0..1] */
-		long int idx2 = idx + 1;
-		double newX;
-		double ci;
-		while (idx2 >= inArrayLens[X]) {
-			idx2--;
-		}
-		newX = inArrays[X][idx] + (inArrays[X][idx2] - inArrays[X][idx])*skew;
- 		ci = inArrays[Y][i] - a - b*newX;
-		chi2 += ci*ci;
-	}
+  if (xOut->length() != 2) {
+    xOut->resize( 2, false );
+    }
+  if (yOut->length() != 2) {
+    yOut->resize( 2, false );
+  }
 
-	outScalars[0] = a;
-	outScalars[1] = b;
-	outScalars[2] = chi2;
+  if (yIn->length() == 1) {
+    xOut->value()[0] = xIn->value()[0];
+    xOut->value()[1] = xIn->value()[xIn->length() - 1];
+    yOut->value()[0] = yIn->value()[0];
+    yOut->value()[1] = yIn->value()[0];
+    _a->setValue( yIn->value()[0] );
+    _b->setValue( 0 );
+    _chi2->setValue( chi2 );
+    return true;
+  }
 
-	return 0;
+  xScale = xIn->length()/yIn->length();
+
+  for (i = 0; i < yIn->length(); i++) {
+    double z = xScale*i;
+    long int idx = long(rint(z));
+    double skew = z - floor(z); /* [0..1] */
+    long int idx2 = idx + 1;
+    sy += yIn->value()[i];
+    while (idx2 >= yIn->length()) {
+      idx2--;
+    }
+    sx += xIn->value()[idx] + (xIn->value()[idx2] - xIn->value()[idx])*skew;
+  }
+
+  sxoss = sx / xIn->length();
+
+  for (i = 0; i < xIn->length(); i++) {
+    double t = xIn->value()[i] - sxoss;
+    st2 += t * t;
+    b += t * yIn->value()[i];
+  }
+
+  b /= st2;
+  a = (sy - sx*b)/xIn->length();
+
+  /* could put goodness of fit, etc, in here */
+
+  xOut->value()[0] = xIn->value()[0];
+  xOut->value()[1] = xIn->value()[xIn->length()-1];
+  yOut->value()[0] = a+b*xOut->value()[0];
+  yOut->value()[1] = a+b*xOut->value()[1];
+
+  for (i = 0; i < xIn->length(); i++) {
+    double z = xScale*i;
+    long int idx = long(rint(z));
+    double skew = z - floor(z); /* [0..1] */
+    long int idx2 = idx + 1;
+    double newX;
+    double ci;
+    while (idx2 >= xIn->length()) {
+      idx2--;
+    }
+    newX = xIn->value()[idx] + (xIn->value()[idx2] - xIn->value()[idx])*skew;
+    ci = yIn->value()[i] - a - b*newX;
+    chi2 += ci*ci;
+  }
+
+  _a->setValue( a );
+  _b->setValue( b );
+  _chi2->setValue( chi2 );
+  return true;
 }
 
+
+QStringList LineFit::inputVectorList() const {
+  return QStringList( X_ARRAY ) << Y_ARRAY;
+}
+
+
+QStringList LineFit::inputScalarList() const {
+  return QStringList();
+}
+
+
+QStringList LineFit::inputStringList() const {
+  return QStringList();
+}
+
+
+QStringList LineFit::outputVectorList() const {
+  return QStringList( X_INTERPOLATED ) << Y_INTERPOLATED;
+}
+
+
+QStringList LineFit::outputScalarList() const {
+  return QStringList( A ) << B << CHI2;
+}
+
+
+QStringList LineFit::outputStringList() const {
+  return QStringList();
+}
+
+#include "linefit.moc"


More information about the Kst mailing list