from __future__ import division

import math

class vector(tuple):
    for name, operator in [("neg", "-%s"), ("pos", "+%s"), ("abs", "abs(%s)"), ("invert", "~%s")]:
        exec("def __%s__(self): return vector(%s for x in self)" % (name, operator % "x"))
    for name, operator in [
        ("add", "%s+%s"), ("sub", "%s-%s"), ("mul", "%s*%s"), ("truediv", "%s/%s"), ("floordiv", "%s//%s"),
        ("mod", "%s%%%s"), ("pow", "%s**%s"), ("lshift", "%s<<%s"), ("rshift", "%s>>%s"), ("and", "%s&%s"),
        ("xor", "%s^%s"), ("or", "%s|%s"), ("call", "%s(%s)"),
    ]:
        exec("""def __%s__(self, other):
            try:
                return vector(%s for x, y in zip(self, other))
            except:
                return vector(%s for x in self)""" % (name, operator % ("x", "y"), operator % ("x", "other")))
        exec("""def __r%s__(self, other):
            try:
                return vector(%s for x, y in zip(self, other))
            except:
                return vector(%s for x in self)""" % (name, operator % ("y", "x"), operator % ("other", "x")))
    def __repr__(self):
        return 'v%s'%tuple.__repr__(self)
    def sum(self):
        return sum(self)
    def magnitude(self):
        return (self**2).sum()**.5
    def unit(self):
        if self.magnitude() == 0:
            return self*0
        return self/self.magnitude()
    def spd(self): # square preserve direction
        return self.unit()*(self.magnitude()**2)

def v(*args):
    if len(args) > 1:
        return vector(args)
    else:
        return vector(args[0])
