carl  24.04
Computer ARithmetic Library
LPVariables.cpp
Go to the documentation of this file.
1 #include "LPVariables.h"
2 
3 
4 #include <carl-common/config.h>
5 #ifdef USE_LIBPOLY
6 
7 
8 namespace carl {
9 
10 LPVariables::LPVariables() {
11  lp_var_db = lp_variable_db_new();
12  for (size_t i = 0; i < TEMP_VARIABLE_SIZE; ++ i) {
13  char name[10];
14  sprintf(name, "#%zu", i);
15  lp_var_tmp[i] = lp_variable_db_new_variable(lp_var_db, name);
16  }
17 }
18 
19 LPVariables::~LPVariables() {
20  lp_variable_db_detach(lp_var_db);
21 }
22 
23 
24 std::optional<carl::Variable> LPVariables::carl_variable(lp_variable_t var) const {
25  auto it = vars_libpoly_carl.find(var);
26  if(it == vars_libpoly_carl.end()) return std::nullopt;
27  CARL_LOG_TRACE("carl.poly", "Mapping libpoly variable " << lp_variable_db_get_name(lp_var_db, var) << " (" << var << ") -> " << it->second << " (" << it->second.id() << ")");
28  return it->second;
29 }
30 
31 std::optional<lp_variable_t> LPVariables::lp_variable_opt(carl::Variable var) const {
32  auto it = vars_carl_libpoly.find(var);
33  if(it == vars_carl_libpoly.end()) return std::nullopt;
34  CARL_LOG_TRACE("carl.poly", "Mapping carl variable " << var << " (" << var.id() << ") -> " << lp_variable_db_get_name(lp_var_db, it->second) << " (" << it->second << ")");
35  return it->second;
36 }
37 
38 lp_variable_t LPVariables::lp_variable(carl::Variable var) {
39  auto it = vars_carl_libpoly.find(var);
40  if(it != vars_carl_libpoly.end()) {
41  CARL_LOG_TRACE("carl.poly", "Mapping carl variable " << var << " (" << var.id() << ") -> " << lp_variable_db_get_name(lp_var_db, it->second) << " (" << it->second << ")");
42  return it->second;
43  } else {
44  std::string var_name = var.name();
45  lp_variable_t poly_var = lp_variable_db_new_variable(lp_var_db, &var_name[0]);
46  vars_carl_libpoly.emplace(var, poly_var);
47  vars_libpoly_carl.emplace(poly_var, var);
48  CARL_LOG_TRACE("carl.poly", "Mapping carl variable " << var << " (" << var.id() << ") -> " << lp_variable_db_get_name(lp_var_db, poly_var) << " (" << poly_var << ")");
49  return poly_var;
50  }
51 }
52 
53 } // namespace carl
54 
55 #endif
#define CARL_LOG_TRACE(channel, msg)
Definition: carl-logging.h:44
carl is the main namespace for the library.
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
std::string name() const
Retrieves the name of the variable.
Definition: Variable.cpp:8
constexpr std::size_t id() const noexcept
Retrieves the id of the variable.
Definition: Variable.h:123