SDDSlib
Loading...
Searching...
No Matches
lsfBasisFns.c
Go to the documentation of this file.
1/**
2 * @file lsfBasisFns.c
3 * @brief Basis functions for Least Squares Fits (LSFs) using ordinary and Chebyshev polynomials.
4 *
5 * This file provides a set of functions for evaluating basis functions and their derivatives
6 * for least squares fitting. It includes functions to set and retrieve scaling offsets,
7 * as well as polynomial and Chebyshev polynomial basis functions and their derivatives.
8 *
9 * @copyright
10 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
11 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
12 *
13 * @license
14 * This file is distributed under the terms of the Software License Agreement
15 * found in the file LICENSE included with this distribution.
16 *
17 * @author M. Borland, C. Saunders, R. Soliday
18 */
19
20#include "mdb.h"
21
22static double x_offset = 0;
23static double x_scale = 1;
24
25/**
26 * @brief Set the offset applied to the input argument of basis functions.
27 *
28 * This function updates the global variable used to shift the input argument before
29 * evaluating polynomial or Chebyshev functions.
30 *
31 * @param offset The new offset to apply to the argument.
32 */
33void set_argument_offset(double offset) { x_offset = offset; }
34
35/**
36 * @brief Set the scale factor applied to the input argument of basis functions.
37 *
38 * This function updates the global variable used to scale the input argument before
39 * evaluating polynomial or Chebyshev functions. It ensures the scale factor is not zero.
40 *
41 * @param scale The new scale factor for the argument.
42 */
43void set_argument_scale(double scale) {
44 if (!(x_scale = scale))
45 bomb("argument scale factor is zero", NULL);
46}
47
48/**
49 * @brief Get the current argument offset applied before function evaluations.
50 *
51 * @return The current argument offset.
52 */
53double get_argument_offset() { return x_offset; }
54
55/**
56 * @brief Get the current argument scale factor used before function evaluations.
57 *
58 * @return The current argument scale factor.
59 */
60double get_argument_scale() { return x_scale; }
61
62/**
63 * @brief Evaluate the Chebyshev polynomial of the first kind T_n(x).
64 *
65 * Given x and an order n, this function returns T_n((x - x_offset) / x_scale).
66 * If x is out of the domain [-1,1], it is clipped to ±1 before evaluation.
67 *
68 * @param x The point at which to evaluate the Chebyshev polynomial.
69 * @param n The order of the Chebyshev polynomial.
70 * @return The value of T_n(x).
71 */
72double tcheby(double x, long n) {
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}
81
82/**
83 * @brief Evaluate the derivative of the Chebyshev polynomial T_n(x).
84 *
85 * This function returns d/dx [T_n((x - x_offset)/x_scale)].
86 * If x is out of the domain [-1,1], it is clipped to ±1 before evaluation.
87 *
88 * @param x The point at which to evaluate the derivative of T_n.
89 * @param n The order of the Chebyshev polynomial.
90 * @return The derivative dT_n/dx at the given x.
91 */
92double dtcheby(double x, long n) {
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}
103
104/**
105 * @brief Evaluate a power function x^n.
106 *
107 * This function returns ( (x - x_offset)/x_scale )^n.
108 *
109 * @param x The point at which to evaluate the power.
110 * @param n The exponent.
111 * @return The value of ((x - x_offset)/x_scale)^n.
112 */
113double ipower(double x, long n) {
114 x = (x - x_offset) / x_scale;
115 return (ipow(x, n));
116}
117
118/**
119 * @brief Evaluate the derivative of x^n.
120 *
121 * This function returns d/dx [ (x - x_offset)/x_scale ]^n = n * ((x - x_offset)/x_scale)^(n-1) / x_scale.
122 *
123 * @param x The point at which to evaluate the derivative.
124 * @param n The exponent.
125 * @return The derivative of the power function at the given x.
126 */
127double dipower(double x, long n) {
128 x = (x - x_offset) / x_scale;
129 return (n * ipow(x, n - 1));
130}
131
132/**
133 * @brief Evaluate a sum of basis functions.
134 *
135 * Given a pointer to a function that evaluates a basis function fn(x,order),
136 * this function computes the weighted sum of these functions at x0 using the provided coefficients.
137 *
138 * @param fn A pointer to the basis function to evaluate (fn(x, order)).
139 * @param coef An array of coefficients for each basis function.
140 * @param order An array of orders corresponding to each coefficient.
141 * @param n_coefs The number of coefficients (and basis functions).
142 * @param x0 The point at which to evaluate the sum.
143 * @return The computed sum of the basis functions.
144 */
145double eval_sum(double (*fn)(double x, long ord), double *coef, int32_t *order,
146 long n_coefs, double x0) {
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}
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
double ipow(const double x, const int64_t p)
Compute x raised to the power p (x^p).
Definition ipow.c:33
void set_argument_scale(double scale)
Set the scale factor applied to the input argument of basis functions.
Definition lsfBasisFns.c:43
double dtcheby(double x, long n)
Evaluate the derivative of the Chebyshev polynomial T_n(x).
Definition lsfBasisFns.c:92
void set_argument_offset(double offset)
Set the offset applied to the input argument of basis functions.
Definition lsfBasisFns.c:33
double ipower(double x, long n)
Evaluate a power function x^n.
double get_argument_offset()
Get the current argument offset applied before function evaluations.
Definition lsfBasisFns.c:53
double dipower(double x, long n)
Evaluate the derivative of x^n.
double tcheby(double x, long n)
Evaluate the Chebyshev polynomial of the first kind T_n(x).
Definition lsfBasisFns.c:72
double get_argument_scale()
Get the current argument scale factor used before function evaluations.
Definition lsfBasisFns.c:60
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.