|
cisst-saw
|
Functions | |
| nmrBernsteinPolynomial (VariableIndexType numVariables, PowerType degree) | |
| template<class _vectorType> | |
| bool | nmrLinearRegression (const _vectorType &x, const _vectorType &y, typename _vectorType::value_type &slope, typename _vectorType::value_type &yint, typename _vectorType::value_type *mse=0, typename _vectorType::value_type tolerance=cmnTypeTraits< typename _vectorType::value_type >::DefaultTolerance) |
The main features of cisstNumerical are:
cisst native functionalities:
| nmrBernsteinPolynomial | ( | VariableIndexType | numVariables, |
| PowerType | degree ) |
class nmrBernsteinPolynomial defines a polynomial in Bernstein basis. In a Bernstein polynomial, all the terms are of equal degree (that is, the sum of powers is a constant), and the variables sum up to a unity. Each term is associated with a scalar coefficient and with a multinomial factor, which reflects the relative weight of the term in the expression:
\(1 = (x_0 + ... + x_{n-1}) ^ d = \sum_{(p_0 + ... + p_{n-1}) = d} \choose{d}{p_0 p_1 ... p_{n-1}} x_0^{p_0} ... x_{n-1}^{p_{n-1}}\)
To make repeated evaluations quicker, we cache the multinomial factor along with the coefficient of the term in a BernsteinTermInfo object. Appropriate accessors are defined.
Since the sum of the variables is 1, one of the variables depends on the others. Typically, it is either the last or the first, but the user can choose any variable to be the ``implicit'' variable. Once the implicit variable has been determined, its value cannot be updated directly, and instead it is reassigned every time one of the other variables is set. However, the user of the class still has the flexibility to replace the choice of the implicit variable at any time. The only thing that matters is that the variables sum to 1.
Note: We use dynamic allocation for the term information (BernsteinTermInfo), which is separate from the STL provided dynamic allocation for the container elements. We store the pointer to the dynamically allocated term info in the container. Therefore:
A better solution may be to define a common base type for TermInfo, and have it declare a virtual dtor. Go for it, if you have time. */ class CISST_EXPORT nmrBernsteinPolynomial : public nmrDynAllocPolynomialContainer { public: typedef nmrDynAllocPolynomialContainer BaseType;
typedef std::pair<nmrPolynomialBase::CoefficientType, nmrPolynomialTermPowerIndex::MultinomialCoefficientType> BernsteinTermInfo;
ifdef CISST_COMPILER_IS_MSVC /*! Constructor determines the number of variables and the degree of the polynomial. Note that Bernstein polynomial contains an additional implicit variable. The argument numVariables must include the implicit variable, since we want to be consistent with the GetNumVariables() method. The degree defines both maximum and minimum degrees for the terms, as all terms are of equal degree. The constructor initializes the free variables to zero, and the implicit variable to 1, so that they all sum up to
| bool nmrLinearRegression | ( | const _vectorType & | x, |
| const _vectorType & | y, | ||
| typename _vectorType::value_type & | slope, | ||
| typename _vectorType::value_type & | yint, | ||
| typename _vectorType::value_type * | mse = 0, | ||
| typename _vectorType::value_type | tolerance = cmnTypeTraits<typename _vectorType::value_type>::DefaultTolerance ) |
This computes a linear regression using the least-squares solution. If the regression is successfully computed, the function returns true; otherwise it returns false. Possible reasons for failure are if the x and y vectors are different sizes, if there are fewer than 2 points, or if the line is near vertical (near infinite slope). Note that this check is based on a specified tolerance value. The default tolerances are obtained from cmnTypeTraits. These defaults may be too large for some applications (e.g., the default for float is currently 1e-5 and for double it is 1e-9).
| x | A vector of all the x values |
| y | A vector of all the y values |
| slope | Pointer for returning computed slope (if not null) |
| yint | Pointer for returning computed y-intercept (if not null) |
| mse | Pointer for returning mean square error (if not null) |
| tolerance | Tolerance to use when checking for division by (near) 0 (default from cmnTypeTraits) |