Ask Your Question
0

Expressing a list of vectors in terms of basis elements

asked 2020-06-18 05:21:19 +0100

whatupmatt gravatar image

updated 2020-06-18 05:22:18 +0100

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-06-18 07:17:57 +0100

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]
edit flag offensive delete link more

Comments

Great thanks for your help.

whatupmatt gravatar imagewhatupmatt ( 2020-06-19 19:49:17 +0100 )edit
1

answered 2020-06-18 19:44:07 +0100

eric_g gravatar image

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]]
edit flag offensive delete link more

Comments

Great, thanks for your help.

whatupmatt gravatar imagewhatupmatt ( 2020-06-19 19:49:30 +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

2 followers

Stats

Asked: 2020-06-18 05:21:19 +0100

Seen: 1,273 times

Last updated: Jun 18 '20