Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How do you overload a 2/3D "vector" to produce its length?

Reply
Thread Tools

How do you overload a 2/3D "vector" to produce its length?

 
 
Mirco Wahab
Guest
Posts: n/a
 
      01-12-2009
Hi,

after working with some more algebra and a
homebrewn overloaded (lightweight) vector/matrix
class, I'm somehow weary of writing V.length() all
the time

After looking through available operators,
the only feasible operator would be the '~',
for the vector length, therefore having code
like this:

...
double cos_a = ( (C-A) * (B-A) ) // V * W => some dot product
/ ( ~(C-A) * ~(B-A) ); // ~V => vector scalar length
...

which looks "almost" idiomatic.

Any other ideas? Are there potential
precedence traps to be expected?

Or should I better stick with
...
/ ( (C-A).length() * (B-A).length() )
...

Thanks & Regards

Mirco
 
Reply With Quote
 
 
 
 
joecook@gmail.com
Guest
Posts: n/a
 
      01-12-2009
On Jan 12, 3:22*pm, Mirco Wahab <(E-Mail Removed)-halle.de> wrote:

> Any other ideas? Are there potential
> precedence traps to be expected?
>
> Or should I better stick with
> * *...
> * */ ( (C-A).length() * (B-A).length() )
> * *...


Absolutely. There is no built-in operator that has the same semantics
as "length". Using "~" is impossible to read and maintain. Writing
out "length()" on the other hand is extremely clear.

Joe C

 
Reply With Quote
 
 
 
 
Carlo Milanesi
Guest
Posts: n/a
 
      01-12-2009
Mirco Wahab wrote:
> after working with some more algebra and a
> homebrewn overloaded (lightweight) vector/matrix
> class, I'm somehow weary of writing V.length() all
> the time


I prefer to use the word "norm", as in "v.norm()", instead of "length".

--

Carlo Milanesi
http://digilander.libero.it/carlmila
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      01-12-2009
Carlo Milanesi wrote:
> Mirco Wahab wrote:
>> after working with some more algebra and a
>> homebrewn overloaded (lightweight) vector/matrix
>> class, I'm somehow weary of writing V.length() all
>> the time

>
> I prefer to use the word "norm", as in "v.norm()", instead of "length".
>

And I prefer magnitude()

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-13-2009
"Carlo Milanesi" <(E-Mail Removed)> wrote in message
news:496bceb9$0$11383$(E-Mail Removed)...
> Mirco Wahab wrote:
>> after working with some more algebra and a
>> homebrewn overloaded (lightweight) vector/matrix
>> class, I'm somehow weary of writing V.length() all
>> the time

>
> I prefer to use the word "norm", as in "v.norm()", instead of "length".


However, norm might get confused with being a normalized vector. I would
stick with length or distance.

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-13-2009
On Jan 13, 12:28 am, Jeff Schwab <(E-Mail Removed)> wrote:
> Mirco Wahab wrote:


> > after working with some more algebra and a
> > homebrewn overloaded (lightweight) vector/matrix
> > class, I'm somehow weary of writing V.length() all
> > the time


> > After looking through available operators,
> > the only feasible operator would be the '~',
> > for the vector length, therefore having code
> > like this:


> > ...
> > double cos_a = ( (C-A) * (B-A) ) // V * W => some dot product
> > / ( ~(C-A) * ~(B-A) ); // ~V => vector scalar length
> > ...


> > which looks "almost" idiomatic.


> > Any other ideas? Are there potential
> > precedence traps to be expected?


> You could instead define a stand-alone length function that
> calls the length method. That would save you the . operator,
> and leverage the existing parentheses in your sample code.


> > Or should I better stick with
> > ...
> > / ( (C-A).length() * (B-A).length() )
> > ...

>
> / ( length(C-A) * length(B-A) )


That's an interesting idea. I don't buy the "saving the .
operator" part, but the notation does seem to conform more to
typical mathematic notation. I'd argue for the use of . is
preferred with objects have some associated "behavior", but I
don't think that that's really the case here. And other
functions, which you add later (or which someone who doesn't
have access to the matrix code adds) will also use this syntax.

> It is common in the STL to call these functions "size," rather
> than "length:"


> / ( size(C-A) * size(B-A) )


It's also common in many places for size to indicate the number
of bytes (as in the sizeof operator) and length the number of
elements.

In this case, however, I think he's concerned with a
mathematical concept. In which case, he should use whatever is
most frequent in mathematics. (If he wants to be really clear:
elementCount for the number of elements, magnitude for the
corresponding mathematical concept. Of course, if he's already
complaining about the length of length...)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      01-13-2009
On Jan 12, 8:22*pm, Mirco Wahab <(E-Mail Removed)-halle.de> wrote:

> after working with some more algebra and a
> homebrewn overloaded (lightweight) vector/matrix
> class, I'm somehow weary of writing V.length() all
> the time
>
> After looking through available operators,
> the only feasible operator would be the '~',
> for the vector length, therefore having code
> like this:
>
> * ...
> * double cos_a = ( *(C-A) * *(B-A) ) * * // *V * W => some dot product
> * * * * * * * */ ( ~(C-A) * ~(B-A) ); * *// ~V * * => vector scalar length
> * ...
>
> which looks "almost" idiomatic.
>
> Any other ideas? Are there potential
> precedence traps to be expected?
>
> Or should I better stick with
> * *...
> * */ ( (C-A).length() * (B-A).length() )
> * *...


Another option would be to have length() member function as well as a
"shortcut" namespace:

template<class T>
struct Vector
{
// ...
size_t length() const;
// ...
};

namespace Shortcuts {

template<class T>
inline size_t L(Vector<T> const& v)
{
return v.length();
}

}

int main()
{
Vector<int> v;

size_t s = v.length();

using Shortcuts::L;
s = L(v);
}

--
Max
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      01-13-2009
Thanks to all involved for this interesting
discussion about flavor and elegancy

For my personal taste, I like the STL-like
notion of FUNC(object), as has been suggested
by Jeff. Furthermore, 'length' does change
the meaning depending of context. 'norm' would
be a correct (and short) term but is easy to
confuse with 'normalize' or 'normalized'.

After consulting, Wikipedia
http://en.wikipedia.org/wiki/Euclidean_vector
I think I'll go for 'magnitude' which
can be shortened to 'mag' without ambiguity
(this has been already suggested by Daniel).

A source snippet like below would IMHO be both
self-explanatory and uncluttered:

...
double cos_a = ( (C-A) * (B-A) ) / ( mag(C-A) * mag(B-A) ),
cos_b = ( (A-B) * (C-B) ) / ( mag(A-B) * mag(C-B) ),
cos_c = ( (A-C) * (B-C) ) / ( mag(A-C) * mag(B-C) );
...

Thanks & Regards

M.

 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      01-13-2009
On 13 Jan., 13:02, Mirco Wahab <(E-Mail Removed)-halle.de> wrote:
> * double cos_a = ( (C-A) * (B-A) ) / ( mag(C-A) * mag(B-A) ),
> * * * * *cos_b = ( (A-B) * (C-B) ) / ( mag(A-B) * mag(C-B) ),
> * * * * *cos_c = ( (A-C) * (B-C) ) / ( mag(A-C) * mag(B-C) );


You might also want to
* reuse the result of the vector subtractions
* write a function for this expression
* add a "mag2" function for the squared magnitude
* replace operator* with a more descriptive function name for the
inner product like "inner_prod".

Your function could look like this:

double cosine(Vect x, Vect y, const Vect& sub) {
using std::sqrt;
x -= sub;
y -= sub;
return inner_prod(x,y) / sqrt( mag2(x) * mag2(y) );
}

:
double cos_a = cosine(C,B,A);
:

just my 2 cents,
SG
 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      01-13-2009
Carlo Milanesi wrote:
> Mirco Wahab wrote:
>> after working with some more algebra and a
>> homebrewn overloaded (lightweight) vector/matrix
>> class, I'm somehow weary of writing V.length() all
>> the time

>
> I prefer to use the word "norm", as in "v.norm()", instead of "length".
>


I personally find "norm" quite misleading in this case, because in
mathematics the concept is related to the length of a (numerical) vector
in some space, rather than the dimensionality of the space that is
returned by the length() function.

Zeppe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
can java produce .exe? if it can produce jar,how do you do? aungkopyay@gmail.com Java 5 10-27-2006 02:07 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments