-
Notifications
You must be signed in to change notification settings - Fork 133
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
Better API for generating random elements #2896
Comments
Let me say that a related but somewhat separate issue is that in many cases (for experiments but also algorithms) what one wants is not just a fixed way to get random elements, but really one needs a "random process" which is a stateful object that can be asked to produce additional random elements, but which can uses its state to e.g. increase the parameters over time: e.g. maybe the first 10 random polynomials should have total degree <= 5, and after that this is raised to <= 10, or whatever. But I think this could be mostly discussed in a separate issue (if someone wants to open one...?), here we should only discuss it if it has implications for the API design... |
Also, of course this can also be made to work with even more flexible "sampler" objects as discussed in the |
Do we wanna get consistent with julia Base again in the sense that one can pass |
I like it. But I would be in favour of keeping |
On Sun, Oct 08, 2023 at 03:52:22PM -0700, Lars Göttgens wrote:
Do we wanna get consistent with julia Base again in the sense that one can pass `dims` to generate multiple elements at once? For more details, see https://docs.julialang.org/en/v1/stdlib/Random/#Base.rand
--
Reply to this email directly or view it on GitHub:
#2896 (comment)
You are receiving this because you are subscribed to this thread.
Message ID: ***@***.***>
I think we need to think carefully about what we want here.
RandomExtensions was partly, in the end, supported even by us, but seems to
be dead(?). Furthermore, we receive too many complains about rand and
friends not being useable by "normal" users or not giving what they
want.
Julia is cheating: mostly they are working in a world where
- the type contains "everything"
- most "domains" are finite (Float, Int)
So the complexity is in array dimensions and, maybe, esoteric
distributions.
For us, both assumptions are wrong. How do I create a random polynomial
with entries in a number field? Or worse: a matrix of those?
A random prime(ideal)? A random ideal? A random (sub)group?
I don't know a "good" answer, but looking at some of the complaints, our
current approach of adding parameters to rand following the construction
of the parent is very hard to use, understand and predict.
Any good suggestions anyone?
|
Max is going in the right direction (I read it only afterwards), but as this requires some serious work to roll out, we should think about it carefully. |
Or an interface: random_params(some parent)? To return a dict, as recursive as neccessary, with all(?) parameters set to s.th. sensible(?) Possibly, for the connosieur with the possibility to specify some sizes directly? And some random_grow(Dict from above) that increases all(?) paramters sensibly(?)? |
some notes from discussion today:
|
Some comments:
|
Some more thoughts about (pseudo-)random generators. I wish to revise my previous endorsement of Dict. I now think that a better approach is to use "iterators" I think we should avoid the Julia name "samplers" because, We can also have "iterators" which act as filters: i.e. A constructor for an iterator (random generator) may need I envisage different implementations of random-matrix-iterator For integers we need to specify a range: this could be done For rationals it becomes trickier. I believe it is important A quick comment about multi-threading: as far as I know |
I think we already have the sampler/iterator and I am afraid no one ever uses them. It is very cumbersome to write The generator/iterator thingy will probably be useful for the idea from #2896 (comment). But as I said, this is in principle already how it is implemented, since we use https://github.com/JuliaRandom/RandomExtensions.jl (but we did not implement all the things that you mentioned). |
I'm convinced about having a short form for random integers -- I would expect that random integers are much more widely used than other types of random value. I can see strong arguments in favour of "symmetric" and "non-negative" random integers; personally I'm not convinced by random integers from any other sort of range -- I know we could easily implement such functions, but that doesn't necessary mean that it is a good idea. In CoCoA when we had doubts about easy-to-implement extensions, we would often implement them, and then comment out the implementation with a note saying that it should be activated if there is sufficient demand for it (or presumably deleted if no demand appears before some deadline...). |
How useful are random rationals? Consider generating a random element in QQ[x]; what do we expect to obtain? I suggest that this is not the desired outcome, and that it is likely Here's another "contrived" example: Summary: we need to decide what we really want when we talk of random rationals! |
If it is difficult to agree on what |
(I am aware of issue #100 but I think it is about something else)
I think we need a better interface for
rand
on e.g. polynomial rings and other of our datastructures. The problem with the current system is that it is kinda hard to discover how to use it, and even if you know, it requires a lot of guesswork. At least in my experience.Here is an example of what we have right now:
So
rand
has to be called with three ranges (or vectors): the first indicates the acceptable number of terms, the second indicates the allowed range of degree of each variable, the final one is used to determine which denominators and numerators the coefficients may have (I think).The logic is clear in principle: the first two ranges control what is specific to polynomial rings; everything after controls the randomness of the coefficient ring. This can be nested then:
(In case you wonder about
405//182
: well if some terms are generated multiple times with different coefficients, they are of course merged...)While this is "logical", I still find it difficult to use, and worse: to read in code using it without trying it out to verify.
It is also somewhat limited, e.g. there is no way to e.g. "get a random polynomial where all terms have total degree 4 to 6, and coefficients in the range
-10:10
".I propose that we design and switch to a new system that relies on kwargs to become more "self documenting". Here is a first example of how it might look like in the above examples; it's just a first draft to convey the idea and get the discussion going, so I am happy to hear if others have different and better ideas :-)
I'll give these in the form of examples, without explaining much what they mean, in the hopes that they are mostly self-documenting. These lists are not meant to be exhaustive nor do I insist all variants are needed (or even sensible), I am more concerned about the "flavor" here.
For
QQ
we might offer:rand(QQ; denominator=1:10, numerator=-5:5)
rand(QQ; denominator=10, value=-5:5)
rand(QQ; nbits=1:10)
rand(QQ; nbits=1:10)
rand(QQ, 7:9)
-- for backwards compatibility??rand(QQ)
-- either returns random values wrt some documented default values, or produces an error explaining how to use it / pointing to the docsFor
GF(p)
we might offerrand(GF(p))
rand(GF(p), non_zero=true)
rand(GF(p), value=1:p-1)
For a polynomial ring, specify using
coefficients
the parameters to pass on to therand
function on the coefficient ring. I envision this is either a named tuple (passed as kwargs), or a tuple (passed as regular args) or some other value (passes as a single arg).So e.g. if the coefficient ring is
QQ
then we might offer:rand(R; total_degree=4:6, coefficients=(denominator=1:10, numerator=-5:5))
rand(R; nterms=1:3, degree=4:6, coefficients=7:9)
-- invokesrand(QQ, 7:9)
rand(R)
-- either uses some default settings, or raises a helpful errorIf the coefficient ring is another polynomial ring over
QQ
, one can recurse this, e.g.:rand(R; total_degree=4:6, coefficients=(nterms=1:3, degree=4:6, coefficients=7:9))
Similar strategies should work for matrix spaces, matrix algebras, group rings, Lie algebras, matrix groups, power series, ...
Perhaps there are structures where this is not a good fit, in that case it would be good to know about them, so please list them.
The text was updated successfully, but these errors were encountered: