10int p_merror(
char *message);
12long lsfp(
double *xd,
double *yd,
double *sy,
21 long i, j, unweighted;
23 static MATRIX *X, *Y, *Yp, *C, *C_1, *Xt, *A, *Ca, *XtC, *XtCX, *T, *Tt, *TC;
25 if (n_pts < n_terms) {
26 printf(
"error: insufficient data for requested order of fit\n");
27 printf(
"(%ld data points, %ld terms in fit)\n", n_pts, n_terms);
33 for (i = 1; i < n_pts; i++)
40 m_alloc(&X, n_pts, n_terms);
41 m_alloc(&Y, n_pts, 1);
42 m_alloc(&Yp, n_pts, 1);
43 m_alloc(&Xt, n_terms, n_pts);
45 m_alloc(&C, n_pts, n_pts);
46 m_alloc(&C_1, n_pts, n_pts);
50 m_alloc(&A, n_terms, 1);
51 m_alloc(&Ca, n_terms, n_terms);
52 m_alloc(&XtC, n_terms, n_pts);
53 m_alloc(&XtCX, n_terms, n_terms);
54 m_alloc(&T, n_terms, n_pts);
55 m_alloc(&Tt, n_pts, n_terms);
56 m_alloc(&TC, n_terms, n_pts);
62 for (i = 0; i < n_pts; i++) {
66 C->a[i][i] = sqr(sy[i]);
67 C_1->a[i][i] = 1 / C->a[i][i];
70 for (j = 0; j < n_terms; j++)
71 x_i[j] =
ipow(x0, power[j]);
83 return (p_merror(
"transposing X"));
84 if (!m_mult(XtCX, Xt, X))
85 return (p_merror(
"multiplying Xt.X"));
86 if (!m_invert(XtCX, XtCX))
87 return (p_merror(
"inverting XtCX"));
88 if (!m_mult(T, XtCX, Xt))
89 return (p_merror(
"multiplying XtX.Xt"));
91 return (p_merror(
"multiplying T.Y"));
95 return (p_merror(
"computing transpose of T"));
96 if (!m_mult(Ca, T, Tt))
97 return (p_merror(
"multiplying T.Tt"));
98 if (!m_scmul(Ca, Ca, sy ? sqr(sy[0]) : 1))
99 return (p_merror(
"multiplying T.Tt by scalar"));
102 return (p_merror(
"transposing X"));
103 if (!m_mult(XtC, Xt, C_1))
104 return (p_merror(
"multiplying Xt.C_1"));
105 if (!m_mult(XtCX, XtC, X))
106 return (p_merror(
"multiplying XtC.X"));
107 if (!m_invert(XtCX, XtCX))
108 return (p_merror(
"inverting XtCX"));
109 if (!m_mult(T, XtCX, XtC))
110 return (p_merror(
"multiplying XtCX.XtC"));
111 if (!m_mult(A, T, Y))
112 return (p_merror(
"multiplying T.Y"));
115 if (!m_mult(TC, T, C))
116 return (p_merror(
"multiplying T.C"));
118 return (p_merror(
"computing transpose of T"));
119 if (!m_mult(Ca, TC, Tt))
120 return (p_merror(
"multiplying TC.Tt"));
123 for (i = 0; i < n_terms; i++) {
124 coef[i] = A->a[i][0];
125 s_coef[i] = sqrt(Ca->a[i][i]);
129 if (!m_mult(Yp, X, A))
130 return (p_merror(
"multiplying X.A"));
132 for (i = 0; i < n_pts; i++) {
133 xp = (Yp->a[i][0] - yd[i]);
136 xp /= sy ? sy[i] : 1;
139 if (n_terms != n_pts)
140 *chi /= (n_pts - n_terms);
double ipow(const double x, const int64_t p)
Compute x raised to the power p (x^p).