SDDSlib
Loading...
Searching...
No Matches
lsfBasisFns.c File Reference

Basis functions for Least Squares Fits (LSFs) using ordinary and Chebyshev polynomials. More...

#include "mdb.h"

Go to the source code of this file.

Functions

void set_argument_offset (double offset)
 Set the offset applied to the input argument of basis functions.
 
void set_argument_scale (double scale)
 Set the scale factor applied to the input argument of basis functions.
 
double get_argument_offset ()
 Get the current argument offset applied before function evaluations.
 
double get_argument_scale ()
 Get the current argument scale factor used before function evaluations.
 
double tcheby (double x, long n)
 Evaluate the Chebyshev polynomial of the first kind T_n(x).
 
double dtcheby (double x, long n)
 Evaluate the derivative of the Chebyshev polynomial T_n(x).
 
double ipower (double x, long n)
 Evaluate a power function x^n.
 
double dipower (double x, long n)
 Evaluate the derivative of x^n.
 
double eval_sum (double(*fn)(double x, long ord), double *coef, int32_t *order, long n_coefs, double x0)
 Evaluate a sum of basis functions.
 

Variables

static double x_offset = 0
 
static double x_scale = 1
 

Detailed Description

Basis functions for Least Squares Fits (LSFs) using ordinary and Chebyshev polynomials.

This file provides a set of functions for evaluating basis functions and their derivatives for least squares fitting. It includes functions to set and retrieve scaling offsets, as well as polynomial and Chebyshev polynomial basis functions and their derivatives.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file lsfBasisFns.c.

Function Documentation

◆ dipower()

double dipower ( double x,
long n )

Evaluate the derivative of x^n.

This function returns d/dx [ (x - x_offset)/x_scale ]^n = n * ((x - x_offset)/x_scale)^(n-1) / x_scale.

Parameters
xThe point at which to evaluate the derivative.
nThe exponent.
Returns
The derivative of the power function at the given x.

Definition at line 127 of file lsfBasisFns.c.

127 {
128 x = (x - x_offset) / x_scale;
129 return (n * ipow(x, n - 1));
130}
double ipow(const double x, const int64_t p)
Compute x raised to the power p (x^p).
Definition ipow.c:33

◆ dtcheby()

double dtcheby ( double x,
long n )

Evaluate the derivative of the Chebyshev polynomial T_n(x).

This function returns d/dx [T_n((x - x_offset)/x_scale)]. If x is out of the domain [-1,1], it is clipped to ±1 before evaluation.

Parameters
xThe point at which to evaluate the derivative of T_n.
nThe order of the Chebyshev polynomial.
Returns
The derivative dT_n/dx at the given x.

Definition at line 92 of file lsfBasisFns.c.

92 {
93 x = (x - x_offset) / x_scale;
94 if (x > 1 || x < -1) {
95 /* fprintf(stderr, "warning: argument %e is out of range for tcheby()\n",
96 * x); */
97 x = SIGN(x);
98 }
99 if (x != 1 && x != -1)
100 return (n * sin(n * acos(x)) / sqrt(1 - sqr(x)));
101 return (1.0 * n * n);
102}

◆ eval_sum()

double eval_sum ( double(* fn )(double x, long ord),
double * coef,
int32_t * order,
long n_coefs,
double x0 )

Evaluate a sum of basis functions.

Given a pointer to a function that evaluates a basis function fn(x,order), this function computes the weighted sum of these functions at x0 using the provided coefficients.

Parameters
fnA pointer to the basis function to evaluate (fn(x, order)).
coefAn array of coefficients for each basis function.
orderAn array of orders corresponding to each coefficient.
n_coefsThe number of coefficients (and basis functions).
x0The point at which to evaluate the sum.
Returns
The computed sum of the basis functions.

Definition at line 145 of file lsfBasisFns.c.

146 {
147 double sum = 0;
148 long i;
149
150 for (i = sum = 0; i < n_coefs; i++)
151 sum += (fn)(x0, order[i]) * coef[i];
152 return (sum);
153}

◆ get_argument_offset()

double get_argument_offset ( )

Get the current argument offset applied before function evaluations.

Returns
The current argument offset.

Definition at line 53 of file lsfBasisFns.c.

53{ return x_offset; }

◆ get_argument_scale()

double get_argument_scale ( )

Get the current argument scale factor used before function evaluations.

Returns
The current argument scale factor.

Definition at line 60 of file lsfBasisFns.c.

60{ return x_scale; }

◆ ipower()

double ipower ( double x,
long n )

Evaluate a power function x^n.

This function returns ( (x - x_offset)/x_scale )^n.

Parameters
xThe point at which to evaluate the power.
nThe exponent.
Returns
The value of ((x - x_offset)/x_scale)^n.

Definition at line 113 of file lsfBasisFns.c.

113 {
114 x = (x - x_offset) / x_scale;
115 return (ipow(x, n));
116}

◆ set_argument_offset()

void set_argument_offset ( double offset)

Set the offset applied to the input argument of basis functions.

This function updates the global variable used to shift the input argument before evaluating polynomial or Chebyshev functions.

Parameters
offsetThe new offset to apply to the argument.

Definition at line 33 of file lsfBasisFns.c.

33{ x_offset = offset; }

◆ set_argument_scale()

void set_argument_scale ( double scale)

Set the scale factor applied to the input argument of basis functions.

This function updates the global variable used to scale the input argument before evaluating polynomial or Chebyshev functions. It ensures the scale factor is not zero.

Parameters
scaleThe new scale factor for the argument.

Definition at line 43 of file lsfBasisFns.c.

43 {
44 if (!(x_scale = scale))
45 bomb("argument scale factor is zero", NULL);
46}
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26

◆ tcheby()

double tcheby ( double x,
long n )

Evaluate the Chebyshev polynomial of the first kind T_n(x).

Given x and an order n, this function returns T_n((x - x_offset) / x_scale). If x is out of the domain [-1,1], it is clipped to ±1 before evaluation.

Parameters
xThe point at which to evaluate the Chebyshev polynomial.
nThe order of the Chebyshev polynomial.
Returns
The value of T_n(x).

Definition at line 72 of file lsfBasisFns.c.

72 {
73 x = (x - x_offset) / x_scale;
74 if (x > 1 || x < -1) {
75 /* fprintf(stderr, "warning: argument %e is out of range for tcheby()\n",
76 * x); */
77 x = SIGN(x);
78 }
79 return (cos(n * acos(x)));
80}

Variable Documentation

◆ x_offset

double x_offset = 0
static

Definition at line 22 of file lsfBasisFns.c.

◆ x_scale

double x_scale = 1
static

Definition at line 23 of file lsfBasisFns.c.