def test_constructors(): int_row = 10 int_col = 20 # int row/col m1 = Matrix(int_row, int_col) check_dense_mat(m1, int_row, int_col) # int row/col w/ name m2 = Matrix("m2", int_row, int_col) check_dense_mat(m2, int_row, int_col, "m2") dim_row = Dimension([3, 2, 1, 4]) dim_col = Dimension([4, 2, 0, 2]) # dim row/col (default sym) m3 = Matrix("m3", dim_row, dim_col) check_block_sparse_mat(m3, 4, dim_row, dim_col, "m3") # dim row/col symm specified m4 = Matrix("m4", dim_row, dim_col, 2) check_block_sparse_mat(m4, 4, dim_row, dim_col, "m4", 2)
def test_clone(): dim = Dimension([1, 2, 3]) vec = Vector(dim) copy = vec.clone() assert copy.dimpi() == dim
a_name = "A^T" else: a_name = "A " if bt: b_name = "B^T" else: b_name = "B " return f" N(G){gsz} || G(A): {Ga} || G(B): {Gb} || doublet({a_name} x {b_name}) || {sq_or_rec.upper()}" dim_choices1 = [2, 3, 4, 5, 6, 7, 8, 9] dim_choices2 = [x + 1 for x in dim_choices1] doublet_args = [] group_size = 4 for group_size in [1, 2, 4, 8]: d1 = Dimension([dim_choices1[x] for x in range(group_size)]) d2 = Dimension([dim_choices2[x] for x in range(group_size)]) a11_set = [(d1, d1, H) for H in range(group_size)] b11_set = [(d1, d1, H) for H in range(group_size)] for aargs, bargs, at, bt in itertools.product(a11_set, b11_set, [True, False], [True, False]): adl, adr, Ga = aargs bdl, bdr, Gb = bargs doublet_args.append( (group_size, adl, adr, Ga, bdl, bdr, Gb, at, bt, 'square')) a12_set = [(d1, d2, H) for H in range(group_size)] b12_set = [(d1, d2, H) for H in range(group_size)] b21_set = [(d2, d1, H) for H in range(group_size)]
def array_to_matrix( self, arr: Union[np.ndarray, List[np.ndarray]], name: str = "New Matrix", dim1: Union[List, Tuple, core.Dimension] = None, dim2: core.Dimension = None) -> Union[core.Matrix, core.Vector]: """ Converts a numpy array or list of numpy arrays into a Psi4 Matrix (irreped if list). Parameters ---------- arr Numpy array or list of arrays to use as the data for a new core.Matrix name Name to give the new core.Matrix dim1 If a single dense numpy array is given, a dimension can be supplied to apply irreps to this array. Note that this discards all extra information given in the matrix besides the diagonal blocks determined by the passed dimension. dim2 Same as dim1 only if using a psi4.core.Dimension object. Returns ------- Matrix or Vector Returns the given Psi4 object Notes ----- This is a generalized function to convert a NumPy array to a Psi4 object Examples -------- >>> data = np.random.rand(20,1) >>> vector = psi4.core.Matrix.from_array(data) >>> irrep_data = [np.random.rand(2, 2), np.empty(shape=(0,3)), np.random.rand(4, 4)] >>> matrix = psi4.core.Matrix.from_array(irrep_data) >>> print(matrix.rowdim().to_tuple()) (2, 0, 4) """ # What type is it? MRO can help. arr_type = self.__mro__[0] # Irreped case if isinstance(arr, (list, tuple)): if (dim1 is not None) or (dim2 is not None): raise ValidationError( "Array_to_Matrix: If passed input is list of arrays dimension cannot be specified." ) irreps = len(arr) if arr_type == core.Matrix: sdim1 = core.Dimension(irreps) sdim2 = core.Dimension(irreps) for i in range(irreps): d1, d2 = _find_dim(arr[i], 2) sdim1[i] = d1 sdim2[i] = d2 ret = self(name, sdim1, sdim2) elif arr_type == core.Vector: sdim1 = core.Dimension(irreps) for i in range(irreps): d1 = _find_dim(arr[i], 1) sdim1[i] = d1[0] ret = self(name, sdim1) else: raise ValidationError( "Array_to_Matrix: type '%s' is not recognized." % str(arr_type)) for view, vals in zip(ret.nph, arr): if 0 in view.shape: continue view[:] = vals return ret # No irreps implied by list else: if arr_type == core.Matrix: # Build an irreped array back out if dim1 is not None: if dim2 is None: raise ValidationError( "Array_to_Matrix: If dim1 is supplied must supply dim2 also" ) dim1 = core.Dimension.from_list(dim1) dim2 = core.Dimension.from_list(dim2) if dim1.n() != dim2.n(): raise ValidationError( "Array_to_Matrix: Length of passed dim1 must equal length of dim2." ) ret = self(name, dim1, dim2) start1 = 0 start2 = 0 for num, interface in enumerate(ret.nph): d1 = dim1[num] d2 = dim2[num] if (d1 == 0) or (d2 == 0): continue view = np.asarray(interface) view[:] = arr[start1:start1 + d1, start2:start2 + d2] start1 += d1 start2 += d2 return ret # Simple case without irreps else: ret = self(name, arr.shape[0], arr.shape[1]) ret.np[:] = arr return ret elif arr_type == core.Vector: # Build an irreped array back out if dim1 is not None: if dim2 is not None: raise ValidationError( "Array_to_Matrix: If dim2 should not be supplied for 1D vectors." ) dim1 = core.Dimension.from_list(dim1) ret = self(name, dim1) start1 = 0 for num, interface in enumerate(ret.nph): d1 = dim1[num] if (d1 == 0): continue view = np.asarray(interface) view[:] = arr[start1:start1 + d1] start1 += d1 return ret # Simple case without irreps else: ret = self(name, arr.shape[0]) ret.np[:] = arr return ret else: raise ValidationError( "Array_to_Matrix: type '%s' is not recognized." % str(arr_type))