Boost logo

Boost :

From: Jeremy Siek (jsiek_at_[hidden])
Date: 2001-01-12 13:07:42


On Fri, 12 Jan 2001, Moore, Paul wrote:
paul.m> > "while (m != IntType())" is much better. Even better
paul.m> > would be for boost to have some sort of number_traits
paul.m> > class that defines one() and zero() functions.
paul.m> [snip]
paul.m> I think I'll assume that IntType(0) and IntType(1) are the zero and one for
paul.m> IntType. The only reason for using IntType(0) rather than IntType() is a
paul.m> sense that "explicit is better than implicit", and as I need IntType(1),
paul.m> there's no point in *not* using IntType(0).

Let's just take the plunge and do the right thing :)

To do this *really* right we need to define a concept such as Integer...
and while we're at it we ought to define concepts for the various other
kinds of numbers, such as floating point or reals. To be really generic,
we should start at the bottom, with the basic algebraic concepts, and then
build up from there. Below are some suggestions for some of these basic
algebraic types. Perhaps someone with more expertise in algebra could also
make some suggestions.

As for the one() and zero() functions, you'll see below I didn't use a
traits function, but instead used a free function that takes an argument
of the number type (the argument isn't really used, it just carries the
type). This interface has the benefit that it could even work for things
like matrices where one might need to create an identity element with the
correct number of rows and columns.

  // an additive Abelian group
  template <class X>
  struct AbelianGroupConcept {
    void constraints() {
      c = a + b;
      b += a;
      b = -a;
      c = a - b;
      b -= a;
      b = zero(a);
    }
    X a, b, c;
  };

  // really a ring with identity
  template <class X>
  struct RingConcept {
    void constraints() {
      function_requires< AbelianGroup<X> >();
      c = a * b;
      // leave out *=? doesn't work well with matrices
      // because retangular matrices are really rings, oh well
      b = identity(a);
    }
    X a, b, c;
  };
  
  template <class X>
  struct FieldConcept {
    void constraints() {
      function_requires< Ring<X> >();
      function_requires< EqualityComparable<X> >();
      function_requires< LessThanComparable<X> >();
      c = a / b;
      b /= a;
    }
    X a, b, c;
  };

----------------------------------------------------------------------
 Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
 Ph.D. Candidate email: jsiek_at_[hidden]
 Univ. of Notre Dame work phone: (219) 631-3906
----------------------------------------------------------------------


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk