473,806 Members | 2,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vector multiplication in NRVec

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

Oct 25 '07 #1
11 1911
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*(doubl e 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
Oct 25 '07 #2
On Oct 25, 12:13 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
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*(doubl e 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*" . I did in fact implemenet an
operator that performs "vector*num ber" and this works fine:

declaration:

NRVec operator*(const T &rhs);

and prototype:

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

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

Holger

Oct 25 '07 #3
Holgerson wrote:
On Oct 25, 12:13 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
>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*(doubl e 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*num ber" and this works fine:

declaration:

NRVec operator*(const T &rhs);

and prototype:

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

However it looks that I'm not smart enough to figure out the
"number*vec tor" thing. Any ideas?
Use a freestanding operator* with first argument double and second argument
vector.

Best

Kai-Uwe Bux
Oct 25 '07 #4
On 2007-10-25 12:38, Holgerson wrote:
On Oct 25, 12:13 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
>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*(doubl e 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*" . I did in fact implemenet an
operator that performs "vector*num ber" and this works fine:

declaration:

NRVec operator*(const T &rhs);

and prototype:

template <class T>
NRVec<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(n n);
for(int i=0;i<nn;i++)
{
result[i]=v[i]*rhs;
}
return result;
}
Assuming that NRVec has a copy-constructor you could speed up the code
slightly by doing

template <class T>
NRVec<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(* this); // Create a copy
for(int i=0;i<nn;i++)
{
result[i] *= 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<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(* this);
return result *=rhs;
}

--
Erik Wikström
Oct 25 '07 #5
On Oct 25, 1:36 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Holgerson wrote:
On Oct 25, 12:13 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
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*(doubl e 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*num ber" and this works fine:
declaration:
NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(n n);
for(int i=0;i<nn;i++)
{
result[i]=v[i]*rhs;
}
return result;
}
However it looks that I'm not smart enough to figure out the
"number*vec tor" 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

Oct 25 '07 #6
On Oct 25, 2:16 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-10-25 12:38, Holgerson wrote:


On Oct 25, 12:13 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
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*(doubl e 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*" . I did in fact implemenet an
operator that performs "vector*num ber" and this works fine:
declaration:
NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(n n);
for(int i=0;i<nn;i++)
{
result[i]=v[i]*rhs;
}
return result;
}

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

template <class T>
NRVec<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(* this); // Create a copy
for(int i=0;i<nn;i++)
{
result[i] *= 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<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(* this);
return result *=rhs;

}

--
Erik Wikström- Hide quoted text -

- Show quoted text -
Hi Erik,

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

Holger

Oct 25 '07 #7
Holgerson wrote:
On Oct 25, 1:36 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>Holgerson wrote:
On Oct 25, 12:13 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
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*(doubl e 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*num ber" and this works fine:
declaration:
NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(n n);
for(int i=0;i<nn;i++)
{
result[i]=v[i]*rhs;
}
return result;
}
However it looks that I'm not smart enough to figure out the
"number*vec tor" 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
Oct 25 '07 #8
On Oct 25, 5:25 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Holgerson wrote:
On Oct 25, 1:36 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Holgerson wrote:
On Oct 25, 12:13 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
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*(doubl e 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 (possiblya
friend). In that form, it takes two arguments.
I did in fact implemenet an
operator that performs "vector*num ber" and this works fine:
declaration:
NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(n n);
for(int i=0;i<nn;i++)
{
result[i]=v[i]*rhs;
}
return result;
}
However it looks that I'm not smart enough to figure out the
"number*vec tor" 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

Oct 25 '07 #9
On Oct 25, 5:25 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Holgerson wrote:
On Oct 25, 1:36 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Holgerson wrote:
On Oct 25, 12:13 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
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*(doubl e 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 (possiblya
friend). In that form, it takes two arguments.
I did in fact implemenet an
operator that performs "vector*num ber" and this works fine:
declaration:
NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<TNRVec<T: : operator*(const T &rhs)
{
NRVec<Tresult(n n);
for(int i=0;i<nn;i++)
{
result[i]=v[i]*rhs;
}
return result;
}
However it looks that I'm not smart enough to figure out the
"number*vec tor" 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
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 -

Oct 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3212
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
19
3305
by: Carlo Milanesi | last post by:
Mathematically speaking, a 'vector' is something you can add to another vector and multiply by a number. But in C++, the following code is illegal: std::vector<double> v1(3), v2(3); v1 + v2; // Illegal v1 * 2.; // Illegal In addition, vectors of different dimensions belong two different spaces (types). But in C++, the following code is legal:
1
1802
by: raylegendkiller | last post by:
NEED TO MAKE A PROGRAM which computes the current value of the vectors {x} based on the following forward iterations: this >>> {x}(n+1) = {x}(n), n = 0,1,2, ... ,8,9. In other words, the next vector {x} is equal to the product of and the current vector {x}. Perform the matrix multiplication by using the function:
5
10359
by: =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?= | last post by:
Hello! I'm trying to find what package I should use if I want to: 1. Create 3d vectors. 2. Normalize those vectors. 3. Create a 3x3 rotation matrix from a unit 3-d vector and an angle in radians. 4. Perform matrix multiplication.
13
2966
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
2
1670
by: curious2007 | last post by:
Hi everyone, This is a code from Numerical Recipes book. It the vector class. However, I am a little confused about how this works. template <class T> class NRVec { private: int nn; //size of array. upper index is nn-1
7
3898
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to allocate multidimentional arrays like so: int *v_base = (int *) malloc(1000000*sizeof(int)); int **v = (int **) malloc(1000*sizeof(int *));
3
2153
by: Holgerson | last post by:
Hi everybody, I wonder if someone knows how to implement an operator* into NRVec and/ or NRMat, respectively, that can perform these kind of operations: Matrix*Vector and Vector*Matrix. Thanks a lot, Holger
5
3077
by: yogi_bear_79 | last post by:
Distant learning student. My lab is to write a function to perform the addition of large integers, with no limit to the number of digits. (Also have to do a subtraction, division, and multiplication lab). It is suggested to treat each number as a sequence. This is what I have so far, this is rough code just to get it working: 1. I can't get the syntax correct on the 'for' statement in the longAdditon function. No matter what I try I...
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.