Skip to content

Instantly share code, notes, and snippets.

@vene
Created November 8, 2021 17:39
Show Gist options
  • Select an option

  • Save vene/5944abacc79bb8016ca60403f5ded4af to your computer and use it in GitHub Desktop.

Select an option

Save vene/5944abacc79bb8016ca60403f5ded4af to your computer and use it in GitHub Desktop.

Revisions

  1. vene created this gist Nov 8, 2021.
    61 changes: 61 additions & 0 deletions check_det_without_rotat.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    """ trying to understand the determinant of sphere-to-cyl """

    import numpy as np
    import jax.numpy as jnp
    from jax import jacfwd


    # map from cylinder to sphere
    def phi(zr):
    d = zr.shape[0]
    w = jnp.full(d-1, jnp.sqrt(1 - zr[-1]**2))
    w = jnp.append(w, 1)
    return w * zr


    def phi_inv(x):
    r = x[-1] # generally, dot(x, mu)
    z = x[:-1] / jnp.sqrt(1 - r**2)
    return jnp.append(z, r)


    def main():

    d = 4

    # get some point on d-sphere
    x = np.random.randn(d+1)
    x /= np.linalg.norm(x)

    # map to cylinder

    zr = phi_inv(x)

    # jacobian
    J = jacfwd(phi)(zr)

    z, r = zr[:-1], zr[-1]

    # expected end result is
    res = (1 - r ** 2) ** (d-2)
    print(res)

    # find a basis on T_z S^d-1

    P = np.eye(d) - np.outer(z, z)
    eigvals, eigvecs = np.linalg.eigh(P)
    E = eigvecs[:, np.abs(eigvals - 1) < 1e-5]
    # print(E @ E.T - P) # == 0

    # append a 1 to the bottom-right of E
    E = np.block([[E, np.zeros((4, 1))], [np.zeros((1, 3)), np.eye(1)]])


    JE = J @ E

    print(jnp.linalg.det(JE.T @ JE))


    if __name__ == '__main__':
    main()