vector1.py

Created by asness

Created on February 27, 2019

1.16 KB

I split up vector.py into several scripts to work around the memory allocation error.


from math import acos, atan2, cos, degrees, radians, sin, sqrt, tan
# N.B. NumWorks doesn't support math.hypot

'''Revised 190226'''

'''utilities'''
def reduce(func, aList):
    '''A simple reduce function.
    Doesn't do anything fancy with empty lists.'''
    if aList:
        out = aList[0]
        for item in aList[1:]:
            out = func(out, item)
        return out
    return None

'''trig shortcuts'''

def rad(degAngle):
    return radians(degAngle)

def deg(radAngle):
    return degrees(radAngle)

def rads(degAngles):
    return [rad(d) for d in degAngles]

def degs(radAngles):
    return [deg(r) for r in radAngles]

def C(degAngle):
    return cos(rad(degAngle))

def S(degAngle):
    return sin(rad(degAngle))

def T(degAngle):
    return tan(rad(degAngle))

def AC(x):
    return deg(acos(x))

'''polar <-> cartesian conversions'''
def r(a):
    '''length'''
    return sqrt(sum([i**2 for i in a]))

def c2p(x, y):
    return [r([x, y]), deg(atan2(y, x))]

def c2ps(xs, ys):
    return [c2p(x, y) for (x, y) in zip(xs, ys)]

def p2c(r, th):
    return [r*C(th), r*S(th)]

def p2cs(rs, ths):
    return [p2c(r, th) for (r, th) in zip(rs, ths)]