Skip to content

Instantly share code, notes, and snippets.

@smathot
Last active February 22, 2016 11:24

Revisions

  1. smathot revised this gist Feb 22, 2016. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions xytouv.py
    Original file line number Diff line number Diff line change
    @@ -22,17 +22,19 @@ def xytouv(x, y, Bu=1.4, A=3., Bv=1.8):
    u = Bu*np.log(
    np.sqrt(R**2 + A**2 + 2*A*R*np.cos(phi))) \
    - Bu*np.log(A)
    v = Bv*np.arctan(
    (R*np.sin(phi)) \
    / (R*np.cos(phi)+A)
    v = Bv*np.arctan2(
    (R*np.sin(phi)),
    R*np.cos(phi)+A
    )
    return u, v

    def unit_test():

    x1 = np.random.randint(0, 10, 1000)
    y1 = np.random.randint(-10, 10, 1000)
    x1 = np.random.random(1000)*20-10
    y1 = np.random.random(1000)*20-10
    u, v = xytouv(x1, y1)
    x2, y2 = uvtoxy(u, v)
    assert(np.sum(np.abs(x1-x2)) < .0001)
    assert(np.sum(np.abs(y1-y2)) < .0001)

    unit_test()
  2. smathot created this gist Feb 22, 2016.
    38 changes: 38 additions & 0 deletions xytouv.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    import numpy as np

    def uvtoxy(u, v, Bu=1.4, A=3., Bv=1.8):

    u = np.array(u, dtype=float)
    v = np.array(v, dtype=float)
    R = A * np.sqrt(np.exp(2*u/ Bu) - (2*np.exp(u/Bu)*np.cos(v/Bv)) + 1)
    phi = np.arctan2(
    (np.exp(u/Bu)*np.sin(v/Bv)),
    ((np.exp(u/Bu)*np.cos(v/Bv))-1)
    )
    x = R*np.cos(phi)
    y = R*np.sin(phi)
    return x, y

    def xytouv(x, y, Bu=1.4, A=3., Bv=1.8):

    x = np.array(x, dtype=float)
    y = np.array(y, dtype=float)
    R = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    u = Bu*np.log(
    np.sqrt(R**2 + A**2 + 2*A*R*np.cos(phi))) \
    - Bu*np.log(A)
    v = Bv*np.arctan(
    (R*np.sin(phi)) \
    / (R*np.cos(phi)+A)
    )
    return u, v

    def unit_test():

    x1 = np.random.randint(0, 10, 1000)
    y1 = np.random.randint(-10, 10, 1000)
    u, v = xytouv(x1, y1)
    x2, y2 = uvtoxy(u, v)
    assert(np.sum(np.abs(x1-x2)) < .0001)
    assert(np.sum(np.abs(y1-y2)) < .0001)