📄 cpolyfit.h
字号:
//---------------------------------------------------------------------------
#ifndef CPolyfitH
#define CPolyfitH
#include <vcl.h>
#include <fstream.h>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
//---------------------------------------------------------------------------
using std::vector;
using std::string;
template <class FloatType>
class CPolyFit
{
typedef vector<vector<FloatType> > Matrix;
public:
static FloatType __fastcall PolyFit(const vector<FloatType> &x, // independent variable
const vector<FloatType> &y, // dependent variable
vector<FloatType> &coef); // coefficients
// returns correlation coef, -1 on error
static FloatType __fastcall PolyFit2 (const vector<FloatType> &x, // does the work
const vector<FloatType> &y,
vector<FloatType> &coef);
// Least-squares fit to nrow sets of x and y pairs of points
static void __fastcall LinFit(const vector<FloatType> &x, // independent variable
vector<FloatType> &y, // dependent variable
vector<FloatType> &y_calc, // calculated dep. variable
vector<FloatType> &resid, // array of residuals
vector<FloatType> &coef, // coefficients
vector<FloatType> &sig, // error on coefficients
const size_t nrow, // length of variable array
size_t ncol); // number of terms
private:
CPolyFit &operator = (const CPolyFit &); // disable assignment
static void __fastcall Normalise(vector<FloatType> &s, // normalised x y values
vector<FloatType> &t,
FloatType &x0, // min x
FloatType &xm, // max x
FloatType &y0,
FloatType &ym);
static void __fastcall RestoreCoeffs(vector<FloatType> &coef,
FloatType x0,
FloatType xm,
FloatType y0,
FloatType ym);
static void __fastcall GeneratePascalsTriangle(vector<vector<int> > &Pascal, size_t n);
static void __fastcall Square (const Matrix &x, // Matrix multiplication routine
const vector<FloatType> &y,
Matrix &a, // A = transpose X times X
vector<FloatType> &g, // G = Y times X
const size_t nrow, const size_t ncol);
// Forms square coefficient matrix
static bool __fastcall GaussJordan (Matrix &b, // square matrix of coefficients
const vector<FloatType> &y, // constant vector
vector<FloatType> &coef); // solution vector
// returns false if matrix singular
static bool __fastcall GaussJordan2(Matrix &b,
const vector<FloatType> &y,
Matrix &w,
vector<vector<int> > &index);
};
// some utility functions
namespace NSUtility
{
template <typename X> inline void Swap(X &a, X &b) {X t = a; a = b; b = t;}
template <typename X> void Zeroise(vector<X> &array, size_t n);
template <typename X> void Zeroise(vector<vector<X> > &matrix, size_t m, size_t n);
template <typename X> inline X Sqr(const X &x) {return x * x;}
template <typename X> X Pow(const X &x, int i);
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -