# Expressing a list of vectors in terms of basis elements

Let's assume I have a list for my basis [a,b,c,d,e]=BasisList. We can think of a,b,c,d,e as standard basis vectors with a being (1,0,0,0,0) for example.

I also have another list of many vectors each with 5 entries. For example, ExampleList=[(1,2,3,4,5), (2,3,4,1,8),...]. Is there a fast way to express each vector in terms of my basis instead of mulitplying term by term and summing them up. In the example above, I want to end up with the a list

EndList=[a+2b+3c+4d+5e, 2a+3b+4c+d+8e,...] I can of course make a loop and do this summing+multiplying but it gets very long and sometimes redundant if my vector has zeros. I was thinking if there is a way to tell Sage to let a be (1,0,0,0,0), b be (0,1,0,0,0), etc and just do 1 for loop with command like "for element in ExampleList, express with basis given."

edit retag close merge delete

Sort by » oldest newest most voted

What sort of objects are the "basis elements"?

sage: var('a b c d e') # symbolic variables
(a, b, c, d, e)


or

sage: R.<a,b,c,d,e> = QQ[] # indeterminate with rational coefficients


In either case:

sage: basis = (a,b,c,d,e)
sage: ExampleList=[(1,2,3,4,5), (2,3,4,1,8), (5,4,3,0,2)]
sage: [sum(x*y for (x,y) in zip(v, basis)) for v in ExampleList]
[a + 2*b + 3*c + 4*d + 5*e, 2*a + 3*b + 4*c + d + 8*e, 5*a + 4*b + 3*c + 2*e]

more

( 2020-06-19 19:49:17 +0200 )edit

Another possibility is

sage: V = FiniteRankFreeModule(QQ, 5); V
5-dimensional vector space over the Rational Field
sage: basis = V.basis(('a', 'b', 'c', 'd', 'e'), symbol_dual=('A', 'B', 'C', 'D', 'E'))
sage: ExampleList = [V((1,2,3,4,5)), V((2,3,4,1,8)), V((5,4,3,0,2))]
sage: EndList = [v.display() for v in ExampleList]; EndList
[a + 2 b + 3 c + 4 d + 5 e, 2 a + 3 b + 4 c + d + 8 e, 5 a + 4 b + 3 c + 2 e]


Being the first basis defined on V, basis is the default basis. Its individual elements are accessed via

sage: a, b, c, d, e = basis[:]


and their components w.r.t. basis are

sage: a[:]
[1, 0, 0, 0, 0]
sage: b[:]
[0, 1, 0, 0, 0]


etc.

Similarly

sage: [v[:] for v in ExampleList]
[[1, 2, 3, 4, 5], [2, 3, 4, 1, 8], [5, 4, 3, 0, 2]]

more

( 2020-06-19 19:49:30 +0200 )edit