Skip to content
Snippets Groups Projects
Commit 4e9e72b4 authored by Thomas Purcell's avatar Thomas Purcell
Browse files

Update bindings to include __pow__, __repr__, and __str__ explicitly

Make it easier to work with units and models in python
parent c66953b4
No related branches found
No related tags found
No related merge requests found
...@@ -173,7 +173,8 @@ void Model::register_python() ...@@ -173,7 +173,8 @@ void Model::register_python()
class_<Model>("Model", init<std::vector<double>, std::vector<double>, std::vector<model_node_ptr>, std::vector<int>, std::vector<int>>()) class_<Model>("Model", init<std::vector<double>, std::vector<double>, std::vector<model_node_ptr>, std::vector<int>, std::vector<int>>())
.def("predict", &Model::predict) .def("predict", &Model::predict)
.def("fit", &Model::predict_train) .def("fit", &Model::predict_train)
// .def(str(self)) .def("__str__", &Model::toString)
.def("__repr__", &Model::toString)
.def_readonly("_n_samp_train", &Model::_n_samp_train) .def_readonly("_n_samp_train", &Model::_n_samp_train)
.def_readonly("_n_samp_test", &Model::_n_samp_test) .def_readonly("_n_samp_test", &Model::_n_samp_test)
.def_readonly("_n_dim", &Model::_n_dim) .def_readonly("_n_dim", &Model::_n_dim)
......
...@@ -106,6 +106,31 @@ Unit Unit::operator/(Unit unit_2) ...@@ -106,6 +106,31 @@ Unit Unit::operator/(Unit unit_2)
return Unit(to_out); return Unit(to_out);
} }
Unit& Unit::operator*=(Unit unit_2)
{
for(auto& el : unit_2.dct())
{
if(_dct.count(el.first) > 0)
_dct[el.first] += el.second;
else
_dct[el.first] = el.second;
}
return *this;
}
Unit& Unit::operator/=(Unit unit_2)
{
for(auto& el : unit_2.dct())
{
if(_dct.count(el.first) > 0)
_dct[el.first] -= el.second;
else
_dct[el.first] = -1.0 * el.second;
}
return *this;
}
Unit Unit::operator^(double power) Unit Unit::operator^(double power)
{ {
std::map<std::string, double> to_out = dct(); std::map<std::string, double> to_out = dct();
...@@ -158,7 +183,6 @@ std::ostream& operator<< (std::ostream& outStream, const Unit& unit) ...@@ -158,7 +183,6 @@ std::ostream& operator<< (std::ostream& outStream, const Unit& unit)
return outStream; return outStream;
} }
void Unit::register_python() void Unit::register_python()
{ {
using namespace boost::python; using namespace boost::python;
...@@ -166,12 +190,15 @@ void Unit::register_python() ...@@ -166,12 +190,15 @@ void Unit::register_python()
.def(init<std::map<std::string, double>>()) .def(init<std::map<std::string, double>>())
.def(init<std::string>()) .def(init<std::string>())
.def(init<Unit&>()) .def(init<Unit&>())
// .def(str(self)) .def("__str__", &Unit::toString)
.def("__repr__", &Unit::toString)
.def("inverse", &Unit::inverse) .def("inverse", &Unit::inverse)
.def(self * self) .def(self * self)
.def(self / self) .def(self / self)
.def(self *= self)
.def(self /= self)
.def(self == self) .def(self == self)
.def(self != self) .def(self != self)
// .def(pow(self, other<double>)) .def("__pow__", &Unit::operator^)
.add_property("dct", &Unit::dct); .add_property("dct", &Unit::dct);
} }
\ No newline at end of file
...@@ -74,6 +74,22 @@ public: ...@@ -74,6 +74,22 @@ public:
*/ */
Unit operator/(Unit unit_2); Unit operator/(Unit unit_2);
/**
* @brief Multiply operator for units
*
* @param unit_2 The second unit to multiply by
* @return The product of this unit with unit_2
*/
Unit& operator*=(Unit unit_2);
/**
* @brief Divide operator for units
*
* @param unit_2 The second unit to divide by
* @return The quotient of this unit with unit_2
*/
Unit& operator/=(Unit unit_2);
/** /**
* @brief Exponentiation operator for units * @brief Exponentiation operator for units
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment