Initial commit

This commit is contained in:
2026-04-03 00:22:39 -05:00
commit eca1e8c458
945 changed files with 218160 additions and 0 deletions

View File

@@ -0,0 +1,238 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief Vector class operators.
*/
#ifndef vector_class_ops_h
#define vector_class_ops_h
#if defined(_MSC_VER) && _MSC_VER < 1400
#pragma warning(disable:4003)
// XXX Horrible hack to turn off warnings about "not enough actual params"
// for the macros below.
#endif
/* XXX HACK!!! This is a hack to resize in the assign() functions only when
* auto resizing is turned off.
*/
#if !defined(CML_VECTOR_RESIZE_ON_ASSIGNMENT)
#define _DO_VECTOR_SET_RESIZE(_N_) cml::et::detail::Resize(*this,_N_)
#else
#define _DO_VECTOR_SET_RESIZE(_N_)
#endif
/** Set a vector from 2 values. */
#define CML_ASSIGN_VEC_2 \
vector_type& \
set(ELEMENT_ARG_TYPE e0, ELEMENT_ARG_TYPE e1) { \
_DO_VECTOR_SET_RESIZE(2); \
/* This is overkill, but simplifies size checking: */ \
value_type v[] = {e0,e1}; \
typedef et::OpAssign<Element,Element> OpT; \
cml::vector< const value_type, external<2> > src(v); \
et::UnrollAssignment<OpT>(*this,src); \
return *this; \
}
/** Set a vector from 3 values. */
#define CML_ASSIGN_VEC_3 \
vector_type& \
set( \
ELEMENT_ARG_TYPE e0, \
ELEMENT_ARG_TYPE e1, \
ELEMENT_ARG_TYPE e2 \
) \
{ \
_DO_VECTOR_SET_RESIZE(3); \
/* This is overkill, but simplifies size checking: */ \
value_type v[] = {e0,e1,e2}; \
typedef et::OpAssign<Element,Element> OpT; \
cml::vector< const value_type, external<3> > src(v); \
et::UnrollAssignment<OpT>(*this,src); \
return *this; \
}
/** Create a vector from 4 values. */
#define CML_ASSIGN_VEC_4 \
vector_type& \
set( \
ELEMENT_ARG_TYPE e0, \
ELEMENT_ARG_TYPE e1, \
ELEMENT_ARG_TYPE e2, \
ELEMENT_ARG_TYPE e3 \
) \
{ \
_DO_VECTOR_SET_RESIZE(4); \
/* This is overkill, but simplifies size checking: */ \
value_type v[] = {e0,e1,e2,e3}; \
typedef et::OpAssign<Element,Element> OpT; \
cml::vector< const value_type, external<4> > src(v); \
et::UnrollAssignment<OpT>(*this,src); \
return *this; \
}
/** Create a vector from 2 values. */
#define CML_CONSTRUCT_VEC_2(_add_) \
vector(ELEMENT_ARG_TYPE e0, ELEMENT_ARG_TYPE e1) _add_ { \
set(e0,e1); \
}
/** Create a vector from 3 values. */
#define CML_CONSTRUCT_VEC_3(_add_) \
vector( \
ELEMENT_ARG_TYPE e0, \
ELEMENT_ARG_TYPE e1, \
ELEMENT_ARG_TYPE e2 \
) _add_ \
{ \
set(e0,e1,e2); \
}
/** Create a vector from 4 values. */
#define CML_CONSTRUCT_VEC_4(_add_) \
vector( \
ELEMENT_ARG_TYPE e0, \
ELEMENT_ARG_TYPE e1, \
ELEMENT_ARG_TYPE e2, \
ELEMENT_ARG_TYPE e3 \
) _add_ \
{ \
set(e0,e1,e2,e3); \
}
/** Create a (fixed-size) N vector from an N-1-vector and a scalar. */
#define CML_CONSTRUCT_FROM_SUBVEC(_add_) \
vector( \
const subvector_type& s, \
ELEMENT_ARG_TYPE e \
) _add_ \
{ \
_DO_VECTOR_SET_RESIZE(s.size()+1); \
for(size_t i = 0; i < s.size(); ++ i) \
(*this)[i] = s[i]; \
(*this)[s.size()] = e; \
}
/** Copy-construct a vector from a fixed-size array of values. */
#define CML_VEC_COPY_FROM_FIXED_ARRAY(_N_,_add_) \
vector(const value_type v[_N_]) _add_ { \
typedef et::OpAssign<Element,Element> OpT; \
cml::vector< const value_type, external<_N_> > src(v); \
et::UnrollAssignment<OpT>(*this,src); \
}
/** Copy-construct a vector from a runtime-sized array of values. */
#define CML_VEC_COPY_FROM_ARRAY(_add_) \
vector(const value_type* const v, size_t N) _add_ { \
typedef et::OpAssign<Element,Element> OpT; \
cml::vector<const value_type, external<> > src(v,N); \
et::UnrollAssignment<OpT>(*this,src); \
}
/** Copy-construct a vector.
*
* @internal This is required for GCC4, since it won't elide the default
* copy constructor.
*/
#define CML_VEC_COPY_FROM_VECTYPE(_add_) \
vector(const vector_type& v) _add_ { \
typedef et::OpAssign<Element,Element> OpT; \
et::UnrollAssignment<OpT>(*this,v); \
}
/** Construct from an arbitrary vector.
*
* @param v the vector to copy from.
*/
#define CML_VEC_COPY_FROM_VEC \
template<typename E, class AT> \
vector(const vector<E,AT>& m) { \
typedef et::OpAssign<Element,E> OpT; \
et::UnrollAssignment<OpT>(*this,m); \
}
/** Construct from a vector expression.
*
* @param expr the expression to copy from.
*/
#define CML_VEC_COPY_FROM_VECXPR \
template<class XprT> \
vector(VECXPR_ARG_TYPE e) { \
/* Verify that a promotion exists at compile time: */ \
typedef typename et::VectorPromote< \
vector_type, typename XprT::result_type>::type result_type; \
typedef typename XprT::value_type src_value_type; \
typedef et::OpAssign<Element,src_value_type> OpT; \
et::UnrollAssignment<OpT>(*this,e); \
}
/** Assign from the same vector type.
*
* @param v the vector to copy from.
*/
#define CML_VEC_ASSIGN_FROM_VECTYPE \
vector_type& operator=(const vector_type& v) { \
typedef et::OpAssign<Element,Element> OpT; \
et::UnrollAssignment<OpT>(*this,v); \
return *this; \
}
/** Assign this vector from another using the given elementwise op.
*
* This allows assignment from arbitrary vector types.
*
* @param _op_ the operator (e.g. +=)
* @param _op_name_ the op functor (e.g. et::OpAssign)
*/
#define CML_VEC_ASSIGN_FROM_VEC(_op_, _op_name_) \
template<typename E, class AT> vector_type& \
operator _op_ (const cml::vector<E,AT>& m) { \
typedef _op_name_ <Element,E> OpT; \
cml::et::UnrollAssignment<OpT>(*this,m); \
return *this; \
}
/** Declare a function to assign this vector from a vector expression.
*
* @param _op_ the operator (e.g. +=)
* @param _op_name_ the op functor (e.g. et::OpAssign)
*/
#define CML_VEC_ASSIGN_FROM_VECXPR(_op_, _op_name_) \
template<class XprT> vector_type& \
operator _op_ (VECXPR_ARG_TYPE e) { \
/* Verify that a promotion exists at compile time: */ \
typedef typename et::VectorPromote< \
vector_type, typename XprT::result_type>::type result_type; \
typedef typename XprT::value_type src_value_type; \
typedef _op_name_ <Element,src_value_type> OpT; \
cml::et::UnrollAssignment<OpT>(*this,e); \
return *this; \
}
/** Declare a function to assign this vector from a scalar.
*
* @param _op_ the operator (e.g. *=)
* @param _op_name_ the op functor (e.g. et::OpAssign)
*
* @internal This shouldn't be used for ops, like +=, which aren't
* defined in vector algebra.
*/
#define CML_VEC_ASSIGN_FROM_SCALAR(_op_, _op_name_) \
vector_type& operator _op_ (ELEMENT_ARG_TYPE s) { \
typedef _op_name_ <Element,Element> OpT; \
cml::et::UnrollAssignment<OpT>(*this,s); \
return *this; \
}
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,190 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief Specialization for resizeable, dynamic-memory vector.
*/
#ifndef dynamic_vector_h
#define dynamic_vector_h
#include <cml/core/dynamic_1D.h>
#include <cml/vector/vector_expr.h>
#include <cml/vector/class_ops.h>
#include <cml/vector/vector_unroller.h>
namespace cml {
/** Resizeable, dynamic-memory vector. */
template<typename Element, typename Alloc>
class vector< Element, dynamic<Alloc> >
: public dynamic_1D<Element,Alloc>
{
public:
/* Shorthand for the generator: */
typedef dynamic<> storage_type;
typedef dynamic<Alloc> generator_type;
/* Shorthand for the array type: */
typedef dynamic_1D<Element,Alloc> array_type;
/* Shorthand for the type of this vector: */
typedef vector<Element,generator_type> vector_type;
/* The vector coordinate type: */
typedef Element coordinate_type;
/* For integration into the expression template code: */
typedef vector_type expr_type;
/* For integration into the expression template code: */
typedef vector_type temporary_type;
/* The type for a vector in one lower dimension: */
typedef vector_type subvector_type;
/* Standard: */
typedef typename array_type::value_type value_type;
typedef typename array_type::reference reference;
typedef typename array_type::const_reference const_reference;
/* For integration into the expression templates code: */
typedef vector_type& expr_reference;
typedef const vector_type& expr_const_reference;
/* For matching by storage type: */
typedef typename array_type::memory_tag memory_tag;
/* For matching by size type: */
typedef typename array_type::size_tag size_tag;
/* For matching by resizability: */
typedef typename array_type::resizing_tag resizing_tag;
/* For matching by result-type: */
typedef cml::et::vector_result_tag result_tag;
/* For matching by assignability: */
typedef cml::et::assignable_tag assignable_tag;
public:
/** Return square of the length. */
value_type length_squared() const {
return cml::dot(*this,*this);
}
/** Return the length. */
value_type length() const {
return std::sqrt(length_squared());
}
/** Normalize the vector. */
vector_type& normalize() {
return (*this /= length());
}
/** Set this vector to [0]. */
vector_type& zero() {
typedef cml::et::OpAssign<Element,Element> OpT;
cml::et::UnrollAssignment<OpT>(*this,Element(0));
return *this;
}
/** Set this vector to a cardinal vector. */
vector_type& cardinal(size_t i) {
zero();
(*this)[i] = Element(1);
return *this;
}
/** Pairwise minimum of this vector with another. */
template<typename E, class AT>
void minimize(const vector<E,AT>& v) {
/* XXX This should probably use ScalarPromote: */
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = std::min((*this)[i],v[i]);
}
}
/** Pairwise maximum of this vector with another. */
template<typename E, class AT>
void maximize(const vector<E,AT>& v) {
/* XXX This should probably use ScalarPromote: */
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = std::max((*this)[i],v[i]);
}
}
/** Fill vector with random elements. */
void random(value_type min, value_type max) {
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = cml::random_real(min,max);
}
}
/** Return a subvector by removing element i.
*
* @internal This is horribly inefficient...
*/
subvector_type subvector(size_t i) const {
subvector_type s; s.resize(this->size()-1);
for(size_t m = 0, n = 0; m < this->size(); ++ m)
if(m != i) s[n++] = (*this)[m];
return s;
};
public:
/** Default constructor. */
vector() : array_type() {}
/** Construct given array size. */
vector(size_t N) : array_type(N) {}
public:
/* Define common class operators: */
CML_CONSTRUCT_VEC_2(: array_type())
CML_CONSTRUCT_VEC_3(: array_type())
CML_CONSTRUCT_VEC_4(: array_type())
CML_VEC_COPY_FROM_ARRAY(: array_type())
CML_VEC_COPY_FROM_VECTYPE(: array_type())
CML_VEC_COPY_FROM_VEC
CML_VEC_COPY_FROM_VECXPR
CML_ASSIGN_VEC_2
CML_ASSIGN_VEC_3
CML_ASSIGN_VEC_4
CML_VEC_ASSIGN_FROM_VECTYPE
CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
};
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,343 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief Specializations for external-memory vectors.
*
* @note Copy-constructing one external<> vector from another is not
* supported, since an external<> vector is essentially a wrapper for a
* pointer and has no allocated storage of its own.
*/
#ifndef external_vector_h
#define external_vector_h
#include <cml/core/external_1D.h>
#include <cml/vector/vector_expr.h>
#include <cml/vector/class_ops.h>
#include <cml/vector/vector_unroller.h>
#include <cml/vector/dynamic.h>
namespace cml {
/** Fixed-size, fixed-memory vector. */
template<typename Element, int Size>
class vector< Element, external<Size> >
: public external_1D<Element,Size>
{
public:
/* Shorthand for the generator: */
typedef external<> storage_type;
typedef external<Size> generator_type;
/* Shorthand for the array type: */
typedef external_1D<Element,Size> array_type;
/* Shorthand for the type of this vector: */
typedef vector<Element,generator_type> vector_type;
/* The vector coordinate type: */
typedef Element coordinate_type;
/* For integration into the expression template code: */
typedef vector_type expr_type;
/* For integration into the expression template code: */
typedef vector<typename cml::remove_const<Element>::type,
fixed<Size> > temporary_type;
/* Note: this ensures that an external vector is copied into the proper
* temporary; external<> temporaries are not allowed.
*/
/* The type for a vector in one lower dimension: */
typedef typename temporary_type::subvector_type subvector_type;
/* Standard: */
typedef typename array_type::value_type value_type;
typedef typename array_type::reference reference;
typedef typename array_type::const_reference const_reference;
/* For integration into the expression templates code: */
typedef vector_type& expr_reference;
typedef const vector_type& expr_const_reference;
/* For matching by storage type: */
typedef typename array_type::memory_tag memory_tag;
/* For matching by size type: */
typedef typename array_type::size_tag size_tag;
/* For matching by result-type: */
typedef cml::et::vector_result_tag result_tag;
/* For matching by assignability: */
typedef cml::et::assignable_tag assignable_tag;
public:
/** Static constant containing the vector's space dimension. */
enum { dimension = Size };
public:
/** Return square of the length. */
value_type length_squared() const {
return cml::dot(*this,*this);
}
/** Return the length. */
value_type length() const {
return std::sqrt(length_squared());
}
/** Normalize the vector. */
vector_type& normalize() {
return (*this /= length());
}
/** Set this vector to [0]. */
vector_type& zero() {
typedef cml::et::OpAssign<Element,Element> OpT;
cml::et::UnrollAssignment<OpT>(*this,Element(0));
return *this;
}
/** Set this vector to a cardinal vector. */
vector_type& cardinal(size_t i) {
zero();
(*this)[i] = Element(1);
return *this;
}
/** Pairwise minimum of this vector with another. */
template<typename E, class AT>
void minimize(const vector<E,AT>& v) {
/* XXX This should probably use ScalarPromote: */
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = std::min((*this)[i],v[i]);
}
}
/** Pairwise maximum of this vector with another. */
template<typename E, class AT>
void maximize(const vector<E,AT>& v) {
/* XXX This should probably use ScalarPromote: */
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = std::max((*this)[i],v[i]);
}
}
/** Fill vector with random elements. */
void random(value_type min, value_type max) {
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = cml::random_real(min,max);
}
}
/** Return a subvector by removing element i.
*
* @internal This is horribly inefficient...
*/
subvector_type subvector(size_t i) const {
subvector_type s;
for(size_t m = 0, n = 0; m < this->size(); ++ m)
if(m != i) s[n++] = (*this)[m];
return s;
};
public:
/** Construct from an array of values. */
vector(Element* const array) : array_type(array) {}
public:
CML_ASSIGN_VEC_2
CML_ASSIGN_VEC_3
CML_ASSIGN_VEC_4
CML_VEC_ASSIGN_FROM_VECTYPE
/* Only assignment operators can be used to copy from other types: */
CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
};
/** Run-time sized vector. */
template<typename Element>
class vector< Element, external<> >
: public external_1D<Element>
{
public:
/* Shorthand for the generator: */
typedef external<> storage_type;
typedef external<> generator_type;
/* Shorthand for the array type: */
typedef external_1D<Element> array_type;
/* Shorthand for the type of this vector: */
typedef vector<Element,generator_type> vector_type;
/* For integration into the expression template code: */
typedef vector_type expr_type;
/* For integration into the expression template code: */
typedef vector<typename cml::remove_const<Element>::type,
dynamic<> > temporary_type;
/* Note: this ensures that an external vector is copied into the proper
* temporary; external<> temporaries are not allowed.
*/
/* The type for a vector in one lower dimension: */
typedef typename temporary_type::subvector_type subvector_type;
/* Standard: */
typedef typename array_type::value_type value_type;
typedef typename array_type::reference reference;
typedef typename array_type::const_reference const_reference;
/* For integration into the expression templates code: */
typedef vector_type& expr_reference;
typedef const vector_type& expr_const_reference;
/* For matching by storage type: */
typedef typename array_type::memory_tag memory_tag;
/* For matching by size type: */
typedef typename array_type::size_tag size_tag;
/* For matching by resizability: */
typedef typename array_type::resizing_tag resizing_tag;
/* For matching by result-type: */
typedef cml::et::vector_result_tag result_tag;
/* For matching by assignability: */
typedef cml::et::assignable_tag assignable_tag;
public:
/** Return square of the length. */
value_type length_squared() const {
return dot(*this,*this);
}
/** Return the length. */
value_type length() const {
return std::sqrt(length_squared());
}
/** Normalize the vector. */
vector_type& normalize() {
return (*this /= length());
}
/** Set this vector to [0]. */
vector_type& zero() {
typedef cml::et::OpAssign<Element,Element> OpT;
cml::et::UnrollAssignment<OpT>(*this,Element(0));
return *this;
}
/** Set this vector to a cardinal vector. */
vector_type& cardinal(size_t i) {
zero();
(*this)[i] = Element(1);
return *this;
}
/** Pairwise minimum of this vector with another. */
template<typename E, class AT>
void minimize(const vector<E,AT>& v) {
/* XXX This should probably use ScalarPromote: */
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = std::min((*this)[i],v[i]);
}
}
/** Pairwise maximum of this vector with another. */
template<typename E, class AT>
void maximize(const vector<E,AT>& v) {
/* XXX This should probably use ScalarPromote: */
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = std::max((*this)[i],v[i]);
}
}
/** Fill vector with random elements. */
void random(value_type min, value_type max) {
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = random_real(min,max);
}
}
/** Return a subvector by removing element i.
*
* @internal This is horribly inefficient...
*/
subvector_type subvector(size_t i) const {
subvector_type s; s.resize(this->size()-1);
for(size_t m = 0, n = 0; m < this->size(); ++ m)
if(m != i) s[n++] = (*this)[m];
return s;
};
public:
/** Construct from an array of values and the size. */
vector(Element* const array, size_t size)
: array_type(array, size) {}
public:
/* Define class operators for external vectors. Note: external vectors
* cannot be copy-constructed, but they can be assigned to:
*/
CML_ASSIGN_VEC_2
CML_ASSIGN_VEC_3
CML_ASSIGN_VEC_4
CML_VEC_ASSIGN_FROM_VECTYPE
/* Only assignment operators can be used to copy from other types: */
CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
};
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,196 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief Specialization for fixed-size, fixed-memory vectors.
*/
#ifndef fixed_vector_h
#define fixed_vector_h
#include <cml/core/fixed_1D.h>
#include <cml/vector/vector_expr.h>
#include <cml/vector/class_ops.h>
#include <cml/vector/vector_unroller.h>
#include <cml/vector/external.h>
#include <cml/util.h>
namespace cml {
/** Fixed-size, fixed-memory vector. */
template<typename Element, int Size>
class vector< Element, fixed<Size> >
: public fixed_1D<Element,Size>
{
public:
/* Shorthand for the generator: */
typedef fixed<> storage_type;
typedef fixed<Size> generator_type;
/* Shorthand for the array type: */
typedef fixed_1D<Element,Size> array_type;
/* Shorthand for the type of this vector: */
typedef vector<Element,generator_type> vector_type;
/* The vector coordinate type: */
typedef Element coordinate_type;
/* For integration into the expression template code: */
typedef vector_type expr_type;
/* For integration into the expression template code: */
typedef vector_type temporary_type;
/* The type for a vector in one lower dimension: */
typedef vector< Element, fixed<Size-1> > subvector_type;
/* The type for a vector in one higher dimension: */
typedef vector< Element, fixed<Size+1> > supervector_type;
/* Standard: */
typedef typename array_type::value_type value_type;
typedef typename array_type::reference reference;
typedef typename array_type::const_reference const_reference;
/* For integration into the expression templates code: */
typedef vector_type& expr_reference;
typedef const vector_type& expr_const_reference;
/* For matching by storage type: */
typedef typename array_type::memory_tag memory_tag;
/* For matching by size type: */
typedef typename array_type::size_tag size_tag;
/* For matching by result-type: */
typedef cml::et::vector_result_tag result_tag;
/* For matching by assignability: */
typedef cml::et::assignable_tag assignable_tag;
public:
/** Static constant containing the vector's space dimension. */
enum { dimension = Size };
public:
/** Return square of the length. */
value_type length_squared() const {
return cml::dot(*this,*this);
}
/** Return the length. */
value_type length() const {
return std::sqrt(length_squared());
}
/** Normalize the vector. */
vector_type& normalize() {
return (*this /= length());
}
/** Set this vector to [0]. */
vector_type& zero() {
typedef cml::et::OpAssign<Element,Element> OpT;
cml::et::UnrollAssignment<OpT>(*this,Element(0));
return *this;
}
/** Set this vector to a cardinal vector. */
vector_type& cardinal(size_t i) {
zero();
(*this)[i] = Element(1);
return *this;
}
/** Pairwise minimum of this vector with another. */
template<typename E, class AT>
void minimize(const vector<E,AT>& v) {
/* XXX This should probably use ScalarPromote: */
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = std::min((*this)[i],v[i]);
}
}
/** Pairwise maximum of this vector with another. */
template<typename E, class AT>
void maximize(const vector<E,AT>& v) {
/* XXX This should probably use ScalarPromote: */
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = std::max((*this)[i],v[i]);
}
}
/** Fill vector with random elements. */
void random(value_type min, value_type max) {
for (size_t i = 0; i < this->size(); ++i) {
(*this)[i] = cml::random_real(min,max);
}
}
/** Return a subvector by removing element i.
*
* @internal This is horribly inefficient...
*/
subvector_type subvector(size_t i) const {
subvector_type s;
for(size_t m = 0, n = 0; m < this->size(); ++ m)
if(m != i) s[n++] = (*this)[m];
return s;
};
public:
vector() : array_type() {}
public:
/* Define common class operators: */
CML_CONSTRUCT_VEC_2(/**/)
CML_CONSTRUCT_VEC_3(/**/)
CML_CONSTRUCT_VEC_4(/**/)
CML_CONSTRUCT_FROM_SUBVEC(/**/)
CML_VEC_COPY_FROM_FIXED_ARRAY(array_type::array_size,/**/)
CML_VEC_COPY_FROM_VECTYPE(: array_type())
CML_VEC_COPY_FROM_VEC
CML_VEC_COPY_FROM_VECXPR
CML_ASSIGN_VEC_2
CML_ASSIGN_VEC_3
CML_ASSIGN_VEC_4
CML_VEC_ASSIGN_FROM_VECTYPE
CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign)
CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign)
CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign)
CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign)
CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign)
CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign)
CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign)
CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign)
};
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,248 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief Defines the various combinations of vector expressions.
*
* Create unary and binary operators with macros. The available combinations
* are:
*
* Unary expressions:
*
* op Vector -> Vector
* op VecXpr -> VecXpr
*
* Binary expressions:
*
* Vector op Vector -> Vector
* VecXpr op Vector -> VecXpr
* Vector op VecXpr -> VecXpr
* VecXpr op VecXpr -> VecXpr
*
* Vector op Scalar -> Vector
* Scalar op Vector -> Vector
* VecXpr op Scalar -> VecXpr
* Scalar op VecXpr -> VecXpr
*
* All of the generator functions compress the expression tree by hoisting
* subexpressions into the containing expression. This has the effect of
* forcing only the root node of the expression tree to be a VectorXpr.
* Every other node is a Unary or BinaryVectorOp.
*
* @todo Should ScalarT in expressions be passed by reference or by value?
*/
#ifndef vecop_macros_h
#define vecop_macros_h
/** Declare a unary operator taking a vector operand. */
#define CML_VEC_UNIOP(_op_, _OpT_) \
template<typename E, class AT> \
inline et::VectorXpr< \
et::UnaryVectorOp< vector<E,AT>, _OpT_ <E> > \
> \
\
_op_ (const vector<E,AT>& arg) \
{ \
typedef et::UnaryVectorOp< \
vector<E,AT>, _OpT_ <E> \
> ExprT; \
return et::VectorXpr<ExprT>(ExprT(arg)); \
}
/** Declare a unary operator taking a et::VectorXpr operand. */
#define CML_VECXPR_UNIOP(_op_, _OpT_) \
template<class XprT> \
inline et::VectorXpr< \
et::UnaryVectorOp< XprT, _OpT_ <typename XprT::value_type> > \
> \
\
_op_ (VECXPR_ARG_TYPE arg) \
{ \
typedef et::UnaryVectorOp< \
XprT, _OpT_ <typename XprT::value_type> \
> ExprT; \
return et::VectorXpr<ExprT>(ExprT(arg.expression())); \
}
/** Declare an operator taking two vector operands. */
#define CML_VEC_VEC_BINOP(_op_, _OpT_) \
template<typename E1, class AT1, typename E2, class AT2> \
inline et::VectorXpr< \
et::BinaryVectorOp< \
vector<E1,AT1>, vector<E2,AT2>, _OpT_ <E1,E2> \
> \
> \
\
_op_ ( \
const vector<E1,AT1>& left, \
const vector<E2,AT2>& right) \
{ \
typedef et::BinaryVectorOp< \
vector<E1,AT1>, vector<E2,AT2>, _OpT_ <E1,E2> \
> ExprT; \
return et::VectorXpr<ExprT>(ExprT(left,right)); \
}
/** Declare an operator taking a vector and a et::VectorXpr. */
#define CML_VEC_VECXPR_BINOP(_op_, _OpT_) \
template<typename E, class AT, class XprT> \
inline et::VectorXpr< \
et::BinaryVectorOp< \
vector<E,AT>, XprT, _OpT_ <E, typename XprT::value_type> \
> \
> \
\
_op_ ( \
const vector<E,AT>& left, \
VECXPR_ARG_TYPE right) \
{ \
typedef et::BinaryVectorOp< \
vector<E,AT>, XprT, \
_OpT_ <E, typename XprT::value_type> \
> ExprT; \
return et::VectorXpr<ExprT>(ExprT(left,right.expression())); \
}
/** Declare an operator taking an et::VectorXpr and a vector. */
#define CML_VECXPR_VEC_BINOP(_op_, _OpT_) \
template<class XprT, typename E, class AT> \
inline et::VectorXpr< \
et::BinaryVectorOp< \
XprT, vector<E,AT>, _OpT_ <typename XprT::value_type, E> \
> \
> \
\
_op_ ( \
VECXPR_ARG_TYPE left, \
const vector<E,AT>& right) \
{ \
typedef et::BinaryVectorOp< \
XprT, vector<E,AT>, \
_OpT_ <typename XprT::value_type, E> \
> ExprT; \
return et::VectorXpr<ExprT>(ExprT(left.expression(),right)); \
}
/** Declare an operator taking two et::VectorXpr operands. */
#define CML_VECXPR_VECXPR_BINOP(_op_, _OpT_) \
template<class XprT1, class XprT2> \
inline et::VectorXpr< \
et::BinaryVectorOp< \
XprT1, XprT2, \
_OpT_ < \
typename XprT1::value_type, \
typename XprT2::value_type \
> \
> \
> \
\
_op_ ( \
VECXPR_ARG_TYPE_N(1) left, \
VECXPR_ARG_TYPE_N(2) right) \
{ \
typedef et::BinaryVectorOp< \
XprT1, XprT2, \
_OpT_ < \
typename XprT1::value_type, \
typename XprT2::value_type> \
> ExprT; \
return et::VectorXpr<ExprT>( \
ExprT(left.expression(),right.expression())); \
}
/** Declare an operator taking a vector and a scalar. */
#define CML_VEC_SCALAR_BINOP(_op_, _OpT_) \
template<typename E, class AT, typename ScalarT> \
inline et::VectorXpr< \
et::BinaryVectorOp< \
vector<E,AT>, ScalarT, _OpT_ <E,ScalarT> \
> \
> \
\
_op_ ( \
const vector<E,AT>& left, \
SCALAR_ARG_TYPE right) \
{ \
typedef et::BinaryVectorOp< \
vector<E,AT>, ScalarT, _OpT_ <E,ScalarT> \
> ExprT; \
return et::VectorXpr<ExprT>(ExprT(left,right)); \
}
/** Declare an operator taking a scalar and a vector. */
#define CML_SCALAR_VEC_BINOP(_op_, _OpT_) \
template<typename ScalarT, typename E, class AT> \
inline et::VectorXpr< \
et::BinaryVectorOp< \
ScalarT, vector<E,AT>, _OpT_ <ScalarT,E> \
> \
> \
\
_op_ ( \
SCALAR_ARG_TYPE left, \
const vector<E,AT>& right) \
{ \
typedef et::BinaryVectorOp< \
ScalarT, vector<E,AT>, _OpT_ <ScalarT,E> \
> ExprT; \
return et::VectorXpr<ExprT>(ExprT(left,right)); \
}
/** Declare an operator taking a et::VectorXpr and a scalar. */
#define CML_VECXPR_SCALAR_BINOP(_op_, _OpT_) \
template<class XprT, typename ScalarT> \
inline et::VectorXpr< \
et::BinaryVectorOp< \
XprT, ScalarT, _OpT_ <typename XprT::value_type,ScalarT> \
> \
> \
\
_op_ ( \
VECXPR_ARG_TYPE left, \
SCALAR_ARG_TYPE right) \
{ \
typedef et::BinaryVectorOp< \
XprT, ScalarT, _OpT_ <typename XprT::value_type,ScalarT> \
> ExprT; \
return et::VectorXpr<ExprT>(ExprT(left.expression(),right)); \
}
/** Declare an operator taking a scalar and a et::VectorXpr. */
#define CML_SCALAR_VECXPR_BINOP(_op_, _OpT_) \
template<typename ScalarT, class XprT> \
inline et::VectorXpr< \
et::BinaryVectorOp< \
ScalarT, XprT, _OpT_ <ScalarT, typename XprT::value_type> \
> \
> \
\
_op_ ( \
SCALAR_ARG_TYPE left, \
VECXPR_ARG_TYPE right) \
{ \
typedef et::BinaryVectorOp< \
ScalarT, XprT, \
_OpT_ <ScalarT, typename XprT::value_type> \
> ExprT; \
return et::VectorXpr<ExprT>(ExprT(left,right.expression())); \
}
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,236 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief
*/
#ifndef vector_comparison_h
#define vector_comparison_h
#include <cml/core/cml_assert.h>
#include <cml/et/size_checking.h>
#include <cml/et/scalar_ops.h>
/* This is used below to create a more meaningful compile-time error when
* vector_comparison is not provided with vector or VectorExpr arguments:
*/
struct vector_comparison_expects_vector_args_error;
#define CML_VEC_VEC_ORDER(_order_, _op_, _OpT_) \
template<typename E1, class AT1, typename E2, class AT2> \
inline bool \
_op_ ( \
const vector<E1,AT1>& left, \
const vector<E2,AT2>& right) \
{ \
return detail::vector_##_order_ (left, right, _OpT_ <E1,E2>()); \
}
#define CML_VEC_VECXPR_ORDER(_order_, _op_, _OpT_) \
template<typename E, class AT, class XprT> \
inline bool \
_op_ ( \
const vector<E,AT>& left, \
VECXPR_ARG_TYPE right) \
{ \
return detail::vector_##_order_ (left, right, \
_OpT_ <E, typename XprT::value_type>()); \
}
#define CML_VECXPR_VEC_ORDER(_order_, _op_, _OpT_) \
template<class XprT, typename E, class AT> \
inline bool \
_op_ ( \
VECXPR_ARG_TYPE left, \
const vector<E,AT>& right) \
{ \
return detail::vector_##_order_ (left, right, \
_OpT_ <typename XprT::value_type, E>()); \
}
#define CML_VECXPR_VECXPR_ORDER(_order_, _op_, _OpT_) \
template<class XprT1, class XprT2> \
inline bool \
_op_ ( \
VECXPR_ARG_TYPE_N(1) left, \
VECXPR_ARG_TYPE_N(2) right) \
{ \
return detail::vector_##_order_ (left, right, \
_OpT_ < \
typename XprT1::value_type, \
typename XprT2::value_type>()); \
}
namespace cml {
namespace detail {
/** Vector strict weak ordering relationship.
*
* OpT must implement a strict weak order on the vector element type.
* operator< and operator> on integer and floating-point types are
* examples.
*/
template<typename LeftT, typename RightT, typename OpT>
inline bool
vector_weak_order(const LeftT& left, const RightT& right, OpT)
{
/* Shorthand: */
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
/* vector_comparison() requires vector expressions: */
CML_STATIC_REQUIRE_M(
(et::VectorExpressions<LeftT,RightT>::is_true),
vector_comparison_expects_vector_args_error);
/* Note: parens are required here so that the preprocessor ignores the
* commas:
*/
typedef typename et::VectorPromote<
typename left_traits::result_type,
typename right_traits::result_type
>::type result_type;
typedef typename result_type::size_tag size_tag;
/* Verify expression size: */
ssize_t N = (ssize_t) et::CheckedSize(left,right,size_tag());
for(ssize_t i = 0; i < N; ++ i) {
if(OpT().apply(
left_traits().get(left,i),
right_traits().get(right,i)
))
{
/* If weak order (a < b) is satisfied, return true: */
return true;
} else if(OpT().apply(
right_traits().get(right,i),
left_traits().get(left,i)
))
{
/* If !(b < a), then return false: */
return false;
} else {
/* Have !(a < b) && !(b < a) <=> (a >= b && b >= a) <=> (a == b).
* so need to test next element:
*/
continue;
}
}
/* XXX Can this be unrolled in any reasonable way? */
/* If we get here, then left == right: */
return false;
}
/** Vector total order relationship.
*
* OpT must implement a total order on the vector element type. operator<=
* and operator>= on integer and floating-point types are examples.
*/
template<typename LeftT, typename RightT, typename OpT>
inline bool
vector_total_order(const LeftT& left, const RightT& right, OpT)
{
/* Shorthand: */
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
/* vector_comparison() requires vector expressions: */
CML_STATIC_REQUIRE_M(
(et::VectorExpressions<LeftT,RightT>::is_true),
vector_comparison_expects_vector_args_error);
/* Note: parens are required here so that the preprocessor ignores the
* commas:
*/
typedef typename et::VectorPromote<
typename left_traits::result_type,
typename right_traits::result_type
>::type result_type;
typedef typename result_type::size_tag size_tag;
/* Verify expression size: */
ssize_t N = (ssize_t) et::CheckedSize(left,right,size_tag());
for(ssize_t i = 0; i < N; ++ i) {
/* Test total order: */
if(OpT().apply(
left_traits().get(left,i),
right_traits().get(right,i)
))
{
/* Automatically true if weak order (a <= b) && !(b <= a) <=>
* (a <= b) && (b > a) <=> (a < b) is satisfied:
*/
if(!OpT().apply(
right_traits().get(right,i),
left_traits().get(left,i)
))
return true;
/* Otherwise, have equality (a <= b) && (b <= a), so continue
* to next element:
*/
else
continue;
} else {
/* Total order isn't satisfied (a > b), so return false: */
return false;
}
}
/* XXX Can this be unrolled in any reasonable way? */
/* Total (==) or weak (<) order was satisfied, so return true: */
return true;
}
}
/* XXX There is a better way to handle these with operator traits... */
CML_VEC_VEC_ORDER( total_order, operator==, et::OpEqual)
CML_VECXPR_VEC_ORDER( total_order, operator==, et::OpEqual)
CML_VEC_VECXPR_ORDER( total_order, operator==, et::OpEqual)
CML_VECXPR_VECXPR_ORDER( total_order, operator==, et::OpEqual)
CML_VEC_VEC_ORDER( weak_order, operator!=, et::OpNotEqual)
CML_VECXPR_VEC_ORDER( weak_order, operator!=, et::OpNotEqual)
CML_VEC_VECXPR_ORDER( weak_order, operator!=, et::OpNotEqual)
CML_VECXPR_VECXPR_ORDER( weak_order, operator!=, et::OpNotEqual)
CML_VEC_VEC_ORDER( weak_order, operator<, et::OpLess)
CML_VECXPR_VEC_ORDER( weak_order, operator<, et::OpLess)
CML_VEC_VECXPR_ORDER( weak_order, operator<, et::OpLess)
CML_VECXPR_VECXPR_ORDER( weak_order, operator<, et::OpLess)
CML_VEC_VEC_ORDER( weak_order, operator>, et::OpGreater)
CML_VECXPR_VEC_ORDER( weak_order, operator>, et::OpGreater)
CML_VEC_VECXPR_ORDER( weak_order, operator>, et::OpGreater)
CML_VECXPR_VECXPR_ORDER( weak_order, operator>, et::OpGreater)
CML_VEC_VEC_ORDER( total_order, operator<=, et::OpLessEqual)
CML_VECXPR_VEC_ORDER( total_order, operator<=, et::OpLessEqual)
CML_VEC_VECXPR_ORDER( total_order, operator<=, et::OpLessEqual)
CML_VECXPR_VECXPR_ORDER( total_order, operator<=, et::OpLessEqual)
CML_VEC_VEC_ORDER( total_order, operator>=, et::OpGreaterEqual)
CML_VECXPR_VEC_ORDER( total_order, operator>=, et::OpGreaterEqual)
CML_VEC_VECXPR_ORDER( total_order, operator>=, et::OpGreaterEqual)
CML_VECXPR_VECXPR_ORDER( total_order, operator>=, et::OpGreaterEqual)
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,458 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief Vector linear expression classes.
*/
#ifndef vector_expr_h
#define vector_expr_h
#include <cmath>
#include <cml/et/size_checking.h>
#include <cml/vector/vector_traits.h>
#include <cml/vector/vector_promotions.h>
/* XXX Don't know which it should be just yet, since RVO seems to obviate
* need for a reference type. However, copy by value copies the entire
* expression tree rooted at the VectorXpr<>, so this choice is bound to
* affect performace for some compiler or another:
*/
#define VECXPR_ARG_TYPE const et::VectorXpr<XprT>&
#define VECXPR_ARG_TYPE_N(_N_) const et::VectorXpr<XprT##_N_>&
//#define VECXPR_ARG_TYPE const et::VectorXpr<XprT>
//#define VECXPR_ARG_TYPE_N(_N_) const et::VectorXpr<XprT##_N_>
namespace cml {
namespace et {
/** A placeholder for a vector expression in an expression tree. */
template<class ExprT>
class VectorXpr
{
public:
typedef VectorXpr<ExprT> expr_type;
/* Record ary-ness of the expression: */
typedef typename ExprT::expr_ary expr_ary;
/* Copy the expression by value into higher-up expressions: */
typedef expr_type expr_const_reference;
typedef typename ExprT::value_type value_type;
typedef vector_result_tag result_tag;
typedef typename ExprT::size_tag size_tag;
/* Store the expression traits: */
typedef ExprTraits<ExprT> expr_traits;
/* Get the reference type: */
typedef typename expr_traits::const_reference expr_reference;
/* Get the result type: */
typedef typename expr_traits::result_type result_type;
/* For matching by assignability: */
typedef cml::et::not_assignable_tag assignable_tag;
/* Get the temporary type: */
typedef typename result_type::temporary_type temporary_type;
public:
/** Record result size as an enum. */
enum { array_size = ExprT::array_size };
public:
/** Return square of the length. */
value_type length_squared() const {
return m_expr.length_squared();
}
/** Return the length. */
value_type length() const {
return m_expr.length();
}
/** Return the result as a normalized vector. */
result_type normalize() const {
return m_expr.normalize();
}
/** Compute value at index i of the result vector. */
value_type operator[](size_t i) const {
return m_expr[i];
}
public:
/** Return size of this expression (same as subexpression's size). */
size_t size() const {
return m_expr.size();
}
/** Return reference to contained expression. */
expr_reference expression() const { return m_expr; }
public:
/** Construct from the subexpression to store. */
explicit VectorXpr(expr_reference expr) : m_expr(expr) {}
/** Copy constructor. */
VectorXpr(const expr_type& e) : m_expr(e.m_expr) {}
protected:
expr_reference m_expr;
private:
/* Cannot be assigned to: */
expr_type& operator=(const expr_type&);
};
/** Expression traits class for VectorXpr<>. */
template<class ExprT>
struct ExprTraits< VectorXpr<ExprT> >
{
typedef VectorXpr<ExprT> expr_type;
typedef ExprT arg_type;
typedef typename expr_type::value_type value_type;
typedef typename expr_type::expr_const_reference const_reference;
typedef typename expr_type::result_tag result_tag;
typedef typename expr_type::size_tag size_tag;
typedef typename expr_type::result_type result_type;
typedef typename expr_type::assignable_tag assignable_tag;
typedef expr_node_tag node_tag;
value_type get(const expr_type& v, size_t i) const { return v[i]; }
size_t size(const expr_type& e) const { return e.size(); }
};
/** A unary vector expression.
*
* The operator's operator() method must take exactly one argument.
*/
template<class ExprT, class OpT>
class UnaryVectorOp
{
public:
typedef UnaryVectorOp<ExprT,OpT> expr_type;
/* Record ary-ness of the expression: */
typedef unary_expression expr_ary;
/* Copy the expression by value into higher-up expressions: */
typedef expr_type expr_const_reference;
typedef typename OpT::value_type value_type;
typedef vector_result_tag result_tag;
typedef typename ExprT::size_tag size_tag;
/* Store the expression traits for the subexpression: */
typedef ExprTraits<ExprT> expr_traits;
/* Reference type for the subexpression: */
typedef typename expr_traits::const_reference expr_reference;
/* Get the result type (same as for subexpression): */
typedef typename expr_traits::result_type result_type;
/* For matching by assignability: */
typedef cml::et::not_assignable_tag assignable_tag;
/* Get the temporary type: */
typedef typename result_type::temporary_type temporary_type;
public:
/** Record result size as an enum. */
enum { array_size = ExprT::array_size };
public:
/** Return square of the length. */
value_type length_squared() const {
return dot(
VectorXpr<expr_type>(*this),
VectorXpr<expr_type>(*this));
}
/** Return the length. */
value_type length() const {
return std::sqrt(length_squared());
}
/** Return the result as a normalized vector. */
result_type normalize() const {
result_type v(VectorXpr<expr_type>(*this));
return v.normalize();
}
/** Compute value at index i of the result vector. */
value_type operator[](size_t i) const {
/* This uses the expression traits to figure out how to access the
* i'th index of the subexpression:
*/
return OpT().apply(expr_traits().get(m_expr,i));
}
public:
/** Return size of this expression (same as argument's size). */
size_t size() const {
return m_expr.size();
}
/** Return reference to contained expression. */
expr_reference expression() const { return m_expr; }
public:
/** Construct from the subexpression. */
explicit UnaryVectorOp(expr_reference expr) : m_expr(expr) {}
/** Copy constructor. */
UnaryVectorOp(const expr_type& e) : m_expr(e.m_expr) {}
protected:
expr_reference m_expr;
private:
/* Cannot be assigned to: */
expr_type& operator=(const expr_type&);
};
/** Expression traits class for UnaryVectorOp<>. */
template<class ExprT, class OpT>
struct ExprTraits< UnaryVectorOp<ExprT,OpT> >
{
typedef UnaryVectorOp<ExprT,OpT> expr_type;
typedef ExprT arg_type;
typedef typename expr_type::value_type value_type;
typedef typename expr_type::expr_const_reference const_reference;
typedef typename expr_type::result_tag result_tag;
typedef typename expr_type::size_tag size_tag;
typedef typename expr_type::result_type result_type;
typedef typename expr_type::assignable_tag assignable_tag;
typedef expr_node_tag node_tag;
value_type get(const expr_type& v, size_t i) const { return v[i]; }
size_t size(const expr_type& e) const { return e.size(); }
};
/** A binary vector expression.
*
* The operator's operator() method must take exactly two arguments.
*/
template<class LeftT, class RightT, class OpT>
class BinaryVectorOp
{
public:
typedef BinaryVectorOp<LeftT,RightT,OpT> expr_type;
/* Record ary-ness of the expression: */
typedef binary_expression expr_ary;
/* Copy the expression by value into higher-up expressions: */
typedef expr_type expr_const_reference;
typedef typename OpT::value_type value_type;
typedef vector_result_tag result_tag;
/* Store the expression traits types for the two subexpressions: */
typedef ExprTraits<LeftT> left_traits;
typedef ExprTraits<RightT> right_traits;
/* Reference types for the two subexpressions: */
typedef typename left_traits::const_reference left_reference;
typedef typename right_traits::const_reference right_reference;
/* Figure out the expression's resulting (vector) type: */
typedef typename left_traits::result_type left_result;
typedef typename right_traits::result_type right_result;
typedef typename VectorPromote<left_result,right_result>::type result_type;
typedef typename result_type::size_tag size_tag;
/* For matching by assignability: */
typedef cml::et::not_assignable_tag assignable_tag;
/* Get the temporary type: */
typedef typename result_type::temporary_type temporary_type;
/* Define a size checker: */
typedef GetCheckedSize<LeftT,RightT,size_tag> checked_size;
public:
/** Record result size as an enum (if applicable). */
enum { array_size = result_type::array_size };
public:
/** Return square of the length. */
value_type length_squared() const {
return dot(
VectorXpr<expr_type>(*this),
VectorXpr<expr_type>(*this));
}
/** Return the length. */
value_type length() const {
return std::sqrt(length_squared());
}
/** Return the result as a normalized vector. */
result_type normalize() const {
result_type v(VectorXpr<expr_type>(*this));
return v.normalize();
}
/** Compute value at index i of the result vector. */
value_type operator[](size_t i) const {
/* This uses the expression traits to figure out how to access the
* i'th index of the two subexpressions:
*/
return OpT().apply(
left_traits().get(m_left,i),
right_traits().get(m_right,i));
}
public:
/** Return the size of the vector result.
*
* @throws std::invalid_argument if the expressions do not have the same
* size.
*/
size_t size() const {
/* Note: This actually does a check only if
* CML_CHECK_VECTOR_EXPR_SIZES is set:
*/
return CheckedSize(m_left,m_right,size_tag());
}
/** Return reference to left expression. */
left_reference left_expression() const { return m_left; }
/** Return reference to right expression. */
right_reference right_expression() const { return m_right; }
public:
/** Construct from the two subexpressions. */
explicit BinaryVectorOp(left_reference left, right_reference right)
: m_left(left), m_right(right) {}
/** Copy constructor. */
BinaryVectorOp(const expr_type& e)
: m_left(e.m_left), m_right(e.m_right) {}
protected:
left_reference m_left;
right_reference m_right;
private:
/* This ensures that a compile-time size check is executed: */
typename checked_size::check_type _dummy;
private:
/* Cannot be assigned to: */
expr_type& operator=(const expr_type&);
};
/** Expression traits class for BinaryVectorOp<>. */
template<class LeftT, class RightT, class OpT>
struct ExprTraits< BinaryVectorOp<LeftT,RightT,OpT> >
{
typedef BinaryVectorOp<LeftT,RightT,OpT> expr_type;
typedef LeftT left_type;
typedef RightT right_type;
typedef typename expr_type::value_type value_type;
typedef typename expr_type::expr_const_reference const_reference;
typedef typename expr_type::result_tag result_tag;
typedef typename expr_type::size_tag size_tag;
typedef typename expr_type::result_type result_type;
typedef typename expr_type::assignable_tag assignable_tag;
typedef expr_node_tag node_tag;
value_type get(const expr_type& v, size_t i) const { return v[i]; }
size_t size(const expr_type& e) const { return e.size(); }
};
/* Helper struct to verify that both arguments are vector expressions: */
template<typename LeftTraits, typename RightTraits>
struct VectorExpressions
{
/* Require that both arguments are vector expressions: */
typedef typename LeftTraits::result_tag left_result;
typedef typename RightTraits::result_tag right_result;
enum { is_true = (same_type<left_result,et::vector_result_tag>::is_true
&& same_type<right_result,et::vector_result_tag>::is_true) };
};
namespace detail {
template<typename VecT, typename RT, typename MT> inline
void Resize(VecT&,size_t,RT,MT) {}
template<typename VecT> inline
void Resize(VecT& v, size_t S, resizable_tag, dynamic_memory_tag) {
v.resize(S);
}
template<typename VecT> inline
void Resize(VecT& v, size_t S) {
Resize(v, S, typename VecT::resizing_tag(), typename VecT::memory_tag());
}
} // namespace detail
} // namespace et
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,73 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief
*/
#ifndef vector_functions_h
#define vector_functions_h
namespace cml {
/** Squared length of a vector. */
template<typename E, class AT>
inline typename vector<E,AT>::value_type
length_squared(const vector<E,AT>& arg)
{
return arg.length_squared();
}
/** Squared length of a vector expr. */
template<typename XprT>
inline typename XprT::value_type
length_squared(VECXPR_ARG_TYPE arg)
{
return arg.length_squared();
}
/** Length of a vector. */
template<typename E, class AT>
inline typename vector<E,AT>::value_type
length(const vector<E,AT>& arg)
{
return arg.length();
}
/** Length of a vector expr. */
template<typename XprT>
inline typename XprT::value_type
length(VECXPR_ARG_TYPE arg)
{
return arg.length();
}
/** Normalize a vector. */
template<typename E, class AT>
inline vector<E, AT>
normalize(const vector<E,AT>& arg)
{
vector<E, AT> result(arg);
result.normalize();
return result;
}
/** Normalize a vector expr. */
template<typename XprT>
inline typename XprT::result_type
normalize(VECXPR_ARG_TYPE arg)
{
return arg.normalize();
}
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,51 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief Defines vector operators.
*/
#ifndef vector_ops_h
#define vector_ops_h
#include <cml/et/scalar_ops.h>
#include <cml/vector/vector_expr.h>
#include <cml/vector/vecop_macros.h>
namespace cml {
CML_VEC_UNIOP( operator+, et::OpPos)
CML_VECXPR_UNIOP( operator+, et::OpPos)
CML_VEC_UNIOP( operator-, et::OpNeg)
CML_VECXPR_UNIOP( operator-, et::OpNeg)
CML_VEC_VEC_BINOP( operator+, et::OpAdd)
CML_VECXPR_VEC_BINOP( operator+, et::OpAdd)
CML_VEC_VECXPR_BINOP( operator+, et::OpAdd)
CML_VECXPR_VECXPR_BINOP( operator+, et::OpAdd)
CML_VEC_VEC_BINOP( operator-, et::OpSub)
CML_VECXPR_VEC_BINOP( operator-, et::OpSub)
CML_VEC_VECXPR_BINOP( operator-, et::OpSub)
CML_VECXPR_VECXPR_BINOP( operator-, et::OpSub)
CML_VEC_SCALAR_BINOP( operator*, et::OpMul)
CML_SCALAR_VEC_BINOP( operator*, et::OpMul)
CML_VECXPR_SCALAR_BINOP( operator*, et::OpMul)
CML_SCALAR_VECXPR_BINOP( operator*, et::OpMul)
CML_VEC_SCALAR_BINOP( operator/, et::OpDiv)
CML_VECXPR_SCALAR_BINOP( operator/, et::OpDiv)
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,47 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief
*/
#ifndef vector_print_h
#define vector_print_h
#include <iostream>
namespace cml {
/** Output a vector to a std::ostream. */
template<typename E, class AT > inline std::ostream&
operator<<(std::ostream& os, const vector<E,AT>& v)
{
os << v[0];
for (size_t i = 1; i < v.size(); ++i) {
os << " " << v[i];
}
return os;
}
/** Output a vector expression to a std::ostream. */
template< class XprT > inline std::ostream&
operator<<(std::ostream& os, const et::VectorXpr<XprT>& v)
{
os << v[0];
for (size_t i = 1; i < v.size(); ++i) {
os << " " << v[i];
}
return os;
}
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,361 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief Defines vector dot and outer products.
*
* @todo Figure out if the source or destination size type should trigger
* unrolling. May need a per-compiler compile-time option for this.
*/
#ifndef vector_products_h
#define vector_products_h
#include <cml/core/cml_assert.h>
#include <cml/et/scalar_promotions.h>
#include <cml/et/size_checking.h>
#include <cml/vector/vector_unroller.h>
#include <cml/vector/vector_expr.h>
#include <cml/matrix/matrix_expr.h>
/* This is used below to create a more meaningful compile-time error when
* dot() is not provided with vector or VectorExpr arguments:
*/
struct dot_expects_vector_args_error;
/* This is used below to create a more meaningful compile-time error when
* perp_dot() is not provided with 2D vector or VectorExpr arguments:
*/
struct perp_dot_expects_vector_args_error;
struct perp_dot_expects_2D_vector_args_error;
/* This is used below to create a more meaningful compile-time error when
* outer() is not provided with vector or VectorExpr arguments:
*/
struct outer_expects_vector_args_error;
/* This is used below to create a more meaningful compile-time error when
* cross() is not provided with 3D vector or VectorExpr arguments:
*/
struct cross_expects_vector_args_error;
struct cross_expects_3D_vector_args_error;
namespace cml {
namespace detail {
template<typename LeftT, typename RightT>
struct DotPromote
{
/* Shorthand: */
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
typedef typename left_traits::value_type left_value;
typedef typename right_traits::value_type right_value;
/* Deduce the promoted scalar type: */
typedef et::OpMul<left_value, right_value> op_mul;
typedef typename et::OpAdd<
typename op_mul::value_type,
typename op_mul::value_type> op_add;
typedef typename op_add::value_type promoted_scalar;
};
template<typename LeftT, typename RightT>
struct CrossPromote
{
/* Shorthand: */
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
typedef typename left_traits::result_type left_type;
typedef typename right_traits::result_type right_type;
/* Deduce the matrix result type: */
typedef typename et::VectorPromote<
left_type,right_type>::temporary_type promoted_vector;
};
template<typename LeftT, typename RightT>
struct OuterPromote
{
/* Shorthand: */
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
typedef typename left_traits::result_type left_type;
typedef typename right_traits::result_type right_type;
/* Deduce the matrix result type: */
typedef typename et::MatrixPromote<
left_type,right_type>::temporary_type promoted_matrix;
};
/** Construct a dot unroller for fixed-size arrays.
*
* @note This should only be called for vectors.
*
* @sa cml::dot
*/
template<typename LeftT, typename RightT>
inline typename DotPromote<LeftT,RightT>::promoted_scalar
UnrollDot(const LeftT& left, const RightT& right, fixed_size_tag)
{
/* Shorthand: */
typedef DotPromote<LeftT,RightT> dot_helper;
/* Compile-type vector size check: */
typedef typename et::GetCheckedSize<LeftT,RightT,fixed_size_tag>
::check_type check_sizes;
/* Get the fixed array size using the helper: */
enum { Len = check_sizes::array_size };
/* Record the unroller type: */
typedef typename dot_helper::op_mul op_mul;
typedef typename dot_helper::op_add op_add;
typedef typename et::detail::VectorAccumulateUnroller<
op_add,op_mul,LeftT,RightT>::template
Eval<0, Len-1, (Len <= CML_VECTOR_DOT_UNROLL_LIMIT)> Unroller;
/* Note: Len is the array size, so Len-1 is the last element. */
/* Now, call the unroller: */
return Unroller()(left,right);
}
/** Use a loop to compute the dot product for dynamic arrays.
*
* @note This should only be called for vectors.
*
* @sa cml::dot
*/
template<typename LeftT, typename RightT>
inline typename DotPromote<LeftT,RightT>::promoted_scalar
UnrollDot(const LeftT& left, const RightT& right, dynamic_size_tag)
{
/* Shorthand: */
typedef DotPromote<LeftT,RightT> dot_helper;
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
typedef typename dot_helper::op_mul op_mul;
typedef typename dot_helper::op_add op_add;
/* Record the return type: */
typedef typename dot_helper::promoted_scalar sum_type;
/* Verify expression sizes: */
const size_t N = et::CheckedSize(left,right,dynamic_size_tag());
/* Initialize the sum. Left and right must be vector expressions, so
* it's okay to use array notation here:
*/
sum_type sum(op_mul().apply(left[0],right[0]));
for(size_t i = 1; i < N; ++i) {
/* XXX This might not be optimized properly by some compilers.
* but to do anything else requires changing the requirements
* of a scalar operator, or requires defining a new class of scalar
* <op>= operators.
*/
sum = op_add().apply(sum, op_mul().apply(left[i], right[i]));
/* Note: we don't need get(), since both arguments are required to
* be vector expressions.
*/
}
return sum;
}
/** For cross(): compile-time check for a 3D vector. */
template<typename VecT> inline void
Require3D(const VecT&, fixed_size_tag) {
CML_STATIC_REQUIRE_M(
((size_t)VecT::array_size == 3),
cross_expects_3D_vector_args_error);
}
/** For cross(): run-time check for a 3D vector. */
template<typename VecT> inline void
Require3D(const VecT& v, dynamic_size_tag) {
et::GetCheckedSize<VecT,VecT,dynamic_size_tag>()
.equal_or_fail(v.size(),size_t(3));
}
/** For perp_dot(): compile-time check for a 2D vector. */
template<typename VecT> inline void
Require2D(const VecT& v, fixed_size_tag) {
CML_STATIC_REQUIRE_M(
((size_t)VecT::array_size == 2),
perp_dot_expects_2D_vector_args_error);
}
/** For perp_dot(): run-time check for a 2D vector. */
template<typename VecT> inline void
Require2D(const VecT& v, dynamic_size_tag) {
et::GetCheckedSize<VecT,VecT,dynamic_size_tag>()
.equal_or_fail(v.size(),size_t(2));
}
} // namespace detail
/** Vector dot (inner) product implementation.
*/
template<typename LeftT, typename RightT>
inline typename detail::DotPromote<LeftT,RightT>::promoted_scalar
dot(const LeftT& left, const RightT& right)
{
/* Shorthand: */
typedef detail::DotPromote<LeftT,RightT> dot_helper;
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
typedef typename left_traits::result_type left_type;
typedef typename right_traits::result_type right_type;
typedef typename left_traits::size_tag left_size;
typedef typename right_traits::size_tag right_size;
/* dot() requires vector expressions: */
CML_STATIC_REQUIRE_M(
(et::VectorExpressions<LeftT,RightT>::is_true),
dot_expects_vector_args_error);
/* Note: parens are required here so that the preprocessor ignores the
* commas:
*/
/* Figure out the unroller to use (fixed or dynamic): */
typedef typename et::VectorPromote<
left_type, right_type>::temporary_type promoted_vector;
typedef typename promoted_vector::size_tag size_tag;
/* Call unroller: */
return detail::UnrollDot(left,right,size_tag());
}
/** perp_dot()
*/
template<typename LeftT, typename RightT>
inline typename detail::DotPromote<LeftT,RightT>::promoted_scalar
perp_dot(const LeftT& left, const RightT& right)
{
/* Shorthand: */
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
typedef typename left_traits::result_tag left_result;
typedef typename right_traits::result_tag right_result;
/* perp_dot() requires vector expressions: */
CML_STATIC_REQUIRE_M(
(same_type<left_result, et::vector_result_tag>::is_true
&& same_type<right_result, et::vector_result_tag>::is_true),
perp_dot_expects_vector_args_error);
/* Note: parens are required here so that the preprocessor ignores the
* commas.
*/
/* Make sure arguments are 2D vectors: */
detail::Require2D(left, typename left_traits::size_tag());
detail::Require2D(right, typename right_traits::size_tag());
/* Get result type: */
typedef typename detail::DotPromote<
LeftT,RightT>::promoted_scalar result_type;
/* Compute and return: */
return result_type(left[0]*right[1]-left[1]*right[0]);
}
template<typename LeftT, typename RightT>
inline typename detail::CrossPromote<LeftT,RightT>::promoted_vector
cross(const LeftT& left, const RightT& right)
{
/* Shorthand: */
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
typedef typename left_traits::result_tag left_result;
typedef typename right_traits::result_tag right_result;
/* outer() requires vector expressions: */
CML_STATIC_REQUIRE_M(
(same_type<left_result, et::vector_result_tag>::is_true
&& same_type<right_result, et::vector_result_tag>::is_true),
cross_expects_vector_args_error);
/* Note: parens are required here so that the preprocessor ignores the
* commas.
*/
/* Make sure arguments are 3D vectors: */
detail::Require3D(left, typename left_traits::size_tag());
detail::Require3D(right, typename right_traits::size_tag());
/* Get result type: */
typedef typename detail::CrossPromote<
LeftT,RightT>::promoted_vector result_type;
/* Now, compute and return the cross product: */
result_type result(
left[1]*right[2] - left[2]*right[1],
left[2]*right[0] - left[0]*right[2],
left[0]*right[1] - left[1]*right[0]
);
return result;
}
/** Return the triple product of three 3D vectors.
*
* No checking is done here, as dot() and cross() will catch any size or
* type errors.
*/
template < class VecT_1, class VecT_2, class VecT_3 >
typename detail::DotPromote<
VecT_1, typename detail::CrossPromote< VecT_2, VecT_3 >::promoted_vector
>::promoted_scalar
triple_product(const VecT_1& v1, const VecT_2& v2, const VecT_3& v3) {
return dot(v1,cross(v2,v3));
}
template<typename LeftT, typename RightT>
inline typename detail::OuterPromote<LeftT,RightT>::promoted_matrix
outer(const LeftT& left, const RightT& right)
{
/* Shorthand: */
typedef et::ExprTraits<LeftT> left_traits;
typedef et::ExprTraits<RightT> right_traits;
typedef typename left_traits::result_tag left_result;
typedef typename right_traits::result_tag right_result;
/* outer() requires vector expressions: */
CML_STATIC_REQUIRE_M(
(same_type<left_result, et::vector_result_tag>::is_true
&& same_type<right_result, et::vector_result_tag>::is_true),
dot_expects_vector_args_error);
/* Note: parens are required here so that the preprocessor ignores the
* commas.
*/
/* Create a matrix with the right size (resize() is a no-op for
* fixed-size matrices):
*/
typename detail::OuterPromote<LeftT,RightT>::promoted_matrix C;
cml::et::detail::Resize(C, left.size(), right.size());
/* Now, compute the outer product: */
for(size_t i = 0; i < left.size(); ++i) {
for(size_t j = 0; j < right.size(); ++j) {
C(i,j) = left[i]*right[j];
/* Note: both arguments are vectors, so array notation
* is okay here.
*/
}
}
return C;
}
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,77 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief
*
* Defines promotions for vectors used in vector/vector or vector/scalar
* expressions.
*
* @sa BinaryVectorOp
*/
#ifndef vector_promotions_h
#define vector_promotions_h
#include <cml/et/scalar_promotions.h>
#include <cml/et/array_promotions.h>
namespace cml {
namespace et {
/* Default vector type promotion template. */
template<class LeftT, class RightT> struct VectorPromote;
/** Type promotion for two vector types. */
template<typename E1, class AT1, typename E2, class AT2>
struct VectorPromote< cml::vector<E1,AT1>, cml::vector<E2,AT2> >
{
typedef typename ArrayPromote<
typename cml::vector<E1,AT1>::array_type,
typename cml::vector<E2,AT2>::array_type
>::type promoted_array;
/* The deduced vector result type: */
typedef cml::vector<
typename promoted_array::value_type,
typename promoted_array::generator_type
> type;
/* The deduced temporary type: */
typedef typename type::temporary_type temporary_type;
};
/** Type promotion for a vector and a scalar. */
template<typename E, class AT, typename S>
struct VectorPromote<cml::vector<E,AT>, S>
{
/* The deduced vector result type (the array type is the same): */
typedef cml::vector<typename ScalarPromote<E,S>::type, AT> type;
/* The deduced temporary type: */
typedef typename type::temporary_type temporary_type;
};
/** Type promotion for a scalar and a vector. */
template<typename S, typename E, class AT>
struct VectorPromote<S, cml::vector<E,AT> >
{
/* The deduced vector result type (the array type is the same): */
typedef cml::vector<typename ScalarPromote<S,E>::type, AT> type;
/* The deduced temporary type: */
typedef typename type::temporary_type temporary_type;
};
} // namespace et
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,47 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief
*/
#ifndef vector_traits_h
#define vector_traits_h
#include <cml/et/traits.h>
namespace cml {
namespace et {
/** Expression traits for a vector<> type. */
template<typename E, class AT>
struct ExprTraits< cml::vector<E,AT> >
{
typedef typename cml::vector<E,AT>::expr_type expr_type;
typedef typename expr_type::value_type value_type;
typedef typename expr_type::expr_reference reference;
typedef typename expr_type::expr_const_reference const_reference;
typedef typename expr_type::result_tag result_tag;
typedef typename expr_type::size_tag size_tag;
typedef typename expr_type::resizing_tag resizing_tag;
typedef typename expr_type::assignable_tag assignable_tag;
typedef expr_type result_type;
typedef expr_leaf_tag node_tag;
value_type get(const expr_type& v, size_t i) const { return v[i]; }
size_t size(const expr_type& v) const { return v.size(); }
};
} // namespace et
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp

View File

@@ -0,0 +1,259 @@
/* -*- C++ -*- ------------------------------------------------------------
Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
The Configurable Math Library (CML) is distributed under the terms of the
Boost Software License, v1.0 (see cml/LICENSE for details).
*-----------------------------------------------------------------------*/
/** @file
* @brief
*
* Defines vector unrollers.
*
* @todo Add unrolling for dynamic vectors, and for vectors longer than
* CML_VECTOR_UNROLL_LIMIT.
*
* @todo Does it make sense to unroll an assignment if either side of the
* assignment has a fixed size, or just when the target vector is fixed
* size?
*/
#ifndef vector_unroller_h
#define vector_unroller_h
#include <cml/et/traits.h>
#include <cml/et/size_checking.h>
#include <cml/et/scalar_ops.h>
#if !defined(CML_VECTOR_UNROLL_LIMIT)
#error "CML_VECTOR_UNROLL_LIMIT is undefined."
#endif
namespace cml {
namespace et {
namespace detail {
/** Unroll a binary assignment operator on a fixed-size vector.
*
* This uses forward iteration to make efficient use of the cache.
*
* @sa cml::vector
* @sa cml::et::OpAssign
*
* @bug Need to verify that OpT is actually an assignment operator.
*/
template<class OpT, typename E, class AT, class SrcT>
class VectorAssignmentUnroller
{
protected:
/* Forward declare: */
template<int N, int Last, bool can_unroll> struct Eval;
/* The vector type being assigned to: */
typedef cml::vector<E,AT> vector_type;
/* Record traits for the arguments: */
typedef ExprTraits<vector_type> dest_traits;
typedef ExprTraits<SrcT> src_traits;
/** Evaluate the binary operator for the first Len-1 elements. */
template<int N, int Last> struct Eval<N,Last,true> {
void operator()(vector_type& dest, const SrcT& src) const {
/* Apply to current N: */
OpT().apply(dest[N], src_traits().get(src,N));
/* Note: we don't need get(), since dest is a vector. */
/* Apply to N+1: */
Eval<N+1,Last,true>()(dest, src);
}
};
/** Evaluate the binary operator at element Last. */
template<int Last> struct Eval<Last,Last,true> {
void operator()(vector_type& dest, const SrcT& src) const {
/* Apply to last element: */
OpT().apply(dest[Last], src_traits().get(src,Last));
/* Note: we don't need get(), since dest is a vector. */
}
};
/** Evaluate the binary operator using a loop.
*
* This is used when the vector's length is longer than
* CML_VECTOR_UNROLL_LIMIT
*/
template<int N, int Last> struct Eval<N,Last,false> {
void operator()(vector_type& dest, const SrcT& src) const {
for(size_t i = 0; i <= Last; ++i) {
OpT().apply(dest[i], src_traits().get(src,i));
/* Note: we don't need get(), since dest is a vector. */
}
}
};
public:
/** Unroll assignment to a fixed-sized vector. */
void operator()(vector_type& dest, const SrcT& src, cml::fixed_size_tag)
{
typedef cml::vector<E,AT> vector_type;
enum { Len = vector_type::array_size };
typedef typename VectorAssignmentUnroller<OpT,E,AT,SrcT>::template
Eval<0, Len-1, (Len <= CML_VECTOR_UNROLL_LIMIT)> Unroller;
/* Note: Len is the array size, so Len-1 is the last element. */
/* Use a run-time check if src is a run-time sized expression: */
typedef typename ExprTraits<SrcT>::size_tag src_size;
typedef typename select_if<
same_type<src_size,dynamic_size_tag>::is_true,
dynamic_size_tag, fixed_size_tag>::result size_tag;
/* Check the expression size (the returned size isn't needed): */
CheckedSize(dest,src,size_tag());
/* Note: for two fixed-size expressions, the if-statements and
* comparisons should be completely eliminated as dead code. If src
* is a dynamic-sized expression, the check will still happen.
*/
/* Now, call the unroller: */
Unroller()(dest,src);
}
private:
/* XXX Blah, a temp. hack to fix the auto-resizing stuff below. */
size_t CheckOrResize(
vector_type& dest, const SrcT& src, cml::resizable_tag)
{
#if defined(CML_AUTOMATIC_VECTOR_RESIZE_ON_ASSIGNMENT)
/* Get the size of src. This also causes src to check its size: */
size_t N = std::max(dest.size(),src_traits().size(src));
/* Set the destination vector's size: */
cml::et::detail::Resize(dest,N);
#else
size_t N = CheckedSize(dest,src,dynamic_size_tag());
#endif
return N;
}
size_t CheckOrResize(
vector_type& dest, const SrcT& src, cml::not_resizable_tag)
{
return CheckedSize(dest,src,dynamic_size_tag());
}
/* XXX Blah, a temp. hack to fix the auto-resizing stuff below. */
public:
/** Just use a loop to assign to a runtime-sized vector. */
void operator()(vector_type& dest, const SrcT& src, cml::dynamic_size_tag)
{
/* Shorthand: */
typedef ExprTraits<SrcT> src_traits;
size_t N = this->CheckOrResize(
dest,src,typename vector_type::resizing_tag());
for(size_t i = 0; i < N; ++i) {
OpT().apply(dest[i], src_traits().get(src,i));
/* Note: we don't need get(), since dest is a vector. */
}
}
};
/** Unroll a vector accumulation/reduction operator.
*
* This uses forward iteration to make efficient use of the cache.
*/
template<class AccumT, class OpT, class LeftT, class RightT>
struct VectorAccumulateUnroller
{
/* Forward declare: */
template<int N, int Last, bool can_unroll> struct Eval;
/* Record traits for the arguments: */
typedef ExprTraits<LeftT> left_traits;
typedef ExprTraits<RightT> right_traits;
/* Figure out the return type: */
typedef typename AccumT::value_type result_type;
/** Evaluate for the first Len-1 elements. */
template<int N, int Last> struct Eval<N,Last,true> {
result_type operator()(
const LeftT& left, const RightT& right) const
{
/* Apply to last value: */
return AccumT().apply(
OpT().apply(left[N], right_traits().get(right,N)),
Eval<N+1,Last,true>()(left, right));
/* Note: we don't need get(), since dest is a vector. */
}
};
/** Evaluate the binary operator at element Last. */
template<int Last> struct Eval<Last,Last,true> {
result_type operator()(
const LeftT& left, const RightT& right) const
{
return OpT().apply(left[Last],right_traits().get(right,Last));
/* Note: we don't need get(), since dest is a vector. */
}
};
/** Evaluate using a loop. */
template<int N, int Last> struct Eval<N,Last,false> {
result_type operator()(
const LeftT& left, const RightT& right) const
{
result_type accum = OpT().apply(left[0],right[0]);
for(size_t i = 1; i <= Last; ++i) {
/* XXX This might not be optimized properly by some compilers,
* but to do anything else requires changing the requirements
* of a scalar operator.
*/
accum = AccumT().apply(accum, OpT().apply(
left[i],right_traits().get(right,i)));
/* Note: we don't need get(), since dest is a vector. */
}
}
};
};
}
/** Construct an assignment unroller.
*
* The operator must be an assignment op, otherwise, this doesn't make any
* sense.
*
* @bug Need to verify that OpT is actually an assignment operator.
*/
template<class OpT, class SrcT, typename E, class AT> inline
void UnrollAssignment(cml::vector<E,AT>& dest, const SrcT& src)
{
/* Record the destination vector type, and the expression traits: */
typedef cml::vector<E,AT> vector_type;
/* Record the type of the unroller: */
typedef detail::VectorAssignmentUnroller<OpT,E,AT,SrcT> unroller;
/* Do the unroll call: */
unroller()(dest, src, typename vector_type::size_tag());
/* XXX It may make sense to unroll if either side is a fixed size. */
}
} // namespace et
} // namespace cml
#endif
// -------------------------------------------------------------------------
// vim:ft=cpp