Patrick Kowalzick wrote:

Dear Steven,

I am a little bit scared not to answer your question, and then I will get

a harsh response.

Did you think about something like this (avoiding your problem)?

template <typename T, size_t dim>

struct IDM

{

static size_t size1() { return dim; }

static size_t size2() { return dim; }

T operator[](const size_t & index1, const size_t & index2) const

{

// range checking if needed

if (index1 == index2) return 1;

else return 0;

}

};

Yes. I thought about it. I even wrote the code, and then realized it won't

serve my purposes. I can't pass it as an array, and I'm not sure it will

have the same performance as using an actual array. I did come up with /a/

solution. It's not my idea of elegant, but it seems to work.

Java has something called static member initialization blocks which allow

you to execute code the first time a class is loaded. I don't know if that

could be added to C++, but it sure is nice. Then again, I don't believ you

can make the members of a Java array constant. If you can access the array,

you can modify it.

This is not a finished product, just a rough draft:

/************************************************** *************************

* Copyright (C) 2004 by Steven T. Hatton *

*

ha*****@globalsymmetry.com *

* *

* This program is free software; you can redistribute it and/or modify *

* it under the terms of the GNU General Public License as published by *

* the Free Software Foundation; either version 2 of the License, or *

* (at your option) any later version. *

* *

* This program 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 General Public License for more details. *

* *

* You should have received a copy of the GNU General Public License *

* along with this program; if not, write to the *

* Free Software Foundation, Inc., *

* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *

************************************************** *************************/

#ifndef TENSOR_HH

#define TENSOR_HH

#include "sth/util/Printable_IF.hh" // ABC: print() and operator<<(&ostream)

#include "vmath_impl.hh" // namespace_name

namespace sth{

namespace vmath{

namespace {

using sth::util::Printable_IF;

using std::string;

using std::ostream;

}

template <typename T, unsigned ORDER>

class IDM{

T _m[ORDER][ORDER];

public:

typedef const T (&ref)[ORDER][ORDER];

IDM()

:ID(_m)

{

for(unsigned i = 0; i < ORDER; i++)

{

for(unsigned j = 0; j < ORDER; j++)

{

_m[i][j] = i == j ? T(1): T(0);

}

}

}

ref ID;

};

template<typename T, unsigned RANK, unsigned ORDER>

class Tensor: public Printable_IF {

public:

typedef const T (&ref)[ORDER][ORDER];

static const string class_name;

static const unsigned _RANK = RANK;

static const unsigned _ORDER = ORDER;

Tensor()

{}

virtual ostream& print(ostream& out) const;

protected:

static const ref _ID;

static const IDM<T, ORDER> _IDM;

};

template<typename T, unsigned RANK, unsigned ORDER>

const string Tensor<T, RANK, ORDER>::class_name = "Tensor<T,RANK,

ORDER>";

template<typename T, unsigned RANK, unsigned ORDER>

const IDM<T, ORDER> Tensor<T, RANK, ORDER>::_IDM = IDM<T, ORDER>();

template<typename T, unsigned RANK, unsigned ORDER>

const T (&Tensor<T, RANK, ORDER>::_ID)[ORDER][ORDER] = Tensor<T, RANK,

ORDER>::_IDM.ID;

template<typename T, unsigned RANK, unsigned ORDER>

ostream& Tensor<T, RANK, ORDER>::print(ostream& out) const

{

return out << namespace_name << "::" << class_name << "\n";

}

}

}

#endif

--

"If our hypothesis is about anything and not about some one or more

particular things, then our deductions constitute mathematics. Thus

mathematics may be defined as the subject in which we never know what we

are talking about, nor whether what we are saying is true." - Bertrand

Russell