Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add debug logging and fix coefficient handling #770

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions omnn/math/Sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,23 @@ namespace
if (dn != constants::one) {
lcm.lcm(dn);
}
} else if (c.IsProduct()) {
auto& p = c.as<Product>();
if (p.size() == 2) {
auto it = p.begin();
if (*it == -1) {
auto next = std::next(it);
if (next->IsUnivariable()) {
auto& var = next->as<Variable>();
if (var == va) {
isNormalizedPolynomial = true; // This is a valid normalized form
continue; // Skip further normalization for -1*x case
}
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coefficient handling logic in Sum.cpp needs better documentation:

  1. The special case for -1*x pattern should explain why this case needs different handling
  2. The comment // Don't set isNormalizedPolynomial to false here should explain the reasoning
  3. Consider extracting the coefficient normalization logic into a separate method for better readability

// Don't set isNormalizedPolynomial to false here
// Let other checks determine normalization status
} else {
LOG_AND_IMPLEMENT("Solving " << va << " in " << *this << std::endl
<< "need to normalize coefficient: " << c << std::endl);
Expand All @@ -2137,14 +2154,14 @@ namespace
a.optimize();
}
auto k = coefficients[0];
if(!k.IsInt()) {
if(!k.IsInt() && !k.IsProduct()) {
#if !defined(NDEBUG) && !defined(NOOMDEBUG)
std::cout << "free member needed optimization: " << k << std::endl;
#endif
OptimizeOn oo;
k.optimize();
}
if(a.IsInt() && k.IsInt()) {
if(a.IsInt() && (k.IsInt() || k.IsProduct())) {
Valuable test;
auto& aFactors = omnn::rt::DivisorsLookupTable::Divisors(a.ca());

Expand Down
18 changes: 17 additions & 1 deletion omnn/math/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
using namespace omnn;
using namespace math;

std::ostream& operator<<(std::ostream& os, const System& sys) {
for (const auto& eq : sys) {
os << eq << std::endl;
}
return os;
}

bool is_subset(const auto& smaller_set, const auto& larger_set) {
return std::includes(larger_set.begin(), larger_set.end(), smaller_set.begin(), smaller_set.end());
Expand Down Expand Up @@ -265,10 +271,15 @@

System::solutions_t System::Solve(const Variable& va)
{
std::cout << "Solving system for variable " << va << std::endl;
std::cout << "System equations: " << *this << std::endl;

Check failure on line 275 in omnn/math/System.cpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘omnn::math::System’)

Check failure on line 275 in omnn/math/System.cpp

View workflow job for this annotation

GitHub Actions / build (windows-latest)

binary '<<': no operator found which takes a right-hand operand of type 'omnn::math::System' (or there is no acceptable conversion)

auto solutions = Known(va);
InProgress SolvingInProgress(solving, va);
if (SolvingInProgress)
if (SolvingInProgress) {
std::cout << "Already solving " << va << ", returning known solutions: " << solutions.size() << std::endl;
return solutions;
}

if (makeTotalEqu) {
auto vars = sqs.Vars();
Expand Down Expand Up @@ -503,6 +514,11 @@
}
}

std::cout << "Found " << solutions.size() << " solutions for " << va << std::endl;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug logging in System.cpp should be conditionally compiled:

  1. Use #ifdef DEBUG or similar to prevent logging in production
  2. Consider using a logging framework instead of direct cout
  3. The solution printing loop could be expensive for large solution sets

for (const auto& sol : solutions) {
std::cout << "Solution: " << sol << std::endl;
}

return solutions;
}

Expand Down
4 changes: 1 addition & 3 deletions omnn/math/test/08_System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,7 @@ BOOST_AUTO_TEST_CASE(sq_System_test
}
}

BOOST_AUTO_TEST_CASE(Quadratic_System_test
, *disabled() // Enable after review
) {
BOOST_AUTO_TEST_CASE(Quadratic_System_test) {
DECL_VARS(l);
System sys;

Expand Down
19 changes: 19 additions & 0 deletions omnn/math/test/Product_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,25 @@ BOOST_AUTO_TEST_CASE(Product_optimize_off_comparision_test)
EqualOrderCheck(_1, _2);
}

BOOST_AUTO_TEST_CASE(Product_coefficient_handling_test)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Product_coefficient_handling_test should be expanded to cover more cases:

  1. Test with larger polynomials (>2 terms)
  2. Test edge cases with zero coefficients
  3. Test with non-integer coefficients

{
DECL_VA(x);

// Test direct multiplication with negative coefficients
auto _1 = -1_v * x;
auto _2 = x * -1_v;
BOOST_TEST(_1 == _2); // Order independence

// Test coefficient normalization
auto _3 = (-1_v * x) * (-1_v * x);
BOOST_TEST(_3 == (x ^ 2)); // Double negation

// Test polynomial coefficient handling
auto _4 = -1_v * x + x * x;
auto _5 = (x ^ 2) - x;
BOOST_TEST(_4 == _5); // Equivalent polynomial forms
}

BOOST_AUTO_TEST_CASE(Product_tests)
{
auto f = 1_v / 2;
Expand Down
Loading