Gadi A
Gadi A

Reputation: 3539

Sage: Finding linear combination of a specified basis giving a specified element?

Given a vector space V, a basis B (list of tuples each of size corresponding to the dimension of V and over the same field) and a vector v - what is the sage command to find the (unique) linear combination of the elements of B giving v?

Upvotes: 2

Views: 1786

Answers (1)

DSM
DSM

Reputation: 353379

IIUC, you probably want to do something like this:

sage: basis = [(2,3,4),(1,23/4,3), (9,8/17,11)]
sage: F = QQ
sage: F
Rational Field
sage: 
sage: # build the vector space
sage: dim = len(basis[0])
sage: VS = (F**dim).span_of_basis(basis)
sage: VS
Vector space of degree 3 and dimension 3 over Rational Field
User basis matrix:
[   2    3    4]
[   1 23/4    3]
[   9 8/17   11]

and then, having constructed the vector space over the field:

sage: # choose some random vector
sage: v = vector(F, (19/2, 5/13, -4))
sage: v
(19/2, 5/13, -4)
sage: 
sage: # get its coordinates
sage: c = VS.coordinate_vector(v)
sage: c
(-470721/19708, 59705/4927, 24718/4927)
sage: parent(c)
Vector space of dimension 3 over Rational Field
sage: 
sage: # aside: there's also .coordinates(), but that returns a list instead
sage: 
sage: # sanity check:
sage: VS.basis_matrix().transpose() * c
(19/2, 5/13, -4)
sage: VS.basis_matrix().transpose() * c == v
True

Upvotes: 3

Related Questions