Ask Your Question
0

How do I create and plot a vector from the difference between two points?

asked 2015-03-07 22:43:41 +0100

nardaen gravatar image

updated 2015-03-07 22:57:24 +0100

Having points $a$ and $b$, how do I create a vector that goes from $a$ to $b$? I'm trying the following but I get an error:

a=[(1,0,0)]
b=[(0,-1,0)]
u=vector(b-a)

Then, how do I plot $a$, $b$ and $u$ at the position of $a$? The error is

File "/tmp/tmpoTA_jw/___code___.py", line 6, in <module> exec compile(u'u=vector(b-a) File "", line 1, in <module> TypeError: unsupported operand type(s) for -: 'list' and 'list'

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2015-03-07 22:57:46 +0100

tmonteil gravatar image

When you write a=[(1,0,0)], a becomes the name of a list that contains a single element, which is the tuple (1,0,0), list and tuples are Python objects.

Note that tuples can be added, but since most Python programmers are not mathematicians, the most useful way to define addition is by concatenation:

sage: (1,0,0) + (3,4)
(1, 0, 0, 3, 4)
sage: (1,0,0) + ('a','b')
(1, 0, 0, 'a', 'b')

For tuples and lists, subtraction is not defined:

sage: (1,0,0) - (3,4,4)
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'

So, it s better to define points in a vector space as vectors themselves, and the vector joining the two points is just their difference:

sage: a = vector((1,0,0))
sage: b = vector((0,-1,0))
sage: u = b-a
sage: u
(-1, -1, 0)
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-03-07 22:43:41 +0100

Seen: 2,065 times

Last updated: Mar 07 '15