Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

What's the meaning of a scalar divided by a vector

Started by charlie_craft Nov 25, 2009 at 5:39 AM 16 replies 26.4k views
Original Post
charlie_craft
charlie_craft
I was perplexed by some code when I am reading the Ogre source. inline friend Vector2 operator * ( const Real fScalar, const Vector2& rkVector ) { return Vector2( fScalar * rkVector.x, fScalar * rkVector.y); } inline friend Vector2 operator / ( const Real fScalar, const Vector2& rkVector ) { return Vector2( fScalar / rkVector.x, fScalar / rkVector.y); } What's the meaning of a scalar divided by a vector in the second operator '/'? I suess it's a Ctrl+c and Ctrl+v mistake.
http://www.wantgame.cn
Numsgil
Numsgil
It's not a common math function, if that's what you're asking. You can see what it actually does well enough, though.
[size=2]Darwinbots - [size=2]Artificial life simulation
swiftcoder
swiftcoder
Quote:
Original post by charlie_craft
What's the meaning of a scalar divided by a vector in the second operator '/'?
It follows the usual ules of type-promotion (as in int --> float). If a scalar multiplied by a vector results in a vector, then a scalar divided by a vector should also be valid, and result in a vector.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
alvaro
alvaro
Quote:
Original post by swiftcoder
Quote:
Original post by charlie_craft
What's the meaning of a scalar divided by a vector in the second operator '/'?
It follows the usual ules of type-promotion (as in int --> float). If a scalar multiplied by a vector results in a vector, then a scalar divided by a vector should also be valid, and result in a vector.


That's not right. A scalar multiplied by a vector yields a vector because that operation (scaling) is part of the definition of vector space.

We expect A/B=C to mean something close to A=C*B. If dividing a scalar by a vector yields a vector, you have a problem, since the structure of a vector space doesn't define a product between vectors.

In general, diving by a vector is not a good idea.
lmelior
lmelior
Quote:
Original post by alvaro
Quote:
Original post by swiftcoder
Quote:
Original post by charlie_craft
What's the meaning of a scalar divided by a vector in the second operator '/'?
It follows the usual ules of type-promotion (as in int --> float). If a scalar multiplied by a vector results in a vector, then a scalar divided by a vector should also be valid, and result in a vector.


That's not right.


Well, it might not be right mathematically, but it's right based on the source code given. :) Why anybody would want to do that ever, I have no idea.

EDIT: I was curious, and it does have the interesting property of mirroring a vector about the nearest 45 degree axis and scaling the magnitude by 1/scalar. Still don't know why you'd do that, with the instability that occurs as either x or y approach zero.
swiftcoder
swiftcoder
Quote:
Original post by alvaro
Quote:
Original post by swiftcoder
It follows the usual ules of type-promotion (as in int --> float). If a scalar multiplied by a vector results in a vector, then a scalar divided by a vector should also be valid, and result in a vector.
That's not right. A scalar multiplied by a vector yields a vector because that operation (scaling) is part of the definition of vector space.
And a float multiplied by an int results in a float because that operation (multiplication by int) is part of the definition of floating point.

From a mathematical standpoint you are certainly correct, but it saves a fair amount of 'promote scalar to vector, then divide' in your math code, for example, taking the multiplicative inverse of a vector.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
alvaro
alvaro
Quote:
Original post by lmelior
Well, it might not be right mathematically, but it's right based on the source code given. :) Why anybody would want to do that ever, I have no idea.


Defining operations that don't have a reasonable mathematical equivalent is asking for trouble.


Quote:
Original post by swiftcoder
And a float multiplied by an int results in a float because that operation (multiplication by int) is part of the definition of floating point.

The mathematical objects that int and float approximate are integers and real numbers, and the multiplication of a real number by an integer is well defined because there is a "natural injection" (the mathematical equivalent of a type promotion) from integers to real numbers.

Quote:
From a mathematical standpoint you are certainly correct, but it saves a fair amount of 'promote scalar to vector, then divide' in your math code, for example, taking the multiplicative inverse of a vector.

The type promotion from scalar to vector doesn't have a reasonable mathematical equivalent, and I don't think people typically implement it in their libraries either. And I don't know what you mean by "the multiplicative inverse of a vector." I am really not trying to be difficult here: I just don't see how you would have any meaningful definitions for these things.

Sneftel
Sneftel
Promoting a scalar to a vector isn't a mathematically sound operation, but it is a common operation in vectorized programming. It's quite common in FORTRAN and MATLAB, where a single statement like A=(B+C)/2 is high-performance shorthand for for(int i=0; i[j] = (B[j] + C[j]) / 2; } }. Of course, such semantics are almost entirely at odds with the use of vectors to represent cartesian coordinates, and so it's unusual to see them in a game/graphics math library like that.
swiftcoder
swiftcoder
Quote:
Original post by alvaro
I don't think people typically implement it in their libraries either.
It is certainly implemented in my math code, Ogre's math library, and I believe also in CML (though I don't have the source handy).
Quote:
And I don't know what you mean by "the multiplicative inverse of a vector."
The multiplicative inverse of a real, r, is defined as (1.0 / r). For a vector, v, we can perform a similar operation (vector(1.0) / v), or in typical shorthand (1.0 / v).

Programmers tend to be lazy, and despite being mathematically unsound, this sort of thing is in pretty much every math library I have used [smile]
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
Zakwayda
Zakwayda
Quote:
It is certainly implemented in my math code, Ogre's math library, and I believe also in CML (though I don't have the source handy).
We didn't implement scalar/vector division in the CML, and I don't think I'd ever seen it in a math library until this thread. It sounds like it's one of those shorthands (such as adding a vector and a scalar) that some developers find useful, and some find unclear. I'm in the latter category, but that's just personal preference (obviously others feel differently).
alvaro
alvaro
Yup, I just checked, and Ogre's library does have promotion from scalar to vector and division by vector. It's pretty horrible. :)

Steadtler
Steadtler
Sounds more like a programming syntaxic shortcut than a mathematical operation to me. But then, what use does a strongly-typed language have if you begin to do things like that...
lmelior
lmelior
I'd like to see a practical application for this, if anybody has an example. I can't seem to find any reference to a vector multiplicative inverse being useful for anything other than being mathematically interesting. I see that it's almost useful for quaternions since it's related to the conjugate, but the conjugate is the useful part. It can't just be for promoting a scalar into a vector, since you can just as easily multiply by a vector, which makes far more sense and will never blow up on you.
Medium9
Medium9
I would guess that it just is there to save a bit of CPU time. With this, you may save computing the componentwise reciprokal and instanciation of a new vector object/struct.

I couldn't think of an actual direct mathematical use right now too though, but I guess some people may need a vector of one and the same scalar divided by some different numbers as a vector - what ever for :)

It IS dangerous however. With such an operator defined, you might search your "peach" off until you found the place where you accidently divided by a vector due to a typo or something.
swiftcoder
swiftcoder
Quote:
Original post by Medium9
I would guess that it just is there to save a bit of CPU time. With this, you may save computing the componentwise reciprokal and instanciation of a new vector object/struct.

I couldn't think of an actual direct mathematical use right now too though, but I guess some people may need a vector of one and the same scalar divided by some different numbers as a vector - what ever for :)
Particularly if you are implementing SIMD vectors, there are a couple of places you can achieve optimizations. Apart from that, it shows up in a number of 'stuff data into a uniform vector' operations in my code.
Quote:
Original post by Steadtler
But then, what use does a strongly-typed language have if you begin to do things like that...
Not quite sure I buy this line of reasoning. It is roughly the same argument the Java crowd makes against having operator overloading - that modifying the meaning of established operators is dangerous.

While I agree in principle with that sentiment, all we are doing here is slightly extending the definition of a mathematical operator, in order to simplify code - pretty much the same idea as overloading + to append std::strings...
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
alvaro
alvaro
Quote:
Original post by swiftcoder

Quote:
Original post by Steadtler
But then, what use does a strongly-typed language have if you begin to do things like that...
Not quite sure I buy this line of reasoning. It is roughly the same argument the Java crowd makes against having operator overloading - that modifying the meaning of established operators is dangerous.

While I agree in principle with that sentiment, all we are doing here is slightly extending the definition of a mathematical operator, in order to simplify code - pretty much the same idea as overloading + to append std::strings...


That case is not comparable. At least concatenating strings is a natural thing to do. Whether you use s1 + s2' or s1.concatenate(s2)' of concatenate(s1,s2)', I don't particularly care. The operation of dividing by a vector is not natural or meaningful, and I would also object to having it as a function. If you really, really need it, perhaps you can call it divide_component_by_component' or something like that, so people know what it does.
swiftcoder
swiftcoder
Quote:
Original post by alvaro
If you really, really need it, perhaps you can call it divide_component_by_component' or something like that, so people know what it does.
It seems fairly self evident to me: it takes a three element vector and computes s/c for each component, c.
Quote:
Whether you use s1 + s2' or s1.concatenate(s2)' of concatenate(s1,s2)', I don't particularly care.
Not buying this line of reasoning either. Why would you care anymore whether I call it 's / v' or 'scalar_promote_and_divide(s, v)'?
Quote:
The operation of dividing by a vector is not natural or meaningful, and I would also object to having it as a function.
Nor is the operation of adding a scalar to a vector, or component-wise multiplication/division of vectors, but I don't see anyone on the warpath to have those excised from all math libraries. They are all handy code shortcuts which violate (somewhat*) the rules of mathematics.

*I say 'somewhat', because we can accept compent-wise multiplication as a valid mathematical definition of multiplication of vectors, and from that it follows that both division and a multiplicative inverse must exist. The usefulness of this definition outside of quaternion math is however debatable.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
owl
owl
I'd be good to see what operators they're using for dot_product/cross_product and see if they match (graphically) those used in algebra...

I'd certainly double-check the documentation for any character being used as an operator in a vectorial library.
[size="2"]I like the Walrus best.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.