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

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

Sort by ยป oldest newest most voted

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)

more