// // Copyright 2013 Francisco Jerez // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // #ifndef CLOVER_UTIL_ALGEBRA_HPP #define CLOVER_UTIL_ALGEBRA_HPP #include #include "util/range.hpp" #include "util/functional.hpp" namespace clover { /// /// Class that identifies vectors (in the linear-algebraic sense). /// /// There should be a definition of this class for each type that /// makes sense as vector arithmetic operand. /// template struct vector_traits; /// /// References of vectors are vectors. /// template struct vector_traits::enable> { typedef void enable; }; /// /// Constant vectors are vectors. /// template struct vector_traits::enable> { typedef void enable; }; /// /// Arrays of arithmetic types are vectors. /// template struct vector_traits, typename std::enable_if< std::is_arithmetic::value>::type> { typedef void enable; }; namespace detail { template struct are_defined { typedef void enable; }; } /// /// The result of mapping a vector is a vector. /// template struct vector_traits, typename detail::are_defined< typename vector_traits::enable...>::enable> { typedef void enable; }; /// /// Vector sum. /// template::enable, typename = typename vector_traits::enable> adaptor_range operator+(U &&u, V &&v) { return map(plus(), std::forward(u), std::forward(v)); } /// /// Vector difference. /// template::enable, typename = typename vector_traits::enable> adaptor_range operator-(U &&u, V &&v) { return map(minus(), std::forward(u), std::forward(v)); } /// /// Scalar multiplication. /// template::enable> adaptor_range, U> operator*(U &&u, T &&a) { return map(multiplies_by(std::forward(a)), std::forward(u)); } /// /// Scalar multiplication. /// template::enable> adaptor_range, U> operator*(T &&a, U &&u) { return map(multiplies_by(std::forward(a)), std::forward(u)); } /// /// Additive inverse. /// template::enable> adaptor_range operator-(U &&u) { return map(negate(), std::forward(u)); } namespace detail { template using dot_type = typename std::common_type< typename std::remove_reference::type::value_type, typename std::remove_reference::type::value_type >::type; } /// /// Dot product of two vectors. /// /// It can also do matrix multiplication if \a u or \a v is a /// vector of vectors. /// template::enable, typename = typename vector_traits::enable> detail::dot_type dot(U &&u, V &&v) { return fold(plus(), detail::dot_type(), map(multiplies(), u, v)); } } #endif