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

various improvements: add decoration type, improve tropicalnumber, some graph stuff #60

Merged
merged 10 commits into from
May 31, 2024
Prev Previous commit
Next Next commit
more const for integer and rational
  • Loading branch information
benlorenz committed May 24, 2024
commit 0ceb9292574e476d47bca0de158c22bcfa439494
54 changes: 27 additions & 27 deletions src/type_integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,69 +65,69 @@ void add_integer(jlcxx::Module& jlpolymake)
.add_type<pm::Integer>("Integer",
jlcxx::julia_type("Integer", "Base"))
.constructor<int64_t>()
.method("<", [](pm::Integer& a, pm::Integer& b) { return a < b; })
.method("<", [](pm::Integer& a,
.method("<", [](const pm::Integer& a, const pm::Integer& b) { return a < b; })
.method("<", [](const pm::Integer& a,
int64_t b) { return a < static_cast<pm::Int>(b); })
.method("<", [](int64_t a,
pm::Integer& b) { return static_cast<pm::Int>(a) < b; })
.method("<=", [](pm::Integer& a, pm::Integer& b) { return a <= b; })
.method("<=", [](pm::Integer& a,
const pm::Integer& b) { return static_cast<pm::Int>(a) < b; })
.method("<=", [](const pm::Integer& a, const pm::Integer& b) { return a <= b; })
.method("<=", [](const pm::Integer& a,
int64_t b) { return a <= static_cast<pm::Int>(b); })
.method("<=",
[](int64_t a, pm::Integer& b) {
[](int64_t a, const pm::Integer& b) {
return static_cast<pm::Int>(a) <= b;
})

.method("show_small_obj",
[](pm::Integer& i) {
[](const pm::Integer& i) {
return show_small_object<pm::Integer>(i, false);
})
.method("isfinite", [](const pm::Integer& i) { return isfinite(i); })
.method("Float64", [](pm::Integer& a) { return double(a); })
.method("-", [](pm::Integer& a, pm::Integer& b) { return a - b; })
.method("-", [](pm::Integer& a,
.method("Float64", [](const pm::Integer& a) { return double(a); })
.method("-", [](const pm::Integer& a, const pm::Integer& b) { return a - b; })
.method("-", [](const pm::Integer& a,
int64_t b) { return a - static_cast<pm::Int>(b); })
.method("-", [](int64_t a,
pm::Integer& b) { return static_cast<pm::Int>(a) - b; })
const pm::Integer& b) { return static_cast<pm::Int>(a) - b; })
// unary minus
.method("-", [](pm::Integer& a) { return -a; })
.method("-", [](const pm::Integer& a) { return -a; })

.method("div", [](pm::Integer& a, pm::Integer& b) { return a / b; })
.method("div", [](pm::Integer& a,
.method("div", [](const pm::Integer& a, const pm::Integer& b) { return a / b; })
.method("div", [](const pm::Integer& a,
int64_t b) { return a / static_cast<pm::Int>(b); })
.method("div",
[](int64_t a, pm::Integer& b) {
[](int64_t a, const pm::Integer& b) {
return static_cast<pm::Int>(a) / b;
})

.method("rem", [](pm::Integer& a, pm::Integer& b) { return a % b; })
.method("rem", [](pm::Integer& a,
.method("rem", [](const pm::Integer& a, const pm::Integer& b) { return a % b; })
.method("rem", [](const pm::Integer& a,
int64_t b) { return a % static_cast<pm::Int>(b); })
.method("rem",
[](int64_t a, pm::Integer& b) {
[](int64_t a, const pm::Integer& b) {
return static_cast<pm::Int>(a) % b;
});

jlpolymake.set_override_module(jlpolymake.julia_module());
jlpolymake.method("==", [](pm::Integer& a, pm::Integer& b) {
jlpolymake.method("==", [](const pm::Integer& a, const pm::Integer& b) {
return a == b; });
jlpolymake.method("==", [](pm::Integer& a, int64_t b) {
jlpolymake.method("==", [](const pm::Integer& a, int64_t b) {
return a == static_cast<pm::Int>(b); });
jlpolymake.method("==", [](int64_t a, pm::Integer& b) {
jlpolymake.method("==", [](int64_t a, const pm::Integer& b) {
return static_cast<pm::Int>(a) == b; });

// the symmetric definitions are on the julia side
jlpolymake.method("+", [](pm::Integer& a, pm::Integer& b) {
jlpolymake.method("+", [](const pm::Integer& a, const pm::Integer& b) {
return a + b; });
jlpolymake.method("+", [](pm::Integer& a, int64_t b) {
jlpolymake.method("+", [](const pm::Integer& a, int64_t b) {
return a + static_cast<pm::Int>(b); });
jlpolymake.method("+", [](int64_t a, pm::Integer& b) {
jlpolymake.method("+", [](int64_t a, const pm::Integer& b) {
return static_cast<pm::Int>(a) + b; });
jlpolymake.method("*", [](pm::Integer& a, pm::Integer& b) {
jlpolymake.method("*", [](const pm::Integer& a, const pm::Integer& b) {
return a * b; });
jlpolymake.method("*", [](pm::Integer& a, int64_t b) {
jlpolymake.method("*", [](const pm::Integer& a, int64_t b) {
return a * static_cast<pm::Int>(b); });
jlpolymake.method("*", [](int64_t a, pm::Integer& b) {
jlpolymake.method("*", [](int64_t a, const pm::Integer& b) {
return static_cast<pm::Int>(a) * b; });
jlpolymake.unset_override_module();

Expand Down
78 changes: 41 additions & 37 deletions src/type_rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ namespace jlpolymake {

pm::Rational new_rational_from_fmpq(jl_value_t* rational)
{
JL_GC_PUSH1(&rational);
mpz_t n, d;
mpz_init(n);
mpz_init(d);
fmpq_get_mpz_frac(n, d, *reinterpret_cast<fmpq_t*>(rational));
JL_GC_POP();
return pm::Rational(std::move(n), std::move(d));
}

pm::Rational new_rational_from_fmpz(jl_value_t* integer)
{
JL_GC_PUSH1(&integer);
mpz_t z_mp;
mpz_init(z_mp);
fmpz_get_mpz(z_mp, *reinterpret_cast<fmpz_t*>(integer));
JL_GC_POP();
return pm::Rational(std::move(z_mp));
}

Expand Down Expand Up @@ -78,20 +82,20 @@ void add_rational(jlcxx::Module& jlpolymake)
const jlcxx::StrictlyTypedNumber<long> den) {
return pm::Rational(num.value, den.value);
})
.method("<", [](pm::Rational& a, pm::Rational& b) { return a < b; })
.method("<", [](pm::Rational& a, pm::Integer& b) { return a < b; })
.method("<", [](pm::Rational& a,
.method("<", [](const pm::Rational& a, const pm::Rational& b) { return a < b; })
.method("<", [](const pm::Rational& a, const pm::Integer& b) { return a < b; })
.method("<", [](const pm::Rational& a,
int64_t b) { return a < static_cast<pm::Int>(b); })
.method("<", [](pm::Integer& a, pm::Rational& b) { return a < b; })
.method("<", [](const pm::Integer& a, const pm::Rational& b) { return a < b; })
.method("<", [](int64_t a,
pm::Rational& b) { return static_cast<pm::Int>(a) < b; })
const pm::Rational& b) { return static_cast<pm::Int>(a) < b; })

.method("<=", [](pm::Rational& a, pm::Rational& b) { return a <= b; })
.method("<=", [](pm::Rational& a, pm::Integer& b) { return a <= b; })
.method("<=", [](pm::Rational& a,
.method("<=", [](const pm::Rational& a, const pm::Rational& b) { return a <= b; })
.method("<=", [](const pm::Rational& a, const pm::Integer& b) { return a <= b; })
.method("<=", [](const pm::Rational& a,
int64_t b) { return a <= static_cast<pm::Int>(b); })
.method("<=", [](pm::Integer& a, pm::Rational& b) { return a <= b; })
.method("<=", [](int64_t a, pm::Rational& b) {
.method("<=", [](const pm::Integer& a, const pm::Rational& b) { return a <= b; })
.method("<=", [](int64_t a, const pm::Rational& b) {
return static_cast<pm::Int>(a) <= b;
})

Expand All @@ -106,56 +110,56 @@ void add_rational(jlcxx::Module& jlpolymake)
return show_small_object<pm::Rational>(r, false);
})
.method("isfinite", [](const pm::Rational& r) { return isfinite(r); })
.method("Float64", [](pm::Rational& a) { return double(a); })
.method("-", [](pm::Rational& a, pm::Rational& b) { return a - b; })
.method("-", [](pm::Rational& a, pm::Integer& b) { return a - b; })
.method("-", [](pm::Rational& a,
.method("Float64", [](const pm::Rational& a) { return double(a); })
.method("-", [](const pm::Rational& a, const pm::Rational& b) { return a - b; })
.method("-", [](const pm::Rational& a, const pm::Integer& b) { return a - b; })
.method("-", [](const pm::Rational& a,
int64_t b) { return a - static_cast<pm::Int>(b); })
.method("-", [](pm::Integer& a, pm::Rational& b) { return a - b; })
.method("-", [](const pm::Integer& a, const pm::Rational& b) { return a - b; })
.method("-", [](int64_t a,
pm::Rational& b) { return static_cast<pm::Int>(a) - b; })
const pm::Rational& b) { return static_cast<pm::Int>(a) - b; })
// unary minus
.method("-", [](pm::Rational& a) { return -a; })
.method("-", [](const pm::Rational& a) { return -a; })

.method("//", [](pm::Rational& a, pm::Rational& b) { return a / b; })
.method("//", [](pm::Rational& a, pm::Integer& b) { return a / b; })
.method("//", [](pm::Rational& a, int64_t b) {
.method("//", [](const pm::Rational& a, const pm::Rational& b) { return a / b; })
.method("//", [](const pm::Rational& a, const pm::Integer& b) { return a / b; })
.method("//", [](const pm::Rational& a, int64_t b) {
return a / static_cast<pm::Int>(b); })
.method("//", [](pm::Integer& a, pm::Rational& b) { return a / b; })
.method("//", [](int64_t a, pm::Rational& b) {
.method("//", [](const pm::Integer& a, const pm::Rational& b) { return a / b; })
.method("//", [](int64_t a, const pm::Rational& b) {
return static_cast<pm::Int>(a) / b; });

jlpolymake.set_override_module(jlpolymake.julia_module());
jlpolymake.method("==", [](pm::Rational& a, pm::Rational& b) {
jlpolymake.method("==", [](const pm::Rational& a, const pm::Rational& b) {
return a == b; });
jlpolymake.method("==", [](pm::Rational& a, pm::Integer& b) {
jlpolymake.method("==", [](const pm::Rational& a, const pm::Integer& b) {
return a == b; });
jlpolymake.method("==", [](pm::Integer& a, pm::Rational& b) {
jlpolymake.method("==", [](const pm::Integer& a, const pm::Rational& b) {
return a == b; });
jlpolymake.method("==", [](pm::Rational& a, int64_t b) {
jlpolymake.method("==", [](const pm::Rational& a, int64_t b) {
return a == static_cast<pm::Int>(b); });
jlpolymake.method("==", [](int64_t a, pm::Rational& b) {
jlpolymake.method("==", [](int64_t a, const pm::Rational& b) {
return static_cast<pm::Int>(a) == b; });
// the symmetric definitions are on the julia side
jlpolymake.method("+", [](pm::Rational& a, pm::Rational& b) {
jlpolymake.method("+", [](const pm::Rational& a, const pm::Rational& b) {
return a + b; });
jlpolymake.method("+", [](pm::Rational& a, pm::Integer& b) {
jlpolymake.method("+", [](const pm::Rational& a, const pm::Integer& b) {
return a + b; });
jlpolymake.method("+", [](pm::Integer& a, pm::Rational& b) {
jlpolymake.method("+", [](const pm::Integer& a, const pm::Rational& b) {
return a + b; });
jlpolymake.method("+", [](pm::Rational& a, int64_t b) {
jlpolymake.method("+", [](const pm::Rational& a, int64_t b) {
return a + static_cast<pm::Int>(b); });
jlpolymake.method("+", [](int64_t a, pm::Rational& b) {
jlpolymake.method("+", [](int64_t a, const pm::Rational& b) {
return static_cast<pm::Int>(a) + b; });
jlpolymake.method("*", [](pm::Rational& a, pm::Rational& b) {
jlpolymake.method("*", [](const pm::Rational& a, const pm::Rational& b) {
return a * b; });
jlpolymake.method("*", [](pm::Rational& a, pm::Integer& b) {
jlpolymake.method("*", [](const pm::Rational& a, const pm::Integer& b) {
return a * b; });
jlpolymake.method("*", [](pm::Integer& a, pm::Rational& b) {
jlpolymake.method("*", [](const pm::Integer& a, const pm::Rational& b) {
return a * b; });
jlpolymake.method("*", [](pm::Rational& a, int64_t b) {
jlpolymake.method("*", [](const pm::Rational& a, int64_t b) {
return a * static_cast<pm::Int>(b); });
jlpolymake.method("*", [](int64_t a, pm::Rational& b) {
jlpolymake.method("*", [](int64_t a, const pm::Rational& b) {
return static_cast<pm::Int>(a) * b; });
jlpolymake.unset_override_module();

Expand Down