I’m trying to parameterize orthogonal matrix via a series of Givens rotations as follows

```
data{
int TT; //TT * TT matrix
}
transformed data{
int dim_theta_i2toQ=TT*(Q-1)-(Q-1)*(Q+2)/2; //dimentionality of dim_theta_i2toQ
}
parameters {
vector<lower=-pi(),upper=pi()>[TT-1] theta_i1; //theta[1,j]; j=2,...,TT
vector<lower=0,upper=pi()>[dim_theta_i2toQ] theta_i2toQ; //theta[i,j]; i=2,...,TT-1; j=i+1,...,TT
} //There is bijection between theta=[theta_i1,theta_i2toQ] (theta_i1 is in [-pi,pi] and theta_i2 is in [0,pi]) and TT*TT orthogonal matrix U
transformed parameters {
matrix[TT,TT] U_transpose=diag_matrix(rep_vector(1,TT));
{
int index=1;
real cos1;
real sin1;
vector[TT] Ui_tmp;
vector[TT] Uj_tmp;
for(i in 1:(TT-1)){
for(j in (i+1):TT){
cos1=cos(theta_i1[index]);
sin1=sin(theta_i1[index]);
Ui_tmp=cos1*U_transpose[:,i]-sin1*U_transpose[:,j];
Uj_tmp=sin1*U_transpose[:,i]+cos1*U_transpose[:,j];
U_transpose[:,i]=Ui_tmp;
U_transpose[:,j]=Uj_tmp;
index+=1;
}
index=1;
}else{
for(j in (i+1):TT){
cos1=cos(theta_i2toQ[index]);
sin1=sin(theta_i2toQ[index]);
Ui_tmp=cos1*U_transpose[:,i]-sin1*U_transpose[:,j];
Uj_tmp=sin1*U_transpose[:,i]+cos1*U_transpose[:,j];
U_transpose[:,i]=Ui_tmp;
U_transpose[:,j]=Uj_tmp;
index+=1;
}
}
}
}
```

Mathematics about orthogonal matrix parameterization [1]:

This is part of my program, and I found it runs slowly. Could this orthogonal matrix parameterization be more efficient?

Thanks.

Reference:

[1] Matteson, D. S., & Tsay, R. S. (2017). Independent component analysis via distance covariance. *Journal of the American Statistical Association* , *112* (518), 623-637.