Skip to content

Commit

Permalink
factories: Remove namespaces 'real' and 'complex'
Browse files Browse the repository at this point in the history
By default, all factory functions will return expressions with
ScalarType = double (except for S_x() and S_y(), which necessitate
complex coefficients).

A new function make_complex() has been added to easily convert real
expressions to their equivalents with complex coefficients.
  • Loading branch information
krivenko committed Sep 27, 2020
1 parent b3332fc commit 6541c45
Show file tree
Hide file tree
Showing 24 changed files with 192 additions and 388 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ int main() {

// The following 'factory' functions make quantum operators with
// statically typed indices and real coefficients.
using static_indices::real::c_dag; // Create an electron
using static_indices::real::c; // Destroy an electron
using static_indices::real::n; // Number of electrons
using static_indices::real::a_dag; // Create a phonon
using static_indices::real::a; // Destroy a phonon
using static_indices::c_dag; // Create an electron
using static_indices::c; // Destroy an electron
using static_indices::n; // Number of electrons
using static_indices::a_dag; // Create a phonon
using static_indices::a; // Destroy a phonon

// Hopping terms of H
for(auto spin : {"up", "down"}) {
Expand Down Expand Up @@ -234,9 +234,9 @@ int main() {

// The following 'factory' functions make spin operators with statically typed
// indices and real coefficients.
using static_indices::real::S_p; // Spin-1/2 raising operator S_+
using static_indices::real::S_m; // Spin-1/2 lowering operator S_-
using static_indices::real::S_z; // Spin-1/2 operator S_z
using static_indices::S_p; // Spin-1/2 raising operator S_+
using static_indices::S_m; // Spin-1/2 lowering operator S_-
using static_indices::S_z; // Spin-1/2 operator S_z

// Expression 'H' will represent the exchange interaction term.
// Our spin operators will carry one integer index (site 1 or 2).
Expand Down
6 changes: 3 additions & 3 deletions examples/exchange_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ int main() {

// The following 'factory' functions make spin operators with statically typed
// indices and real coefficients.
using static_indices::real::S_p; // Spin-1/2 raising operator S_+
using static_indices::real::S_m; // Spin-1/2 lowering operator S_-
using static_indices::real::S_z; // Spin-1/2 operator S_z
using static_indices::S_p; // Spin-1/2 raising operator S_+
using static_indices::S_m; // Spin-1/2 lowering operator S_-
using static_indices::S_z; // Spin-1/2 operator S_z

// Expression 'H' will represent the exchange interaction term.
// Our spin operators will carry one integer index (site 1 or 2).
Expand Down
2 changes: 1 addition & 1 deletion examples/heisenberg_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main() {

// For functions S_x(), S_y() and S_z().
// Note that the x/y functions exist only for the complex expressions.
using namespace static_indices::complex;
using namespace static_indices;

// Number of spins in the chain
const int N = 20;
Expand Down
10 changes: 5 additions & 5 deletions examples/holstein.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ int main() {

// Use functions c_dag() and c() that return fermionic creation/annihilation
// operators.
using libcommute::static_indices::real::c_dag;
using libcommute::static_indices::real::c;
using libcommute::static_indices::c_dag;
using libcommute::static_indices::c;

// Iterate over spin projections
for(auto spin : {"up", "down"}) {
Expand Down Expand Up @@ -79,8 +79,8 @@ int main() {

// Use functions a_dag() and a() that return bosonic creation/annihilation
// operators.
using libcommute::static_indices::real::a_dag;
using libcommute::static_indices::real::a;
using libcommute::static_indices::a_dag;
using libcommute::static_indices::a;

// Iterate over all lattice sites
for(int ix = 0; ix < N; ++ix) {
Expand All @@ -99,7 +99,7 @@ int main() {
decltype(H_e) H_e_ph;

// Use function n() that returns the fermionic number operator n = c_dag * c
using libcommute::static_indices::real::n;
using libcommute::static_indices::n;

// Iterate over spin projections
for(auto spin : {"up", "down"}) {
Expand Down
10 changes: 5 additions & 5 deletions examples/hubbard_holstein.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ int main() {

// The following 'factory' functions make quantum operators with
// statically typed indices and real coefficients.
using static_indices::real::c_dag; // Create an electron
using static_indices::real::c; // Destroy an electron
using static_indices::real::n; // Number of electrons
using static_indices::real::a_dag; // Create a phonon
using static_indices::real::a; // Destroy a phonon
using static_indices::c_dag; // Create an electron
using static_indices::c; // Destroy an electron
using static_indices::n; // Number of electrons
using static_indices::a_dag; // Create a phonon
using static_indices::a; // Destroy a phonon

// Hopping terms of H
for(auto spin : {"up", "down"}) {
Expand Down
2 changes: 1 addition & 1 deletion examples/jaynes_cummings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main() {
const double delta = w_a - w_c;

// For a_dag(), a(), S_p(), S_m() and S_z()
using namespace static_indices::real;
using namespace static_indices;

// The Jaynes-Cummings Hamiltonian
auto H = hbar * w_c * a_dag() * a() +
Expand Down
2 changes: 1 addition & 1 deletion examples/kanamori.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using namespace libcommute;
int main() {

// For functions c_dag(), c() and n().
using namespace static_indices::real;
using namespace static_indices;

// Orbital degeneracy of the shell (t_{2g} triplet)
const int n_orbs = 3;
Expand Down
2 changes: 1 addition & 1 deletion examples/partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace libcommute;

int main() {

using namespace static_indices::real; // For c(), c_dag() and n()
using namespace static_indices; // For c(), c_dag() and n()

//
// Build Hamiltonian of the 3-orbital Hubbard-Kanamori atom
Expand Down
71 changes: 9 additions & 62 deletions include/libcommute/expression/factories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace static_indices {

#define INDICES std::forward<IndexTypes>(indices)...
#define DEFINE_FACTORY(NAME, ...) \
template<typename ScalarType, typename... IndexTypes> \
template<typename ScalarType = double, typename... IndexTypes> \
inline \
expression<ScalarType, c_str_to_string_t<IndexTypes>...> \
NAME(IndexTypes&&... indices) { \
Expand All @@ -42,7 +42,9 @@ NAME(IndexTypes&&... indices) { \
}

#define DEFINE_FACTORY_SPIN(NAME, ...) \
template<int Multiplicity, typename ScalarType, typename... IndexTypes> \
template<int Multiplicity, \
typename ScalarType = double, \
typename... IndexTypes> \
inline \
expression<ScalarType, c_str_to_string_t<IndexTypes>...> \
NAME(IndexTypes&&... indices) { \
Expand All @@ -53,22 +55,6 @@ NAME(IndexTypes&&... indices) { \
); \
}

#define DEFINE_FACTORY_SCALAR_TYPE(NAME, S) \
template<typename... IndexTypes> \
inline expression<S, c_str_to_string_t<IndexTypes>...> \
NAME(IndexTypes&&... indices) { \
using libcommute::static_indices::NAME; \
return NAME<S>(INDICES); \
}

#define DEFINE_FACTORY_SPIN_SCALAR_TYPE(NAME, S) \
template<int Multiplicity, typename... IndexTypes> \
inline expression<S, c_str_to_string_t<IndexTypes>...> \
NAME(IndexTypes&&... indices) { \
using libcommute::static_indices::NAME; \
return NAME<Multiplicity, S>(INDICES); \
}

//
// Free functions to make fermionic operators
//
Expand Down Expand Up @@ -113,47 +99,6 @@ DEFINE_FACTORY_SPIN(S_m, (make_spin((Multiplicity-1)/2.0,
DEFINE_FACTORY_SPIN(S_z, (make_spin((Multiplicity-1)/2.0,
spin_component::z,INDICES)))

//
// Specializations for ScalarType = double
//

namespace real {
DEFINE_FACTORY_SCALAR_TYPE(c_dag, double)
DEFINE_FACTORY_SCALAR_TYPE(c, double)
DEFINE_FACTORY_SCALAR_TYPE(n, double)

DEFINE_FACTORY_SCALAR_TYPE(a_dag, double)
DEFINE_FACTORY_SCALAR_TYPE(a, double)

DEFINE_FACTORY_SCALAR_TYPE(S_p, double)
DEFINE_FACTORY_SCALAR_TYPE(S_m, double)
DEFINE_FACTORY_SCALAR_TYPE(S_z, double)

DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_p, double)
DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_m, double)
DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_z, double)
} // namespace libcommute::static_indices::real

//
// Specializations for ScalarType = std::complex<double>
//

namespace complex {
DEFINE_FACTORY_SCALAR_TYPE(c_dag, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(c, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(n, std::complex<double>)

DEFINE_FACTORY_SCALAR_TYPE(a_dag, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(a, std::complex<double>)

DEFINE_FACTORY_SCALAR_TYPE(S_p, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(S_m, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(S_z, std::complex<double>)

DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_p, std::complex<double>)
DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_m, std::complex<double>)
DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_z, std::complex<double>)

//
// In the complex case, we can additionally define S_x and S_y
//
Expand Down Expand Up @@ -182,15 +127,17 @@ S_y(IndexTypes&&... indices) {
S_m<Multiplicity>(INDICES));
}

} // namespace libcommute::static_indices::complex
// Make a complex expression out of a real one
template<typename... IndexTypes>
expr_complex<IndexTypes...> make_complex(expr_real<IndexTypes...> const& expr) {
return expr_complex<IndexTypes...>(expr);
}

} // namespace libcommute::static_indices
} // namespace libcommute

#undef DEFINE_FACTORY
#undef DEFINE_FACTORY_SPIN
#undef DEFINE_FACTORY_SCALAR_TYPE
#undef DEFINE_FACTORY_SPIN_SCALAR_TYPE
#undef INDICES

#endif
70 changes: 8 additions & 62 deletions include/libcommute/expression/factories_dyn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace dynamic_indices {

#define INDICES std::forward<IndexTypes>(indices)...
#define DEFINE_FACTORY(NAME, ...) \
template<typename ScalarType, typename... IndexTypes> \
template<typename ScalarType = double, typename... IndexTypes> \
inline \
expression<ScalarType, dyn_indices> \
NAME(IndexTypes&&... indices) { \
Expand All @@ -47,7 +47,9 @@ NAME(IndexTypes&&... indices) { \
}

#define DEFINE_FACTORY_SPIN(NAME, ...) \
template<int Multiplicity, typename ScalarType, typename... IndexTypes> \
template<int Multiplicity, \
typename ScalarType = double, \
typename... IndexTypes> \
inline \
expression<ScalarType, dyn_indices> \
NAME(IndexTypes&&... indices) { \
Expand All @@ -58,22 +60,6 @@ NAME(IndexTypes&&... indices) { \
); \
}

#define DEFINE_FACTORY_SCALAR_TYPE(NAME, S) \
template<typename... IndexTypes> \
inline expression<S, dyn_indices> \
NAME(IndexTypes&&... indices) { \
using libcommute::dynamic_indices::NAME; \
return NAME<S>(INDICES); \
}

#define DEFINE_FACTORY_SPIN_SCALAR_TYPE(NAME, S) \
template<int Multiplicity, typename... IndexTypes> \
inline expression<S, dyn_indices> \
NAME(IndexTypes&&... indices) { \
using libcommute::dynamic_indices::NAME; \
return NAME<Multiplicity, S>(INDICES); \
}

//
// Free functions to make fermionic operators
//
Expand Down Expand Up @@ -118,47 +104,6 @@ DEFINE_FACTORY_SPIN(S_m, (make_spin((Multiplicity-1)/2.0,
DEFINE_FACTORY_SPIN(S_z, (make_spin((Multiplicity-1)/2.0,
spin_component::z,INDICES)))

//
// Specializations for ScalarType = double
//

namespace real {
DEFINE_FACTORY_SCALAR_TYPE(c_dag, double)
DEFINE_FACTORY_SCALAR_TYPE(c, double)
DEFINE_FACTORY_SCALAR_TYPE(n, double)

DEFINE_FACTORY_SCALAR_TYPE(a_dag, double)
DEFINE_FACTORY_SCALAR_TYPE(a, double)

DEFINE_FACTORY_SCALAR_TYPE(S_p, double)
DEFINE_FACTORY_SCALAR_TYPE(S_m, double)
DEFINE_FACTORY_SCALAR_TYPE(S_z, double)

DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_p, double)
DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_m, double)
DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_z, double)
} // namespace libcommute::dynamic_indices::real

//
// Specializations for ScalarType = std::complex<double>
//

namespace complex {
DEFINE_FACTORY_SCALAR_TYPE(c_dag, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(c, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(n, std::complex<double>)

DEFINE_FACTORY_SCALAR_TYPE(a_dag, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(a, std::complex<double>)

DEFINE_FACTORY_SCALAR_TYPE(S_p, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(S_m, std::complex<double>)
DEFINE_FACTORY_SCALAR_TYPE(S_z, std::complex<double>)

DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_p, std::complex<double>)
DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_m, std::complex<double>)
DEFINE_FACTORY_SPIN_SCALAR_TYPE(S_z, std::complex<double>)

//
// In the complex case, we can additionally define S_x and S_y
//
Expand Down Expand Up @@ -187,12 +132,13 @@ S_y(IndexTypes&&... indices) {
S_m<Multiplicity>(INDICES));
}

} // namespace libcommute::dynamic_indices::complex
// Make a complex expression out of a real one
expr_complex make_complex(expr_real const& expr) {
return expr_complex(expr);
}

#undef DEFINE_FACTORY
#undef DEFINE_FACTORY_SPIN
#undef DEFINE_FACTORY_SCALAR_TYPE
#undef DEFINE_FACTORY_SPIN_SCALAR_TYPE
#undef INDICES

} // namespace libcommute::dynamic_indices
Expand Down
Loading

0 comments on commit 6541c45

Please sign in to comment.