1 文本格式
using System;
namespace Legalsoft.Truffer
{
/// <summary>
/// Fitting Data to a Straight Line
/// </summary>
public class Fitab
{
private int ndata { get; set; }
private double a { get; set; }
public double b { get; set; }
private double siga { get; set; }
private double sigb { get; set; }
private double chi2 { get; set; }
private double q { get; set; }
private double sigdat { get; set; }
private double[] x { get; set; }
private double[] y { get; set; }
private double[] sig { get; set; }
public Fitab(double[] xx, double[] yy, double[] ssig)
{
this.ndata = xx.Length;
this.x = xx;
this.y = yy;
this.sig = ssig;
this.chi2 = 0.0;
this.q = 1.0;
this.sigdat = 0.0;
Gamma gam = new Gamma();
double ss = 0.0;
double sx = 0.0;
double sy = 0.0;
double st2 = 0.0;
b = 0.0;
for (int i = 0; i < ndata; i++)
{
double wt = 1.0 / Globals.SQR(sig[i]);
ss += wt;
sx += x[i] * wt;
sy += y[i] * wt;
}
double sxoss = sx / ss;
for (int i = 0; i < ndata; i++)
{
double t = (x[i] - sxoss) / sig[i];
st2 += t * t;
b += t * y[i] / sig[i];
}
b /= st2;
a = (sy - sx * b) / ss;
siga = Math.Sqrt((1.0 + sx * sx / (ss * st2)) / ss);
sigb = Math.Sqrt(1.0 / st2);
for (int i = 0; i < ndata; i++)
{
chi2 += Globals.SQR((y[i] - a - b * x[i]) / sig[i]);
}
if (ndata > 2)
{
q = gam.gammq(0.5 * (ndata - 2), 0.5 * chi2);
}
}
public Fitab(double[] xx, double[] yy)
{
this.ndata = xx.Length;
this.x = xx;
this.y = yy;
this.sig = xx;
this.chi2 = 0.0;
this.q = 1.0;
this.sigdat = 0.0;
double sx = 0.0;
double sy = 0.0;
double st2 = 0.0;
b = 0.0;
for (int i = 0; i < ndata; i++)
{
sx += x[i];
sy += y[i];
}
double ss = ndata;
double sxoss = sx / ss;
for (int i = 0; i < ndata; i++)
{
double t = x[i] - sxoss;
st2 += t * t;
b += t * y[i];
}
b /= st2;
a = (sy - sx * b) / ss;
siga = Math.Sqrt((1.0 + sx * sx / (ss * st2)) / ss);
sigb = Math.Sqrt(1.0 / st2);
for (int i = 0; i < ndata; i++)
{
chi2 += Globals.SQR(y[i] - a - b * x[i]);
}
if (ndata > 2)
{
sigdat = Math.Sqrt(chi2 / (ndata - 2));
}
siga *= sigdat;
sigb *= sigdat;
}
}
}
2 代码格式
using System;namespace Legalsoft.Truffer
{/// <summary>/// Fitting Data to a Straight Line/// </summary>public class Fitab{private int ndata { get; set; }private double a { get; set; }public double b { get; set; }private double siga { get; set; }private double sigb { get; set; }private double chi2 { get; set; }private double q { get; set; }private double sigdat { get; set; }private double[] x { get; set; }private double[] y { get; set; }private double[] sig { get; set; }public Fitab(double[] xx, double[] yy, double[] ssig){this.ndata = xx.Length;this.x = xx;this.y = yy;this.sig = ssig;this.chi2 = 0.0;this.q = 1.0;this.sigdat = 0.0;Gamma gam = new Gamma();double ss = 0.0;double sx = 0.0;double sy = 0.0;double st2 = 0.0;b = 0.0;for (int i = 0; i < ndata; i++){double wt = 1.0 / Globals.SQR(sig[i]);ss += wt;sx += x[i] * wt;sy += y[i] * wt;}double sxoss = sx / ss;for (int i = 0; i < ndata; i++){double t = (x[i] - sxoss) / sig[i];st2 += t * t;b += t * y[i] / sig[i];}b /= st2;a = (sy - sx * b) / ss;siga = Math.Sqrt((1.0 + sx * sx / (ss * st2)) / ss);sigb = Math.Sqrt(1.0 / st2);for (int i = 0; i < ndata; i++){chi2 += Globals.SQR((y[i] - a - b * x[i]) / sig[i]);}if (ndata > 2){q = gam.gammq(0.5 * (ndata - 2), 0.5 * chi2);}}public Fitab(double[] xx, double[] yy){this.ndata = xx.Length;this.x = xx;this.y = yy;this.sig = xx;this.chi2 = 0.0;this.q = 1.0;this.sigdat = 0.0;double sx = 0.0;double sy = 0.0;double st2 = 0.0;b = 0.0;for (int i = 0; i < ndata; i++){sx += x[i];sy += y[i];}double ss = ndata;double sxoss = sx / ss;for (int i = 0; i < ndata; i++){double t = x[i] - sxoss;st2 += t * t;b += t * y[i];}b /= st2;a = (sy - sx * b) / ss;siga = Math.Sqrt((1.0 + sx * sx / (ss * st2)) / ss);sigb = Math.Sqrt(1.0 / st2);for (int i = 0; i < ndata; i++){chi2 += Globals.SQR(y[i] - a - b * x[i]);}if (ndata > 2){sigdat = Math.Sqrt(chi2 / (ndata - 2));}siga *= sigdat;sigb *= sigdat;}}
}