diff --git a/src/descriptor_identifier/Model/Model.cpp b/src/descriptor_identifier/Model/Model.cpp index 1e05768c11d080094847d1c0c812f40651d9badb..9cc1384013ff547404d76433633882c3b4b34f90 100644 --- a/src/descriptor_identifier/Model/Model.cpp +++ b/src/descriptor_identifier/Model/Model.cpp @@ -112,3 +112,80 @@ std::vector<double> Model::eval(std::map<std::string, std::vector<double>> x_in_ } return eval(x_in); } + +void Model::write_matlab_fxn(std::string fxn_filename) +{ + if(fxn_filename.substr(fxn_filename.size() - 2, 2).compare(".m") != 0) + { + fxn_filename += ".m"; + } + + boost::filesystem::path p(fxn_filename.c_str()); + boost::filesystem::path parent = p.remove_filename(); + if(parent.string().size() > 0) + { + boost::filesystem::create_directories(parent); + } + + std::ofstream out_file_stream = std::ofstream(); + out_file_stream.open(fxn_filename); + + // Get the list of all unique leaves in all features + std::vector<std::string> leaves; + for(auto& feat : _feats) + { + std::vector<std::string> x_in = feat->get_x_in_expr_list(); + leaves.insert(leaves.end(), x_in.begin(), x_in.end()); + } + leaves = vector_utils::unique<std::string>(leaves); + std::transform(leaves.begin(), leaves.end(), leaves.begin(), [](std::string s){return str_utils::matlabify(s);}); + + // Write the header of the function + out_file_stream << "function P = " << fxn_filename.substr(0, fxn_filename.size() - 2) << "(X)\n"; + out_file_stream << "% Returns the value of " << _prop_label << " = " << toString() << "\n%\n"; + out_file_stream << "% X = [\n"; + for(auto & leaf : leaves) + { + out_file_stream << "% " << leaf << ",\n"; + } + out_file_stream << "% ]\n\n"; + + // Check X contains the correct number of columns + out_file_stream << "if(size(X, 2) ~= " << leaves.size() << ")\n"; + out_file_stream << " error(\"ERROR: X must have a size of " << leaves.size() << " in the second dimension.\")\n"; + out_file_stream << "end\n"; + int len_rename = (*std::max_element(leaves.begin(), leaves.end(), [](std::string s1, std::string s2){return s1.size() < s2.size();})).size(); + + // Rename X + for(int ll = 0; ll < leaves.size(); ++ll) + { + out_file_stream << std::setw(len_rename) << leaves[ll] << std::setw(3) << " = " << "reshape(X(:, " << ll + 1 << "), 1, []);\n"; + } + out_file_stream << "\n"; + + // Set features + for(int ff = 0; ff < _feats.size(); ++ff) + { + out_file_stream << "f" << ff << " = " << _feats[ff]->matlab_fxn_expr() << ";\n"; + } + out_file_stream << "\n"; + + // Set Constants + if(_fix_intercept) + { + out_file_stream << "c0 = 0.0;\n"; + } + else + { + out_file_stream << "c0 = " << _coefs[0].back() << ";\n"; + } + for(int ff = 0; ff < _feats.size(); ++ff) + { + out_file_stream << "a" << ff <<" = " << _coefs[0][ff] << ";\n"; + } + out_file_stream << "\n"; + + // Calculate the property + out_file_stream << "P = reshape(" << matlab_expr() << ", [], 1);\nend\n"; + out_file_stream.close(); +} diff --git a/src/descriptor_identifier/Model/Model.hpp b/src/descriptor_identifier/Model/Model.hpp index ba5456816c34dd7fa5336854e55fdae1d293ce92..1210caaa17fcc321c4df8bb60ec9745927450b7a 100644 --- a/src/descriptor_identifier/Model/Model.hpp +++ b/src/descriptor_identifier/Model/Model.hpp @@ -20,6 +20,7 @@ #include "feature_creation/node/ModelNode.hpp" #include "utils/string_utils.hpp" +#include "utils/vector_utils.hpp" #ifdef PY_BINDINGS namespace np = boost::python::numpy; @@ -232,6 +233,27 @@ public: */ inline std::vector<std::vector<double>> coefs() const {return _coefs;} + /** + * @brief Convert the model to a string + + * @return The string representation of the model + */ + virtual std::string toString() const = 0; + + /** + * @brief Get the matlab expression for this model + + * @return The matlab exprssion for the model + */ + virtual std::string matlab_expr() const = 0; + + /** + * @brief Convert the model into a Matlab function + * + * @param fxn_filename name of the file to print the function to + */ + void write_matlab_fxn(std::string fxn_filename); + #ifdef PY_BINDINGS // DocString: model_coefs /** diff --git a/src/descriptor_identifier/Model/ModelClassifier.cpp b/src/descriptor_identifier/Model/ModelClassifier.cpp index 3fb778cf03fac87f99621e34e025202735bcd25c..18b7c9ee8057d5d5bbfb50576aa3dfa1635e2b24 100644 --- a/src/descriptor_identifier/Model/ModelClassifier.cpp +++ b/src/descriptor_identifier/Model/ModelClassifier.cpp @@ -97,12 +97,21 @@ ModelClassifier::ModelClassifier(const std::string train_file) std::string postfix_expr = split_str[3]; std::string expr = split_str[4]; std::string latex_expr = "Property"; + std::string matlab_expr; std::vector<std::string> x_in_expr_list; if(split_str.size() > 5) { - x_in_expr_list = str_utils::split_string_trim(split_str[5]); + x_in_expr_list = str_utils::split_string_trim(split_str.back()); } - feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); + if(split_str.size() > 6) + { + matlab_expr = split_str[5]; + } + else + { + matlab_expr = "NaN"; + } + feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, matlab_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); } else { @@ -110,12 +119,21 @@ ModelClassifier::ModelClassifier(const std::string train_file) std::string postfix_expr = split_str[2]; std::string expr = split_str[3]; std::string latex_expr = split_str[4]; + std::string matlab_expr; std::vector<std::string> x_in_expr_list; if(split_str.size() > 5) { - x_in_expr_list = str_utils::split_string_trim(split_str[5]); + x_in_expr_list = str_utils::split_string_trim(split_str.back()); + } + if(split_str.size() > 6) + { + matlab_expr = split_str[5]; } - feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); + else + { + matlab_expr = "NaN"; + } + feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, matlab_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); } _feats.push_back(feat); } @@ -157,12 +175,21 @@ ModelClassifier::ModelClassifier(const std::string train_file, const std::string std::string postfix_expr = split_str[3]; std::string expr = split_str[4]; std::string latex_expr = "Property"; + std::string matlab_expr; std::vector<std::string> x_in_expr_list; if(split_str.size() > 5) { - x_in_expr_list = str_utils::split_string_trim(split_str[5]); + x_in_expr_list = str_utils::split_string_trim(split_str.back()); + } + if(split_str.size() > 6) + { + matlab_expr = split_str[5]; + } + else + { + matlab_expr = "NaN"; } - feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); + feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, matlab_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); } else { @@ -170,12 +197,21 @@ ModelClassifier::ModelClassifier(const std::string train_file, const std::string std::string postfix_expr = split_str[2]; std::string expr = split_str[3]; std::string latex_expr = split_str[4]; + std::string matlab_expr; std::vector<std::string> x_in_expr_list; if(split_str.size() > 5) { - x_in_expr_list = str_utils::split_string_trim(split_str[5]); + x_in_expr_list = str_utils::split_string_trim(split_str.back()); + } + if(split_str.size() > 6) + { + matlab_expr = split_str[5]; + } + else + { + matlab_expr = "NaN"; } - feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); + feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, matlab_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); } _feats.push_back(feat); } @@ -572,6 +608,17 @@ std::string ModelClassifier::toLatexString() const return unit_rep.str(); } +std::string ModelClassifier::matlab_expr() const +{ + std::string to_ret = ""; + to_ret += "c0"; + for(int ff = 0; ff < _feats.size(); ++ff) + { + to_ret += " + a" + std::to_string(ff) + " * f" + std::to_string(ff); + } + return to_ret; +} + std::ostream& operator<< (std::ostream& outStream, const ModelClassifier& model) { outStream << model.toString(); @@ -632,6 +679,7 @@ void ModelClassifier::to_file(std::string filename, bool train, std::vector<int> out_file_stream << _feats[ff]->postfix_expr() + "; "; out_file_stream << _feats[ff]->expr() + "; "; out_file_stream << _feats[ff]->latex_expr() + "; "; + out_file_stream << _feats[ff]->matlab_fxn_expr() + "; "; out_file_stream << boost::algorithm::join(_feats[ff]->get_x_in_expr_list(), ",") << std::endl; } diff --git a/src/descriptor_identifier/Model/ModelClassifier.hpp b/src/descriptor_identifier/Model/ModelClassifier.hpp index 1ff38237e0d90ba2a1b29dc882ed8e7842d79d0a..13169d09911f376e6976cb32347914a31ef7a0e6 100644 --- a/src/descriptor_identifier/Model/ModelClassifier.hpp +++ b/src/descriptor_identifier/Model/ModelClassifier.hpp @@ -166,6 +166,13 @@ public: */ std::string toLatexString() const; + /** + * @brief Get the matlab expression for this model + + * @return The matlab exprssion for the model + */ + std::string matlab_expr() const; + /** * @brief Copy the error into a new array * diff --git a/src/descriptor_identifier/Model/ModelLogRegressor.cpp b/src/descriptor_identifier/Model/ModelLogRegressor.cpp index 56e6fbee59e56e5afa98bf983c3b0d00192660c6..64b6b347a8e75380080943cff83f0e077813c394 100644 --- a/src/descriptor_identifier/Model/ModelLogRegressor.cpp +++ b/src/descriptor_identifier/Model/ModelLogRegressor.cpp @@ -247,7 +247,7 @@ std::string ModelLogRegressor::toLatexString() const } else { - model_rep << "$c_0"; + model_rep << "$\\exp\\left(c_0\\right)"; for(int ff = 0; ff < _feats.size(); ++ff) { model_rep << "\\left(" << _feats[ff]->get_latex_expr() << "\\right)^{a_" << ff << "}"; @@ -257,6 +257,17 @@ std::string ModelLogRegressor::toLatexString() const return model_rep.str(); } +std::string ModelLogRegressor::matlab_expr() const +{ + std::string to_ret = ""; + to_ret += "exp(c0)"; + for(int ff = 0; ff < _feats.size(); ++ff) + { + to_ret += " .* f" + std::to_string(ff) + ".^a" + std::to_string(ff); + } + return to_ret; +} + std::ostream& operator<< (std::ostream& outStream, const ModelLogRegressor& model) { outStream << model.toString(); diff --git a/src/descriptor_identifier/Model/ModelLogRegressor.hpp b/src/descriptor_identifier/Model/ModelLogRegressor.hpp index 3309bcf1299cd4b9dd0e73be6d96f37154e06a48..ae5375795dd07278c5dcab1f60fe56043c4bc407 100644 --- a/src/descriptor_identifier/Model/ModelLogRegressor.hpp +++ b/src/descriptor_identifier/Model/ModelLogRegressor.hpp @@ -135,7 +135,7 @@ public: * @return The string representation of the model */ - virtual std::string toString() const; + std::string toString() const; // DocString: model_log_reg_latex_str /** @@ -145,6 +145,13 @@ public: */ std::string toLatexString() const; + /** + * @brief Get the matlab expression for this model + + * @return The matlab exprssion for the model + */ + std::string matlab_expr() const; + /** * @brief Copy the error into a new array * diff --git a/src/descriptor_identifier/Model/ModelRegressor.cpp b/src/descriptor_identifier/Model/ModelRegressor.cpp index 59a50872e1baa21a29d7eb0ea085891b2fc1eef3..f60220bc02116f08a513d9cc7e4ceca297aecbe7 100644 --- a/src/descriptor_identifier/Model/ModelRegressor.cpp +++ b/src/descriptor_identifier/Model/ModelRegressor.cpp @@ -108,12 +108,21 @@ ModelRegressor::ModelRegressor(const std::string train_file) std::string postfix_expr = split_str[3]; std::string expr = split_str[4]; std::string latex_expr = "Property"; + std::string matlab_expr; std::vector<std::string> x_in_expr_list; if(split_str.size() > 5) { - x_in_expr_list = str_utils::split_string_trim(split_str[5]); + x_in_expr_list = str_utils::split_string_trim(split_str.back()); } - feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); + if(split_str.size() > 6) + { + matlab_expr = split_str[5]; + } + else + { + matlab_expr = "NaN"; + } + feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, matlab_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); } else { @@ -121,12 +130,21 @@ ModelRegressor::ModelRegressor(const std::string train_file) std::string postfix_expr = split_str[2]; std::string expr = split_str[3]; std::string latex_expr = split_str[4]; + std::string matlab_expr; std::vector<std::string> x_in_expr_list; if(split_str.size() > 5) { - x_in_expr_list = str_utils::split_string_trim(split_str[5]); + x_in_expr_list = str_utils::split_string_trim(split_str.back()); + } + if(split_str.size() > 6) + { + matlab_expr = split_str[5]; } - feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); + else + { + matlab_expr = "NaN"; + } + feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, matlab_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); } _feats.push_back(feat); } @@ -161,12 +179,21 @@ ModelRegressor::ModelRegressor(const std::string train_file, std::string test_fi std::string postfix_expr = split_str[3]; std::string expr = split_str[4]; std::string latex_expr = "Property"; + std::string matlab_expr; std::vector<std::string> x_in_expr_list; if(split_str.size() > 5) { - x_in_expr_list = str_utils::split_string_trim(split_str[5]); + x_in_expr_list = str_utils::split_string_trim(split_str.back()); + } + if(split_str.size() > 6) + { + matlab_expr = split_str[5]; + } + else + { + matlab_expr = "NaN"; } - feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); + feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, matlab_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); } else { @@ -174,12 +201,21 @@ ModelRegressor::ModelRegressor(const std::string train_file, std::string test_fi std::string postfix_expr = split_str[2]; std::string expr = split_str[3]; std::string latex_expr = split_str[4]; + std::string matlab_expr; std::vector<std::string> x_in_expr_list; if(split_str.size() > 5) { - x_in_expr_list = str_utils::split_string_trim(split_str[5]); + x_in_expr_list = str_utils::split_string_trim(split_str.back()); } - feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); + if(split_str.size() > 6) + { + matlab_expr = split_str[5]; + } + else + { + matlab_expr = "NaN"; + } + feat = std::make_shared<ModelNode>(ff, rung, expr, latex_expr, postfix_expr, matlab_expr, feat_val, feat_test_val, x_in_expr_list, Unit(unit_str)); } _feats.push_back(feat); } @@ -414,6 +450,17 @@ std::string ModelRegressor::toLatexString() const return model_rep.str(); } +std::string ModelRegressor::matlab_expr() const +{ + std::string to_ret = ""; + to_ret += "c0"; + for(int ff = 0; ff < _feats.size(); ++ff) + { + to_ret += " + a" + std::to_string(ff) + " * f" + std::to_string(ff); + } + return to_ret; +} + std::ostream& operator<< (std::ostream& outStream, const ModelRegressor& model) { outStream << model.toString(); @@ -425,7 +472,9 @@ void ModelRegressor::to_file(const std::string filename, const bool train, const boost::filesystem::path p(filename.c_str()); boost::filesystem::path parent = p.remove_filename(); if(parent.string().size() > 0) + { boost::filesystem::create_directories(parent); + } std::ofstream out_file_stream = std::ofstream(); out_file_stream.open(filename); @@ -474,6 +523,7 @@ void ModelRegressor::to_file(const std::string filename, const bool train, const out_file_stream << std::setw(50) << _feats[ff]->unit().toString() + "; "; out_file_stream << _feats[ff]->postfix_expr() + "; " << _feats[ff]->expr() + "; "; out_file_stream << _feats[ff]->latex_expr() + "; "; + out_file_stream << _feats[ff]->matlab_fxn_expr() + "; "; out_file_stream << boost::algorithm::join(_feats[ff]->get_x_in_expr_list(), ",") << std::endl; } diff --git a/src/descriptor_identifier/Model/ModelRegressor.hpp b/src/descriptor_identifier/Model/ModelRegressor.hpp index 86fbdac3bb4030b60d7f0998cf57e329d4c26a3e..4bdbece93f487fd1fb7ba45ece68c7d662d399eb 100644 --- a/src/descriptor_identifier/Model/ModelRegressor.hpp +++ b/src/descriptor_identifier/Model/ModelRegressor.hpp @@ -163,6 +163,13 @@ public: */ std::string toLatexString() const; + /** + * @brief Get the matlab expression for this model + + * @return The matlab exprssion for the model + */ + virtual std::string matlab_expr() const; + /** * @brief Copy the error into a new array * diff --git a/src/descriptor_identifier/SISSO_DI/SISSORegressor.cpp b/src/descriptor_identifier/SISSO_DI/SISSORegressor.cpp index d710b5e1784efe1d300d09ff7125e3728700d980..c46950a4d1ef6727880090da26d5c8263ca3b571 100644 --- a/src/descriptor_identifier/SISSO_DI/SISSORegressor.cpp +++ b/src/descriptor_identifier/SISSO_DI/SISSORegressor.cpp @@ -110,7 +110,9 @@ void SISSORegressor::set_error(const std::vector<int>& inds, const double* coefs void SISSORegressor::add_model(const std::vector<int> indexes) { if(_models.size() < indexes.size()) + { _models.push_back({}); + } std::vector<model_node_ptr> min_nodes(indexes.size()); for(int ii = 0; ii < indexes.size(); ++ii) diff --git a/src/feature_creation/node/FeatureNode.hpp b/src/feature_creation/node/FeatureNode.hpp index 0bb3be61a75bfae15c8e91c121bdc413724900b8..f19e63f86b2ef1a9dad4524325b5d3298a2be68a 100644 --- a/src/feature_creation/node/FeatureNode.hpp +++ b/src/feature_creation/node/FeatureNode.hpp @@ -286,6 +286,17 @@ public: */ inline std::string get_postfix_term() const {return std::to_string(_feat_ind);} + // DocString: feat_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + virtual inline std::string matlab_fxn_expr() const + { + return str_utils::matlabify(_expr); + } + //DocString: feat_node_nfeats /** * @brief Number of features used for an operator node @@ -416,6 +427,19 @@ public: */ inline std::string get_latex_expr(const double* params, const int depth=1) const {return str_utils::latexify(_expr);} + // DocString: feat_node_matlab_expr_param + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + virtual inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return str_utils::matlabify(_expr); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/ModelNode.cpp b/src/feature_creation/node/ModelNode.cpp index c23d0aa2613192c849fdad089d66454b07427625..92f07ceefb32590f90879dc46a22dd5152dbf1e2 100644 --- a/src/feature_creation/node/ModelNode.cpp +++ b/src/feature_creation/node/ModelNode.cpp @@ -9,6 +9,7 @@ ModelNode::ModelNode( const std::string expr, const std::string latex_expr, const std::string expr_postfix, + const std::string matlab_fxn_expr, const std::vector<double> value, const std::vector<double> test_value, const std::vector<std::string> x_in_expr_list, @@ -20,6 +21,7 @@ ModelNode::ModelNode( _x_in_expr_list(x_in_expr_list), _latex_expr(latex_expr), _expr_postfix(expr_postfix), + _matlab_fxn_expr(matlab_fxn_expr), _b_remap_svm(0.0), _w_remap_svm(1.0), _rung(rung), @@ -55,6 +57,7 @@ ModelNode::ModelNode(node_ptr in_node) : _x_in_expr_list(in_node->get_x_in_expr_list()), _latex_expr(in_node->latex_expr()), _expr_postfix(in_node->postfix_expr()), + _matlab_fxn_expr(in_node->matlab_fxn_expr()), _b_remap_svm(0.0), _w_remap_svm(1.0), _rung(in_node->rung()), diff --git a/src/feature_creation/node/ModelNode.hpp b/src/feature_creation/node/ModelNode.hpp index 9ee722b820b2a87eeb40f23b8d37a6326022f0f9..0c19dbe13815e4f8c34043691046740f5a7dd59a 100644 --- a/src/feature_creation/node/ModelNode.hpp +++ b/src/feature_creation/node/ModelNode.hpp @@ -39,6 +39,7 @@ class ModelNode: public FeatureNode ar & _x_in_expr_list; ar & _expr_postfix; ar & _latex_expr; + ar & _matlab_fxn_expr; ar & _b_remap_svm; ar & _w_remap_svm; ar & _rung; @@ -54,6 +55,7 @@ protected: std::vector<std::string> _x_in_expr_list; //!< vector storing the expressions for all primary features that show up in feature in the order they appear in the postfix notation std::string _expr_postfix; //!< postfix expression for the feature std::string _latex_expr; //!< postfix expression for the feature + std::string _matlab_fxn_expr; //!< The Matlab code needed to evaluate the feature double _b_remap_svm; //!< value to remap the b from SVM to real life double _w_remap_svm; //!< value to remap the w from SVM to real life @@ -73,6 +75,8 @@ public: * @param feat_ind index of the feature * @param rung the rung of the feature * @param expr Expression for the feature + * @param latex_expr The LaTeXified expression for the feature + * @param matlab_fxn_expr The code to evaluate the feature in matlab * @param value Value of the feature for each sample * @param test_value Value of the feature for each test sample * @param x_in_expr_list //!< vector storing the expressions for all primary features that show up in feature in the order they appear in the postfix notation @@ -84,6 +88,7 @@ public: const std::string expr, const std::string latex_expr, const std::string expr_postfix, + const std::string matlab_fxn_expr, const std::vector<double> value, const std::vector<double> test_value, const std::vector<std::string> x_in_expr_list, @@ -334,6 +339,17 @@ public: */ inline std::string get_latex_expr() const {return _latex_expr.substr(1, _latex_expr.size() - 2);} + // DocString: model_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return _matlab_fxn_expr; + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -400,6 +416,23 @@ public: */ inline py::list x_in_expr_list_py(){return python_conv_utils::to_list<std::string>(_x_in_expr_list);} #endif + + #ifdef PARAMETERIZE + // DocString: model_node_matlab_expr_param + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + std::cerr << "WARNING: ModelNode can't accept new parameters for getting Matlab code expressions" << std::endl; + return _matlab_fxn_expr; + } + #endif + }; #endif diff --git a/src/feature_creation/node/Node.hpp b/src/feature_creation/node/Node.hpp index 4f0bc9d4fc7172ca295691c3ab4215dbba3d485b..ecd01989607823fd8fb9c35432d9fa5da8d52428 100644 --- a/src/feature_creation/node/Node.hpp +++ b/src/feature_creation/node/Node.hpp @@ -326,6 +326,13 @@ public: */ virtual std::string get_postfix_term() const = 0; + // DocString: node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * @return The matlab code for the feature + */ + virtual std::string matlab_fxn_expr() const = 0; + /** * @brief update the dictionary used to check if an Add/Sub/AbsDiff node is valid * @@ -433,6 +440,16 @@ public: */ virtual std::string get_latex_expr(const double* params, const int depth=1) const = 0; + // DocString: node_matlab_expr_param + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + virtual std::string matlab_fxn_expr(const double* params, const int depth=1) const = 0; + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/OperatorNode.hpp b/src/feature_creation/node/operator_nodes/OperatorNode.hpp index 8583f9efbb0250fcc3f0ebf69796f3017b5d6421..64a19d35f98fb263e4b9d95d6a6a4694581ff672 100644 --- a/src/feature_creation/node/operator_nodes/OperatorNode.hpp +++ b/src/feature_creation/node/operator_nodes/OperatorNode.hpp @@ -331,6 +331,14 @@ public: */ virtual std::string get_postfix_term() const = 0; + // DocString: op_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + virtual std::string matlab_fxn_expr() const = 0; + //DocString: op_node_nfeats /** * @brief Number of features used for an operator node @@ -494,6 +502,15 @@ public: */ virtual std::string get_latex_expr(const double* params, const int depth=1) const = 0; + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + virtual std::string matlab_fxn_expr(const double* params, const int depth=1) const = 0; + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs/absolute_value.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs/absolute_value.hpp index 7446c42f1a6f9404cdac0e2ead6dc7f1b9195430..a747492264dc60c9d54cc713290787e748ad605f 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs/absolute_value.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs/absolute_value.hpp @@ -125,6 +125,20 @@ public: */ inline std::string get_postfix_term() const {return "abs";} + // DocString: abs_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "abs({})", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -225,6 +239,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "abs({:.10e}.*{}{:+15.10e})", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs/parameterized_absolute_value.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs/parameterized_absolute_value.hpp index 5f666a6c25f49da05fe0e5df9db7c70dc32dd795..1be28f8ea738494ab71463a3b78f8ec476f4d8e7 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs/parameterized_absolute_value.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs/parameterized_absolute_value.hpp @@ -41,6 +41,7 @@ protected: using AbsNode::test_value_ptr; using AbsNode::expr; using AbsNode::get_latex_expr; + using AbsNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector double _sign_alpha; //!< 1 if alpha is positive, -1 if alpha is negative @@ -143,6 +144,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: abs_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs_diff/absolute_difference.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs_diff/absolute_difference.hpp index 0a7ce56c7574e9b44ae4e1b039e24ad3fc7414ef..bb4715bd236f2dc50c2a0cae0fa8146644204c99 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs_diff/absolute_difference.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs_diff/absolute_difference.hpp @@ -132,6 +132,21 @@ public: */ inline std::string get_postfix_term() const {return "abd";} + // DocString: abs_diff_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "abs({} - {})", + _feats[0]->matlab_fxn_expr(), + _feats[1]->matlab_fxn_expr() + ); + } + /** * @brief Check if the feature will be valid, if it is then set the value * @return True if the feature is valid @@ -240,6 +255,24 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "abs({} - ({:.10e}.*{}{:+15.10e}))", + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + _feats[1]->n_params() + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs_diff/parameterized_absolute_difference.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs_diff/parameterized_absolute_difference.hpp index 2e18643e284015614f214b3e138d05c0347526f6..2fdf16beeb0b22587110315febf9af61b2d3cd45 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs_diff/parameterized_absolute_difference.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/abs_diff/parameterized_absolute_difference.hpp @@ -39,6 +39,7 @@ protected: using AbsDiffNode::test_value_ptr; using AbsDiffNode::expr; using AbsDiffNode::get_latex_expr; + using AbsDiffNode::matlab_fxn_expr; std::vector<double> _params; public: @@ -143,6 +144,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: abs_diff_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/add/add.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/add/add.hpp index 63f8e814fda85492d0f67c7b6a08e707d7a521b2..6c24be40777ce2749329945200e92b595e469f45 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/add/add.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/add/add.hpp @@ -128,6 +128,20 @@ public: */ inline std::string get_postfix_term() const {return "add";} + // DocString: add_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "({} + {})", + _feats[0]->matlab_fxn_expr(), + _feats[1]->matlab_fxn_expr() + ); + } /** * @brief Check if the feature will be valid, if it is then set the value * @return True if the feature is valid @@ -236,6 +250,24 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "({} + ({:.10e}.*{}{:+15.10e}))", + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + _feats[1]->n_params() + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/add/parameterized_add.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/add/parameterized_add.hpp index 627066213974d6f60df929aeae029a0b0c805e90..1b79820f8b3ff2f4ba097c61132e8dc75ea58de4 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/add/parameterized_add.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/add/parameterized_add.hpp @@ -40,6 +40,7 @@ protected: using AddNode::test_value_ptr; using AddNode::expr; using AddNode::get_latex_expr; + using AddNode::matlab_fxn_expr; std::vector<double> _params; @@ -144,6 +145,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: add_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cb/cube.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cb/cube.hpp index b3002a0eac282c3fdc815a829d60565360363696..a16d6e224bf457f892f96398301f905e18e66b02 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cb/cube.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cb/cube.hpp @@ -125,6 +125,20 @@ public: */ inline std::string get_postfix_term() const {return "cb";} + // DocString: cb_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "({}).^3", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -225,6 +239,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "({:.10e}.*{}{:+15.10e}).^3", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cb/parameterized_cube.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cb/parameterized_cube.hpp index 1bcb3511365d19c9f5fe6d51a1d048f022997c5b..940b5f2b084446a5b4ff6ce5bf225d24b10b28ed 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cb/parameterized_cube.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cb/parameterized_cube.hpp @@ -40,6 +40,7 @@ protected: using CbNode::test_value_ptr; using CbNode::expr; using CbNode::get_latex_expr; + using CbNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector @@ -141,6 +142,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: cb_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cbrt/cube_root.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cbrt/cube_root.hpp index e78d6b3db14da861e5f72ba8d4f60dc002978b37..bc5223cbea28192b5710f57f9007ab2db5714bad 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cbrt/cube_root.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cbrt/cube_root.hpp @@ -125,6 +125,20 @@ public: */ inline std::string get_postfix_term() const {return "cbrt";} + // DocString: cbrt_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "nthroot({}, 3)", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -224,6 +238,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "nthroot({:.10e}.*{}{:+15.10e}, 3)", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cbrt/parameterized_cube_root.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cbrt/parameterized_cube_root.hpp index 30dc9375f66ec4d466705f4e8106eb7ae04c9154..7f64a53950992d87300492530b3e01cdc5d66f17 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cbrt/parameterized_cube_root.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cbrt/parameterized_cube_root.hpp @@ -40,6 +40,7 @@ protected: using CbrtNode::test_value_ptr; using CbrtNode::expr; using CbrtNode::get_latex_expr; + using CbrtNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector public: @@ -140,6 +141,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: cbrt_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cos/cos.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cos/cos.hpp index 0d7ae874b7f416988e347b2be149b6394bf1861f..bbfcc9e429c595a7024d9f094f649a628f0d5415 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cos/cos.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cos/cos.hpp @@ -125,6 +125,20 @@ public: */ inline std::string get_postfix_term() const {return "cos";} + // DocString: cos_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "cos({})", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -224,6 +238,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "cos({:.10e}.*{}{:+15.10e})", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cos/parameterized_cos.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cos/parameterized_cos.hpp index a46796d8df1e22f2f98a02896d5dabc993993514..5c527bc9fb6fc4ada7e9ba945715daf221f4c5fe 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cos/parameterized_cos.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/cos/parameterized_cos.hpp @@ -40,6 +40,7 @@ protected: using CosNode::test_value_ptr; using CosNode::expr; using CosNode::get_latex_expr; + using CosNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector @@ -141,6 +142,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: cos_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/div/divide.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/div/divide.hpp index e9be9a2749853d3f2d4ea9bb52a763a624f0dd54..04da33a0d15edfd68f2df05323ce7f14032c7bcb 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/div/divide.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/div/divide.hpp @@ -129,6 +129,21 @@ public: */ inline std::string get_postfix_term() const {return "div";} + // DocString: div_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "({} ./ {})", + _feats[0]->matlab_fxn_expr(), + _feats[1]->matlab_fxn_expr() + ); + } + /** * @brief Check if the feature will be valid, if it is then set the value * @return True if the feature is valid @@ -237,6 +252,24 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "({} ./ ({:.10e}.*{}{:+15.10e}))", + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + _feats[1]->n_params() + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/div/parameterized_divide.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/div/parameterized_divide.hpp index 8c666a0fbe8129a2e3bc4a398347e52f462558ba..7e9b24ebd262d1d38c6c841697cba67c277ca547 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/div/parameterized_divide.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/div/parameterized_divide.hpp @@ -40,6 +40,7 @@ protected: using DivNode::test_value_ptr; using DivNode::expr; using DivNode::get_latex_expr; + using DivNode::matlab_fxn_expr; std::vector<double> _params; @@ -144,6 +145,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: div_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/exp/exponential.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/exp/exponential.hpp index 7766f1331d74ce066a2a56e5e07040d81392f99d..eb27deccb1917878b8c9cbf74d89341e281ed56b 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/exp/exponential.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/exp/exponential.hpp @@ -125,6 +125,20 @@ public: */ inline std::string get_postfix_term() const {return "exp";} + // DocString: exp_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "exp({})", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -224,6 +238,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "exp({:.10e}.*{}{:+15.10e})", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/exp/parameterized_exponential.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/exp/parameterized_exponential.hpp index ae31484d3567316013e1e3bb156322274a1e31a6..4a0696994cfa820164c7db17f9c8b1f5c8dbe58a 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/exp/parameterized_exponential.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/exp/parameterized_exponential.hpp @@ -40,6 +40,7 @@ protected: using ExpNode::test_value_ptr; using ExpNode::expr; using ExpNode::get_latex_expr; + using ExpNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector @@ -141,6 +142,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: exp_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/inv/inverse.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/inv/inverse.hpp index 62da8626e85a5cb4efe528457cd88ea74b00d290..a83a7a05a7ce10d3439c567fb928bd5bb679c13e 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/inv/inverse.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/inv/inverse.hpp @@ -121,6 +121,20 @@ public: */ inline std::string get_postfix_term() const {return "inv";} + // DocString: inv_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "({}).^(-1)", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -220,6 +234,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "({:.10e}.*{}{:+15.10e}).^(-1)", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/inv/parameterized_inverse.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/inv/parameterized_inverse.hpp index 8f58e32ef7375b0571648718bf8e716adb940206..51d8f5db0bc6c233ac98c358d74243dc584aaf68 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/inv/parameterized_inverse.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/inv/parameterized_inverse.hpp @@ -40,6 +40,7 @@ protected: using InvNode::test_value_ptr; using InvNode::expr; using InvNode::get_latex_expr; + using InvNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector @@ -141,6 +142,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: inv_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/log/log.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/log/log.hpp index f6a87c75da0280b78be6488403f3d569c3d51444..717260aacaaea29ba7503499070c22fb1a3ebca7 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/log/log.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/log/log.hpp @@ -131,6 +131,20 @@ public: */ inline std::string get_postfix_term() const {return "log";} + // DocString: log_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "log({})", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -224,6 +238,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "log({:.10e}.*{}{:+15.10e})", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/log/parameterized_log.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/log/parameterized_log.hpp index 9fb591a95709c281172bd2b064b2d8681bd84e82..3c742c52877ba545a5252ad25a13c084b446ed33 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/log/parameterized_log.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/log/parameterized_log.hpp @@ -40,6 +40,7 @@ protected: using LogNode::value_ptr; using LogNode::test_value_ptr; using LogNode::get_latex_expr; + using LogNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector @@ -141,6 +142,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: log_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/mult/multiply.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/mult/multiply.hpp index 6def1093c4867db3f84286466485622c566556c1..38eea1b02bfeb7e68fbc6a8344b072468bf232f7 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/mult/multiply.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/mult/multiply.hpp @@ -130,6 +130,21 @@ public: */ inline std::string get_postfix_term() const {return "mult";} + // DocString: mult_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "({} .* {})", + _feats[0]->matlab_fxn_expr(), + _feats[1]->matlab_fxn_expr() + ); + } + /** * @brief Check if the feature will be valid, if it is then set the value * @return True if the feature is valid @@ -238,6 +253,24 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "({} .* ({:.10e}.*{}{:+15.10e}))", + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + _feats[1]->n_params() + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/mult/parameterized_multiply.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/mult/parameterized_multiply.hpp index e55534a2e91ce9fac5406724cb9fa3592e515412..29d4681c848794838a22a87a1b16b7ac21d36115 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/mult/parameterized_multiply.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/mult/parameterized_multiply.hpp @@ -40,6 +40,7 @@ protected: using MultNode::test_value_ptr; using MultNode::expr; using MultNode::get_latex_expr; + using MultNode::matlab_fxn_expr; std::vector<double> _params; @@ -142,6 +143,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: mult_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/neg_exp/negative_exponential.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/neg_exp/negative_exponential.hpp index 98975e3c9ec14259285cf5872e55cbf150765ee0..24af3755798140daca47a68b1bf8f14df2cdab37 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/neg_exp/negative_exponential.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/neg_exp/negative_exponential.hpp @@ -126,6 +126,20 @@ public: */ inline std::string get_postfix_term() const {return "nexp";} + // DocString: neg_exp_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "exp(-1.0 * ({}))", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -201,7 +215,7 @@ public: inline std::string expr(const double* params, const int depth=1) const { return fmt::format( - "(exp(-1.0 * {:.10e}*{}{:+15.10e}))", + "(exp(-1.0 * ({:.10e}*{}{:+15.10e})))", params[0], (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->expr(params + 2, depth + 1) : _feats[0]->expr()), params[1] @@ -218,13 +232,30 @@ public: inline std::string get_latex_expr(const double* params, const int depth=1) const { return fmt::format( - "\\left(\\exp{{ \\left(-\\left({:.3e}{}{:+8.3e} \\right)\\right)}}\\right)", + "\\left(\\exp{{ \\left(-1.0\\left({:.3e}{}{:+8.3e} \\right)\\right)}}\\right)", params[0], (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->get_latex_expr(params + 2, depth + 1) : _feats[0]->get_latex_expr()), params[1] ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "exp(-1.0 .* ({:.10e}.*{}{:+15.10e}))", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/neg_exp/parameterized_negative_exponential.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/neg_exp/parameterized_negative_exponential.hpp index 749560352ea7c70766d1bbf889e42bf1e32947a4..a828030b44379cd72228e8dbc9597593ab9a395e 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/neg_exp/parameterized_negative_exponential.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/neg_exp/parameterized_negative_exponential.hpp @@ -40,6 +40,7 @@ protected: using NegExpNode::test_value_ptr; using NegExpNode::expr; using NegExpNode::get_latex_expr; + using NegExpNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector @@ -107,6 +108,14 @@ public: */ inline void set_test_value(int offset=-1, const bool for_comp=false) const {set_test_value(_params.data(), offset, for_comp);} + // DocString: neg_exp_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief Get the pointer to the feature's training data * @details If the feature is not already stored in memory, then calculate the feature and return the pointer to the data diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sin/parameterized_sin.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sin/parameterized_sin.hpp index a2f951724b4f48a36804543b5fa2d50aca306fa1..48d84813768db52f5f40bf8584ca40ee70f7e94e 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sin/parameterized_sin.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sin/parameterized_sin.hpp @@ -40,6 +40,7 @@ protected: using SinNode::test_value_ptr; using SinNode::expr; using SinNode::get_latex_expr; + using SinNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector @@ -141,6 +142,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: sin_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sin/sin.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sin/sin.hpp index 1af69ec16a01f4b1d739479a8c01cccc0ff9d07c..f363f2ddf06344d6461b1661b16145aeb360fca0 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sin/sin.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sin/sin.hpp @@ -126,6 +126,20 @@ public: */ inline std::string get_postfix_term() const {return "sin";} + // DocString: sin_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "sin({})", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -224,6 +238,24 @@ public: params[1] ); } + + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "sin({:.10e}.*{}{:+15.10e})", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/six_pow/parameterized_sixth_power.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/six_pow/parameterized_sixth_power.hpp index 0d3fcf3d1097d6ee585174a670770b492af1ecd3..29c3bc2cb1d73bcad6d17e3473323540a9cec02c 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/six_pow/parameterized_sixth_power.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/six_pow/parameterized_sixth_power.hpp @@ -40,6 +40,7 @@ protected: using SixPowNode::test_value_ptr; using SixPowNode::expr; using SixPowNode::get_latex_expr; + using SixPowNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector @@ -141,6 +142,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: six_pow_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/six_pow/sixth_power.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/six_pow/sixth_power.hpp index fbf27095e0984389529595e29b22423392542032..73e1ecb51960ec5f55de2f2457f1e226534f09fe 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/six_pow/sixth_power.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/six_pow/sixth_power.hpp @@ -126,6 +126,20 @@ public: */ inline std::string get_postfix_term() const {return "sp";} + // DocString: six_pow_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "({}).^6", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -226,6 +240,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "({:.10e}.*{}{:+15.10e}).^6", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sq/parameterized_square.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sq/parameterized_square.hpp index 626c67b975f427fd763b437757014eef82d197d6..b9012a6ebfbcd4f96ac72929de4a0afb9ab7c3b9 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sq/parameterized_square.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sq/parameterized_square.hpp @@ -40,6 +40,7 @@ protected: using SqNode::test_value_ptr; using SqNode::expr; using SqNode::get_latex_expr; + using SqNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector @@ -141,6 +142,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: sq_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sq/square.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sq/square.hpp index d852d1b872cb5f57a0794ce832bb78c385ff676a..3dc753b2f80d57cc65a40286f74f222f4789b922 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sq/square.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sq/square.hpp @@ -125,6 +125,20 @@ public: */ inline std::string get_postfix_term() const {return "sq";} + // DocString: sq_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "({}).^2", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -225,6 +239,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "({:.10e}.*{}{:+15.10e}).^6", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sqrt/parameterized_square_root.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sqrt/parameterized_square_root.hpp index 96abd3dfc813e9ae16b654dfe6715e167ed349ae..f7b71322aee1a04a9facb9d27d441e6b1adff253 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sqrt/parameterized_square_root.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sqrt/parameterized_square_root.hpp @@ -41,6 +41,7 @@ protected: using SqrtNode::test_value_ptr; using SqrtNode::expr; using SqrtNode::get_latex_expr; + using SqrtNode::matlab_fxn_expr; std::vector<double> _params; //!< The parameters vector double _sign_alpha; //!< 1 if alpha is positive, -1 if alpha is negative @@ -143,6 +144,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: sqrt_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sqrt/square_root.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sqrt/square_root.hpp index d13662a3babc1de8d995415c584dc73a6cf7b76e..2d4ef7368a74cd52b9bdb20c17a98c4f34801240 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sqrt/square_root.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sqrt/square_root.hpp @@ -126,6 +126,20 @@ public: */ inline std::string get_postfix_term() const {return "sqrt";} + // DocString: sqrt_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "sqrt({})", + _feats[0]->matlab_fxn_expr() + ); + } + /** * @brief update the dictionary used to check if an Add/Sub node is valid * @@ -225,6 +239,23 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "sqrt({:.10e}.*{}{:+15.10e})", + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sub/parameterized_subtract.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sub/parameterized_subtract.hpp index 056f11b5834e42a223bcfbba81403ac55207febe..8550534f34ab934eaed9f6568ad031e301923456 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sub/parameterized_subtract.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sub/parameterized_subtract.hpp @@ -40,6 +40,7 @@ protected: using SubNode::test_value_ptr; using SubNode::expr; using SubNode::get_latex_expr; + using SubNode::matlab_fxn_expr; std::vector<double> _params; @@ -144,6 +145,14 @@ public: */ inline std::string get_latex_expr() const {return get_latex_expr(_params.data());} + // DocString: sub_param_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const {return matlab_fxn_expr(_params.data());} + /** * @brief The parameters used for introducing more non linearity in the operators */ diff --git a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sub/subtract.hpp b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sub/subtract.hpp index 8bafd68cb7dc336521197e125487eb3e91de3a52..046453666e332a07c361f49830c41e0523fb9dd4 100644 --- a/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sub/subtract.hpp +++ b/src/feature_creation/node/operator_nodes/allowed_operator_nodes/sub/subtract.hpp @@ -131,6 +131,21 @@ public: */ inline std::string get_postfix_term() const {return "sub";} + // DocString: sub_node_matlab_expr + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr() const + { + return fmt::format( + "({} - {})", + _feats[0]->matlab_fxn_expr(), + _feats[1]->matlab_fxn_expr() + ); + } + /** * @brief Check if the feature will be valid, if it is then set the value * @return True if the feature is valid @@ -239,6 +254,24 @@ public: ); } + /** + * @brief Get the string that corresponds to the code needed to evaluate the node in matlab + * + * @param params parameter values for non-linear operations + * @param depth the current depth of the node on the Binary expression tree + * @return The matlab code for the feature + */ + inline std::string matlab_fxn_expr(const double* params, const int depth=1) const + { + return fmt::format( + "({} - ({:.10e}.*{}{:+15.10e}))", + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + _feats[1]->n_params() + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[0], + (depth < nlopt_wrapper::MAX_PARAM_DEPTH ? _feats[0]->matlab_fxn_expr(params + 2, depth + 1) : _feats[0]->matlab_fxn_expr()), + params[1] + ); + } + /** * @brief Set the bounds for the nl parameterization * diff --git a/src/python/bindings_docstring_keyed.cpp b/src/python/bindings_docstring_keyed.cpp index 0fc8656800f484b52c680dda3d53563f95b7c105..55cf3be330bdce2a2c54b0370db9cb35a39a8299 100644 --- a/src/python/bindings_docstring_keyed.cpp +++ b/src/python/bindings_docstring_keyed.cpp @@ -42,7 +42,7 @@ void sisso::register_all() def("phi_selected_from_file", &str2node::phi_selected_from_file_py); def("initialize_values_arr", init_val_ar); def("initialize_d_matrix_arr", &node_value_arrs::initialize_d_matrix_arr); - + def("matlabify", &str_utils::matlabify); #ifdef PARAMETERIZE sisso::feature_creation::node::registerAddParamNode(); @@ -266,10 +266,9 @@ void sisso::feature_creation::registerUnit() void sisso::feature_creation::node::registerFeatureNode() { - std::string (FeatureNode::*expr_1)() const = &FeatureNode::expr; - std::string (FeatureNode::*expr_const)() const = &FeatureNode::expr; void (FeatureNode::*set_value_no_param)(int, bool) const = &FeatureNode::set_value; void (FeatureNode::*set_test_value_no_param)(int, bool) const = &FeatureNode::set_test_value; + std::string (FeatureNode::*expr_no_param)() const = &FeatureNode::expr; using namespace boost::python; class_<FeatureNode, bases<Node>>("FeatureNode", init<int, std::string, np::ndarray, np::ndarray, Unit>()) @@ -278,8 +277,7 @@ void sisso::feature_creation::node::registerFeatureNode() .def("is_const", &FeatureNode::is_const, "@DocString_feat_node_is_const@") .def("set_value", set_value_no_param, "@DocString_feat_node_set_value@") .def("set_test_value", set_test_value_no_param, "@DocString_feat_node_set_test_value@") - .add_property("expr", expr_1, "@DocString_feat_node_expr_1@") - .add_property("expr", expr_const, "@DocString_feat_node_expr_const@") + .add_property("expr", expr_no_param, "@DocString_feat_node_expr_const@") .add_property("unit", &FeatureNode::unit, "@DocString_feat_node_unit@") .add_property("rung", &FeatureNode::rung, "@DocString_feat_node_rung@") .add_property("n_leaves", &FeatureNode::n_leaves) @@ -289,9 +287,6 @@ void sisso::feature_creation::node::registerFeatureNode() void sisso::feature_creation::node::registerModelNode() { - std::string (ModelNode::*expr_1)() const = &ModelNode::expr; - std::string (ModelNode::*expr_const)() const = &ModelNode::expr; - np::ndarray (ModelNode::*eval_many_dict)(py::dict) = &ModelNode::eval_many_py; np::ndarray (ModelNode::*eval_many_ndarr)(np::ndarray) = &ModelNode::eval_many_py; @@ -300,7 +295,7 @@ void sisso::feature_creation::node::registerModelNode() double (ModelNode::*eval_dict)(py::dict) = &ModelNode::eval_py; using namespace boost::python; - class_<ModelNode, bases<FeatureNode>>("ModelNode", init<int, int, std::string, std::string, std::string, std::vector<double>, std::vector<double>, std::vector<std::string>, Unit>()) + class_<ModelNode, bases<FeatureNode>>("ModelNode", init<int, int, std::string, std::string, std::string, std::string, std::vector<double>, std::vector<double>, std::vector<std::string>, Unit>()) .def(init<node_ptr>()) .def("is_nan", &ModelNode::is_nan, "@DocString_model_node_is_nan@") .def("is_const", &ModelNode::is_const, "@DocString_model_node_is_const@") @@ -842,6 +837,7 @@ void sisso::descriptor_identifier::registerModel() .def("eval", eval_ndarr) .def("eval", eval_list) .def("eval", eval_dict) + .def("write_matlab_fxn", &Model::write_matlab_fxn) .add_property("n_samp_train", &Model::n_samp_train, "@DocString_model_n_samp_train@") .add_property("n_samp_test", &Model::n_samp_test, "@DocString_model_n_samp_test@") .add_property("n_dim", &Model::n_dim, "@DocString_model_n_dim@") diff --git a/src/python/bindings_docstring_keyed.hpp b/src/python/bindings_docstring_keyed.hpp index 156e338420763d34a8b6c1617994ce0d1e19b947..412c5e2f783c1cec7e24eb4294da6753d792ec3c 100644 --- a/src/python/bindings_docstring_keyed.hpp +++ b/src/python/bindings_docstring_keyed.hpp @@ -63,6 +63,8 @@ namespace sisso inline std::string expr(double*, int depth=1) const {return this->get_override("expr")();} inline std::string get_latex_expr() const {return this->get_override("latex_expr")();} inline std::string get_latex_expr(double*, int depth=1) const {return this->get_override("latex_expr")();} + inline std::string matlab_fxn_expr() const {return this->get_override("matlab_fxn_expr")();} + inline std::string matlab_fxn_expr(const double*, int depth=1) const {return this->get_override("matlab_fxn_expr")();} inline Unit unit() const {return this->get_override("unit")();} inline std::vector<double> value() const {return this->get_override("value")();} inline std::vector<double> test_value() const {return this->get_override("test_value")();} @@ -116,6 +118,8 @@ namespace sisso inline std::string expr() const {return this->get_override("expr")();} inline std::string get_latex_expr(const double* params, const int depth=1) const {return this->get_override("latex_expr")();} inline std::string get_latex_expr() const {return this->get_override("latex_expr")();} + inline std::string matlab_fxn_expr() const {return this->get_override("matlab_fxn_expr")();} + inline std::string matlab_fxn_expr(const double*, int depth=1) const {return this->get_override("matlab_fxn_expr")();} inline void update_add_sub_leaves(std::map<std::string, int>& add_sub_leaves, const int pl_mn, int& expected_abs_tot) const {this->get_override("update_add_sub_leaves")();} inline void update_div_mult_leaves(std::map<std::string, double>& div_mult_leaves, const double fact, double& expected_abs_tot) const {this->get_override("update_div_mult_leaves")();} inline void get_parameters(std::shared_ptr<NLOptimizer> optimizer){this->get_override("get_parameters")();} @@ -135,6 +139,7 @@ namespace sisso public: inline std::string expr() const {return this->get_override("expr")();} inline std::string get_latex_expr() const {return this->get_override("latex_expr")();} + inline std::string matlab_fxn_expr() const {return this->get_override("matlab_fxn_expr")();} inline Unit unit() const {return this->get_override("unit")();} inline std::vector<double> value() const {return this->get_override("value")();} inline std::vector<double> test_value() const {return this->get_override("test_value")();} @@ -172,6 +177,7 @@ namespace sisso inline std::string get_postfix_term() const {return this->get_override("get_postfix_term")();} inline std::string expr() const {return this->get_override("expr")();} inline std::string get_latex_expr() const {return this->get_override("latex_expr")();} + inline std::string matlab_fxn_expr() const {return this->get_override("matlab_fxn_expr")();} inline void update_add_sub_leaves(std::map<std::string, int>& add_sub_leaves, const int pl_mn, int& expected_abs_tot) const {this->get_override("update_add_sub_leaves")();} inline void update_div_mult_leaves(std::map<std::string, double>& div_mult_leaves, const double fact, double& expected_abs_tot) const {this->get_override("update_div_mult_leaves")();} }; @@ -421,6 +427,7 @@ namespace sisso struct Model_Wrap : Model, py::wrapper<Model> { inline void to_file(std::string filename, bool train = true, std::vector<int> test_inds = {}){this->get_override("to_file")();} + inline void matlab_expr(){this->get_override("matlab_expr")();} }; /** diff --git a/src/utils/string_utils.cpp b/src/utils/string_utils.cpp index e7b71b8c2ca1a6dbe758072c3cb325d3e790b672..366c250c98e9f241bf7900bb302c217d774be9dd 100644 --- a/src/utils/string_utils.cpp +++ b/src/utils/string_utils.cpp @@ -33,3 +33,37 @@ std::string str_utils::latexify(const std::string str) } return to_ret.substr(0, to_ret.size() - 1); } + +std::string str_utils::op2str(const std::string str, const std::string op, const std::string op_str) +{ + std::string to_ret = ""; + std::vector<std::string> split_term_str = split_string_trim(str, op); + to_ret += split_term_str[0]; + for(int ii = 1; ii < split_term_str.size(); ++ii) + { + to_ret += op_str + split_term_str[ii]; + } + + return to_ret; +} + +std::string str_utils::matlabify(const std::string str) +{ + std::string to_ret = ""; + + std::string copy_str = str; + std::replace(copy_str.begin(), copy_str.end(), ' ', '_'); + + std::vector<std::string> split_str = split_string_trim(str, "\\"); + for(auto& term_str : split_str) + { + std::string add_str = term_str; + add_str = op2str(add_str, "/", "_div_"); + add_str = op2str(add_str, "*", "_mult_"); + add_str = op2str(add_str, "+", "_add_"); + add_str = op2str(add_str, "-", "_sub_"); + add_str = op2str(add_str, "^", "_pow_"); + to_ret += add_str; + } + return to_ret; +} diff --git a/src/utils/string_utils.hpp b/src/utils/string_utils.hpp index 53ce035fac1ecf34a36ba97b257b8867412d749e..28a4ff59d4626f7bfc6413f578e934ed82b59df4 100644 --- a/src/utils/string_utils.hpp +++ b/src/utils/string_utils.hpp @@ -7,10 +7,11 @@ #ifndef STRING_UTILS #define STRING_UTILS +#include <algorithm> #include <cmath> +#include <iostream> #include <string> #include <vector> -#include <iostream> #include <boost/algorithm/string.hpp> #include <boost/algorithm/string/trim.hpp> @@ -33,6 +34,24 @@ namespace str_utils * @return The latexified version of the string */ std::string latexify(const std::string str); + + /** + * @brief Convert a string into a matlab valid name + * + * @param str String to convert to a valid matlab name + * @return The matlab safe version of the string + */ + std::string matlabify(const std::string str); + + /** + * @brief Convert an operator into a valid matlab string + * + * @param str String to remove operators from + * @param op The operator to replace + * @param op_str The string to replace the operator with + * @return The string with the operators replaced + */ + std::string op2str(const std::string str, const std::string op, const std::string op_str); } diff --git a/src/utils/vector_utils.hpp b/src/utils/vector_utils.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c60771e2835bf6af86c16a1ae9f835f608761d02 --- /dev/null +++ b/src/utils/vector_utils.hpp @@ -0,0 +1,32 @@ +/** @file utils/vector_utils.hpp + * @brief A set of functions to manipulate vectors + * + * @author Thomas A. R. Purcell (tpurcell) + * @bug No known bugs. + */ +#ifndef VECTOR_UTILS +#define VECTOR_UTILS + +#include <algorithm> +#include <vector> + +namespace vector_utils +{ +template<typename T> +std::vector<T> unique(const std::vector<T> in_vec) +{ + std::vector<T> out_vec; + for(auto& el : in_vec) + { + if(find(out_vec.begin(), out_vec.end(), el) == out_vec.end()) + { + out_vec.push_back(el); + } + } + + return out_vec; +} +} + + +#endif