Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

A simple answer is to select a random subset of the basis of your vector space and multiply them by a random subset of the vector space base ring. Core implementation :

sage: def rv(R, p): return list(map(lambda a,b:a*b
                                                    random.sample(R.basis(), p),
                                                    random.sample(R.base_ring().list(), p)))
sage: rv(v, 5)
[(0, 0, 8, 0, 0, 0, 0, 0),
 (0, 0, 0, 0, 0, 7, 0, 0),
 (0, 0, 0, 0, 2, 0, 0, 0),
 (0, 0, 0, 0, 0, 0, 9, 0),
 (0, 0, 0, 0, 0, 0, 0, 6)]

This version lacks basic checks (number oif elements <= vector space diension, finite base ring, etc...). Of course, this will sample only a small subset of your vector space. It will return quickly (the algorithm is deterministic).

If you want to sample all your vector space, you can brute-force sample p elements from it until the p elements are independent :

sage: def rv2(R, p):
....:         S = [R.random_element() for u in range(p)]
....:         while len(R.linear_dependence(S))>0 :
....:             S = [R.random_element() for u in range(p)]
....:         return S
....:     
sage: rv2(v, 3)
[(8, 5, 2, 10, 0, 8, 6, 3), (4, 8, 4, 3, 1, 5, 0, 7), (7, 3, 3, 9, 8, 4, 4, 1)]

Again, basic checks must be added. Credit where credit is due : this solution is inspired from this answer. Its based on the supposition that R.random_element() actually samples R efficiently and correctly. Note also that the algorithm is non deterministic (and therefore has no upper bound for execution time...).

HTH,

A simple answer is to select a random subset of the basis of your vector space and multiply them by a random subset of the vector space base ring. Core implementation :

sage: def rv(R, p): return list(map(lambda a,b:a*b
                                                    random.sample(R.basis(), p),
                                                    random.sample(R.base_ring().list(), p)))
sage: rv(v, 5)
[(0, 0, 8, 0, 0, 0, 0, 0),
 (0, 0, 0, 0, 0, 7, 0, 0),
 (0, 0, 0, 0, 2, 0, 0, 0),
 (0, 0, 0, 0, 0, 0, 9, 0),
 (0, 0, 0, 0, 0, 0, 0, 6)]

This version lacks basic checks (number oif elements <= vector space diension, finite base ring, etc...). Of course, this will sample only a small subset of your vector space. It will return quickly (the algorithm is deterministic).

If you want to sample all your vector space, you can brute-force sample p elements from it until the p elements are independent :

sage: def rv2(R, p):
....:         S = [R.random_element() for u in range(p)]
....:         while len(R.linear_dependence(S))>0 :
....:             S = [R.random_element() for u in range(p)]
....:         return S
....:     
sage: rv2(v, 3)
[(8, 5, 2, 10, 0, 8, 6, 3), (4, 8, 4, 3, 1, 5, 0, 7), (7, 3, 3, 9, 8, 4, 4, 1)]

Again, basic checks must be added. Credit where credit is due : this solution is inspired from this answer. Its based on the supposition that R.random_element() actually samples R efficiently and correctly. Note also that the algorithm is non deterministic (and therefore has no upper bound for execution time...).

A better answer is given here :

sage: def rv3(R, p):
....:     q=len((R1:=R.base_ring()).list())
....:     return [Matrix( SL(q, R1).random_element()).rows()[u] for u in random.sample(range(q), p)]
....: 
sage: rv3(v, 3)
[(6, 4, 4, 0, 3, 3, 10, 0, 0, 0, 10),
 (4, 7, 2, 2, 1, 6, 6, 10, 5, 4, 2),
 (0, 5, 4, 2, 5, 7, 10, 1, 0, 6, 0)]

One may finally note that the two latter solutions were among the first five answers to googling sagemath check that vectors are linearly independent...

HTH,