Skip to content
Snippets Groups Projects
Commit 8a1258de authored by Oliver Sander's avatar Oliver Sander
Browse files

Kleinere Aufräumarbeiten. FixedArray hat jetzt ein eigenes

File, ebenso Vec, daß sich in fixedvector.hh befindet.
Außerdem erbt Vec jetzt von FixedArray.  Leider sind
einige Änderungen nicht rückwärtskompatibel.  Insbesondere
fehlen Vec::print (man nehme stattdessen operator<<)
und Vec::read (man nehme const T& operator[])
Entschuldigt die Unannehmlichkeiten.

[[Imported from SVN: r503]]
parent 8d9e227e
Branches
Tags
No related merge requests found
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef __DUNE_FIXEDARRAY_HH
#define __DUNE_FIXEDARRAY_HH
//***********************************************************************
//
// implementation of peter array
//
//***********************************************************************
#include <iostream>
#include <iomanip>
#include <string>
namespace Dune
{
/** @addtogroup Common
@{
*/
/** \brief Simple fixed size array class
*
*/
template<class T, int N>
class FixedArray {
public:
//! Remember the storage type
typedef T MemberType;
//! The actual number of elements that gets allocated.
//! It's always at least 1.
enum { n = (N > 0) ? N : 1 };
//! Know your own length
enum { dimension = N };
//! Create uninitialized array
FixedArray () {}
//! Initialize all components with same size
FixedArray (T t)
{
for (int i=0; i<N; i++) a[i]=t;
}
//! Assign value to all entries
FixedArray<T,N>& operator= (const T& t)
{
for (int i=0; i<N; i++) a[i]=t;
return (*this);
}
//! Component access
T& operator[] (int i)
{
return a[i];
}
//! Const component access
const T& operator[] (int i) const
{
return a[i];
}
//! \todo Please doc me!
FixedArray<T,N-1> shrink (int comp)
{
FixedArray<T,N-1> x;
for (int i=0; i<comp; i++) x[i] = a[i];
for (int i=comp+1; i<N; i++) x[i-1] = a[i];
return x;
}
//! \todo Please doc me!
FixedArray<T,N+1> expand (int comp, T value)
{
FixedArray<T,N+1> x;
for (int i=0; i<comp; i++) x[i] = a[i];
x[comp] = value;
for (int i=comp+1; i<N+1; i++) x[i] = a[i-1];
return x;
}
protected:
T a[n];
};
//! Output operator for FixedArray
template <class T, int N>
inline std::ostream& operator<< (std::ostream& s, FixedArray<T,N> e)
{
s << "[";
for (int i=0; i<N-1; i++) s << e[i] << ",";
s << e[N-1] << "]";
return s;
}
/** @} */
} // end namespace Dune
#endif
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef __DUNE_FIXEDVECTOR_HH__
#define __DUNE_FIXEDVECTOR_HH__
#include <iostream>
#include <math.h>
#include "misc.hh"
#include "fixedarray.hh"
namespace Dune {
/** @defgroup Common Dune Common Module
This module contains classes for general usage in dune, such as e.g.
(small) dense matrices and vectors or containers.
@{
*/
//************************************************************************
/*!
Generic vector class for short vectors in d dimensions. Used e.g.
for global or local coordinates.
*/
template<int dim, class T = double>
class Vec : public FixedArray<T, dim> {
public:
//! Constructor making uninitialized vector
Vec() {}
//! Constructor making vector from built-in array
Vec (T* y) {for(int i=0; i<n; i++) x[i]=y[i];}
//! Constructor making vector with one coordinate set, others zeroed
Vec (int k, T t)
{
for (int i=0; i<n; i++) a[i] = 0;
a[k] = t;
}
//! Constructor making vector with identical coordinates
Vec (T t)
{
for (int i=0; i<n; i++) a[i] = t;
}
//! operator () for read/write access to element of the vector
T& operator() (int i) {return a[i];}
//! operator () for read access to element of the vector
const T& operator() (int i) const {return a[i];}
//! operator+ adds two vectors
Vec<n,T>& operator+= (const Vec<n,T>& b)
{
for (int i=0; i<n; i++) a[i] += b.a[i];
return *this;
}
//! Vector addition
Vec<n,T> operator+ (const Vec<n,T>& b) const
{
Vec<n,T> z = *this;
return (z+=b);
}
//! operator- binary minus
Vec<n,T>& operator-= (const Vec<n,T>& b)
{
for (int i=0; i<n; i++) a[i] -= b.a[i];
return *this;
}
//! Vector subtraction
Vec<n,T> operator- (const Vec<n,T>& b) const
{
Vec<n,T> z = *this;
return (z-=b);
}
//! scalar product of two vectors with operator*
T operator* (const Vec<n,T>& b) const
{
T s=0; for (int i=0; i<n; i++) s += a[i]*b.a[i];return s;
}
//! multiplication of vector with scalar
T operator* (T k) const
{
Vec<n,T> z; for (int i=0; i<n; i++) z.a[i] = k*a[i];return z;
}
//! multiplication assignment with scalar
Vec<n,T>& operator*= (T k)
{
for (int i=0; i<n; i++) a[i] *= k;
return *this;
}
//! 1 norm
T norm1 () const
{
T s=0.0;
for (int i=0; i<n; i++) s += ABS(a[i]);
return s;
}
//! 2 norm
T norm2 () const
{
T s=0.0;
for (int i=0; i<n; i++) s += a[i]*a[i];
return sqrt(s);
}
//! Infinity norm
T norminfty () const
{
T s=0.0;
for (int i=0; i<n; i++)
if (ABS(x[i])>s) s = ABS(a[i]);
return s;
}
//! Euclidean distance of two vectors
T distance (const Vec<n,T>& b) const
{
T s=0.0;
for (int i=0; i<n; i++) s += (a[i]-b.a[i])*(a[i]-b.a[i]);
return sqrt(s);
}
};
//! multiplication of scalar with vector
template<int n, class T>
inline Vec<n,T> operator* (T k, Vec<n,T> b)
{
Vec<n,T> z; for (int i=0; i<n; i++) z(i) = k*b(i);return z;
}
//! unary minus
template<int n, class T>
inline Vec<n,T> operator- (Vec<n,T> b)
{
Vec<n,T> z; for (int i=0; i<n; i++) z(i) = -b(i);return z;
}
/** @} */
} // end namespace Dune
#endif
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef __DUNE_SIMPLEVECTOR_HH
#define __DUNE_SIMPLEVECTOR_HH
//***********************************************************************
//
// implementation of peter array
//
//***********************************************************************
#include <iostream>
#include <iomanip>
#include <string>
#include <rpc/xdr.h>
#include <array.h>
namespace Dune
{
/** @addtogroup Common
@{
*/
//! A simple vector class
template <class T>
class SimpleVector : public Array<T> {
public:
//! make empty vector
SimpleVector() {};
//! make array with m components
SimpleVector(int m) : Array<T>::Array(m) {}
//! assignment from scalar
SimpleVector<T>& operator= (const T t)
{
for (int i=0; i<this->n; ++i) this->p[i] = t;
return *this;
}
//!
SimpleVector<T>& operator+= (const SimpleVector<T>& vec)
{
for (int i=0; i<this->n; ++i)
this->p[i] += vec[i];
return *this;
}
//! scalar product of two vectors, no check for size !
T ddot (const SimpleVector<T>& x)
{
T sum = 0;
for (int i=0; i<this->n; ++i) sum += this->p[i]*x.p[i];
return sum;
}
//! add scalar times other vector
void daxpy (T a, const SimpleVector<T>& x)
{
for (int i=0; i<this->n; ++i) this->p[i] += a*x.p[i];
}
//! Vector subtraction
friend SimpleVector<T> operator-(const SimpleVector<T>& v1,
const SimpleVector<T>& v2) {
assert(v1.size() == v2.size());
SimpleVector<T> out(v1.size());
for (int i=0; i<out.size(); i++)
out[i] = v1[i] - v2[i];
return out;
}
};
/** @} */
} // end namespace Dune
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment