# subtracting quaternions

is there a way to subtract two quaternions in c#??

Multiply the difference?

Only problem with your answer hippocoder, is that there is no âdifferenceâ method either.

Quaternions are a kind of matrix (a 1x4 matrix containing complex numbers). To append (or add) a matrix, you use matrix multiplication. There is no âremove/subtractâ a matrix. But you can get the effective result by appending the inverse of a matrix.

A * B * iB = A

Look to Quaternion.Inverse to get the inverse.

Note - the reason why their isnât a âdivide/subtractâ operation defined mathematically that just is multiply by inverse, is because the inverse of a matrix does not always exist. Thankfully though, because unity normalizes their quaternions for the representation of rotation, there is always an inverse.

5 Likes

so u mean if i have to subtract two quaternions âŚall i have to do is this

q1 = q2 * Quaternion.Inverse(q3);

6 Likes

Pretty much.

Itâs just not exactly the same as subtraction, as quats are multidimensional. But the result is what you or I would probably call âsubtractâ.

Sorry for butting in, but Iâm a bit lost by this discussion, and Iâm not entirely sure whether itâs because Iâm missing something or someone else is.

In mathematical terms, when you add or subtract matrices, you usually do memberwise addition or subtraction of each element. Itâs completely straightforward. As a mathematical construct, quaternions are not quite the same as matrices, but you can still do memberwise addition or subtraction and itâs still completely straightforward.

That doesnât necessarily mean that Unityâs implementation of quaternions supports that concept, of course.

And in games, quaternions usually come up as a way of representing orientations in 3D space, in which specific case Iâm not sure that memberwise addition or subtraction is a conceptually-meaningful operation to perform.

But in terms of raw mathematics, Iâm pretty confident that subtracting quaternions is absolutely something you should be able to do, and means something completely different from inverse-multiplication.

You may be having an issue, because the nomenclature being used is very laymen.

The adding of matrices doesnât result in an effect the resembles what a laymen would call âaddingâ or âsubtractingâ. Instead the multiplication operation results in something a laymen might call âaddingâ. Which is why my last post said:

Secondly. YES, quats are a kind of matrix. Theyâre 1x4 matrix, they follow the operational rules of a matrix, the thing that stands them apart is that they contain complex numbers.

Just like Vectors are a kind of matrix.

Or rather I should say theyâre often represented in their matrix forms, if weâd rather speak technical rather than lay. Where as you can represent both quats and vectors in their functional form as well, ala:

<x, y, z, w>
OR
xi + yj + zk + w

Either way though, the operations follow the same rules as matrix operations.

AND IN THE END, relating to OPâs question.

Inverse multiplication is what they were looking for. And they got their answer. With out my having to give them a lengthy math lesson explaining that the verbage of âaddition and subtraction isnât actually what theyâre looking for, but instead the inverse appending that would result in what a layman with no mathematical background would understand as subtractionâ.

And people wonder why so many people hate math!

1 Like

Quaternions are like complex numbers, except they have 4 parts instead of 2. If quaternions are matrices, then so are complex numbers.

The phrase âa matrix that contains complex numbersâ suggests to me that a ânormalâ matrix is a Matrix but that youâre using a Matrix, i.e. a matrix where each individual number inside the matrix happens to be a complex number. That is definitely not what a quaternion is.

No they donât. You canât multiply two 1x4 matrices at all using standard matrix multiplication rules. Memberwise multiplication doesnât give the same result as quaternion multiplication, either.

OK, so the thing that I was missing is that everyone else in the entire thread somehow assumed that âsubtractionâ actually meant something other than subtraction, even though the OP just said âsubtractâ with absolutely no context and subtraction is a real and valid operation that would be completely legitimate to discuss?

I realize that quaternions are complicated and I donât expect every Unity user to be familiar with them, but I do generally expect that people know the difference between addition and multiplication (and their inverses), and that the * operator means multiplication. I donât think I would ever have guessed that someone asking how to âsubtractâ actually meant âdo the inverse of the * operatorâ.

ActuallyâŚ the funny thing is. Complex numbers CAN be represented as matrices.

http://en.wikipedia.org/wiki/Complex_number#Matrix_representation_of_complex_numbers

Huh? Are you serious?

http://en.wikipedia.org/wiki/Matrix_multiplication#Row_vector_and_column_vector

I guess youâre not familiar with the fact that in game design forums around the internet when people multiple quats they often say the coloquial phrase âadding quatsâ.

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=adding quaternions

Oh, whatâs that, several forum posts where people use the term in that manner. To describe the situation of âappending a rotation to the end of another rotationâ.

LikeâŚ I want to rotate 30 degrees, and then another 20 degrees, around the x axis. Oh, well youâd multiply the 2 quats to get that result. Resulting in 50 degrees around the x axis. Which to a lay personâŚ 50 = 30 PLUS 20.

I get it, you lacked the context. Thatâs fine. And you are more familiar than most with what a quaternion actually is, than most. But youâre just derailing the hell out of this all to what? ReallyâŚ nevermind, I donât care. Enjoy yourâŚ whatever you want to call all this. Victory? Youâre victorious, there you go.

You rebut me, then declare the conversation over? Classy.

Forum threads are frequently read (or quoted) for information by people other than the original question-asker; having the correct mathematics in this thread is potentially useful for posterity even if the OP doesnât care.

And to answer your question, yes, Iâm serious. Your link doesnât show the multiplication of two 1xN matrices, it shows the multiplication of a 1xN matrix and an Nx1 matrix, which are not the same thing. And even if we ignore that difference, the result is either a 1x1 matrix or an NxN matrix, so the product of two 1x4 matrices could never be another 1x4 matrix, so thatâs still obviously not how quaternions work.

If you really didnât want to get into a tangential debateâand assuming (as Iâm not certain I should) that you understand quaternionsâyou could have replied to my first post with an explanation of the âsubtractâ terminology issue without spending several paragraphs defending your assertion that quaternions are really just 1x4 matrices containing complex numbers. If you are unhappy with the direction the thread took after that, perhaps you should reflect on your own conduct.

3 Likes

I declared that I was bored with continuing the conversation after what I had to say.

I forgot to hit unsubscribe though.

Just did, tootles.

2 Likes

quaternion1 / quaternion2 thats how u subtract

operator â/â cannot be applied to Quaternions

Incase anyone else digs up this thread, this is the correct answer.

3 Likes

Yes, though that is essentially dividing the quaternions through each other. Since we are dealing with unit quaternions only we can ignore the length. Usually the process is like this:

qr = q1 / q2

we need a non complex divisor so we simply multiply the top and bottom by q2^-1 so we get

qr = q1 * q2^-1 / (q2 * q2^-1)

Multiplying a quaternion by itâs own conjugate yields a non complex result and you get the squared magnitude of the quaternion. Since we have unit quaternions the divisor will be just 1. The result is simply

qr = q1 * q2^-1

This is exactly the same how you divide complex numbers. Unity does not implement the division operator so you have to multiply it manually by the inverse.

Just to clear up some confusion: Yes usually you can add, subtract, multiply and divide quaternions. However we only use quaternions to represent rotations. With rotations adding and subtracting them makes no sense just like adding and subtracting transformation matrices makes no sense. Thatâs why Unityâs Quaternion type does not implement those operations.

Just to point out: this thread is five and a half years old.

And thatâs not even THE correct answer. When combining rotations thereâs always an extra choice: which one is local with respect to the other. A*B.inverse() subtracts B using Aâs local coords, and B.inverse()*A does it with B global. It could be either one.

Well Iâm glad to see everyone is still alive and well.

11 Likes