28 #ifndef _mpqc_src_lib_chemistry_qc_mbptr12_pt2r12_utils_h
29 #define _mpqc_src_lib_chemistry_qc_mbptr12_pt2r12_utils_h
34 int triang_half_INDEX_ordered(
int i,
int j) {
39 int triang_half_INDEX(
int i,
int j) {
40 return((i>j) ? triang_half_INDEX_ordered(i,j) : triang_half_INDEX_ordered(j,i));
44 int ordinary_INDEX(
int i,
int j,
int coldim) {
49 int tpdm_index(
int i,
int j,
int k,
int l,
int coldim){
52 int ind_half1=ordinary_INDEX(i,j,coldim);
53 int ind_half2=ordinary_INDEX(k,l,coldim);
54 return(triang_half_INDEX(ind_half1,ind_half2));
58 void vector_to_symmmatrix(RefSymmSCMatrix &matrix,
const RefSCVector &vector) {
59 int dim = matrix.dim().n();
60 for(
int i=0; i<dim; i++){
61 for(
int j=0; j<=i; j++) {
62 matrix.set_element(i,j,vector.get_element(triang_half_INDEX(i,j)));
68 void symmmatrix_to_vector(RefSCVector &vector,
const RefSymmSCMatrix &matrix) {
69 int dim = matrix.dim().n();
70 for(
int i=0; i<dim; i++){
71 for(
int j=0; j<=i; j++) {
72 vector.set_element(triang_half_INDEX(i,j),matrix.get_element(i,j));
78 void vector_to_matrix(RefSCMatrix &matrix,
const RefSCVector &vector) {
79 int dim1 = matrix.rowdim().n();
80 int dim2 = matrix.coldim().n();
81 for(
int i=0; i<dim1; i++) {
82 for(
int j=0; j<dim2; j++) {
83 matrix.set_element(i,j,vector.get_element(ordinary_INDEX(i,j,dim2)));
88 int lowerupper_index(
int p,
int q);
91 void vector_to_matrix(RefSCMatrix &matrix,
const RefSCVector &vector,
const SpinCase2 &pairspin) {
92 int dim1 = matrix.rowdim().n();
93 int dim2 = matrix.coldim().n();
94 if(pairspin==AlphaBeta) {
95 for(
int i=0; i<dim1; i++) {
96 for(
int j=0; j<dim2; j++) {
97 matrix.set_element(i,j,vector.get_element(ordinary_INDEX(i,j,dim2)));
103 for(
int i=0; i<dim1; i++) {
104 for(
int j=0; j<i; j++) {
105 const double value = vector.get_element(lowerupper_index(i,j));
106 matrix.set_element(i,j,value);
107 matrix.set_element(j,i,-value);
114 void matrix_to_vector(RefSCVector &vector,
const RefSCMatrix &matrix) {
115 int dim1 = matrix.rowdim().n();
116 int dim2 = matrix.coldim().n();
117 for(
int i=0; i<dim1; i++) {
118 for(
int j=0; j<dim2; j++) {
119 vector.set_element(ordinary_INDEX(i,j,dim2),matrix.get_element(i,j));
125 void matrix_to_vector(RefSCVector &vector,
const RefSymmSCMatrix &matrix) {
126 int n = matrix.dim().n();
127 for(
int i=0; i<n; i++) {
128 for(
int j=0; j<=i; j++) {
129 const double value = matrix.get_element(i,j);
130 vector.set_element(ordinary_INDEX(i,j,n),value);
131 vector.set_element(ordinary_INDEX(j,i,n),value);
137 void matrix_to_vector(RefSCVector &vector,
const RefDiagSCMatrix &matrix) {
138 int n = matrix.dim().n();
139 for(
int i=0; i<n; i++) {
140 const double value = matrix.get_element(i);
141 vector.set_element(ordinary_INDEX(i,i,n),value);
146 void matrix_to_vector(RefSCVector &vector,
const RefSCMatrix &matrix,
const SpinCase2 &pairspin) {
147 int dim1 = matrix.rowdim().n();
148 int dim2 = matrix.coldim().n();
149 if(pairspin==AlphaBeta) {
150 for(
int i=0; i<dim1; i++) {
151 for(
int j=0; j<dim2; j++) {
152 vector.set_element(ordinary_INDEX(i,j,dim2),matrix.get_element(i,j));
157 for(
int i=0; i<dim1; i++) {
158 for(
int j=0; j<i; j++) {
159 vector.set_element(lowerupper_index(i,j),matrix.get_element(i,j));
166 int lowertriang_index(
int p,
int q) {
168 throw ProgrammingError(
"lowertriang_index(p,q) -- q must be smaller than p.",__FILE__,__LINE__);
170 int index=p*(p+1)/2+q-p;
174 int lowerupper_index(
int p,
int q) {
176 return(lowertriang_index(p,q));
179 return(lowertriang_index(q,p));
182 throw ProgrammingError(
"lowerupper_index(p,q) -- p and q are not allowed to be equal.",__FILE__,__LINE__);
185 double indexsizeorder_sign(
int p,
int q) {
197 int antisym_pairindex(
int i,
int j) {
198 int max_ij = std::max(i, j);
199 int min_ij = std::min(i, j);
200 return (max_ij -1)* max_ij/2 + min_ij;
206 template <
typename MatrixType>
207 double get_4ind_antisym_matelement(MatrixType & MM,
const int & U1,
const int & U2,
208 const int & L1,
const int & L2)
210 if(U1 == U2 || L1 == L2)
return 0.0;
213 int uppind = antisym_pairindex(U1, U2);
214 int lowind = antisym_pairindex(L1, L2);
215 const double totalsign = indexsizeorder_sign(U1,U2) * indexsizeorder_sign(L1, L2);
216 return totalsign * MM.get_element(uppind, lowind);
222 template <
typename MatrixType>
223 double get_4ind_matelement(MatrixType & MM,
const int & U1,
const int & U2,
224 const int & L1,
const int & L2,
225 const int & UppDim,
const int & LowDim)
227 int uppind = U1 * UppDim + U2;
228 int lowind = L1 * LowDim + L2;
229 return MM.get_element(uppind, lowind);
232 RefSCMatrix convert_RefSC_to_local_kit(
const RefSCMatrix& A)
235 Ref<LocalSCMatrixKit> kit_cast_to_local; kit_cast_to_local << A.kit();
236 if (kit_cast_to_local.null()) {
237 Ref<LocalSCMatrixKit> local_kit =
new LocalSCMatrixKit();
238 RefSCMatrix A_local = local_kit->matrix(A.rowdim(), A.coldim());
248 RefSCMatrix transform_one_ind(RefSCMatrix AA, RefSCMatrix BB,
int whichindex,
int onedim)
251 ExEnv::out0() <<
"test transform_one_ind:\n";
252 ExEnv::out0() << (onedim*onedim)<<
", " << BB->nrow() <<
", "<< BB->ncol() <<
"\n";
256 MPQC_ASSERT(((onedim*onedim) == BB->nrow()) and (BB->nrow() == BB->ncol()));
257 RefSCMatrix res = BB->clone();
259 int ext_ind, int_ind, row, col, Ap, Bp, Cp, Dp, A, B, C, D, a, b, c, d, f;
260 ext_ind = int_ind = row = col = Ap = Bp = Cp = Dp = A = B = C = D = a = b = c = d = f = 0;
262 for (a = 0; a < onedim; ++a)
264 for (b = 0; b < onedim; ++b)
266 for (c = 0; c < onedim; ++c)
268 for (d = 0; d < onedim; ++d)
271 for (f = 0; f < onedim; ++f)
276 ext_ind = Ap = a; int_ind = A = f; B = b; C = c; D = d;
277 xx += AA->get_element(ext_ind, int_ind) * BB->get_element(A*onedim + B, C*onedim + D);
280 ext_ind = Bp = a; int_ind = B = f; A = b; C = c; D = d;
281 xx += AA->get_element(ext_ind, int_ind) *BB->get_element(A*onedim + B, C*onedim + D);
284 ext_ind = Cp = a; int_ind = C = f; A = b; B = c; D = d;
285 xx += BB->get_element(A*onedim + B, C*onedim + D)*AA->get_element(ext_ind, int_ind);
288 ext_ind = Dp = a; int_ind = D = f; A = b; B = c; C = d;
289 xx += BB->get_element(A*onedim + B, C*onedim + D)*AA->get_element(ext_ind, int_ind);
297 col = C*onedim + D; row = Ap*onedim +B;
300 col = C*onedim + D; row = A*onedim + Bp;
303 col = Cp *onedim + D; row = A*onedim + B;
306 col = C*onedim + Dp; row = A*onedim + B;
310 res->set_element(row,col, xx);
320 RefSymmSCMatrix convert_to_local_kit(
const RefSymmSCMatrix& A) {
321 RefSymmSCMatrix result;
322 Ref<LocalSCMatrixKit> kit_cast_to_local; kit_cast_to_local << A.kit();
323 if (kit_cast_to_local.null()) {
324 Ref<LocalSCMatrixKit> local_kit =
new LocalSCMatrixKit();
325 RefSymmSCMatrix A_local = local_kit->symmmatrix(A.dim());
335 RefSCMatrix RefSCMAT_combine234(RefSCMatrix mat,
const int b1,
const int b2,
336 const int k1,
const int k2)
338 const int ncol = b2 * k1 * k2;
339 RefSCDimension rowdim =
new SCDimension(b1);
340 RefSCDimension coldim =
new SCDimension(b2*k1*k2);
341 RefSCMatrix res = mat->kit()->matrix(rowdim, coldim);
343 const int num_e = k2*k1;
345 for (
int I1 = 0; I1 < b1; ++I1)
347 for (
int I2 = 0; I2 < b2; ++I2)
349 const int mat_row = I1 * b2 + I2;
350 const int blockbegin = I2*num_e;
351 const int blockend = blockbegin + num_e - 1;
357 res->assign_subblock(mat, I1, I1, blockbegin, blockend, mat_row, 0);
366 #endif // end of header guard