Skip to content

Commit

Permalink
math/poly: using error-handling interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaeddert committed Aug 9, 2020
1 parent 2553798 commit 78b9a6f
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 145 deletions.
88 changes: 44 additions & 44 deletions include/liquid.h
Original file line number Diff line number Diff line change
Expand Up @@ -6123,21 +6123,21 @@ T POLY(_val)(T * _p, \
/* _n : number of samples in _x and _y */ \
/* _p : polynomial coefficients output [size _k x 1] */ \
/* _k : polynomial coefficients length, order is _k - 1 */ \
void POLY(_fit)(T * _x, \
T * _y, \
unsigned int _n, \
T * _p, \
unsigned int _k); \
int POLY(_fit)(T * _x, \
T * _y, \
unsigned int _n, \
T * _p, \
unsigned int _k); \
\
/* Perform Lagrange polynomial exact fit on data set */ \
/* _x : x-value sample set, size [_n x 1] */ \
/* _y : y-value sample set, size [_n x 1] */ \
/* _n : number of samples in _x and _y */ \
/* _p : polynomial coefficients output [size _n x 1] */ \
void POLY(_fit_lagrange)(T * _x, \
T * _y, \
unsigned int _n, \
T * _p); \
int POLY(_fit_lagrange)(T * _x, \
T * _y, \
unsigned int _n, \
T * _p); \
\
/* Perform Lagrange polynomial interpolation on data set without */ \
/* computing coefficients as an intermediate step. */ \
Expand All @@ -6154,9 +6154,9 @@ T POLY(_interp_lagrange)(T * _x, \
/* _x : x-value sample set, size [_n x 1] */ \
/* _n : number of samples in _x */ \
/* _w : barycentric weights normalized so _w[0]=1, size [_n x 1] */ \
void POLY(_fit_lagrange_barycentric)(T * _x, \
unsigned int _n, \
T * _w); \
int POLY(_fit_lagrange_barycentric)(T * _x, \
unsigned int _n, \
T * _w); \
\
/* Perform Lagrange polynomial interpolation using the barycentric form */ \
/* of the weights. */ \
Expand All @@ -6178,8 +6178,8 @@ T POLY(_val_lagrange_barycentric)(T * _x, \
/* NOTE: _p has order n (coefficients has length n+1) */ \
/* _n : polynomial order */ \
/* _p : polynomial coefficients [size: _n+1 x 1] */ \
void POLY(_expandbinomial)(unsigned int _n, \
T * _p); \
int POLY(_expandbinomial)(unsigned int _n, \
T * _p); \
\
/* Perform positive/negative binomial expansion on the polynomial */ \
/* \( P_n(x) = (1+x)^m (1-x)^k \) */ \
Expand All @@ -6189,9 +6189,9 @@ void POLY(_expandbinomial)(unsigned int _n, \
/* _m : number of '1+x' terms */ \
/* _k : number of '1-x' terms */ \
/* _p : polynomial coefficients [size: _m+_k+1 x 1] */ \
void POLY(_expandbinomial_pm)(unsigned int _m, \
unsigned int _k, \
T * _p); \
int POLY(_expandbinomial_pm)(unsigned int _m, \
unsigned int _k, \
T * _p); \
\
/* Perform root expansion on the polynomial */ \
/* \( P_n(x) = (x-r[0]) (x-r[1]) ... (x-r[n-1]) \) */ \
Expand All @@ -6202,9 +6202,9 @@ void POLY(_expandbinomial_pm)(unsigned int _m, \
/* _r : roots of polynomial [size: _n x 1] */ \
/* _n : number of roots in polynomial */ \
/* _p : polynomial coefficients [size: _n+1 x 1] */ \
void POLY(_expandroots)(T * _r, \
unsigned int _n, \
T * _p); \
int POLY(_expandroots)(T * _r, \
unsigned int _n, \
T * _p); \
\
/* Perform root expansion on the polynomial */ \
/* \( P_n(x) = (xb[0]-a[0]) (xb[1]-a[1])...(xb[n-1]-a[n-1]) \) */ \
Expand All @@ -6215,35 +6215,35 @@ void POLY(_expandroots)(T * _r, \
/* _b : multiplicant of polynomial roots [size: _n x 1] */ \
/* _n : number of roots in polynomial */ \
/* _p : polynomial coefficients [size: _n+1 x 1] */ \
void POLY(_expandroots2)(T * _a, \
T * _b, \
unsigned int _n, \
T * _p); \
int POLY(_expandroots2)(T * _a, \
T * _b, \
unsigned int _n, \
T * _p); \
\
/* Find the complex roots of a polynomial. */ \
/* _p : polynomial coefficients [size: _n x 1] */ \
/* _k : polynomial length */ \
/* _roots : resulting complex roots [size: _k-1 x 1] */ \
void POLY(_findroots)(T * _poly, \
unsigned int _n, \
TC * _roots); \
int POLY(_findroots)(T * _poly, \
unsigned int _n, \
TC * _roots); \
\
/* Find the complex roots of the polynomial using the Durand-Kerner */ \
/* method */ \
/* _p : polynomial coefficients [size: _n x 1] */ \
/* _k : polynomial length */ \
/* _roots : resulting complex roots [size: _k-1 x 1] */ \
void POLY(_findroots_durandkerner)(T * _p, \
unsigned int _k, \
TC * _roots); \
int POLY(_findroots_durandkerner)(T * _p, \
unsigned int _k, \
TC * _roots); \
\
/* Find the complex roots of the polynomial using Bairstow's method. */ \
/* _p : polynomial coefficients [size: _n x 1] */ \
/* _k : polynomial length */ \
/* _roots : resulting complex roots [size: _k-1 x 1] */ \
void POLY(_findroots_bairstow)(T * _p, \
unsigned int _k, \
TC * _roots); \
int POLY(_findroots_bairstow)(T * _p, \
unsigned int _k, \
TC * _roots); \
\
/* Expand the multiplication of two polynomials */ \
/* \( ( a[0] + a[1]x + a[2]x^2 + ...) (b[0] + b[1]x + b[]x^2 + ...) \) */ \
Expand All @@ -6256,11 +6256,11 @@ void POLY(_findroots_bairstow)(T * _p, \
/* _b : 2nd polynomial coefficients (length is _order_b+1) */ \
/* _order_b : 2nd polynomial order */ \
/* _c : output polynomial [size: _order_a+_order_b+1 x 1] */ \
void POLY(_mul)(T * _a, \
unsigned int _order_a, \
T * _b, \
unsigned int _order_b, \
T * _c); \
int POLY(_mul)(T * _a, \
unsigned int _order_a, \
T * _b, \
unsigned int _order_b, \
T * _c); \

LIQUID_POLY_DEFINE_API(LIQUID_POLY_MANGLE_DOUBLE,
double,
Expand Down Expand Up @@ -6302,17 +6302,17 @@ int liquid_is_prime(unsigned int _n);
// _n : number to factor
// _factors : pre-allocated array of factors [size: LIQUID_MAX_FACTORS x 1]
// _num_factors: number of factors found, sorted ascending
void liquid_factor(unsigned int _n,
unsigned int * _factors,
unsigned int * _num_factors);
int liquid_factor(unsigned int _n,
unsigned int * _factors,
unsigned int * _num_factors);

// compute number's unique prime factors
// _n : number to factor
// _factors : pre-allocated array of factors [size: LIQUID_MAX_FACTORS x 1]
// _num_factors: number of unique factors found, sorted ascending
void liquid_unique_factor(unsigned int _n,
unsigned int * _factors,
unsigned int * _num_factors);
int liquid_unique_factor(unsigned int _n,
unsigned int * _factors,
unsigned int * _num_factors);

// compute greatest common divisor between to numbers P and Q
unsigned int liquid_gcd(unsigned int _P,
Expand Down
12 changes: 6 additions & 6 deletions src/math/src/math.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007 - 2015 Joseph Gaeddert
* Copyright (c) 2007 - 2020 Joseph Gaeddert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -135,8 +135,8 @@ float sincf(float _x) {
unsigned int liquid_nextpow2(unsigned int _x)
{
if (_x == 0) {
fprintf(stderr,"error: liquid_nextpow2(), input must be greater than zero\n");
exit(1);
liquid_error(LIQUID_EICONFIG,"liquid_nextpow2(), input must be greater than zero");
return 0;
}

_x--;
Expand All @@ -153,10 +153,10 @@ float liquid_nchoosek(unsigned int _n, unsigned int _k)
{
//
if (_k > _n) {
fprintf(stderr,"error: liquid_nchoosek(), _k cannot exceed _n\n");
exit(1);
liquid_error(LIQUID_EICONFIG,"liquid_nchoosek(), _k cannot exceed _n");
return 0.0f;
} else if (_k == 0 || _k == _n) {
return 1;
return 1.0f;
}

// take advantage of symmetry and take larger value
Expand Down
8 changes: 4 additions & 4 deletions src/math/src/math.gamma.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007 - 2015 Joseph Gaeddert
* Copyright (c) 2007 - 2020 Joseph Gaeddert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -44,8 +44,8 @@ float liquid_lngammaf(float _z)
{
float g;
if (_z < 0) {
fprintf(stderr,"error: liquid_lngammaf(), undefined for z <= 0\n");
exit(1);
liquid_error(LIQUID_EICONFIG,"liquid_lngammaf(), undefined for z <= 0");
return 0.0f;
} else if (_z < 10.0f) {
#if 0
g = -EULER_GAMMA*_z - logf(_z);
Expand Down Expand Up @@ -116,7 +116,7 @@ float liquid_lnlowergammaf(float _z, float _alpha)
// accumulate e^t
t3 += expf(t);

// check premature exit criteria
// check premature stopping criteria
if (k==0 || t > tmax)
tmax = t;

Expand Down
35 changes: 16 additions & 19 deletions src/math/src/modular_arithmetic.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007 - 2018 Joseph Gaeddert
* Copyright (c) 2007 - 2020 Joseph Gaeddert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,8 +26,7 @@

#include <stdio.h>
#include <stdlib.h>

#include "liquid.h"
#include "liquid.internal.h"

// determine if number is prime (slow, simple method)
// https://en.ikipedia.org/wiki/Primality_test#Pseudocode
Expand All @@ -53,9 +52,9 @@ int liquid_is_prime(unsigned int _n)
// _n : number to factor
// _factors : pre-allocated array of factors [size: LIQUID_MAX_FACTORS x 1]
// _num_factors: number of factors found, sorted ascending
void liquid_factor(unsigned int _n,
unsigned int * _factors,
unsigned int * _num_factors)
int liquid_factor(unsigned int _n,
unsigned int * _factors,
unsigned int * _num_factors)
{
unsigned int k;
unsigned int n = _n;
Expand All @@ -71,21 +70,20 @@ void liquid_factor(unsigned int _n,
}
} while (n > 1 && num_factors < LIQUID_MAX_FACTORS);

if (n > 1 && num_factors == LIQUID_MAX_FACTORS) {
fprintf(stderr,"error, liquid_factor(), could not factor %u in %u numbers\n", _n, LIQUID_MAX_FACTORS);
exit(1);
}
if (n > 1 && num_factors == LIQUID_MAX_FACTORS)
return liquid_error(LIQUID_EICONFIG,"liquid_factor(), could not factor %u in %u numbers", _n, LIQUID_MAX_FACTORS);

*_num_factors = num_factors;
return LIQUID_OK;
}

// compute number's unique prime factors
// _n : number to factor
// _factors : pre-allocated array of factors [size: LIQUID_MAX_FACTORS x 1]
// _num_factors: number of unique factors found, sorted ascending
void liquid_unique_factor(unsigned int _n,
unsigned int * _factors,
unsigned int * _num_factors)
int liquid_unique_factor(unsigned int _n,
unsigned int * _factors,
unsigned int * _num_factors)
{
unsigned int k;
unsigned int n = _n;
Expand All @@ -107,12 +105,11 @@ void liquid_unique_factor(unsigned int _n,
}
} while (n > 1 && num_factors < LIQUID_MAX_FACTORS);

if (n > 1 && num_factors == LIQUID_MAX_FACTORS) {
fprintf(stderr,"error, liquid_unqiue_factor(), could not factor %u in %u numbers\n", _n, LIQUID_MAX_FACTORS);
exit(1);
}
if (n > 1 && num_factors == LIQUID_MAX_FACTORS)
return liquid_error(LIQUID_EICONFIG,"liquid_unqiue_factor(), could not factor %u in %u numbers", _n, LIQUID_MAX_FACTORS);

*_num_factors = num_factors;
return LIQUID_OK;
}

// compute greatest common divisor between to numbers P and Q
Expand All @@ -121,8 +118,8 @@ unsigned int liquid_gcd(unsigned int _P,
{
// check base cases
if (_P == 0 || _Q == 0) {
fprintf(stderr,"error: liquid_gcd(%u,%u), input cannot be zero\n", _P, _Q);
exit(-1);
liquid_error(LIQUID_EICONFIG,"liquid_gcd(%u,%u), input cannot be zero", _P, _Q);
return 0;
} else if (_P == 1 || _Q == 1) {
return 1;
} else if (_P == _Q) {
Expand Down
1 change: 1 addition & 0 deletions src/math/src/poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define MATRIX(name) LIQUID_CONCAT(matrix, name)
#define POLY(name) LIQUID_CONCAT(poly, name)
#define POLY_NAME "poly"
#define EXTENSION ""
#define T double
#define TC double complex

Expand Down
13 changes: 7 additions & 6 deletions src/math/src/poly.common.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007 - 2015 Joseph Gaeddert
* Copyright (c) 2007 - 2020 Joseph Gaeddert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -43,11 +43,11 @@ T POLY(_val)(T * _p, unsigned int _k, T _x)
return y;
}

void POLY(_fit)(T * _x,
T * _y,
unsigned int _n,
T * _p,
unsigned int _k)
int POLY(_fit)(T * _x,
T * _y,
unsigned int _n,
T * _p,
unsigned int _k)
{

// ...
Expand Down Expand Up @@ -88,5 +88,6 @@ void POLY(_fit)(T * _x,
MATRIX(_mul)(G, _k, _k,
Xty,_k, 1,
_p, _k, 1);
return LIQUID_OK;
}

Loading

0 comments on commit 78b9a6f

Please sign in to comment.