2121from collections import Counter
2222from typing import List , Tuple , Iterable , Dict , Any
2323import numpy as np
24+ from scipy .sparse import coo_matrix
2425
2526from . import csf
2627from . import slatdet as sd
2728
28- from liblibra_core import MATRIX , CMATRIX
29+ # from liblibra_core import MATRIX, CMATRIX
2930
3031def find_matches (
3132 config : List [int ],
@@ -297,7 +298,7 @@ def conf2csf_matrix(
297298 all_phases : List [int ],
298299 S : int = 0 ,
299300 Ms : int = 0
300- ) -> "CMATRIX" :
301+ ) -> coo_matrix :
301302 """
302303 Build the configuration-to-CSF (Configuration State Function) transformation matrix.
303304
@@ -321,9 +322,10 @@ def conf2csf_matrix(
321322
322323 Returns
323324 -------
324- T : CMATRIX
325- Complex-valued configuration–CSF transformation matrix of shape
326- `(n_determinants, n_CSFS)`.
325+ T_sparse : scipy.sparse.coo_matrix
326+ Sparse configuration-to-CSF transformation matrix.
327+ Rows correspond to determinants in `all_confs`,
328+ columns correspond to CSFs in `min_basis`.
327329
328330 Notes
329331 -----
@@ -363,6 +365,7 @@ def conf2csf_matrix(
363365
364366 ncsfs = len (csfs [(S , Ms )]) # number of CSFs for the given spin manifold
365367
368+ """
366369 # Initialize complex transformation matrix
367370 T = CMATRIX(nconfigs, ncsfs)
368371
@@ -374,8 +377,29 @@ def conf2csf_matrix(
374377 raise ValueError(f"Determinant {det_tuple} not found in all_confs.")
375378 i = all_confs.index(det_tuple)
376379 T.set(i, j, coeff * all_phases[i] * (1.0 + 0j))
380+ """
381+
382+ # List to store non-zero entries
383+ rows , cols , data = [], [], []
384+
385+ # Build a dict for faster lookup
386+ conf_index = {tuple (conf ): i for i , conf in enumerate (all_confs )}
387+
388+ # Populate sparse T
389+ for j , csf_group in enumerate (csfs [(S , Ms )]):
390+ for det , coeff in csf_group :
391+ det_tuple = tuple (det )
392+ i = conf_index .get (det_tuple )
393+ if i is None :
394+ raise ValueError (f"Determinant { det_tuple } not found in all_confs." )
395+ rows .append (i )
396+ cols .append (j )
397+ data .append (coeff * all_phases [i ] * (1.0 + 0j ))
398+
399+ # Convert to a scipy sparse matrix (CSR)
400+ T_sparse = coo_matrix ((data , (rows , cols )), shape = (nconfigs , ncsfs ), dtype = np .complex128 )
377401
378- return T
402+ return T_sparse
379403
380404
381405
@@ -387,7 +411,7 @@ def configs_and_T_matrix(
387411 nelec : int ,
388412 S : int ,
389413 Ms : int
390- ) -> Tuple [List [Tuple [int , ...]], "CMATRIX" ]:
414+ ) -> Tuple [List [Tuple [int , ...]], coo_matrix ]:
391415 """
392416 Generate the minimal active-space configurations mapped to a given orbital space
393417 and the configuration-to-CSF transformation matrix for a CAS with given spin.
@@ -412,7 +436,7 @@ def configs_and_T_matrix(
412436 mapped_basis : list[tuple[int, ...]]
413437 List of minimal configurations mapped to the specified `orbital_space`,
414438 with signs preserved (positive = α-spin, negative = β-spin).
415- T : CMATRIX
439+ T : coo_matrix
416440 Complex-valued configuration-to-CSF transformation matrix.
417441
418442 Example
@@ -458,7 +482,7 @@ def configs_and_T_matrix_singlet(
458482 nelec : int ,
459483 S : int ,
460484 Ms : int
461- ) -> Tuple [List [Tuple [int , ...]], "CMATRIX" ]:
485+ ) -> Tuple [List [Tuple [int , ...]], coo_matrix ]:
462486 """
463487 Generate the minimal active-space configurations mapped to a given orbital space
464488 and the configuration-to-CSF transformation matrix for a CAS with given spin.
@@ -483,7 +507,7 @@ def configs_and_T_matrix_singlet(
483507 mapped_basis : list[tuple[int, ...]]
484508 List of minimal configurations mapped to the specified `orbital_space`,
485509 with signs preserved (positive = α-spin, negative = β-spin).
486- T : CMATRIX
510+ T : coo_matrix
487511 Complex-valued configuration-to-CSF transformation matrix.
488512
489513 Example
0 commit comments