Skip to content

Commit

Permalink
Integer::LCM
Browse files Browse the repository at this point in the history
  • Loading branch information
ohhmm committed Feb 3, 2024
1 parent 24e5383 commit 1bafafb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
15 changes: 15 additions & 0 deletions omnn/math/Integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,21 @@ namespace math {
: v.GCD(*this);
}

Valuable Integer::LCM(const Valuable& v) const {
return v.IsInt() ? boost::lcm(v.ca(), ca()) : v.LCM(*this);
}

Valuable& Integer::lcm(const Valuable& v) {
if (v.IsInt()) {
arbitrary = boost::lcm(v.ca(), ca());
hash = std::hash<base_int>()(arbitrary);
} else {
Become(v.LCM(*this));
}
return *this;
}


// sqrt using boost
Valuable Integer::Sqrt() const
{
Expand Down
2 changes: 2 additions & 0 deletions omnn/math/Integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ class Integer
bool IsComesBefore(const Valuable& v) const override;
Valuable InCommonWith(const Valuable& v) const override;
Valuable GCD(const Valuable& v) const override;
Valuable LCM(const Valuable& v) const override;
Valuable& lcm(const Valuable& v) override;

std::wstring save(const std::wstring&) const override;
std::ostream& code(std::ostream&) const override;
Expand Down
29 changes: 28 additions & 1 deletion omnn/math/Valuable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,8 +1317,35 @@ bool Valuable::SerializedStrEqual(const std::string_view& s) const {
return Become(GCD(v));
}

Valuable Valuable::LCM(const Valuable& v) const {
if (exp) {
return exp->LCM(v);
} else {
IMPLEMENT
}
}

Valuable& Valuable::lcm(const Valuable& v) {
IMPLEMENT
if (exp) {
Valuable& o = exp->lcm(v);
if (o.exp) {
exp = o.exp;
}
return *this;
}
//else
// IMPLEMENT
if (operator==(constants::zero))
return *this;
else if (v == constants::zero)
return *this;
else if (operator==(v)) {
Become(0);
return *this;
} else {
auto gcd = GCD(v);
return operator*=(v).abs().operator/=(gcd);
}
}

Valuable& Valuable::d(const Variable& x)
Expand Down
5 changes: 3 additions & 2 deletions omnn/math/Valuable.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,9 @@ class Valuable

// returns the greatest common divisor (GCD) with the given object
virtual Valuable GCD(const Valuable& v) const;
Valuable& gcd(const Valuable& v);
Valuable& lcm(const Valuable& v);
virtual Valuable& gcd(const Valuable& v);
virtual Valuable LCM(const Valuable& v) const;
virtual Valuable& lcm(const Valuable& v);

virtual Valuable& d(const Variable& x);
struct IntegrationParams {
Expand Down

0 comments on commit 1bafafb

Please sign in to comment.