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

Functions for evaluating polynomials and their derivatives, as well as solving quadratic equations. More...

#include "mdb.h"

Go to the source code of this file.

Functions

double poly (double *a, long n, double x)
 Evaluate a polynomial at a given point.
 
double dpoly (double *a, long n, double x)
 Evaluate the derivative of a polynomial at a given point.
 
double polyp (double *a, long *power, long n, double x)
 Evaluate a polynomial with arbitrary powers at a given point.
 
double dpolyp (double *a, long *power, long n, double x)
 Evaluate the derivative of a polynomial with arbitrary powers at a given point.
 
int solveQuadratic (double a, double b, double c, double *solution)
 Solve a quadratic equation for real solutions.
 

Detailed Description

Functions for evaluating polynomials and their derivatives, as well as solving quadratic equations.

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 poly.c.

Function Documentation

◆ dpoly()

double dpoly ( double * a,
long n,
double x )

Evaluate the derivative of a polynomial at a given point.

Given an array of coefficients a and the number of terms n, this function returns the value of the derivative of the polynomial at x. The polynomial is assumed to be: a[0] + a[1]*x + a[2]*x^2 + ... Its derivative is: a[1] + 2*a[2]*x + 3*a[3]*x^2 + ...

Parameters
[in]aPointer to the array of polynomial coefficients.
[in]nNumber of coefficients (degree+1 of the polynomial).
[in]xThe point at which to evaluate the derivative.
Returns
The value of the derivative at x.

Definition at line 58 of file poly.c.

58 {
59 register long i;
60 register double sum, xp;
61
62 sum = 0;
63 xp = 1;
64 for (i = 1; i < n; i++) {
65 sum += i * xp * a[i];
66 xp *= x;
67 }
68 return (sum);
69}

◆ dpolyp()

double dpolyp ( double * a,
long * power,
long n,
double x )

Evaluate the derivative of a polynomial with arbitrary powers at a given point.

Given arrays a and power that define a polynomial a[0]*x^(power[0]) + a[1]*x^(power[1]) + ... + a[n-1]*x^(power[n-1]), this function returns the derivative of that polynomial at x: power[0]*a[0]*x^(power[0]-1) + power[1]*a[1]*x^(power[1]-1) + ...

Parameters
[in]aPointer to the array of polynomial coefficients.
[in]powerPointer to the array of powers for each term.
[in]nNumber of terms.
[in]xThe point at which to evaluate the derivative.
Returns
The value of the derivative at x.

Definition at line 111 of file poly.c.

111 {
112 register long i;
113 register double sum, xp;
114
115 xp = ipow(x, power[0] - 1);
116 sum = power[0] * xp * a[0];
117 for (i = 1; i < n; i++) {
118 xp *= ipow(x, power[i] - power[i - 1]);
119 sum += power[i] * xp * a[i];
120 }
121 return (sum);
122}
double ipow(const double x, const int64_t p)
Compute x raised to the power p (x^p).
Definition ipow.c:33

◆ poly()

double poly ( double * a,
long n,
double x )

Evaluate a polynomial at a given point.

Given an array of coefficients a and the number of terms n, this function returns the value of the polynomial at x. The polynomial is assumed to be of the form: a[0] + a[1]*x + a[2]*x^2 + ... + a[n-1]*x^(n-1).

Parameters
[in]aPointer to the array of polynomial coefficients.
[in]nNumber of coefficients (degree+1 of the polynomial).
[in]xThe point at which to evaluate the polynomial.
Returns
The value of the polynomial at x.

Definition at line 30 of file poly.c.

31{
32 register long i;
33 register double sum, xp;
34
35 sum = 0;
36 xp = 1;
37 for (i = 0; i < n; i++) {
38 sum += xp * a[i];
39 xp *= x;
40 }
41 return (sum);
42}

◆ polyp()

double polyp ( double * a,
long * power,
long n,
double x )

Evaluate a polynomial with arbitrary powers at a given point.

This function computes the value of a polynomial at x, where each term may have an arbitrary power. Given arrays a and power, the polynomial is: a[0]*x^(power[0]) + a[1]*x^(power[1]) + ... + a[n-1]*x^(power[n-1]).

Parameters
[in]aPointer to the array of polynomial coefficients.
[in]powerPointer to the array of powers for each term.
[in]nNumber of terms.
[in]xThe point at which to evaluate the polynomial.
Returns
The value of the polynomial at x.

Definition at line 84 of file poly.c.

84 {
85 register long i;
86 register double sum, xp;
87
88 xp = ipow(x, power[0]);
89 sum = xp * a[0];
90 for (i = 1; i < n; i++) {
91 xp *= ipow(x, power[i] - power[i - 1]);
92 sum += xp * a[i];
93 }
94 return (sum);
95}

◆ solveQuadratic()

int solveQuadratic ( double a,
double b,
double c,
double * solution )

Solve a quadratic equation for real solutions.

Given a quadratic equation of the form a*x^2 + b*x + c = 0, this function finds its real solutions, if any. The solutions are returned in the array solution. The function returns the number of real solutions found:

  • 0 if no real solutions,
  • 1 if one real solution (repeated root),
  • 2 if two real solutions.
Parameters
[in]aQuadratic coefficient (for x^2).
[in]bLinear coefficient (for x).
[in]cConstant term.
[out]solutionArray of size 2 for storing the found solutions.
Returns
The number of real solutions (0, 1, or 2).

Definition at line 139 of file poly.c.

139 {
140 double det;
141 if (a == 0) {
142 if (b == 0)
143 return 0;
144 solution[0] = -c / b;
145 return 1;
146 }
147 if ((det = b * b - 4 * a * c) < 0)
148 return 0;
149 if (det == 0) {
150 solution[0] = -b / a;
151 return 1;
152 }
153 solution[0] = (-b - sqrt(det)) / (2 * a);
154 solution[1] = (-b + sqrt(det)) / (2 * a);
155 return 2;
156}