vector1.py

Created by asness

Created on February 28, 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)]

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.