toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Toon-members] TooN TooN.h matrix.h operators.h size_assert.h ...


From: Tom Drummond
Subject: [Toon-members] TooN TooN.h matrix.h operators.h size_assert.h ...
Date: Wed, 07 Jan 2009 14:23:53 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Tom Drummond <twd20>    09/01/07 14:23:53

Modified files:
        .              : TooN.h 
Added files:
        .              : matrix.h operators.h size_assert.h vector.h 

Log message:
        *very* partial TooN2 classes.  Vector nearly complete.  Matrix just 
being shaped

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/TooN.h?cvsroot=toon&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/TooN/matrix.h?cvsroot=toon&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/TooN/operators.h?cvsroot=toon&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/TooN/size_assert.h?cvsroot=toon&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/TooN/vector.h?cvsroot=toon&rev=1.1

Patches:
Index: TooN.h
===================================================================
RCS file: /cvsroot/toon/TooN/TooN.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- TooN.h      12 Dec 2007 15:59:43 -0000      1.10
+++ TooN.h      7 Jan 2009 14:23:52 -0000       1.11
@@ -1,218 +1,4 @@
+//-*- c++ -*-
 
-/*                       
-        Copyright (C) 2005 Tom Drummond
-
-     This library is free software; you can redistribute it and/or
-     modify it under the terms of the GNU Lesser General Public
-     License as published by the Free Software Foundation; either
-     version 2.1 of the License, or (at your option) any later version.
-
-     This library is distributed in the hope that it will be useful,
-     but WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     Lesser General Public License for more details.
-
-     You should have received a copy of the GNU Lesser General Public
-     License along with this library; if not, write to the Free Software
-     Foundation, Inc.
-     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-*/
-#ifndef __TOON_H
-#define __TOON_H
-
-#ifdef WIN32 // to get M_PI, etc.
-#define _USE_MATH_DEFINES   
-#endif
-
-#include <string.h>  // for memcpy
-#include <cmath>    // for sqrt
-#include <iomanip>
-#include <cassert>
-#include <iostream> // for input and output of vectors and matrices
-
-#include <TooN/util.h>
-#ifndef TOON_NO_NAMESPACE
-namespace TooN {
-#endif
-
-static const int General=-1;
-
-
-// actual values are needed in transpose operation
-// which does nothing except return a ref to
-// a matrix with the opposite layout
-//enum{RowMajor=0,ColMajor=1};
-
-
-// just placeholders
-struct RowMajor{
-  static bool is_rowmajor(){return true;}
-};
-struct ColMajor{
-  static bool is_rowmajor(){return false;}
-};
-
-struct NUMERICS {
-  // enum{Stack,Heap}; // allocation zone for fixed size stuff
-  enum{Owner,Reference}; // ownership of variable size stuff
-
-  // static const int DefaultLayout = RowMajor;
-
-  typedef RowMajor DefaultLayout;
-
-  // multiplication policy
-  enum{BlasMult,CPPMult};
-
-  // maximum no of mults in a matrix/vector * matrix/vector
-  // product where we use inline c++
-  // above this number we switch to using BLAS
-  static const int MaxCPPMultCount = 100;
-
-  // maximum no of doubles in an object before
-  // we put it on the heap instead of the stack
-  static const int MaxStackSize=100;  // ie 10x10 matrix
-};
-
-
-// forward declarations of all needed classes
-
-
-/////////////// Memory Management //////////////////
-
-// Fixed Size Memory Access
-template<int Size>
-class Stack;
-
-template<int Size>
-class Heap;
-
-//////////// Vectors ///////////////////
-
-template <int Size, class AllocZone>
-class FixedVAccessor;
-
-template <int Size, int Skip>
-class SkipAccessor;
-
-class DynamicVAccessor;
-class RefSkipAccessor;
-
-template <class Accessor>
-class VectorBase;
-
-template <int Size, class Accessor>
-class FixedVector;
-
-template <class Accessor>
-class DynamicVector;
-
-//class RefVector;
-//class ConstRefVector;
-//class RefSkipVector;
-//class ConstRefSkipVector;
-
-template <int Size=General>
-class Vector;
-
-
-/////////// Matrices ////////////
-
-template <int Rows, int Cols, class Layout, class AllocZone>
-class FixedMAccessor;
-
-template<int Rows, int Cols, int Skip, class Layout>
-class SkipMAccessor;
-
-template<class Layout>
-class DynamicMAccessor;
-
-template <class Layout>
-class RefSkipMAccessor;
-
-template <class Accessor>
-class MatrixBase;
-
-template<int Rows, int Cols, class Accessor>
-class FixedMatrix;
-
-template<class Accessor>
-class DynamicMatrix;
-
-template<class Layout>
-class RefMatrix;
-template<class Layout>
-class ConstRefMatrix;
-
-template<class Layout>
-class RefSkipMatrix;
-template<class Layout>
-class ConstRefSkipMatrix;
-
-template <int Rows=General,
-          int Cols=Rows,
-          class Layout = typename NUMERICS::DefaultLayout >
-class Matrix; 
-
-
-// never actually use one of these
-// they're just used as dummy arguments
-// to destinguish between templated constructors
-// most compilers should be able to optimise them out
-template <class T>
-class Operator {};  
-
-template <int Size, int I>
-class ZoneHandler;
-
-template<int Size>
-class ZoneHandler<Size,0> {
-public:
-  typedef Stack<Size> get_zone;
-};
-
-template<int Size>
-class ZoneHandler<Size,1> {
-public:
-  typedef Heap<Size> get_zone;
-};
-
-template <int Size>
-class SizeTraits : public ZoneHandler<Size,(Size > NUMERICS::MaxStackSize ? 1 
: 0)> {
-};
-
-
-#ifdef TOON_DEBUG
-  #define TOON_ASSERT(X,Y) if(!(X)) throw Y()
-  #define TOON_THROW
-  #include <TooN/accessorexceptions.hh>
-#else
-  #define TOON_ASSERT(X,Y)
-  #define TOON_THROW throw()
-#endif
-
-#include <TooN/vmagic.hh>
-
-#include <TooN/membase.hh>
-#include <TooN/vbase.hh>
-#include <TooN/vaccessor.hh>
-#include <TooN/mbase.hh>
-#include <TooN/maccessor.hh>
-#include <TooN/vclasses.hh>
-#include <TooN/mclasses.hh>
-#include <TooN/blasoperators.hh>
-#include <TooN/linoperators.hh>
-
- namespace util {
-#include <TooN/generated.h>
- }
-
-#ifndef TOON_NO_NAMESPACE
-}
-#endif
-
-#ifdef TOON_USING_NAMESPACE
-       using namespace TooN;
-#endif
-
-#endif
-
+#include <vector.h>
+#include <operators.h>

Index: matrix.h
===================================================================
RCS file: matrix.h
diff -N matrix.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ matrix.h    7 Jan 2009 14:23:52 -0000       1.1
@@ -0,0 +1,87 @@
+// -*- c++ -*-
+
+
+
+
+
+
+
+
+template<int Rows, int Cols>
+class MatrixAllocator{
+
+
+
+
+};
+
+
+
+
+
+
+template<int Rows, int Cols>
+struct RowMajor{
+
+  // has to handle operator[row] and operator(row,col)
+
+  Vector<Cols,SDVBase<Cols,1> > operator[](int row){
+    return Vector<Cols,SDVBase<Cols,1> >(my_data+Cols*row);
+  }
+
+  const Vector<Cols,SDVBase<Cols,1> > operator[](int row) const {
+    return Vector<Cols,SDVBase<Cols,1> >(my_data+Cols*row);
+  }
+
+  double& operator()(int row, int col){
+    return my_data[row*Cols+col];
+  }
+  const double& operator()(int row, int col) const {
+    return my_data[row*Cols+col];
+  }
+};
+
+template<int Rows, int Cols>
+struct ColMajor {
+
+  // has to handle operator[row] and operator(row,col)
+
+};
+
+
+
+template<int Stride>
+struct StrideM {
+  template <int Rows, int Cols>
+  struct RowMajor{
+
+  };
+  template <int Rows, int Cols>
+  struct ColMajor{
+
+
+  };
+};
+
+
+
+template <int Stride>
+struct MStride {
+  template <int Rows, int Cols>
+  struct RowMajor{
+    // in here goes the maths to compute Matrix(r,c) etc.
+  };
+  struct ColMajor{
+    // in here goes the maths to compute Matrix(r,c) etc.
+  };
+};
+
+
+// can now make a matrix of form
+// Matrix<Rows, Cols, MStride<Stride>::RowMajor>
+
+
+
+
+template <int Rows, int Cols, template<int R, int C> Layout = RowMajor>
+class Matrix : public Layout<Rows, Cols>;

Index: operators.h
===================================================================
RCS file: operators.h
diff -N operators.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ operators.h 7 Jan 2009 14:23:52 -0000       1.1
@@ -0,0 +1,71 @@
+//-*- c++ -*-
+
+//////////////////////////////////////////////////////////////////////////////////////////////
+//                                       Operators
+//////////////////////////////////////////////////////////////////////////////////////////////
+
+
+// Dot product Vector * Vector
+template<int Size1, typename Base1, int Size2, typename Base2>
+double operator*(const Vector<Size1, Base1>& v1, const Vector<Size2, Base2>& 
v2){
+  SizeMismatch<Size1, Size2>:: test(v1.size(),v2.size());
+  const int s=v1.size();
+  double result=0;
+  for(int i=0; i<s; i++){
+    result+=v1[i]*v2[i];
+  }
+  return result;
+}
+
+// operator += Vector
+template<int Size1, typename Base1, int Size2, typename Base2>
+Vector<Size1, Base1>& operator += (Vector<Size1, Base1>& lhs, const 
Vector<Size2, Base2>& rhs){
+  SizeMismatch<Size1,Size2>::test(lhs.size(),rhs.size());
+  const int s=lhs.size();
+  for(int i=0; i<s; i++){
+    lhs[i]+=rhs[i];
+  }
+  return lhs;
+}
+
+// operator -= Vector
+template<int Size1, typename Base1, int Size2, typename Base2>
+Vector<Size1, Base1>& operator -= (Vector<Size1, Base1>& lhs, const 
Vector<Size2, Base2>& rhs){
+  SizeMismatch<Size1,Size2>::test(lhs.size(),rhs.size());
+  const int s=lhs.size();
+  for(int i=0; i<s; i++){
+    lhs[i]-=rhs[i];
+  }
+  return lhs;
+}
+
+// operator *= double
+template<int Size, typename Base>
+Vector<Size, Base>& operator *= (Vector<Size, Base>& lhs, double rhs){
+  const int s=lhs.size();
+  for(int i=0; i<s; i++){
+    lhs[i]*=rhs;
+  }
+  return lhs;
+}
+
+// operator /= double
+template<int Size, typename Base>
+Vector<Size, Base>& operator /= (Vector<Size, Base>& lhs, double rhs){
+  const int s=lhs.size();
+  for(int i=0; i<s; i++){
+    lhs[i]/=rhs;
+  }
+  return lhs;
+}
+
+// output operator <<
+template <int Size, typename Base>
+inline std::ostream& operator<< (std::ostream& os, const Vector<Size,Base>& v){
+  for(int i=0; i<v.size(); i++){
+    os << v[i] << " ";
+  }
+  return os;
+}
+
+

Index: size_assert.h
===================================================================
RCS file: size_assert.h
diff -N size_assert.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ size_assert.h       7 Jan 2009 14:23:52 -0000       1.1
@@ -0,0 +1,45 @@
+// -*- c++ -*-
+
+#include<iostream>
+
+// class to generate compile time error
+// general case which doesn't exist
+template<int Size1, int Size2>
+struct SizeMismatch;
+
+// special cases which do exist
+template<int Size>
+struct SizeMismatch<Size,Size>{
+  static inline void test(int size1, int size2){}
+};
+
+template<int Size>
+struct SizeMismatch<-1,Size>{
+  static inline void test(int size1, int size2){
+    if(size1!=size2){
+      std::cerr << "Toon Size Mismatch" << std::endl;
+      abort();
+    }
+  }
+};
+
+template<int Size>
+struct SizeMismatch<Size,-1>{
+  static inline void test(int size1, int size2){
+    if(size1!=size2){
+      std::cerr << "Toon Size Mismatch" << std::endl;
+      abort();
+    }
+  }
+};
+
+template <>
+struct SizeMismatch<-1,-1>{
+  static inline void test(int size1, int size2){
+    if(size1!=size2){
+      std::cerr << "Toon Size Mismatch" << std::endl;
+      abort();
+    }
+  }
+};
+

Index: vector.h
===================================================================
RCS file: vector.h
diff -N vector.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ vector.h    7 Jan 2009 14:23:53 -0000       1.1
@@ -0,0 +1,344 @@
+//-*- c++ -*-
+#ifndef TOON_VECTOR_H
+#define TOON_VECTOR_H
+
+#include "size_assert.h"
+
+// forward declarations
+template<int Size, typename Base>
+class Vector;
+
+template<int Size, int Stride>
+class SVBase;
+
+// forward declaration
+template<typename T>
+class Operator;
+
+// VBase owns its data
+// Size is the static number of elements in the vector
+// Stride is the static gap between elements
+// Type is a hack so that x?y:z expressions work
+// Type=0 means the data is internal to the object
+// Type=1 means the data is external to the object
+template <int Size, int Type>
+class VBase;
+
+
+template<int Size>
+class VBase<Size, 0> {
+public:
+  inline VBase(){}
+  inline VBase(const VBase& from){}
+  
+  // construction from 1-ary operator
+  template <class T, class Op>
+  inline VBase(const T&, const Operator<Op>&){}
+
+  // constructor from 2-ary operator
+  template <class LHS, class RHS, class Op>
+  inline VBase(const LHS& lhs, const RHS& rhs, const Operator<Op>&){}
+  
+  // constructor from arbitrary vector
+  template<int Size2, class Base2>
+  inline VBase(const Vector<Size2,Base2>& from){}
+
+  double* data(){return my_data;}
+  const double* data() const {return my_data;}
+  
+  static int size(){return Size;}
+  static int stride(){return 1;}
+
+  double& operator[](int i){
+    return my_data[i];
+  }
+  const double& operator[](int i) const {
+    return my_data[i];
+  }
+
+  template <int Start, int Length>
+  Vector<Length, SVBase<Length,1> >
+  slice(){
+    return Vector<Length, SVBase<Length,1> >(&(my_data[Start]));
+  }
+
+
+private:
+  double my_data[Size];
+};
+
+template<int Size>
+class VBase<Size,1>{
+public:
+  VBase()
+    : my_data(new double[Size]){
+  }
+
+  VBase(const VBase& from)
+    : my_data(new double[Size]){
+    }
+
+  VBase(int size_in)
+    : my_data(new double[Size]){
+  }
+
+  // construction from 1-ary operator
+  template <class T, class Op>
+  inline VBase(const T&, const Operator<Op>&):
+    my_data(new double[Size]){}
+
+  // constructor from 2-ary operator
+  template <class LHS, class RHS, class Op>
+  inline VBase(const LHS& lhs, const RHS& rhs, const Operator<Op>&):
+    my_data(new double[Size]){}
+
+  // constructor from arbitrary vector
+  template<int Size2, class Base2>
+  inline VBase(const Vector<Size2,Base2>& from):
+    my_data(new double[from.size()]) {}
+
+  ~VBase(){
+    delete[] my_data;
+  }
+
+  double* data(){return my_data;}
+  const double* data() const {return my_data;}
+  
+  static int size(){return Size;}
+  static int stride(){return 1;}
+
+  double& operator[](int i){
+    return my_data[i];
+  }
+  const double& operator[](int i) const {
+    return my_data[i];
+  }
+
+  template <int Start, int Length>
+  Vector<Length, SVBase<Length,1> >
+  slice(){
+    return Vector<Length, SVBase<Length,1> >(&(my_data[Start]));
+  }
+
+private:
+  double* const my_data;
+};
+
+
+// SVBase does not own its data
+// and has a template stride parameter
+template<int Size, int Stride>
+class SVBase{
+public:
+  SVBase(double* data_in):
+    my_data(data_in){
+  }
+
+  SVBase(const SVBase& from):
+    my_data(from.my_data){
+  }
+
+  double* data(){return my_data;}
+  const double* data() const {return my_data;}
+  
+  int size() const {return Size;}
+  int stride() const {return Stride;}
+
+  double& operator[](int i){
+    return my_data[i*Stride];
+  }
+  const double& operator[](int i) const {
+    return my_data[i*Stride];
+  }
+
+  template <int Start, int Length>
+  Vector<Length, SVBase<Length,Stride> >
+  slice(){
+    return Vector<Length, SVBase<Length,1> >(&(my_data[Start*Stride]));
+  }
+
+private:
+  double* const my_data;
+};
+
+
+
+
+// DVBase is for vectors whose size is determined dynamically at runtime
+// They own their data
+class DVBase{
+public:
+  DVBase(int size_in):
+    my_data(new double[size_in]),
+    my_size(size_in){
+  }
+
+  DVBase(const DVBase& from):
+    my_data(new double[from.my_size]),
+    my_size(from.my_size) {
+  }
+
+  // construction from 1-ary operator
+  template <class T, class Op>
+  inline DVBase(const T& arg, const Operator<Op>&):
+    my_data(new double[Op::size(arg)]),
+    my_size(Op::size(arg)) {
+  }
+
+  // constructor from 2-ary operator
+  template <class LHS, class RHS, class Op>
+  inline DVBase(const LHS& lhs, const RHS& rhs, const Operator<Op>&):
+    my_data(new double[Op::size(lhs,rhs)]),
+    my_size(Op::size(lhs,rhs)) {
+  }
+
+  // constructor from arbitrary vector
+  template<int Size2, class Base2>
+  inline DVBase(const Vector<Size2,Base2>& from):
+    my_data(new double[from.size()]),
+    my_size(from.size()) {
+  }
+
+  ~DVBase(){
+    delete[] my_data;
+  }
+
+  double* data(){return my_data;}
+  const double* data() const {return my_data;}
+  
+  int size() const {return my_size;}
+  int stride() const {return 1;}
+
+  double& operator[](int i){
+    return my_data[i];
+  }
+  const double& operator[](int i) const {
+    return my_data[i];
+  }
+private:
+  double* const my_data;
+  int my_size;
+};
+
+// SDVBase is for dynamically sized vectors that do not own their data
+// They have an additional stride member
+class SDVBase{
+public:
+  SDVBase(double* data_in, int size_in, int stride_in):
+    my_data(data_in) {
+    my_size=size_in;
+    my_stride=stride_in;
+  };
+
+  SDVBase(const SDVBase& from)
+    : my_data(from.my_data),
+      my_size(from.my_size),
+      my_stride(from.my_stride){
+  }
+
+  ~SDVBase(){
+    delete[] my_data;
+  }
+
+  double* data(){return my_data;}
+  const double* data() const {return my_data;}
+  
+  int size() const {return my_size;}
+  int stride() const {return my_stride;}
+
+  double& operator[](int i){
+    return my_data[i];
+  }
+  const double& operator[](int i) const {
+    return my_data[i];
+  }
+private:
+  double* const my_data;
+  int my_size;
+  int my_stride;
+};
+
+// traits classes that help with building the vectors you actually
+// construct in code
+static const int MAX_SIZE=10;
+
+template<int Size>
+struct VectorSelector{
+  typedef VBase<Size, (Size>MAX_SIZE)?1:0 > Type;
+};
+
+template<>
+struct VectorSelector<-1>{
+  typedef DVBase Type;
+};
+
+
+////////////////////////////////////////////////////////////////////////////////
+//               The actual Vector classes
+////////////////////////////////////////////////////////////////////////////////
+
+
+
+template<int Size=-1, typename Base= typename VectorSelector<Size>::Type>
+class Vector : public Base {
+public:
+  // sneaky hack: only one of these constructors will work with any given base
+  // class but they don't generate errors unless the user tries to use one of 
them
+  // although the error message may be less than helpful - maybe this can be 
changed?
+  inline Vector(){}
+  inline Vector(double* data) : Base (data) {}
+  inline Vector(int size_in) : Base(size_in) {}
+  inline Vector(double* data_in, int size_in, int stride_in) : Base(data_in, 
size_in, stride_in) {}
+
+
+  // constructors to allow return value optimisations
+  // construction from 1-ary operator
+  template <class T, class Op>
+  inline Vector(const T& arg, const Operator<Op>& op) : Base(arg,op) {
+    Op::eval(*this,arg);
+  }
+
+  // constructor from 2-ary operator
+  template <class LHS, class RHS, class Op>
+  inline Vector(const LHS& lhs, const RHS& rhs, const Operator<Op>& op)
+    : Base(lhs,rhs,op) {
+    Op::eval(*this,lhs,rhs);
+  }
+
+  // copy constructor listed explicitly
+  inline Vector(const Vector<Size,Base>& from)
+    : Base(from) {
+    (*this)=from;
+  }
+
+  // constructor from arbitrary vector
+  template<int Size2, class Base2>
+  inline Vector(const Vector<Size2,Base2>& from):
+    Base(from) {
+    operator=(from);
+  }
+
+  // operator = from copy
+  inline Vector<Size,Base >& operator= (const Vector& from){
+    SizeMismatch<Size,Size>::test(Base::size(), from.size());
+    const int s=Base::size();
+    for(int i=0; i<s; i++){
+      (*this)[i]=from[i];
+    }
+    return *this;
+  }
+
+  // operator =
+  template<int Size2, typename Base2>
+  Vector<Size,Base >& operator= (const Vector<Size2, Base2>& from){
+    SizeMismatch<Size,Size2>::test(Base::size(), from.size());
+    const int s=Base::size();
+    for(int i=0; i<s; i++){
+      (*this)[i]=from[i];
+    }
+    return *this;
+  }
+
+};
+
+#endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]