5
$\begingroup$

RotationMatrix[θ, {0, 0, 1}] // Matrixform

gives

$\begin{pmatrix} \cos (\theta ) & -\sin (\theta ) & 0 \\ \sin (\theta ) & \cos (\theta ) & 0 \\ 0 & 0 & 1 \\ \end{pmatrix}$

Now, I would like to add a row and column to it like this

$\begin{pmatrix} \color{red}{1}&\color{red}{0}&\color{red}{0}&\color{red}{0}\\ \color{red}{0}& \cos (\theta ) & -\sin (\theta ) & 0 \\ \color{red}{0}&\sin (\theta ) & \cos (\theta ) & 0 \\ \color{red}{0}& 0 & 0 & 1 \\ \end{pmatrix}$

I would like to just be able to add red ones on any RotationMatrix[θ, w] that I define as done in the beginning.

Is there a simple way to achieve this?

On a similar note,

RotationMatrix[I ϕ, {0, 0, 1}] // MatrixForm

gives

$\begin{pmatrix} \cosh (\phi ) & -i \sinh (\phi ) & 0 \\ i \sinh (\phi ) & \cosh (\phi ) & 0 \\ 0 & 0 & 1 \\ \end{pmatrix}$

So here I would like to add a row:

$\begin{pmatrix}\cosh \phi & -i\sinh \phi & 0 & \color{red}0 \\ i\sinh \phi & \cosh \phi & 0 & \color{red}0 \\ 0 & 0 & 1 & \color{red}0 \\ \color{red}0 & \color{red}0 & \color{red}0 & \color{red}1\end{pmatrix}$

And here also I would like to have the flexibility of defining the things in a black part about any vector. Any suggestions?

$\endgroup$
2
  • 1
    $\begingroup$ Why couldn't you just use RotationMatrix[] directly? Look at e.g. RotationMatrix[θ, IdentityMatrix[4][[{2, 3}]]] and RotationMatrix[I ϕ, IdentityMatrix[4][[{1, 2}]]]. (See this as well.) $\endgroup$ Jan 29, 2021 at 7:45
  • $\begingroup$ This is perfect. Thanks. Didn't know that I could use two vectors to define a higher dimension rotation like that. $\endgroup$
    – user824530
    Jan 29, 2021 at 16:32

2 Answers 2

10
$\begingroup$
rm = RotationMatrix[θ, {0, 0, 1}];
rm2 = RotationMatrix[I θ, {0, 0, 1}]; 

1. ArrayFlatten

ArrayFlatten[{{1, 0}, {0, rm}}] // MatrixForm

enter image description here

ArrayFlatten[{{rm2, 0}, {0, 1}}] // MatrixForm

enter image description here

2. PadRight + PadLeft

PadRight[{{1}}, {4, 4}] + PadLeft[rm, {4, 4}] // MatrixForm

enter image description here

PadLeft[{{1}}, {4, 4}] + PadRight[rm2, {4, 4}] // MatrixForm

enter image description here

You can also do

MatrixForm /@ {Prepend[UnitVector[4, 1]] @ PadLeft[rm, {3, 4}],
   Append[UnitVector[4, 4]] @ PadRight[rm2, {3, 4}]} // Row

or

MatrixForm /@ {MapAt[1 &, {1, 1}]@PadLeft[rm, {4, 4}],
   MapAt[1 &, {-1, -1}]@PadRight[rm2, {4, 4}]} // Row

or

MatrixForm /@ {ReplacePart[ {1, 1} -> 1] @ PadLeft[rm, {4, 4}],
   ReplacePart[ {-1, -1} -> 1] @ PadRight[rm2, {4, 4}]} // Row

to get

enter image description here

3. ArrayPad

MatrixForm /@ {Prepend[UnitVector[4, 1]] @ ArrayPad[rm, {{0}, {1, 0}}],
   Append[UnitVector[4, 4]] @ ArrayPad[rm2, {{0}, {0, 1}}]} // Row

enter image description here

4. SparseArray`SparseBlockMatrix

SparseArray`SparseBlockMatrix[{{1, 1} -> {{1}}, {2, 2} -> rm}] // MatrixForm

enter image description here

SparseArray`SparseBlockMatrix[{{1, 1} -> rm2, {2, 2} -> {{1}}}] // MatrixForm

enter image description here

$\endgroup$
5
$\begingroup$
matrix = RotationMatrix[θ, {0, 0, 1}];
matrix1 = SparseArray[{Band[{1, 1}] -> {matrix, {{1}}}}];
matrix2 = SparseArray[{Band[{1, 1}] -> {{{1}}, matrix}}];
matrix // MatrixForm
matrix1 // MatrixForm
matrix2 // MatrixForm
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.