[Dune] another little c++ question that bugs me

Aleksejs Fomins aleksejs.fomins at lspr.ch
Wed Aug 13 11:02:48 CEST 2014


Dear Dune,

I have a little c++ question that bugs me.

Basically the two little functions below are meant to calculate the
total quadratic error between two vectors of coordinates.

At the moment the function norm2sqVector gives me the wrong result.

Now here is the funny part. Notice the line

rez += norm2sq<dim>(xVec1[i], xVec2[i]);

If I replace that line with two lines

rez += norm2sq<dim>(xVec1[i], xVec2[i]);
norm2sq<dim>(xVec1[i], xVec2[i]);

by simply calling the function norm2sq twice for no reason, the function
norm2sqVector returns the correct result.

I have no idea why is this happening, but it must be sth trivial that I
can't see.

Could someone please take a look?

Thanks,
Aleksejs



--------------------------------------------------------------



// Finds the 2-norm distance between two coordinates
template<int dim>
double norm2sq(
  FieldVector< double, dim > x1,
  FieldVector< double, dim > x2
) {
	double rez;
	for (int i = 0; i < x1.size(); i++)
	{
		rez += pow(x1[i] - x2[i],2);
	}
	return rez;
}

// Finds the 2-norm distance between two coordinate vectors
template<int dim>
double norm2sqVector(
  std::vector<FieldVector< double, dim >> xVec1,
  std::vector<FieldVector< double, dim >> xVec2
) {
	double rez;
	for (int i = 0; i < xVec1.size(); i++)
	{
		rez += norm2sq<dim>(xVec1[i], xVec2[i]);
	}
	return rez;
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 551 bytes
Desc: OpenPGP digital signature
URL: <https://lists.dune-project.org/pipermail/dune/attachments/20140813/262e4cdb/attachment.sig>


More information about the Dune mailing list