From e61f0b8b3bdd2ee7938a216be510123ffc91e6c8 Mon Sep 17 00:00:00 2001 From: cianciosa Date: Mon, 13 Jul 2026 16:53:17 -0400 Subject: [PATCH 1/2] Add scale and offset into the hash and fix ismatch to check these values. --- graph_framework/piecewise.hpp | 173 +++++++++++++++++++++++++-------- graph_tests/piecewise_test.cpp | 44 +++++++++ 2 files changed, 177 insertions(+), 40 deletions(-) diff --git a/graph_framework/piecewise.hpp b/graph_framework/piecewise.hpp index 56de5c3..3b2937b 100644 --- a/graph_framework/piecewise.hpp +++ b/graph_framework/piecewise.hpp @@ -127,14 +127,20 @@ void compile_index(std::ostringstream &stream, //------------------------------------------------------------------------------ /// @brief Convert node pointer to a string with the argument. /// -/// @param[in] d Backend buffer. -/// @param[in] x Argument. +/// @param[in] d Backend buffer. +/// @param[in] x Argument. +/// @param[in] scale Scale factor for the argument. +/// @param[in] offset Offset factor for the argument. /// @return A string rep of the node. //------------------------------------------------------------------------------ static std::string to_string(const backend::buffer &d, - shared_leaf x) { + shared_leaf x, + const T scale, + const T offset) { return piecewise_1D_node::to_string(d) + - jit::format_to_string(x->get_hash()); + jit::format_to_string(x->get_hash()) + + jit::format_to_string(scale) + + jit::format_to_string(offset); } //------------------------------------------------------------------------------ @@ -177,7 +183,9 @@ void compile_index(std::ostringstream &stream, shared_leaf x, const T scale, const T offset) : - straight_node (x, piecewise_1D_node::to_string(d, x)), + straight_node (x, piecewise_1D_node::to_string(d, x, + scale, + offset)), data_hash(piecewise_1D_node::hash_data(d)), scale(scale), offset(offset) {} @@ -442,7 +450,7 @@ void compile_index(std::ostringstream &stream, if (x_cast.get()) { return this->data_hash == x_cast->data_hash && - this->arg->is_match(x_cast->get_arg()); + this->is_arg_match(x); } return false; @@ -704,17 +712,29 @@ void compile_index(std::ostringstream &stream, //------------------------------------------------------------------------------ /// @brief Convert node pointer to a string with the argument. /// -/// @param[in] d Backend buffer. -/// @param[in] x X argument. -/// @param[in] y Y argument. +/// @param[in] d Backend buffer. +/// @param[in] x X argument. +/// @param[in] x_scale Scale factor for the argument. +/// @param[in] x_offset Offset factor for the x argument. +/// @param[in] y Y argument. +/// @param[in] y_scale Scale factor for the y argument. +/// @param[in] y_offset Offset factor for the y argument. /// @return A string rep of the node. //------------------------------------------------------------------------------ static std::string to_string(const backend::buffer &d, shared_leaf x, - shared_leaf y) { + const T x_scale, + const T x_offset, + shared_leaf y, + const T y_scale, + const T y_offset) { return piecewise_2D_node::to_string(d) + jit::format_to_string(x->get_hash()) + - jit::format_to_string(y->get_hash()); + jit::format_to_string(x_scale) + + jit::format_to_string(x_offset) + + jit::format_to_string(y->get_hash()) + + jit::format_to_string(y_scale) + + jit::format_to_string(y_offset); } //------------------------------------------------------------------------------ @@ -767,7 +787,10 @@ void compile_index(std::ostringstream &stream, shared_leaf y, const T y_scale, const T y_offset) : - branch_node (x, y, piecewise_2D_node::to_string(d, x, y)), + branch_node (x, y, + piecewise_2D_node::to_string(d, + x, x_scale, x_offset, + y, y_scale, y_offset)), data_hash(piecewise_2D_node::hash_data(d)), num_columns(n), x_scale(x_scale), x_offset(x_offset), y_scale(y_scale), y_offset(y_offset) { @@ -1196,9 +1219,8 @@ void compile_index(std::ostringstream &stream, auto x_cast = piecewise_2D_cast(x); if (x_cast.get()) { - return this->data_hash == x_cast->data_hash && - this->left->is_match(x_cast->get_left()) && - this->right->is_match(x_cast->get_right()); + return this->data_hash == x_cast->data_hash && + this->is_arg_match(x); } return false; @@ -1322,7 +1344,7 @@ void compile_index(std::ostringstream &stream, return temp.get() && this->left->is_match(temp->get_arg()) && (temp->get_size() == this->get_num_rows()) && - (temp->get_scale() == this->x_scale) && + (temp->get_scale() == this->x_scale) && (temp->get_offset() == this->x_offset); } @@ -1339,7 +1361,7 @@ void compile_index(std::ostringstream &stream, return temp.get() && this->right->is_match(temp->get_arg()) && (temp->get_size() == this->get_num_columns()) && - (temp->get_scale() == this->y_scale) && + (temp->get_scale() == this->y_scale) && (temp->get_offset() == this->y_offset); } }; @@ -1433,14 +1455,20 @@ void compile_index(std::ostringstream &stream, //------------------------------------------------------------------------------ /// @brief Convert node pointer to a string with the argument. /// -/// @param[in] v Value to index. -/// @param[in] x Argument. +/// @param[in] v Value to index. +/// @param[in] x Argument. +/// @param[in] scale Scale factor for the argument. +/// @param[in] offset Offset factor for the argument. /// @return A string rep of the node. //------------------------------------------------------------------------------ static std::string to_string(shared_leaf v, - shared_leaf x) { + shared_leaf x, + const T scale, + const T offset) { return jit::format_to_string(v->get_hash()) + "[" + - jit::format_to_string(x->get_hash()) + "]"; + jit::format_to_string(x->get_hash()) + + jit::format_to_string(scale) + + jit::format_to_string(offset) + "]"; } public: @@ -1456,7 +1484,9 @@ void compile_index(std::ostringstream &stream, shared_leaf x, const T scale, const T offset) : - branch_node (var, x, index_1D_node::to_string(var, x)), + branch_node (var, x, + index_1D_node::to_string(var, x, + scale, offset)), scale(scale), offset(offset) {} //------------------------------------------------------------------------------ @@ -1544,6 +1574,25 @@ void compile_index(std::ostringstream &stream, return this->shared_from_this(); } +//------------------------------------------------------------------------------ +/// @brief Query if the nodes match. +/// +/// Assumes both arguments are either set or not set. +/// +/// @param[in] x Other graph to check if it is a match. +/// @returns True if the nodes are a match. +//------------------------------------------------------------------------------ + virtual bool is_match(shared_leaf x) { + auto x_cast = index_1D_cast(x); + + if (x_cast.get()) { + return this->left == x_cast->get_left() && + this->is_arg_match(x); + } + + return false; + } + //------------------------------------------------------------------------------ /// @brief Convert the node to latex. //------------------------------------------------------------------------------ @@ -1623,11 +1672,15 @@ void compile_index(std::ostringstream &stream, //------------------------------------------------------------------------------ bool is_arg_match(shared_leaf x) { auto temp = index_1D_cast(x); - return temp.get() && - this->right->is_match(temp->get_right()) && - (temp->get_size() == this->get_size()) && - (temp->get_scale() == this->scale) && - (temp->get_offset() == this->offset); + + if (temp.get()) { + return this->right->is_match(temp->get_right()) && + (temp->get_size() == this->get_size()) && + (temp->get_scale() == this->scale) && + (temp->get_offset() == this->offset); + } + + return false; } //------------------------------------------------------------------------------ @@ -1748,17 +1801,29 @@ void compile_index(std::ostringstream &stream, //------------------------------------------------------------------------------ /// @brief Convert node pointer to a string with the argument. /// -/// @param[in] v Value to index. -/// @param[in] x Argument. -/// @param[in] y Argument. +/// @param[in] v Value to index. +/// @param[in] x Argument. +/// @param[in] x_scale Scale factor for the argument. +/// @param[in] x_offset Offset factor for the x argument. +/// @param[in] y Argument. +/// @param[in] y_scale Scale factor for the y argument. +/// @param[in] y_offset Offset factor for the y argument. /// @return A string rep of the node. //------------------------------------------------------------------------------ static std::string to_string(shared_leaf v, shared_leaf x, - shared_leaf y) { + const T x_scale, + const T x_offset, + shared_leaf y, + const T y_scale, + const T y_offset) { return jit::format_to_string(v->get_hash()) + "[" + - jit::format_to_string(x->get_hash()) + "," + - jit::format_to_string(y->get_hash()) + "]"; + jit::format_to_string(x->get_hash()) + + jit::format_to_string(x_scale) + + jit::format_to_string(x_offset) + "," + + jit::format_to_string(y->get_hash()) + + jit::format_to_string(x_scale) + + jit::format_to_string(x_offset) + "]"; } public: @@ -1782,7 +1847,10 @@ void compile_index(std::ostringstream &stream, shared_leaf y, const T y_scale, const T y_offset) : - triple_node (var, x, y, index_2D_node::to_string(var, x, y)), + triple_node (var, x, y, + index_2D_node::to_string(var, + x, x_scale, x_offset, + y, y_scale, y_offset)), num_columns(n), x_scale(x_scale), x_offset(x_offset), y_scale(y_scale), y_offset(y_offset) { assert(variable_cast(this->left)->size()%n == 0 && @@ -1909,6 +1977,25 @@ void compile_index(std::ostringstream &stream, return this->shared_from_this(); } +//------------------------------------------------------------------------------ +/// @brief Query if the nodes match. +/// +/// Assumes both arguments are either set or not set. +/// +/// @param[in] x Other graph to check if it is a match. +/// @returns True if the nodes are a match. +//------------------------------------------------------------------------------ + virtual bool is_match(shared_leaf x) { + auto x_cast = index_2D_cast(x); + + if (x_cast.get()) { + return this->left == x_cast->get_left() && + this->is_arg_match(x); + } + + return false; + } + //------------------------------------------------------------------------------ /// @brief Convert the node to latex. //------------------------------------------------------------------------------ @@ -1991,12 +2078,18 @@ void compile_index(std::ostringstream &stream, /// @returns True if the arguments match. //------------------------------------------------------------------------------ bool is_arg_match(shared_leaf x) { - auto temp = index_1D_cast(x); - return temp.get() && - this->right->is_match(temp->get_right()) && - (temp->get_size() == this->get_size()) && - (temp->get_scale() == this->scale) && - (temp->get_offset() == this->offset); + auto temp = index_2D_cast(x); + + if (temp.get()) { + return this->right->is_match(temp->get_right()) && + (temp->get_size() == this->get_size()) && + (temp->get_x_scale() == this->x_scale) && + (temp->get_x_offset() == this->x_offset) && + (temp->get_y_scale() == this->y_scale) && + (temp->get_y_offset() == this->y_offset); + } + + return false; } //------------------------------------------------------------------------------ diff --git a/graph_tests/piecewise_test.cpp b/graph_tests/piecewise_test.cpp index 267e88b..eae3207 100644 --- a/graph_tests/piecewise_test.cpp +++ b/graph_tests/piecewise_test.cpp @@ -92,6 +92,19 @@ template void piecewise_1D() { static_cast (4.0), static_cast (6.0)}), a, 1.0, 0.0); + auto p4 = graph::piecewise_1D (std::vector ({static_cast (1.0), + static_cast (2.0), + static_cast (3.0)}), + a, 2.0, 3.0); + auto p5 = graph::piecewise_1D (std::vector ({static_cast (1.0), + static_cast (2.0), + static_cast (3.0)}), + a, 1.0, 0.0); + + assert(!p1->is_match(p4) && "Expected no match."); + assert(p1.get() != p4.get() && "Expected no match."); + assert(p1->is_match(p5) && "Expected match."); + assert(p1.get() == p5.get() && "Expected match."); auto c = graph::constant (static_cast (2.5)); auto pconst = graph::piecewise_1D (std::vector ({static_cast (2.0), @@ -327,6 +340,20 @@ template void piecewise_2D() { static_cast (2.0), static_cast (4.0) }), ay, 1.0, 0.0); + auto p6 = graph::piecewise_2D (std::vector ({ + static_cast (1.0), static_cast (2.0), + static_cast (3.0), static_cast (4.0) + }), 2, ax, 1.0, 0.0, ay, 1.0, 5.0); + auto p7 = graph::piecewise_2D (std::vector ({ + static_cast (1.0), static_cast (2.0), + static_cast (3.0), static_cast (4.0) + }), 2, ax, 1.0, 0.0, ay, 1.0, 0.0); + + assert(!p1->is_match(p6) && "Expected no match."); + assert(p1.get() != p6.get() && "Expected no match."); + assert(p1->is_match(p7) && "Expected match."); + assert(p1.get() == p7.get() && "Expected match."); + auto cx = graph::constant (static_cast (0.5)); auto cy = graph::constant (static_cast (1.5)); auto pconst = graph::piecewise_2D (std::vector ({ @@ -809,6 +836,13 @@ template void index_1D() { auto arg = graph::variable (1, ""); auto index = graph::index_1D (variable, arg, 1.0, 0.0); + auto index2 = graph::index_1D (variable, arg, 1.0, 2.0); + auto index3 = graph::index_1D (variable, arg, 1.0, 0.0); + + assert(!index->is_match(index2) && "Expected no match."); + assert(index.get() != index2.get() && "Expected no match."); + assert(index->is_match(index3) && "Expected match."); + assert(index.get() == index3.get() && "Expected match."); variable->set({0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}); arg->set(static_cast (3.5)); @@ -839,6 +873,16 @@ template void index_2D() { auto index = graph::index_2D (variable, 3, x, 1.0, 0.0, y, 1.0, 0.0); + auto index2 = graph::index_2D (variable, 3, + x, 1.0, 0.0, + y, 1.0, 2.0); + auto index3 = graph::index_2D (variable, 3, + x, 1.0, 0.0, + y, 1.0, 0.0); + assert(!index->is_match(index2) && "Expected no match."); + assert(index.get() != index2.get() && "Expected no match."); + assert(index->is_match(index3) && "Expected match."); + assert(index.get() == index3.get() && "Expected match."); variable->set({ 1.0, 2.0, 3.0, From b81eb9cc269460a82e74da33f65bebcc7e79cc73 Mon Sep 17 00:00:00 2001 From: cianciosa Date: Mon, 13 Jul 2026 17:26:42 -0400 Subject: [PATCH 2/2] Arguments should be tested via is_match. --- graph_framework/piecewise.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graph_framework/piecewise.hpp b/graph_framework/piecewise.hpp index 3b2937b..c9fca38 100644 --- a/graph_framework/piecewise.hpp +++ b/graph_framework/piecewise.hpp @@ -1586,7 +1586,7 @@ void compile_index(std::ostringstream &stream, auto x_cast = index_1D_cast(x); if (x_cast.get()) { - return this->left == x_cast->get_left() && + return this->left->is_match(x_cast->get_left()) && this->is_arg_match(x); } @@ -1989,7 +1989,7 @@ void compile_index(std::ostringstream &stream, auto x_cast = index_2D_cast(x); if (x_cast.get()) { - return this->left == x_cast->get_left() && + return this->left->is_match(x_cast->get_left()) && this->is_arg_match(x); }