## Hello, I’m trying to do the QR decomposition using Gram Schmidt but I’m having problems because I can’t find the R matrix. Could you help me?

First of all I don’t want to use the reflection method. Only Gram-Schmidt.

So here’s the program for Gram-Schidt:

GS[A_] := Module[{u = {}, col, e = {}, ae, t},   col = Transpose[A];(* list with the column vectors of A *)   u = Append[u, col[[1]]];   e = AppendTo[e, u[[1]]/Norm[u[[1]]]];   For[i = 2, i <= Length[A], i++, ae = 0; t = 1;    While[t <= i - 1, ae = ae - (col[[i]].e[[t]])*e[[t]]; t++];    u = AppendTo[u, col[[i]] + ae];    e = Append[e, u[[i]]/Norm[u[[i]]]]]; {u, e}] 

with this simple matrix:

B = {{1, 3, 1}, {2, 2, 1}, {3, 2, 3}}; 

gives

{{{1, 2, 3}, {29/14, 1/7, -(11/14)}, {14/69, -(49/69), 28/69}}, {{1/    Sqrt[14], Sqrt[2/7], 3/Sqrt[14]}, {29/Sqrt[966], Sqrt[2/    483], -(11/Sqrt[966])}, {2/Sqrt[69], -(7/Sqrt[69]), 4/Sqrt[69]}}} 

Now the code for QR decomposition is:

QR[A_] := Module[{Ak, i, Q, R = {}, a = {}, col, k},   Ak = FullSimplify[GS[A]];   col = Transpose[A];(* list with the column vectors of A *)   Q = MatrixForm[Transpose[Ak[[2]]]];   For[i = 1, i <= Length[Ak[[2]]], i++, a = {}; k == 1;    While[k <= Length[col], a = AppendTo[a, col[[k]]. Ak[[2]][[i]]];      k++];     R = AppendTo[R, a]];   R = UpperTriangularize[R]; {Q, R}] 

but I can’t find matrix R, once it’s giving me a list like this{{},{},{}} and I’m stuck. Please help me.Thanks!

## Manipulation of a symbolic matrix expression

I have the following code:

enter code here 

## Simplifying elements of a matrix

These elements of the matrices can be simplified by hand much further{roots can be cancelled and all}, yet the Fullsimplify in Mathematica doesn’t simply it completely.

The matrix is:

{{-((p^2 + Sqrt[m^2 + p^2] p3 - p0 (Sqrt[m^2 + p^2] + p3))/    m), -(((p^2 + (m + Sqrt[m^2 + p^2]) (m - p0)) (-I p2 + Sqrt[       p^2 - p2^2 - p3^2]))/(m (m + Sqrt[m^2 + p^2]))), (1/(   m (m + Sqrt[m^2 + p^2])))(-p^2 p2 + Sqrt[m^2 + p^2] p0 p2 +      m (-Sqrt[m^2 + p^2] + p0) p2 - I p0 p3 Sqrt[p^2 - p2^2 - p3^2] +      I p3 Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)] +      I m^2 (I p2 + Sqrt[p^2 - p2^2 - p3^2]) +      I m (p3 Sqrt[p^2 - p2^2 - p3^2] +         Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)])), (1/(   m (m + Sqrt[m^2 + p^2])))(-I (Sqrt[m^2 + p^2] - p0) p2^2 +      I m^2 (p0 - p3) -      I p3 (p^2 + Sqrt[m^2 + p^2] p3 - p0 (Sqrt[m^2 + p^2] + p3)) +      p2 (-p0 Sqrt[p^2 - p2^2 - p3^2] +         Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)]) +      m (I (p0 - p3) (Sqrt[m^2 + p^2] + p3) +         p2 (-I p2 + Sqrt[p^2 - p2^2 - p3^2])))}, {-(((p^2 + (m + Sqrt[          m^2 + p^2]) (m - p0)) (I p2 + Sqrt[p^2 - p2^2 - p3^2]))/(    m (m + Sqrt[m^2 + p^2]))), (-p^2 + p0 (Sqrt[m^2 + p^2] - p3) +     Sqrt[m^2 + p^2] p3)/m, (1/(   m (m + Sqrt[m^2 + p^2])))(-I (Sqrt[m^2 + p^2] - p0) p2^2 +      I m^2 (p0 + p3) + I m (Sqrt[m^2 + p^2] - p3) (p0 + p3) +      I p3 (p^2 - Sqrt[m^2 + p^2] p0 - Sqrt[m^2 + p^2] p3 + p0 p3) +      p0 p2 Sqrt[p^2 - p2^2 - p3^2] -      p2 Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)] -      m p2 (I p2 + Sqrt[p^2 - p2^2 - p3^2])), (1/(   m (m + Sqrt[m^2 + p^2])))(p^2 p2 + m (Sqrt[m^2 + p^2] - p0) p2 -      Sqrt[m^2 + p^2] p0 p2 + I p0 p3 Sqrt[p^2 - p2^2 - p3^2] -      I p3 Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)] +      m^2 (p2 + I Sqrt[p^2 - p2^2 - p3^2]) +      I m (-p3 Sqrt[p^2 - p2^2 - p3^2] +         Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)]))}, {(1/(   m (m + Sqrt[m^2 + p^2])))(-p^2 p2 + Sqrt[m^2 + p^2] p0 p2 +      m (-Sqrt[m^2 + p^2] + p0) p2 + I p0 p3 Sqrt[p^2 - p2^2 - p3^2] -      I p3 Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)] +      I m^2 (I p2 + Sqrt[p^2 - p2^2 - p3^2]) +      I m (-p3 Sqrt[p^2 - p2^2 - p3^2] +         Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)])), (1/(   m (m + Sqrt[m^2 + p^2])))(I (Sqrt[m^2 + p^2] - p0) p2^2 -      I m^2 (p0 + p3) +      I p3 (-p^2 + Sqrt[m^2 + p^2] p0 + Sqrt[m^2 + p^2] p3 - p0 p3) +      p0 p2 Sqrt[p^2 - p2^2 - p3^2] -      p2 Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)] -      I m ((Sqrt[m^2 + p^2] - p3) (p0 + p3) +         p2 (-p2 - I Sqrt[p^2 - p2^2 - p3^2]))), (-p^2 +     p0 (Sqrt[m^2 + p^2] - p3) + Sqrt[m^2 + p^2] p3)/   m, ((p^2 + (m + Sqrt[m^2 + p^2]) (m - p0)) (-I p2 + Sqrt[      p^2 - p2^2 - p3^2]))/(m (m + Sqrt[m^2 + p^2]))}, {(1/(   m (m + Sqrt[m^2 + p^2])))(I (Sqrt[m^2 + p^2] - p0) p2^2 -      I m^2 (p0 - p3) +      I p3 (p^2 + Sqrt[m^2 + p^2] p3 - p0 (Sqrt[m^2 + p^2] + p3)) +      p2 (-p0 Sqrt[p^2 - p2^2 - p3^2] +         Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)]) +      m (-I (p0 - p3) (Sqrt[m^2 + p^2] + p3) +         p2 (I p2 + Sqrt[p^2 - p2^2 - p3^2]))), (1/(   m (m + Sqrt[m^2 + p^2])))(p^2 p2 + m (Sqrt[m^2 + p^2] - p0) p2 -      Sqrt[m^2 + p^2] p0 p2 - I p0 p3 Sqrt[p^2 - p2^2 - p3^2] +      I p3 Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)] +      m^2 (p2 + I Sqrt[p^2 - p2^2 - p3^2]) +      I m (p3 Sqrt[p^2 - p2^2 - p3^2] +         Sqrt[-(m^2 + p^2) (-p^2 + p2^2 + p3^2)])), ((p^2 + (m + Sqrt[         m^2 + p^2]) (m - p0)) (I p2 + Sqrt[p^2 - p2^2 - p3^2]))/(   m (m + Sqrt[m^2 + p^2])), -((    p^2 + Sqrt[m^2 + p^2] p3 - p0 (Sqrt[m^2 + p^2] + p3))/m)}} 

some of the substitution we can make are (already taken),

{p1 -> Sqrt[p^2 - p2^2 - p3^2], e -> Sqrt[p^2 + m^2]} Assuming[{Element[{p0, p, p2, p3}, Reals], m > 0}, sat2 = sat // FullSimplify] 

What more could be done to simplify the elements of the matrix?

## How to decompose a 4×4 TRS transformation matrix with negative scale?

As the title says I need to decompose 4×4 TRS transformation matrices and extract the proper scale vectors and the proper rotation vectors (or rotation quaternions).

I know how to extract those information when the upper 3×3 matrix determinant is not negative. The problem is that those matrices can have that negative determinant and as far as I could understand, this negative determinant indicates a flip or mirrored transformation.

What do I have to do to extract the proper values for scale and rotation in those cases?

## Mathematica not computing Matrix problem, just returning multiplication expression

I’m doing some very simple matrix operations in Mathematica, but for some reason, the last operation I’m trying to evaluate is not returning the actual product, just shows the symbolic multiplication.

P = { {1, 2}, {3, 4}} /10;  im = {{1},{1}} in = {{1},{1}}  A = ArrayFlatten[ { {KroneckerProduct[in\[Transpose], IdentityMatrix[2]]}, {KroneckerProduct[IdentityMatrix[2], im\[Transpose]]} } ]  p = Flatten[P] // MatrixForm  A.p 

This last operation, $$A\cdot p$$ is returning the following:

Why is that so?

## What is an efficient way to get a look-at direction from either a quaternion or a transformation matrix?

So, I have an object in my custom engine (C++), with a column-major transform in world space. I’m using a package that takes a look-at direction as an input. What’s the most efficient way to get a look-at direction from this transform? Do I extract the rotation matrix? Do I try to extract a quaternion?

## Creating a Matrix from Integrals

I have a table of integrals that I want to put in an nxn matrix. I tried doing it in the following way

phix[x_, n_] := Exp[-n \[Alpha] x^2/2] phiy[y_, m_] := Exp[-m \[Beta] y^2/2] const = {List[     Integrate[      x^2 y^2 phix[x, n1] phix[x, n2] phiy[y, m1] phiy[y, m2], {x, 0,        Infinity}, {y, 0, Infinity}], {n1, 1, 3}, {n2, 1, 3}, {m1, 1,       3}, {m2, 1, 3}]} // MatrixForm 

but what I get as output is the following, instead of the matrix form.

I also tried to use Table instead of list, but still don’t get the output in the matrix form. I need the output as a matrix because I would like to calculate the e-values and e-vectors.

Any help would be greatly appreciated.

## Creating a block matrix from arrays of blocks

I am trying to generate a matrix from square blocks. Effectively, I have a $$n×n$$ matrix polynomial $$P(l)$$, the $$qth$$ derivative of $$P(l)$$ with respect to $$l$$, which is denoted by $$P^{(q)} (l)$$, and a block of zeroes, which I’ll just call $$0$$. I have some integer $$k$$ such that if $$k=1$$ then I am generating the matrix

$$R= \begin{pmatrix} P(l) \end{pmatrix}$$

If $$k=2$$ then I should generate

$$R = \begin{pmatrix} P(l) & 0 \ \frac{1}{1!} P^{(1)}(l) & P(l) \end{pmatrix}$$

If $$k=3$$ then

$$R = \begin{pmatrix} P(l) & 0 & 0 \ \frac{1}{1!} P^{(1)}(l) & P(l) & 0 \ \frac{1}{2!} P^{(2)}(l) & \frac{1}{1!} P^{(1)}(l) & P(l) \end{pmatrix}$$

and so forth. Generally,

$$R = \begin{pmatrix} P(l) & 0 & \cdots & 0 & 0 \ \frac{1}{1!} P^{(1)}(l) & P(l) & \cdots & 0 & 0 \ \frac{1}{2!} P^{(2)}(l) & \frac{1}{1!} P^{(1)}(l) & \cdots & 0 & 0 \ \vdots & \vdots & \ddots & \vdots & \vdots \ \frac{1}{(k-1)!} P^{(k-1)}(l) & \frac{1}{(k-2)!} P^{(k-2)}(l) & \cdots & \frac{1}{1!} P^{(1)}(l) & P(l) \end{pmatrix}$$

is an $$nk×nk$$ matrix.

I prefer a simple and understandable way and for that I thought to start with a zero matrix $$R$$ of dimensions $$nk×nk$$ and then with two "for" loops to full the initial zero matrix, putting the corresponding derivative which is needed. I’m not sure in what should go as my statement in "for" loops. I found other questions which were similar but more complicated and specific. Any help appreciated, thank you.

## Given a row sum vector and a column sum vector, determine if they can form a boolean matrix

For example, for a boolean matrix of size $$3×4$$, the row sum vector $$R = (3, 3, 0, 0)$$ and the column sum vector $$C = (2, 2, 2)$$ form a match because I can construct the boolean matrix:

$$\begin{matrix} & \begin{bmatrix} 1 & 1 & 0 & 0\ 1 & 1 & 0 & 0\ 1 & 1 & 0 & 0 \end{bmatrix} & \begin{pmatrix} 2\2\2 \end{pmatrix} = C \ R = &\begin{pmatrix} 2 & 2 & 0 & 0 \end{pmatrix} \end{matrix}$$

However, the column vector $$C’ = (4, 1, 1)$$ doesn’t form a match with $$R$$.

So given two vectors whose values are sorted in descending order $$R_{1, w}$$ and $$C_{h, 1}$$, and whose accumulated sum is the same, $$T = \sum_jR[1, j] = \sum_iC[i, 1]$$, how can I polynomically check if $$R$$ and $$C$$ form a matching because I can form a matrix $$M_{h,w}$$ having $$R$$ and $$C$$ as row and column sum vectors?

More specifically, in case it can help to make the check algorithm faster, in my specific case, R and C has the following properties:

• $$h \leq w$$
• The number of positive values of $$R$$ and $$C$$ is $$> w$$. For example, $$R$$, in the example, has two positive values and $$C$$ has three positive values, and it happens that $$2 + 3 > w = 4$$.

## Regression coefficients of a matrix

Assume, we have a matrix X with the shape of (1000,20), how to generate the regression coefficients of it ?