clifford icon indicating copy to clipboard operation
clifford copied to clipboard

Ensure full support for Clifford algebras over complex numbers

Open hugohadfield opened this issue 3 years ago • 0 comments

Currently the package has accidental support for complex clifford algebras but this has not been an explicit design goal. Having read https://blogs.mathworks.com/cleve/2013/10/14/complex-step-differentiation/ thanks to a prompting by @tBuLi it seems to me that we might be able to get reasonable autodiff for many algorithms just using the built in complex number support of numpy (and numba). For example:

import numpy as np
import time
from clifford.g3c import *
from clifford.tools.g3c import fast_up
import numba

@numba.njit
def diff_up(x, dxda):
    """
    The derivative of the up function in clifford
    """
    return (dxda|x).value[0]*einf + dxda

def test_diff_up():
    # Make some data
    a = np.random.randn()
    b = np.random.randn()
    c = np.random.randn()

    # Set up a point
    x = a*e1 + b*e2 + c*e3

    # Calculate a the exact derivative
    dxda = e1
    exactdiffup = diff_up(x, dxda)
    print(exactdiffup)

    # Do the complex step derivative
    h = 1E-8
    diff_in = x + 1j*h*e1
    res = fast_up(diff_in)
    comp_step_up = layout.MultiVector(value=res.value.imag/h)
    print(comp_step_up)

    print(np.sum((exactdiffup.value - comp_step_up.value)**2))

    start_time = time.time()
    for i in range(10000):
        diff_up(x, dxda)
    end_time = time.time()
    print(end_time - start_time)

    start_time = time.time()
    for i in range(10000):
        fast_up(diff_in)
    end_time = time.time()
    print(end_time - start_time)

if __name__ == '__main__':
    test_diff_up()

hugohadfield avatar Sep 10 '20 16:09 hugohadfield