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

Update random(QQ) to use standard uniform distribution #3481

Draft
wants to merge 14 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions M2/Macaulay2/d/interface.dd
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ export rawFareyApproximation(e:Expr):Expr := (
setupfun("rawFareyApproximation", rawFareyApproximation);
export rawRandomQQ(e:Expr):Expr := (
when e
is Nothing do toExpr(Ccode(QQ, "rawRandomQQ(", "0)"))
is ht:ZZcell do toExpr(Ccode(QQ, "rawRandomQQ(", ht.v, ")"))
is ht:ZZcell do toExpr(Ccode(QQorNull, "rawRandomQQ(", ht.v, ")"))
Copy link
Member

@mahrud mahrud Sep 22, 2024

Choose a reason for hiding this comment

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

I think it might be simpler to just assert that Height is positive in the top-level, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

My concern was that we also want to catch this when making random matrices, which is a different call to the engine that isn't specific to the coefficient ring. Is height always positive for all the various meanings of height we might want for random? Or could it be zero/negative in other contexts?

else WrongArgZZ());
setupfun("rawRandomQQ",rawRandomQQ);
export rawRandomRRUniform(e:Expr):Expr := (
Expand Down
15 changes: 14 additions & 1 deletion M2/Macaulay2/e/interface/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "interface/random.h"
#include "interface/gmp-util.h"

#include "exceptions.hpp"
#include "error.h"

#define INITIALMAXINT 10

#define IA 16807
Expand Down Expand Up @@ -152,6 +155,9 @@ void rawSetRandomQQ(mpq_ptr result, gmp_ZZ height)
mpfr_t x;

if (height == nullptr) height = maxHeight;
if (mpz_cmp_si(height, 0) <= 0)
throw exc::engine_error("expected a positive height");

mpfr_init2(x, gmp_defaultPrecision);
mpfr_urandomb(x, state);
mpfr_mul_z(x, x, height, MPFR_RNDN);
Expand All @@ -165,7 +171,14 @@ gmp_QQ rawRandomQQ(gmp_ZZ height)
{
mpq_ptr result = getmemstructtype(mpq_ptr);
mpq_init(result);
rawSetRandomQQ(result, height);

try {
rawSetRandomQQ(result, height);
} catch (const exc::engine_error& e) {
ERROR(e.what());
return nullptr;
}

return moveTo_gmpQQ(result);
}

Expand Down
14 changes: 12 additions & 2 deletions M2/Macaulay2/e/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,12 @@ Matrix *Matrix::random(
int32_t u = rawRandomInt((int32_t)10000);
if (u > threshold) continue;
}
mat.set_entry(j, i, R->random());
try {
mat.set_entry(j, i, R->random());
} catch (const exc::engine_error& e) {
ERROR(e.what());
return nullptr;
}
}
}
else if (special_type == 1)
Expand All @@ -760,7 +765,12 @@ Matrix *Matrix::random(
int32_t u = rawRandomInt((int32_t)10000);
if (u > threshold) continue;
}
mat.set_entry(j, i, R->random());
try {
mat.set_entry(j, i, R->random());
} catch (const exc::engine_error& e) {
ERROR(e.what());
return nullptr;
}
}
}
}
Expand Down
Loading