def test_eval_final_poly_circles(self):
        from scene_objects import CircleSurface
        X0 = random_circle()
        X1 = random_circle()
        S = average_objects([X1, X0], [0.2, (1 - 0.2)])
        L = ((S * einf * S) ^ up(0.0) ^ einf).normal()

        Xdash = MultiVectorPolynomial(cf.MVArray([X0, X1 - X0]))
        full_poly = gen_full_poly_circles(Xdash, L)
        print('full_poly ', full_poly)
        print('', flush=True)
        final_scalar_poly = gen_full_scalar_poly_circles(Xdash, L)
        jitted_scalar_poly = jitted_gen_full_scalar_poly_circles(
            cf.MVArray([X0, X1 - X0]), L)

        from pyganja import GanjaScene, Color
        gs = GanjaScene()
        gs.add_objects([X0, X1], color=Color.BLACK)
        gs.add_objects([S], color=Color.RED)
        gs.add_objects([L])
        draw(gs, scale=0.1)

        r = potential_roots_circles(X0, X1, L)

        surf = CircleSurface(X0, X1, nprobe_alphas=2000)
        print('probe method: ', surf.intersection_func(L.value))
        print('\n\n\n')
        print('polynomial method: ', r)
        print(len(surf.probe_alphas), flush=True)
        time.sleep(0.5)

        # for alpha in surf.probe_alphas:
        #     print(full_poly(alpha))

        # test the value at each
        import matplotlib.pyplot as plt
        plt.figure()
        plt.plot(surf.probe_alphas, surf.probe_func(L.value))
        plt.plot(surf.probe_alphas,
                 [final_scalar_poly(alpha) for alpha in surf.probe_alphas],
                 'r')
        plt.plot(surf.probe_alphas,
                 [jitted_scalar_poly(alpha) for alpha in surf.probe_alphas],
                 'g')
        plt.plot(surf.probe_alphas, surf.probe_alphas * 0, 'k')
        plt.legend(["true meet", "polynomial function", "zero"])
        plt.savefig('circle_root_func.png')
        plt.show()
 def test_MultiVectorPolynomial_grade(self):
     pol = MultiVectorPolynomial(cf.MVArray([e1, e12, 1.0 * e123]))
     print(pol)
     print(pol.grade(0))
     print(pol.grade(1))
     print(pol.grade(2))
     print(pol.grade(3))
    def test_jitted_gen_full_scalar_poly_point_pairs(self):

        for i in range(50):
            X0 = random_point_pair()
            X1 = random_point_pair()
            S = average_objects([X1, X0], [0.2, (1 - 0.2)])
            L = ((S * einf * S) ^ up(0.0) ^ einf).normal()

            coef_array = cf.MVArray([X1, X0 - X1])
            polyXdash = MultiVectorPolynomial(coef_array)
            final_poly = gen_full_scalar_poly_point_pairs(polyXdash, L)

            final_poly_jit = jitted_gen_full_scalar_poly_point_pairs(
                coef_array, L)

            #             print('\n\n')
            #             print(final_poly.coef)
            #             print(final_poly_jit.coef)
            #             print('\n\n', flush=True)
            np.testing.assert_allclose(final_poly.coef, final_poly_jit.coef)

        nrepeats = 100

        start_time = time.time()
        for i in range(nrepeats):
            gen_full_scalar_poly_point_pairs(polyXdash, L)
        end_time = time.time()
        print('ms per eval: ', 1000 * (end_time - start_time) / nrepeats)

        start_time = time.time()
        for i in range(nrepeats):
            jitted_gen_full_scalar_poly_point_pairs(coef_array, L)
        end_time = time.time()
        print('ms per eval: ', 1000 * (end_time - start_time) / nrepeats)
 def test_MultiVectorPolynomial_reversion(self):
     X0 = random_point_pair()
     X1 = random_point_pair()
     coef_array = cf.MVArray([X1, X0 - X1])
     poly0 = MultiVectorPolynomial(coef_array)
     print((poly0))
     print((~poly0))
 def test_MultiVectorPolynomial_scalar_poly(self):
     pol = MultiVectorPolynomial(
         cf.MVArray([-1.0 + 0 * e1, 0 * e1, 2.0 + 0 * e1 + e13]))
     print(pol)
     print(pol.grade(0))
     print(pol.scalar_poly)
     print(pol.scalar_roots())
Esempio n. 6
0
def mvconv(a: cf.MVArray, b: cf.MVArray) -> cf.MVArray:
    """
    Performs the 1D discrete convolution of two 1D sequences of
    multivectors a and b
    returns y of shape (a.shape[0] + b.shape[0] - 1, 32)
    """
    valconv = _val_mvconv(a.value, b.value)
    return cf.MVArray(
        [layout.MultiVector(valconv[i, :]) for i in range(valconv.shape[0])])
 def test_MultiVectorPolynomial_eval(self):
     X0 = random_point_pair()
     X1 = random_point_pair()
     coef_array = cf.MVArray([X1, X0 - X1])
     poly0 = MultiVectorPolynomial(coef_array)
     print(X1)
     print(poly0(0))
     print('\n')
     print(X0)
     print(poly0(1.0))
    def test_MultiVectorPolynomial_mult(self):
        X0 = random_point_pair()
        X1 = random_point_pair()

        coef_array = cf.MVArray([X1, X0 - X1])

        poly0 = MultiVectorPolynomial(coef_array)

        print((X0 - X1) * (X0 - X1), (X0 - X1) * X1 + X1 * (X0 - X1), X1 * X1)
        poly2 = poly0 * poly0
        print(poly2.coef[2], poly2.coef[1], poly2.coef[0])
    def test_poly_norm_sigma_sqrd(self):
        X0 = random_point_pair()
        X1 = random_point_pair()
        coef_array = cf.MVArray([X1, X0 - X1])
        polyXdash = MultiVectorPolynomial(coef_array)

        alpha = 0.2
        Xdash = alpha * X0 + (1 - alpha) * X1
        sigma = -Xdash * ~Xdash

        print(poly_norm_sigma_sqrd(poly_sigma(polyXdash))(alpha))
        print((sigma[0]**2 - (sigma(4)**2))[0])
    def test_eval_final_poly_point_pairs(self):
        from scene_objects import PointPairSurface
        for i in range(10):
            X0 = random_point_pair()
            X1 = random_point_pair()
            coef_array = cf.MVArray([X0, X1 - X0])
            polyXdash = MultiVectorPolynomial(coef_array)

            S = average_objects([X1, X0], [0.2, (1 - 0.2)])
            L = ((S * einf * S) ^ up(0.0) ^ einf).normal()
            final_poly = gen_full_scalar_poly_point_pairs(polyXdash, L)

            # Pick an alpha
            alpha = 0.4
            polyoutput = final_poly(alpha)

            Xdash = alpha * X1 + (1 - alpha) * X0
            sigma = -Xdash * ~Xdash
            left_res = (I5 * (L ^ ((sigma(0) - sigma(4)) * Xdash)))**2
            right_res = (I5 * (L ^ (np.sqrt(
                (sigma[0]**2 - (sigma(4)**2))[0]) * Xdash)))**2
            manual_output = left_res - right_res

            np.testing.assert_allclose(polyoutput, manual_output.value[0],
                                       1E-3, 1E-6)


#         from pyganja import GanjaScene, Color
#         gs = GanjaScene()
#         gs.add_objects([X0, X1], color=Color.BLACK)
#         gs.add_objects([S], color=Color.RED)
#         gs.add_objects([L])
#         draw(gs, scale=0.1, browser_window=True)

        print(final_poly.degree())
        r = potential_roots_point_pairs(X0, X1, L)

        surf = PointPairSurface(X0, X1)
        print(surf.intersection_func(L.value))
        print(r)

        # test the value at each
        import matplotlib.pyplot as plt
        plt.plot(surf.probe_alphas, surf.probe_func(L.value))
        plt.plot(surf.probe_alphas,
                 [final_poly(alpha) for alpha in surf.probe_alphas], 'r')
        plt.plot(surf.probe_alphas, surf.probe_alphas * 0, 'k')
        plt.legend(["true meet", "polynomial function", "zero"])
        plt.savefig('point_pair_root_func.png')
Esempio n. 11
0
def potential_roots_circles(X0: cf.MultiVector, X1: cf.MultiVector,
                            L: cf.MultiVector) -> np.ndarray:
    """
    Interpolated circles form of polynomial of order 12
    """
    Xdash = MultiVectorPolynomial(cf.MVArray([X0, X1 - X0]))
    final_poly = gen_full_scalar_poly_circles(Xdash, L)
    # Solve the equation
    root_list = final_poly.roots()
    # Take only the real roots between 0 and 1
    real_valued = root_list.real[abs(root_list.imag) < 1e-5]
    potential_roots = real_valued[(real_valued >= 0) & (real_valued <= 1)]
    filtered_roots = val_filter_roots_circles(potential_roots, X0.value,
                                              X1.value, L.value)
    return filtered_roots
Esempio n. 12
0
def potential_roots_point_pairs(X0: cf.MultiVector, X1: cf.MultiVector,
                                L: cf.MultiVector) -> np.ndarray:
    """
    Point pairs have a maximum of 6 roots
    """

    # Set up the polynomial
    coef_array = cf.MVArray([X0, X1 - X0])
    polyXdash = MultiVectorPolynomial(coef_array)
    final_poly = gen_full_scalar_poly_point_pairs(polyXdash, L)

    # Solve the equation
    root_list = final_poly.roots()

    # Take only the real roots between 0 and 1
    real_valued = root_list.real[abs(root_list.imag) < 1e-5]
    potential_roots = real_valued[(real_valued >= 0) & (real_valued <= 1)]
    filtered_roots = val_filter_roots_point_pairs(potential_roots, X0.value,
                                                  X1.value, L.value)
    return filtered_roots
    def test_mvconv(self):
        X0 = random_point_pair()
        X1 = random_point_pair()

        coef_array = cf.MVArray([X1, X0 - X1])

        poly0 = MultiVectorPolynomial(coef_array)

        print((X0 - X1) * (X0 - X1), (X0 - X1) * X1 + X1 * (X0 - X1), X1 * X1)
        poly2 = poly0 * poly0
        print(poly2.coef[2], poly2.coef[1], poly2.coef[0])

        poly3 = mvconv(coef_array, coef_array)
        print(poly3[2], poly3[1], poly3[0])

        import time

        nrepeats = 10000

        start_time = time.time()
        for i in range(nrepeats):
            poly0 * poly0
        end_time = time.time()
        print('ms per eval: ', 1000 * (end_time - start_time) / nrepeats)

        start_time = time.time()
        for i in range(nrepeats):
            poly0.conv(poly0)
        end_time = time.time()
        print('ms per eval: ', 1000 * (end_time - start_time) / nrepeats)

        start_time = time.time()
        for i in range(nrepeats):
            mvconv(coef_array, coef_array)
        end_time = time.time()
        print('ms per eval: ', 1000 * (end_time - start_time) / nrepeats)
Esempio n. 14
0
 def __array__(self) -> 'cf.MVArray':
     # we are a scalar, and the only appropriate dtype is an object array
     return cf.MVArray([self])
Esempio n. 15
0
def poly_meet_L(p: MultiVectorPolynomial,
                L: cf.MultiVector) -> MultiVectorPolynomial:
    return MultiVectorPolynomial(I5 * ((I5 * L) ^ (I5 * cf.MVArray(p.coef))))
Esempio n. 16
0
 def __invert__(self):
     """ Reversion overload """
     return MultiVectorPolynomial(~cf.MVArray(self.coef))
Esempio n. 17
0
 def scalar_poly(self):
     """ Return a polynomial in the scalar part of the multivectors only """
     return npp.Polynomial(cf.MVArray(self.coef).value[:, 0])
Esempio n. 18
0
 def grade(self, other):
     """ Return a new polynomial with mv coefficients of the desired grade only """
     return MultiVectorPolynomial(cf.MVArray([c(other) for c in self.coef]))