#ifndef __JMATH__JTRIGONOMETRIC__ #define __JMATH__JTRIGONOMETRIC__ #include #include #include #include "JLang/JException.hh" /** * \author mdejong */ namespace JMATH {} namespace JPP { using namespace JMATH; } namespace JMATH { using JLANG::JException; /** * Trigonometric function object for sin and cos. * * Evaluation of function, derivative and integral values. */ class JTrigonometric { public: /** * Type definition of pointer to trigonometric function. */ typedef double (*pF)(double); /** * Constructor. * * \param f1 pointer to function * \param factor multiplication factor */ JTrigonometric(pF f1, const double factor = 1.0) { if (f1 != sin && f1 != cos) { throw JException("Invalid trigonometric function."); } this->f1 = f1; this->factor = factor; } /** * Function value. * * \param x abscissa value * \return function value */ double getValue(const double x) const { return factor * f1(x); } /** * Derivative value. * * \param x abscissa value * \return derivative value */ double getDerivative(const double x) const { return getDerivative().getValue(x); } /** * Integral value. * * \param x abscissa value * \return integral value */ double getIntegral(const double x) const { return getIntegral().getValue(x); } /** * Function value. * * \param x abscissa value * \return function value */ double operator()(const double x) const { return getValue(x); } /** * Derivative function. * * \return derivative function */ JTrigonometric getDerivative() const { if (f1 == sin) return JTrigonometric(cos, +factor); else if (f1 == cos) return JTrigonometric(sin, -factor); else throw JException("Invalid trigonometric function."); } /** * Integral function. * * \return integral function */ JTrigonometric getIntegral() const { if (f1 == sin) return JTrigonometric(cos, -factor); else if (f1 == cos) return JTrigonometric(sin, +factor); else throw JException("Invalid trigonometric function."); } /** * Read trigonometric from input. * * \param in input stream * \param object trigonometric * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JTrigonometric& object) { std::string buffer; if (in >> object.factor >> buffer) { if (buffer == "sin") object.f1 = sin; else if (buffer == "cos") object.f1 = cos; else throw JException("Invalid trigonometric function."); } return in; } /** * Write trigonometric to output. * * \param out output stream * \param object trigonometric * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JTrigonometric& object) { out << object.factor; if (object.f1 == sin) out << ' ' << "sin"; else if (object.f1 == cos) out << ' ' << "cos"; else throw JException("Invalid trigonometric function."); return out; } protected: pF f1; double factor; }; } #endif