# Math help



## dougbenham (May 9, 2009)

I need a little matrix math help. I'm attempting to do some matrix algebra including division. I have found several online resources that say the division of matrices is done like so:
AX=B
X = A^(-1) B

And in order to take the inverse of a matrix it must be a square...

So lets say I have the following:
AB = CD
Where A has dimensions (4, 4), B has the dimensions (4, 1), C has dimensions (4, 4), and D has dimensions (4, 1).

I am solving for A. So I would guess the first step is to "divide" by B. How do I do this since B doesn't have square dimensions? Any suggestions would be appreciated..


----------



## Rubikgenius (May 9, 2009)

Ummm... I am good at math, but matrixes are kind of hard. I kinda forgot how to do them


----------



## qqwref (May 9, 2009)

Here's the thing - both AB and CD are 4x1 matrices, right? They only have 4 'data points'. So you are trying to solve for 16 variables but you only have 4 equations. That's why you can't just use an inverse - if you could, it would imply there was a unique solution, which there isn't.

The best way to get something resembling a solution would be to write out CD (in terms of numbers) and AB (in terms of variables and whatnot); then you would get four equations. For instance the first one would be something like
B_1 A_11 + B_2 A_12 + B_3 A_13 + B_4 A_14 = (CD)_1.
Each of the four equations contains information about every variable in a given row of A. So, unless you know almost everything about A, there are an infinite number of solutions.


----------



## rjohnson_8ball (May 9, 2009)

Wouldn't the inverse of a 4x1 just be a 1x4 where each of the elements are reciprocals? That would cause

B B^(-1) = I

which is what you want. You might test this out on some samples to make sure it works. Let me know what the answer turns out to be, if I am mistaken.

@qqwref, you might be right. Maybe the question is to provide any A that solves it? If so, A could be a diagonal matrix?


----------



## JBCM627 (May 9, 2009)

rjohnson_8ball said:


> Wouldn't the inverse of a 4x1 just be a 1x4 where each of the elements are reciprocals?



Multiplying those won't give you a 4x4 identity matrix.


----------



## rjohnson_8ball (May 9, 2009)

JBCM627 said:


> rjohnson_8ball said:
> 
> 
> > Wouldn't the inverse of a 4x1 just be a 1x4 where each of the elements are reciprocals?
> ...



It might give a 1x1 matrix with a value of 1?


----------



## JBCM627 (May 9, 2009)

rjohnson_8ball said:


> It might give a 1x1 matrix with a value of 1?



4x1.1x4 matrix gives you a 4x4 matrix, which is what you would need. You can't multiply a 4x1.1x4 and end up with an identity matrix, although you can by taking a 1x4.4x1 matrix.


----------



## Kian (May 9, 2009)

Rubikgenius said:


> Ummm... I am good at math, but matrixes are kind of hard. I kinda forgot how to do them



But you're a genius. How can this be?


----------



## dougbenham (May 9, 2009)

qqwref said:


> Here's the thing - both AB and CD are 4x1 matrices, right? They only have 4 'data points'. So you are trying to solve for 16 variables but you only have 4 equations. That's why you can't just use an inverse - if you could, it would imply there was a unique solution, which there isn't.
> 
> The best way to get something resembling a solution would be to write out CD (in terms of numbers) and AB (in terms of variables and whatnot); then you would get four equations. For instance the first one would be something like
> B_1 A_11 + B_2 A_12 + B_3 A_13 + B_4 A_14 = (CD)_1.
> Each of the four equations contains information about every variable in a given row of A. So, unless you know almost everything about A, there are an infinite number of solutions.



Okay I'll tell you the practical side to the problem. I'm doing 3D transformational geometry for the CubeTeacher program. I'm making a function that finds the appropriate rotation matrix to obtain the new position without actually changing the current position variable. Hmm this is hard to explain. Lets say a point is at [0, 1, 0] in the format [x, y ,z].

Lets define the term 'applied point' as when we take the rotation matrix and multiply it by the point [0, 1, 0], therefore applying the rotation to the point. 

Now lets define the starting rotation matrix as the identity matrix:
1 0 0
0 1 0
0 0 1

When we calculate the 'applied point', we should get the same matrix, [0, 1, 0].

Now lets say I want to know what rotation matrix to use to get the 'applied point' to be [1, 0, 0] (rotated clockwise 90 degrees along the y-axis plane). So lets identify what we know and what we are looking for. We know the point ([0, 1, 0]). And we know the new point location ([1, 0, 0]). We want to know the new rotation matrix.

Therefore we can come up with an equation:
AB = C

Where A is the new rotation matrix, B is the original point location ([0, 1, 0]), and C is the new point location ([1, 0, 0]).

*So how would I solve for the new rotation matrix?* You can't divide both sides by B because B is not a square matrix.


----------



## JBCM627 (May 9, 2009)

dougbenham said:


> *So how would I solve for the new rotation matrix?* You can't divide both sides by B because B is not a square matrix.



Oh hey, we are going over SO3 a bit in my GR class right now. What qq suggested is probably similar to what you are looking for. You should be able to reduce A (your rotation matrix) down to a matrix containing 2 variables (since in rotating a point you only have 2 degrees of freedom), which gives you 3 systems of equations to solve with 2 variables after evaluating AC.

I'll be a bit more specific... you can accomplish any 3 space rotation by using just the Rx and Rz matrices, say. So Rz.Rx.B = C... and (Rz.Rx) will be your rotation matrix with 2 variables... the ammount you rotate around the x and z axes. (These rotations will be through angles we can call theta and phi, respectively). You should be able to at least approximately solve for theta and phi then.


----------



## dougbenham (May 9, 2009)

JBCM627 said:


> you can accomplish any 3 space rotation by using just the Rx and Rz matrices, say. So Rz.Rx.B = C... and (Rz.Rx) will be your rotation matrix with 2 variables... the ammount you rotate around the x and z axes. (These rotations will be through angles we can call theta and phi, respectively). You should be able to at least approximately solve for theta and phi then.



Ah.. Thank you for the explanation of this. I had not realized that any 3D space rotation was possible with only Rx and Rz.

Except my rotation matrix is just a single matrix. There is no Rx and Rz.. How would I get those from the total rotation matrix?

And also, I'm not quite sure how I would be able to approach this problem. I understand the logic behind it, however, I don't understand how I can "solve for theta and phi". Could you give me some guidance or perhaps an example with numbers?


----------



## ThatGuy (May 9, 2009)

Multiply C and D. Then create the matrix A of all variables. Then multiply it with B and set each equal. Of course, that gives you a bunch of system of equations. These types of problems don't really appear anywhere much.


----------



## JBCM627 (May 9, 2009)

dougbenham said:


> Except my rotation matrix is just a single matrix. There is no Rx and Rz.. How would I get those from the total rotation matrix?


If you rotate the point B about Rx, you will have:
Rx.B
Then rotating that about Rz, you have:
Rz.(Rx.B)
And by associativity, you have:
(Rz.Rx).B
So your rotation matrix is R = (Rz.Rx)

Explicitly, this is... much easier to type out in mathematica on here. See the bottom of attached file for what R is.



dougbenham said:


> And also, I'm not quite sure how I would be able to approach this problem. I understand the logic behind it, however, I don't understand how I can "solve for theta and phi". Could you give me some guidance or perhaps an example with numbers?


Unless the case is simple (like [1,0,0] -> [0,1,0]), I'm not sure you'll be able to solve for theta and phi analytically.

For simplicity's sake, I'm only going to do this explicitly for the aforementioned matrices. Also, if I type out a 1x3 matrix/vector, assume it should be transposed.

For a vector [x,y,z], it is obvious that in R:[1,0,0] -> [0,1,0], R is a 90-degree rotation about the z-axis. Assuming we don't know that though, we have
R.[1,0,0] = [Cos[phi], Sin[phi], 0] = [0,1,0].
So Sin[phi] = 1, phi = pi/2.

plugging in phi = pi/2 gives us the rotation matrix:

```
0  -Cos[theta] Sin[theta]
1       0         0
0  Sin[theta]  Cos[theta]

And it doesn't matter what theta is, so if you like, it can be something like this:

0  0  1
1  0  0
0  1  0

Or even in this special case,

0  0  0
1  0  0
0  0  0
```

Again, I'm guessing most cases can't be solved analytically, so you would need some way to approximate theta and phi. This also only works for normalized vectors; as in, B and C will have to have the same magnitude.

I'll state the following explicitly too:
if you have B = [x,y,z] and C = [x',y',z']
Then you have the following system of equations to solve for theta and phi:
x = x'*Cos[phi] + y'*Sin[phi]
y = z'*Sin[theta] + Cos[theta]*(y'*Cos[phi] - x'*Sin[phi])
z = z'*Cos[theta] + Sin[theta]*(x'*Sin[phi] - y'*Cos[phi])
...and an approximation would just entail minimizing these functions.

.


----------



## dougbenham (May 10, 2009)

JBCM627 said:


> dougbenham said:
> 
> 
> > Except my rotation matrix is just a single matrix. There is no Rx and Rz.. How would I get those from the total rotation matrix?
> ...



Wow this is difficult stuff. I've reread your post like 10 times to completely understand it  How did you get that system of equations at the end?


----------



## JBCM627 (May 10, 2009)

dougbenham said:


> Wow this is difficult stuff. I've reread your post like 10 times to completely understand it  How did you get that system of equations at the end?


Glad it made sense eventually  I probably could've been a bit clearer. What is actually a really cool extension of this though is to do hyperbolic rotations (replacing sin with sinh and cos with cosh) in one of the matrices... this gives rise to a lot of the math behind special relativity.

For those equations, I had mathematica solve for x, y, and z... I probably should've done it the other way around and just had it evaluate R.B, to give x',y', and z'. That would've made more sense. I'll do that real fast...
x' = x*Cos[Phi] - y*Cos[Theta]*Sin[Phi] + z*Sin[Theta]*Sin[Phi]
y' = y*Cos[Theta]*Cos[Phi] - z*Cos[Phi]*Sin[Theta] + x*Sin[Phi]
z' = z*Cos[Theta] + y*Sin[Theta]

Also, I played around with it a bit more, and you can explicitly solve for Sin[theta] and Sin[phi]. You can just substitute Sqrt[1-Sin[phi]^2] in for Cos[phi], and similarly for Cos[theta]. The exact answers for theta and phi (phi in particular) are horrendously long though, so I'm not going to paste it in here... if you are interested I can email it to you.

Another important thing to note is that this only gives you one particular solution. There are infinitely many ways you could move one point to another on a circle... rotating about the x and z axes (as opposed to other arbitrary ones) just happens to look a bit nicer.


----------



## Lord Voldemort (May 11, 2009)

Rubikgenius said:


> Ummm... I am good at math, but matrixes are kind of hard. I kinda forgot how to do them



I feel like I'm adding to the problem, but what was the point of the post?


----------



## dougbenham (May 11, 2009)

JBCM627 said:


> For those equations, I had mathematica solve for x, y, and z... I probably should've done it the other way around and just had it evaluate R.B, to give x',y', and z'. That would've made more sense. I'll do that real fast...
> x' = x*Cos[Phi] - y*Cos[Theta]*Sin[Phi] + z*Sin[Theta]*Sin[Phi]
> y' = y*Cos[Theta]*Cos[Phi] - z*Cos[Phi]*Sin[Theta] + x*Sin[Phi]
> z' = z*Cos[Theta] + y*Sin[Theta]


Thats exactly what I derived from the rotation matrix you supplied. And I figured I'd be trying to solve for theta in the bottom equation first since it only has theta (not phi). So for the past day I've been sitting and looking at that equation trying to think of how to solve for theta 
I couldn't think of any useful trig identities that would help.



JBCM627 said:


> You can just substitute Sqrt[1-Sin[phi]^2] in for Cos[phi], and similarly for Cos[theta].


Thats what I was looking for haha. And so from that you can derive the value of theta (attached image).


Now I just got mathematica today and I'm a bit curious of how to use it.. I typed in 'Solve[a == zcos[t] + ysin[t], t]' and evaluated it (I used a instead of z'). It gave the response '{{}}'. What did I do wrong? Shouldn't it have given me the long expression I included in the attachment?


----------



## JBCM627 (May 11, 2009)

dougbenham said:


> Now I just got mathematica today and I'm a bit curious of how to use it.. I typed in 'Solve[a == zcos[t] + ysin[t], t]' and evaluated it (I used a instead of z'). It gave the response '{{}}'. What did I do wrong? Shouldn't it have given me the long expression I included in the attachment?



Yeah, careful on the syntax... capital Sin and Cos. You'll need to explicitly type out a space or "*" between the variables and functions too, as in, "z*Cos[t]".

It also doesn't give me exactly what is in your attachment, but perhaps your expression somehow reduces to what it gave me. But haha, just wait til you see how long the expression for phi is... I had to scroll.


----------



## dougbenham (May 11, 2009)

JBCM627 said:


> dougbenham said:
> 
> 
> > Now I just got mathematica today and I'm a bit curious of how to use it.. I typed in 'Solve[a == zcos[t] + ysin[t], t]' and evaluated it (I used a instead of z'). It gave the response '{{}}'. What did I do wrong? Shouldn't it have given me the long expression I included in the attachment?
> ...



Ah thanks. I didn't realize the capital would matter 

I attached what it gave me for theta. Could you tell me how it got that? Like a step-by-step perhaps?


----------



## JBCM627 (May 11, 2009)

dougbenham said:


> I attached what it gave me for theta. Could you tell me how it got that? Like a step-by-step perhaps?


Just algebra...
Let c = Cos[t].
a = s*c+y*Sqrt[1-c^2]
(a-zc)^2 = y^2-(yc)^2
(z^2-y^2)*c^2 - (2az)*c + (a^2+y^2) = 0
Then just use the quadratic formula, and it comes out.


----------



## dougbenham (May 12, 2009)

JBCM627 said:


> dougbenham said:
> 
> 
> > I attached what it gave me for theta. Could you tell me how it got that? Like a step-by-step perhaps?
> ...



Mm. Okay that sounds good. Now solving for phi is a bit longer. I did it in mathematica. It gives me a nice little equation. So this is all making sense now. I just have to plug in the equations to solve for the 2 angles, then plug those angles back into the rotation matrix. Hmm. Is there any way to make mathematica show its work? Or solve step-by-step? If there isn't could you perhaps point me in the right direction for how to solve for phi by hand (w/o mathematica). I have difficulty seeing places to use all the trig identities. Takes some creativity


----------



## JBCM627 (May 12, 2009)

You have solved for Cos[theta] already. The only trig identity you need is Sin = Sqrt[1-Cos^2]... you can use it to get all the equations in terms of Cos, then substitute your solution for Cos[theta] in, and solve for Cos[phi]. It will probably be nasty algebraically; not something I would care to do by hand. I'm also not familiar enough with mathematica to know if you can force it to show steps.


----------



## dougbenham (May 13, 2009)

JBCM627 said:


> You have solved for Cos[theta] already. The only trig identity you need is Sin = Sqrt[1-Cos^2]... you can use it to get all the equations in terms of Cos, then substitute your solution for Cos[theta] in, and solve for Cos[phi]. It will probably be nasty algebraically; not something I would care to do by hand. I'm also not familiar enough with mathematica to know if you can force it to show steps.



Awesome. Thanks for your help. It worked out perfectly in my program. It works extremely well. I can rotate to any point now


----------

