Ask Your Question
0

How can i get n linearly independent vector ?

asked 2023-03-28 19:51:53 +0100

aliosman9522 gravatar image

updated 2023-05-19 21:49:13 +0100

FrédéricC gravatar image

how can i get (n) linearly independent vector for a given space ? for example ; v=VectorSpace(GF(11),8) can i get 6 "RANDOM" linearly independent vector ?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2023-03-28 21:43:01 +0100

Emmanuel Charpentier gravatar image

updated 2023-03-28 22:02:39 +0100

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,

edit flag offensive delete link more

Comments

First of all, thank you very much for the answer, I have one more question.

#i have a list of vectors and I want to run gs method on these vectors
    V=[vector(GF(11),[10,2,4,5,10,6,7,6]),vector(GF(11),[ 1,3,6,4,3,6,7,5])]
    m=Matrix(V)
    G,M=m.gram_schmidt()
    # AttributeError: 'sage.rings.finite_rings.integer_mod.IntegerMod_int' object has no attribute 'conjugate'
    # how can i do this ?
aliosman9522 gravatar imagealiosman9522 ( 2023-03-29 12:00:11 +0100 )edit

1) If you have "one more question", ask a new question, for future (per-)users of ask.sagemath.org's sake ! Your second question doesn't appear on the list of similar questions already answered (on the right of the question list), and this cannot be found by future users.

2) The first result of googling "sagemath gram_schmidt" leads you here, whose first entry should solve your problem :

sage: V=[vector(GF(11),[10,2,4,5,10,6,7,6]),vector(GF(11),[ 1,3,6,4,3,6,7,5])]
sage: from sage.modules.misc import gram_schmidt
sage: G, M = gram_schmidt(V)
sage: G
[(10, 2, 4, 5, 10, 6, 7, 6), (7, 2, 4, 7, 9, 3, 9, 2)]
sage: M
[0 0]
[6 0]

Morality : RTFM...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-03-29 19:55:25 +0100 )edit

Thank for all

aliosman9522 gravatar imagealiosman9522 ( 2023-04-04 15:22:40 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2023-03-28 19:51:53 +0100

Seen: 294 times

Last updated: Mar 28 '23