| Home | Libraries | People | FAQ | More

### Quaternion Non-Member Operators

##### Unary Plus

    template<typename T>
    quaternion<T> operator + (quaternion<T> const & q);

This unary operator simply returns q.

##### Unary Minus

    template<typename T>
    quaternion<T> operator - (quaternion<T> const & q);

This unary operator returns the opposite of q.

##### Binary Addition Operators

    template<typename T>	quaternion<T>	operator + (T const & lhs, quaternion<T> const & rhs);
    template<typename T>	quaternion<T>	operator + (quaternion<T> const & lhs, T const & rhs);
    template<typename T>	quaternion<T>	operator + (::std::complex<T> const & lhs, quaternion<T> const & rhs);
    template<typename T>	quaternion<T>	operator + (quaternion<T> const & lhs, ::std::complex<T> const & rhs);
    template<typename T>	quaternion<T>	operator + (quaternion<T> const & lhs, quaternion<T> const & rhs);

These operators return `quaternion<T>(lhs) += rhs`.

##### Binary Subtraction Operators

    template<typename T>	quaternion<T>	operator - (T const & lhs, quaternion<T> const & rhs);
    template<typename T>	quaternion<T>	operator - (quaternion<T> const & lhs, T const & rhs);
    template<typename T>	quaternion<T>	operator - (::std::complex<T> const & lhs, quaternion<T> const & rhs);
    template<typename T>	quaternion<T>	operator - (quaternion<T> const & lhs, ::std::complex<T> const & rhs);
    template<typename T>	quaternion<T>	operator - (quaternion<T> const & lhs, quaternion<T> const & rhs);

These operators return `quaternion<T>(lhs) -= rhs`.

##### Binary Multiplication Operators

    template<typename T>	quaternion<T>	operator * (T const & lhs, quaternion<T> const & rhs);
    template<typename T>	quaternion<T>	operator * (quaternion<T> const & lhs, T const & rhs);
    template<typename T>	quaternion<T>	operator * (::std::complex<T> const & lhs, quaternion<T> const & rhs);
    template<typename T>	quaternion<T>	operator * (quaternion<T> const & lhs, ::std::complex<T> const & rhs);
    template<typename T>	quaternion<T>	operator * (quaternion<T> const & lhs, quaternion<T> const & rhs);

These operators return `quaternion<T>(lhs) *= rhs`.

##### Binary Division Operators

    template<typename T>	quaternion<T>	operator / (T const & lhs, quaternion<T> const & rhs);
    template<typename T>	quaternion<T>	operator / (quaternion<T> const & lhs, T const & rhs);
    template<typename T>	quaternion<T>	operator / (::std::complex<T> const & lhs, quaternion<T> const & rhs);
    template<typename T>	quaternion<T>	operator / (quaternion<T> const & lhs, ::std::complex<T> const & rhs);
    template<typename T>	quaternion<T>	operator / (quaternion<T> const & lhs, quaternion<T> const & rhs);

These operators return `quaternion<T>(lhs) /= rhs`. It is of course still an error to divide by zero...

##### Equality Operators

    template<typename T>	bool	operator == (T const & lhs, quaternion<T> const & rhs);
    template<typename T>	bool	operator == (quaternion<T> const & lhs, T const & rhs);
    template<typename T>	bool	operator == (::std::complex<T> const & lhs, quaternion<T> const & rhs);
    template<typename T>	bool	operator == (quaternion<T> const & lhs, ::std::complex<T> const & rhs);
    template<typename T>	bool	operator == (quaternion<T> const & lhs, quaternion<T> const & rhs);

These return true if and only if the four components of `quaternion<T>(lhs)` are equal to their counterparts in `quaternion<T>(rhs)`. As with any floating-type entity, this is essentially meaningless.

##### Inequality Operators

    template<typename T>	bool	operator != (T const & lhs, quaternion<T> const & rhs);
    template<typename T>	bool	operator != (quaternion<T> const & lhs, T const & rhs);
    template<typename T>	bool	operator != (::std::complex<T> const & lhs, quaternion<T> const & rhs);
    template<typename T>	bool	operator != (quaternion<T> const & lhs, ::std::complex<T> const & rhs);
    template<typename T>	bool	operator != (quaternion<T> const & lhs, quaternion<T> const & rhs);

These return true if and only if `quaternion<T>(lhs) == quaternion<T>(rhs)` is false. As with any floating-type entity, this is essentially meaningless.

##### Stream Extractor

    template<typename T, typename charT, class traits>
    ::std::basic_istream<charT,traits>& operator >> (::std::basic_istream<charT,traits> & is, quaternion<T> & q);

Extracts a quaternion q of one of the following forms (with a, b, c and d of type `T`):

`a (a), (a,b), (a,b,c), (a,b,c,d) (a,(c)), (a,(c,d)), ((a)), ((a),c), ((a),(c)), ((a),(c,d)), ((a,b)), ((a,b),c), ((a,b),(c)), ((a,b),(c,d))`

The input values must be convertible to `T`. If bad input is encountered, calls `is.setstate(ios::failbit)` (which may throw ios::failure (27.4.5.3)).

Returns: `is`.

The rationale for the list of accepted formats is that either we have a list of up to four reals, or else we have a couple of complex numbers, and in that case if it formated as a proper complex number, then it should be accepted. Thus potential ambiguities are lifted (for instance (a,b) is (a,b,0,0) and not (a,0,b,0), i.e. it is parsed as a list of two real numbers and not two complex numbers which happen to have imaginary parts equal to zero).

##### Stream Inserter

    template<typename T, typename charT, class traits>
    ::std::basic_ostream<charT,traits>& operator << (::std::basic_ostream<charT,traits> & os, quaternion<T> const & q);

Inserts the quaternion q onto the stream `os` as if it were implemented as follows:

    template<typename T, typename charT, class traits>
    ::std::basic_ostream<charT,traits>& operator << (
                   ::std::basic_ostream<charT,traits> & os,
                   quaternion<T> const & q)
    {
       ::std::basic_ostringstream<charT,traits>	s;

       s.flags(os.flags());
       s.imbue(os.getloc());
       s.precision(os.precision());

       s << '(' << q.R_component_1() << ','
                << q.R_component_2() << ','
                << q.R_component_3() << ','
                << q.R_component_4() << ')';

       return os << s.str();
    }