ENH: add StaticAssert to Polynomial - positive number of terms only

ENH: allow construct from UList of coefficients, from C-arrays
     avoid uninitialized values for null constructor
This commit is contained in:
Mark Olesen
2010-10-08 17:54:13 +02:00
parent dd5ed76a70
commit 71b7e7cee1
4 changed files with 85 additions and 44 deletions

View File

@ -33,7 +33,50 @@ Foam::Polynomial<PolySize>::Polynomial()
VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
logActive_(false),
logCoeff_(0.0)
{}
{
for (int i = 0; i < PolySize; ++i)
{
this->v_[i] = 0.0;
}
}
template<int PolySize>
Foam::Polynomial<PolySize>::Polynomial(const scalar coeffs[PolySize])
:
VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
logActive_(false),
logCoeff_(0.0)
{
for (int i=0; i<PolySize; i++)
{
this->v_[i] = coeffs[i];
}
}
template<int PolySize>
Foam::Polynomial<PolySize>::Polynomial(const UList<scalar>& coeffs)
:
VectorSpace<Polynomial<PolySize>, scalar, PolySize>(),
logActive_(false),
logCoeff_(0.0)
{
if (coeffs.size() != PolySize)
{
FatalErrorIn
(
"Polynomial<PolySize>::Polynomial(const UList<scalar>&)"
) << "Size mismatch: Needed " << PolySize
<< " but got " << coeffs.size()
<< nl << exit(FatalError);
}
for (int i = 0; i < PolySize; ++i)
{
this->v_[i] = coeffs[i];
}
}
template<int PolySize>
@ -173,13 +216,9 @@ Foam::Polynomial<PolySize>::integrateMinus1(const scalar intConstant)
}
newCoeffs[0] = intConstant;
if (PolySize > 0)
for (label i=1; i<PolySize; ++i)
{
for (label i=1; i<PolySize; i++)
{
newCoeffs[i] = this->v_[i]/i;
}
newCoeffs[i] = this->v_[i]/i;
}
return newCoeffs;

View File

@ -29,7 +29,7 @@ Description
poly = logCoeff*log(x) + sum(coeff_[i]*x^i)
where 0 <= i <= n
where 0 \<= i \<= n
- integer powers, starting at zero
- evaluate(x) to evaluate the poly for a given value
@ -51,6 +51,7 @@ SourceFiles
#include "scalar.H"
#include "Ostream.H"
#include "VectorSpace.H"
#include "StaticAssert.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -79,6 +80,9 @@ class Polynomial
:
public VectorSpace<Polynomial<PolySize>, scalar, PolySize>
{
//- Size must be positive (non-zero)
StaticAssert(PolySize > 0);
// Private data
//- Include the log term? - only activated using integrateMinus1()
@ -97,9 +101,15 @@ public:
// Constructors
//- Construct null
//- Construct null, with all coefficients = 0.0
Polynomial();
//- Construct from C-array of coefficients
explicit Polynomial(const scalar coeffs[PolySize]);
//- Construct from a list of coefficients
explicit Polynomial(const UList<scalar>& coeffs);
//- Construct from name and Istream
Polynomial(const word& name, Istream& is);