/**
* Set #values# to the point values
* of the function at the #points#.
- * It is assumed that #values# be
- * empty.
+ * It is assumed that #values#
+ * already has the right size, i.e.
+ * the same size as the #points#
+ * array.
*/
virtual void value_list (const vector<Point<dim> > &points,
vector<double> &values) const;
/**
* Set #gradients# to the gradients of
* the function at the #points#.
- * It is assumed that #values# be
- * empty.
+ * It is assumed that #values#
+ * already has the right size, i.e.
+ * the same size as the #points# array.
*/
virtual void gradient_list (const vector<Point<dim> > &points,
vector<Point<dim> > &gradients) const;
/**
* Exception
*/
- DeclException0 (ExcVectorNotEmpty);
+ DeclException2 (ExcVectorHasWrongSize,
+ int, int,
+ << "The vector has size " << arg1 << " but should have "
+ << arg2 << " elements.");
};
/**
* Set #values# to the point values
* of the function at the #points#.
- * It is assumed that #values# be
- * empty.
+ * It is assumed that #values#
+ * already has the right size, i.e.
+ * the same size as the #points#
+ * array.
*/
virtual void value_list (const vector<Point<dim> > &points,
vector<double> &values) const;
/**
* Set #gradients# to the gradients of
* the function at the #points#.
- * It is assumed that #values# be
- * empty.
+ * It is assumed that #values#
+ * already has the right size, i.e.
+ * the same size as the #points# array.
*/
virtual void gradient_list (const vector<Point<dim> > &points,
vector<Point<dim> > &gradients) const;
/**
* Provide a function which always returns a constant value, which is delivered
- * upon construction. Obviously, the derivates of this function are zerom which
+ * upon construction. Obviously, the derivates of this function are zero, which
* is why we derive this class from #ZeroFunction#: we then only have to
- * overload th value functions, not all the derivatives.
+ * overload th value functions, not all the derivatives. In some way, it would
+ * be more obvious to do the derivation in the opposite direction, i.e. let
+ * #ZeroFunction# be a more specialized version of #ConstantFunction#; however,
+ * this would be more inefficient, since we could not make use of the fact that
+ * the function value of the #ZeroFunction# is known at compile time and need
+ * not be looked up somewhere in memory.
*/
template <int dim>
class ConstantFunction : public ZeroFunction<dim> {
/**
* Set #values# to the point values
* of the function at the #points#.
- * It is assumed that #values# be
- * empty.
+ * It is assumed that #values#
+ * already has the right size, i.e.
+ * the same size as the #points#
+ * array.
*/
virtual void value_list (const vector<Point<dim> > &points,
vector<double> &values) const;
#include <vector>
-
template <int dim>
Function<dim>::~Function () {};
template <int dim>
void Function<dim>::value_list (const vector<Point<dim> > &points,
vector<double> &values) const {
- Assert (values.size() == 0,
- ExcVectorNotEmpty());
+ Assert (values.size() == points.size(),
+ ExcVectorHasWrongSize(values.size(), points.size()));
- values.reserve (points.size());
for (unsigned int i=0; i<points.size(); ++i)
- values.push_back (this->operator() (points[i]));
+ values[i] = this->operator() (points[i]);
};
template <int dim>
void Function<dim>::gradient_list (const vector<Point<dim> > &points,
vector<Point<dim> > &gradients) const {
- Assert (gradients.size() == 0,
- ExcVectorNotEmpty());
+ Assert (gradients.size() == points.size(),
+ ExcVectorHasWrongSize(gradients.size(), points.size()));
- gradients.reserve (points.size());
for (unsigned int i=0; i<points.size(); ++i)
- gradients.push_back (gradient(points[i]));
+ gradients[i] = gradient(points[i]);
};
template <int dim>
void ZeroFunction<dim>::value_list (const vector<Point<dim> > &points,
vector<double> &values) const {
- Assert (values.size() == 0,
- ExcVectorNotEmpty());
+ Assert (values.size() == points.size(),
+ ExcVectorHasWrongSize(values.size(), points.size()));
- values.reserve (points.size());
- values.insert (values.begin(), points.size(), 0.);
+ fill_n (values.begin(), points.size(), 0.);
};
template <int dim>
void ZeroFunction<dim>::gradient_list (const vector<Point<dim> > &points,
- vector<Point<dim> > &values) const {
- Assert (values.size() == 0,
- ExcVectorNotEmpty());
+ vector<Point<dim> > &gradients) const {
+ Assert (gradients.size() == points.size(),
+ ExcVectorHasWrongSize(gradients.size(), points.size()));
- values.reserve (points.size());
- values.insert (values.begin(), points.size(), Point<dim>());
+ fill_n (gradients.begin(), points.size(), Point<dim>());
};
template <int dim>
void ConstantFunction<dim>::value_list (const vector<Point<dim> > &points,
vector<double> &values) const {
- Assert (values.size() == 0,
- ExcVectorNotEmpty());
+ Assert (values.size() == points.size(),
+ ExcVectorHasWrongSize(values.size(), points.size()));
- values.reserve (points.size());
- values.insert (values.begin(), points.size(), function_value);
+ fill_n (values.begin(), points.size(), function_value);
};