Vector multiplication in NRVec

H

Holgerson

Hi everybody,

what I try to do is to implement an operator* into NRVec in such a
way, that I can perform an operation like number*vector rather than
vector*number which is easy. Any ideas? Thanks a lot,

Holger
 
G

Guest

Hi everybody,

what I try to do is to implement an operator* into NRVec in such a
way, that I can perform an operation like number*vector rather than
vector*number which is easy. Any ideas? Thanks a lot,

I do not know what NRVec is but I guess that it comes from Numerical
Recipes in C++. First I would like to ask whether the operation you can
do is number vector * number, or vector *= number, the latter being much
more effective since it does not need to create a new vector.

If there really is a vector * number operator it should be trivial to
add a number * vector operator, it would look something like this:

NRVec operator*(double n, const NRVec& v)
{
return v * n;
}

Since it do not know the specifics of NRVec and which operators it
provides this is the best I can offer. A quick read-up on operator-
overloading should give you all information you need to know to do it
yourself.
 
H

Holgerson

I do not know what NRVec is but I guess that it comes from Numerical
Recipes in C++. First I would like to ask whether the operation you can
do is number vector * number, or vector *= number, the latter being much
more effective since it does not need to create a new vector.

If there really is a vector * number operator it should be trivial to
add a number * vector operator, it would look something like this:

NRVec operator*(double n, const NRVec& v)
{
return v * n;

}

Since it do not know the specifics of NRVec and which operators it
provides this is the best I can offer. A quick read-up on operator-
overloading should give you all information you need to know to do it
yourself.

Dear Erik,

thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*". I did in fact implemenet an
operator that performs "vector*number" and this works fine:

declaration:

NRVec operator*(const T &rhs);

and prototype:

template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}

However it looks that I'm not smart enough to figure out the
"number*vector" thing. Any ideas? Thanks again,

Holger
 
K

Kai-Uwe Bux

Holgerson said:
Dear Erik,

thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*".

That is incorrect. operator* can be a free standing function (possibly a
friend). In that form, it takes two arguments.

I did in fact implemenet an
operator that performs "vector*number" and this works fine:

declaration:

NRVec operator*(const T &rhs);

and prototype:

template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}

However it looks that I'm not smart enough to figure out the
"number*vector" thing. Any ideas?


Use a freestanding operator* with first argument double and second argument
vector.



Best

Kai-Uwe Bux
 
G

Guest

I do not know what NRVec is but I guess that it comes from Numerical
Recipes in C++. First I would like to ask whether the operation you can
do is number vector * number, or vector *= number, the latter being much
more effective since it does not need to create a new vector.

If there really is a vector * number operator it should be trivial to
add a number * vector operator, it would look something like this:

NRVec operator*(double n, const NRVec& v)
{
return v * n;

}

Since it do not know the specifics of NRVec and which operators it
provides this is the best I can offer. A quick read-up on operator-
overloading should give you all information you need to know to do it
yourself.

Dear Erik,

thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*". I did in fact implemenet an
operator that performs "vector*number" and this works fine:

declaration:

NRVec operator*(const T &rhs);

and prototype:

template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}


Assuming that NRVec has a copy-constructor you could speed up the code
slightly by doing

template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(*this); // Create a copy
for(int i=0;i<nn;i++)
{
result *= rhs; // Multiply each element
}
return result;
}

This saves you one lookup in each iteration. If NRVec already defined a
*= operator you could possible save even more (depending on how the *=
operator is implemented) by doing

template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(*this);
return result *=rhs;
}
 
H

Holgerson

Dear Erik,
thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*".

That is incorrect. operator* can be a free standing function (possibly a
friend). In that form, it takes two arguments.




I did in fact implemenet an
operator that performs "vector*number" and this works fine:

NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}

However it looks that I'm not smart enough to figure out the
"number*vector" thing. Any ideas?

Use a freestanding operator* with first argument double and second argument
vector.

Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -


Hi Kai-Uwe,

well, I guess that's possible. I just thought I could squeeze it in
into the member functions. When I started out it looked fairly easy. I
didn't quite accomplish it though. As a free standing function may I
still use it like "number*vector"? Thanks,

Holger
 
H

Holgerson

Dear Erik,
thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*". I did in fact implemenet an
operator that performs "vector*number" and this works fine:

NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}


Assuming that NRVec has a copy-constructor you could speed up the code
slightly by doing

template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(*this); // Create a copy
for(int i=0;i<nn;i++)
{
result *= rhs; // Multiply each element
}
return result;

}

This saves you one lookup in each iteration. If NRVec already defined a
*= operator you could possible save even more (depending on how the *=
operator is implemented) by doing

template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(*this);
return result *=rhs;

}


Hi Erik,

thanks again, this helps me with the "vector*number" thing, indeed.
However, still got no solution for the "number*vector" task. Thanks,

Holger
 
K

Kai-Uwe Bux

Holgerson said:
Holgerson said:
On 2007-10-25 11:08, Holgerson wrote:
Hi everybody,
what I try to do is to implement an operator* into NRVec in such a
way, that I can perform an operation like number*vector rather than
vector*number which is easy. Any ideas? Thanks a lot,
I do not know what NRVec is but I guess that it comes from Numerical
Recipes in C++. First I would like to ask whether the operation you
can do is number vector * number, or vector *= number, the latter
being much more effective since it does not need to create a new
vector.
If there really is a vector * number operator it should be trivial to
add a number * vector operator, it would look something like this:
NRVec operator*(double n, const NRVec& v)
{
return v * n;

Since it do not know the specifics of NRVec and which operators it
provides this is the best I can offer. A quick read-up on operator-
overloading should give you all information you need to know to do it
yourself.
Dear Erik,
thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*".

That is incorrect. operator* can be a free standing function (possibly a
friend). In that form, it takes two arguments.




I did in fact implemenet an
operator that performs "vector*number" and this works fine:

NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}

However it looks that I'm not smart enough to figure out the
"number*vector" thing. Any ideas?

Use a freestanding operator* with first argument double and second
argument vector.
[snip]
well, I guess that's possible. I just thought I could squeeze it in
into the member functions. When I started out it looked fairly easy. I
didn't quite accomplish it though. As a free standing function may I
still use it like "number*vector"?


Yes.

If you want to realized a*b by member functions, the corresponding operator
has to be a member of the class of a. In the case, number*vector, you would
need to put the operator* into the number class. For built in classes, that
won't be possible. Consequently, you have to use a free standing operator*.
It will support the same call syntax as a member operator.


Best

Kai-Uwe Bux
 
H

Holgerson

Holgerson said:
Holgerson wrote:
On 2007-10-25 11:08, Holgerson wrote:
Hi everybody,
what I try to do is to implement an operator* into NRVec in such a
way, that I can perform an operation like number*vector rather than
vector*number which is easy. Any ideas? Thanks a lot,
I do not know what NRVec is but I guess that it comes from Numerical
Recipes in C++. First I would like to ask whether the operation you
can do is number vector * number, or vector *= number, the latter
being much more effective since it does not need to create a new
vector.
If there really is a vector * number operator it should be trivial to
add a number * vector operator, it would look something like this:
NRVec operator*(double n, const NRVec& v)
{
return v * n;
}
Since it do not know the specifics of NRVec and which operators it
provides this is the best I can offer. A quick read-up on operator-
overloading should give you all information you need to know to do it
yourself.
--
Erik Wikström
Dear Erik,
thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*".
That is incorrect. operator* can be a free standing function (possibly a
friend). In that form, it takes two arguments.
I did in fact implemenet an
operator that performs "vector*number" and this works fine:
declaration:
NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}
However it looks that I'm not smart enough to figure out the
"number*vector" thing. Any ideas?
Use a freestanding operator* with first argument double and second
argument vector.
[snip]
well, I guess that's possible. I just thought I could squeeze it in
into the member functions. When I started out it looked fairly easy. I
didn't quite accomplish it though. As a free standing function may I
still use it like "number*vector"?


Yes.

If you want to realized a*b by member functions, the corresponding operator
has to be a member of the class of a. In the case, number*vector, you would
need to put the operator* into the number class. For built in classes, that
won't be possible. Consequently, you have to use a free standing operator*.
It will support the same call syntax as a member operator.

Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -


Hi Kai-Uwe,

thank you very much. What you say makes sense to me. I'll do it this
way then.

Holger
 
H

Holgerson

Holgerson said:
Holgerson wrote:
On 2007-10-25 11:08, Holgerson wrote:
Hi everybody,
what I try to do is to implement an operator* into NRVec in such a
way, that I can perform an operation like number*vector rather than
vector*number which is easy. Any ideas? Thanks a lot,
I do not know what NRVec is but I guess that it comes from Numerical
Recipes in C++. First I would like to ask whether the operation you
can do is number vector * number, or vector *= number, the latter
being much more effective since it does not need to create a new
vector.
If there really is a vector * number operator it should be trivial to
add a number * vector operator, it would look something like this:
NRVec operator*(double n, const NRVec& v)
{
return v * n;
}
Since it do not know the specifics of NRVec and which operators it
provides this is the best I can offer. A quick read-up on operator-
overloading should give you all information you need to know to do it
yourself.
--
Erik Wikström
Dear Erik,
thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*".
That is incorrect. operator* can be a free standing function (possibly a
friend). In that form, it takes two arguments.
I did in fact implemenet an
operator that performs "vector*number" and this works fine:
declaration:
NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}
However it looks that I'm not smart enough to figure out the
"number*vector" thing. Any ideas?
Use a freestanding operator* with first argument double and second
argument vector.
[snip]
well, I guess that's possible. I just thought I could squeeze it in
into the member functions. When I started out it looked fairly easy. I
didn't quite accomplish it though. As a free standing function may I
still use it like "number*vector"?


Hi Kai-Uwe,

still doesn't work. If I do it using NRVec in nrutil_nr.h I get an
error message reading "use of class template requires template
argument list". Thanks again,

Holger
 
G

Guest

Holgerson said:
Holgerson wrote:
On 2007-10-25 11:08, Holgerson wrote:
Hi everybody,
what I try to do is to implement an operator* into NRVec in such a
way, that I can perform an operation like number*vector rather than
vector*number which is easy. Any ideas? Thanks a lot,
I do not know what NRVec is but I guess that it comes from Numerical
Recipes in C++. First I would like to ask whether the operation you
can do is number vector * number, or vector *= number, the latter
being much more effective since it does not need to create a new
vector.
If there really is a vector * number operator it should be trivial to
add a number * vector operator, it would look something like this:
NRVec operator*(double n, const NRVec& v)
{
return v * n;

Since it do not know the specifics of NRVec and which operators it
provides this is the best I can offer. A quick read-up on operator-
overloading should give you all information you need to know to do it
yourself.
Dear Erik,
thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*".
That is incorrect. operator* can be a free standing function (possibly a
friend). In that form, it takes two arguments.
I did in fact implemenet an
operator that performs "vector*number" and this works fine:

NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}

However it looks that I'm not smart enough to figure out the
"number*vector" thing. Any ideas?
Use a freestanding operator* with first argument double and second
argument vector. [snip]
well, I guess that's possible. I just thought I could squeeze it in
into the member functions. When I started out it looked fairly easy. I
didn't quite accomplish it though. As a free standing function may I
still use it like "number*vector"?

Hi Kai-Uwe,

still doesn't work. If I do it using NRVec in nrutil_nr.h I get an
error message reading "use of class template requires template
argument list". Thanks again,


It is because NRVec is a parametrised type and you did not make the *
operator parametrised. The operator should look something like this:

template <class T>
NRVec<T> operator*(const T& lhs, const NRVec<T>& rhs)
{
// ...
}
 
H

Holgerson

Holgerson wrote:
Holgerson wrote:
On 2007-10-25 11:08, Holgerson wrote:
Hi everybody,
what I try to do is to implement an operator* into NRVec in such a
way, that I can perform an operation like number*vector rather than
vector*number which is easy. Any ideas? Thanks a lot,
I do not know what NRVec is but I guess that it comes from Numerical
Recipes in C++. First I would like to ask whether the operation you
can do is number vector * number, or vector *= number, the latter
being much more effective since it does not need to create a new
vector.
If there really is a vector * number operator it should be trivial to
add a number * vector operator, it would look something like this:
NRVec operator*(double n, const NRVec& v)
{
return v * n;
}
Since it do not know the specifics of NRVec and which operators it
provides this is the best I can offer. A quick read-up on operator-
overloading should give you all information you need to know to do it
yourself.
--
Erik Wikström
Dear Erik,
thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*".
That is incorrect. operator* can be a free standing function (possibly a
friend). In that form, it takes two arguments.
I did in fact implemenet an
operator that performs "vector*number" and this works fine:
declaration:
NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}
However it looks that I'm not smart enough to figure out the
"number*vector" thing. Any ideas?
Use a freestanding operator* with first argument double and second
argument vector.
[snip]
well, I guess that's possible. I just thought I could squeeze it in
into the member functions. When I started out it looked fairly easy. I
didn't quite accomplish it though. As a free standing function may I
still use it like "number*vector"?

Hi Kai-Uwe,
still doesn't work. If I do it using NRVec in nrutil_nr.h I get an
error message reading "use of class template requires template
argument list". Thanks again,

It is because NRVec is a parametrised type and you did not make the *
operator parametrised. The operator should look something like this:

template <class T>
NRVec<T> operator*(const T& lhs, const NRVec<T>& rhs)
{
// ...

}


Dear Erik,

thanks a lot, that's it! It works now. Really appreciate your help, I
don't think I'd ever managed to do that on my own. Thanks again,

Holger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top