Skip to content

Commit

Permalink
Merge pull request #281 from eric-wieser/unify-mv-constructor
Browse files Browse the repository at this point in the history
Tidy up MultiVector construction in clifford.tools
  • Loading branch information
eric-wieser committed Mar 12, 2020
2 parents f330719 + c48ab8e commit a5c9431
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 55 deletions.
2 changes: 1 addition & 1 deletion clifford/tools/g3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def np_to_euc_mv(np_in):
output[1] = np_in[0]
output[2] = np_in[1]
output[3] = np_in[2]
return layout.MultiVector(value=output)
return layout.MultiVector(output)


def euc_mv_to_np(euc_point):
Expand Down
4 changes: 2 additions & 2 deletions clifford/tools/g3c/GAOnline.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def draw_objects_ganja(objects, color=int('AA000000', 16), print_scene=True):
"""
if isinstance(objects, str):
data_array, metric, basis_names, support = read_ga_file(objects)
mv_list = [layout.MultiVector(value=data_array[i, :]) for i in range(data_array.shape[0])]
mv_list = [layout.MultiVector(data_array[i, :]) for i in range(data_array.shape[0])]
sc = GanjaScene()
sc.add_objects(mv_list, color=color)
if print_scene:
Expand All @@ -260,7 +260,7 @@ def draw_objects(objects, mv_type='interp', color='rgb(0,0,0)', print_scene=True
"""
if isinstance(objects, str):
data_array, metric, basis_names, support = read_ga_file(objects)
mv_list = [layout.MultiVector(value=data_array[i, :]) for i in range(data_array.shape[0])]
mv_list = [layout.MultiVector(data_array[i, :]) for i in range(data_array.shape[0])]
sc = GAScene()
sc.add_object_array(mv_list, mv_type, color=color)
if print_scene:
Expand Down
64 changes: 32 additions & 32 deletions clifford/tools/g3c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def get_line_intersection(L3, Ldd):
Pd = 0.5*(Xdd+Xddd)
P = -(Pd*ninf*Pd)(1)/(2*(Pd|einf)**2)[0]
"""
return layout.MultiVector(value=val_get_line_intersection(L3.value, Ldd.value))
return layout.MultiVector(val_get_line_intersection(L3.value, Ldd.value))


@numba.njit
Expand All @@ -618,15 +618,15 @@ def midpoint_between_lines(L1, L2):
Gets the point that is maximally close to both lines
Hadfield and Lasenby AGACSE2018
"""
return layout.MultiVector(value=val_midpoint_between_lines(L1.value, L2.value))
return layout.MultiVector(val_midpoint_between_lines(L1.value, L2.value))


def midpoint_of_line_cluster(line_cluster):
"""
Gets a center point of a line cluster
Hadfield and Lasenby AGACSE2018
"""
return layout.MultiVector(value=val_midpoint_of_line_cluster(MVArray(line_cluster).value))
return layout.MultiVector(val_midpoint_of_line_cluster(MVArray(line_cluster).value))


@numba.njit
Expand Down Expand Up @@ -809,7 +809,7 @@ def generate_dilation_rotor(scale):
if abs(scale - 1.0) < 0.00001:
u = np.zeros(32)
u[0] = 1.0
return cf.MultiVector(layout, u)
return layout.MultiVector(u)
gamma = math.log(scale)
return math.cosh(gamma/2) + math.sinh(gamma/2)*(ninf^no)

Expand All @@ -818,7 +818,7 @@ def generate_translation_rotor(euc_vector_a):
"""
Generates a rotor that translates objects along the euclidean vector euc_vector_a
"""
return layout.MultiVector(value=val_generate_translation_rotor(euc_vector_a.value))
return layout.MultiVector(val_generate_translation_rotor(euc_vector_a.value))


@numba.njit
Expand All @@ -845,7 +845,7 @@ def meet(A, B):
The meet algorithm as described in "A Covariant Approach to Geometry"
I5*((I5*A) ^ (I5*B))
"""
return cf.MultiVector(layout, meet_val(A.value, B.value))
return layout.MultiVector(meet_val(A.value, B.value))


@numba.njit
Expand Down Expand Up @@ -878,7 +878,7 @@ def intersect_line_and_plane_to_point(line, plane):
ans = val_intersect_line_and_plane_to_point(line.value, plane.value)
if ans[0] == -1.:
return None
return layout.MultiVector(value=ans)
return layout.MultiVector(ans)


@numba.njit
Expand All @@ -897,7 +897,7 @@ def normalise_n_minus_1(mv):
"""
Normalises a conformal point so that it has an inner product of -1 with einf
"""
return layout.MultiVector(value=val_normalise_n_minus_1(mv.value))
return layout.MultiVector(val_normalise_n_minus_1(mv.value))


def quaternion_and_vector_to_rotor(quaternion, vector):
Expand Down Expand Up @@ -952,8 +952,8 @@ def point_pair_to_end_points(T):
Extracts the end points of a point pair bivector
"""
output = val_point_pair_to_end_points(T.value)
A = layout.MultiVector(value=output[0, :])
B = layout.MultiVector(value=output[1, :])
A = layout.MultiVector(output[0, :])
B = layout.MultiVector(output[1, :])
return A, B


Expand Down Expand Up @@ -1037,13 +1037,13 @@ def positive_root(sigma):
Square Root of Rotors - Evaluates the positive root
"""
res_val = positive_root_val(sigma.value)
return cf.MultiVector(layout, res_val)
return layout.MultiVector(res_val)


def negative_root(sigma):
""" Square Root of Rotors - Evaluates the negative root """
res_val = negative_root_val(sigma.value)
return cf.MultiVector(layout, res_val)
return layout.MultiVector(res_val)


@numba.njit
Expand Down Expand Up @@ -1077,7 +1077,7 @@ def general_root_val(sigma_value):
def general_root(sigma):
""" The general case of the root of a grade 0, 4 multivector """
output = general_root_val(sigma.value)
return [cf.MultiVector(layout, output[0, :].copy()), cf.MultiVector(layout, output[1, :].copy())]
return [layout.MultiVector(output[0, :].copy()), layout.MultiVector(output[1, :].copy())]


@numba.njit
Expand All @@ -1090,7 +1090,7 @@ def val_annihilate_k(K_val, C_val):

def annihilate_k(K, C):
""" Removes K from C = KX via (K[0] - K[4])*C """
return cf.MultiVector(layout, val_annihilate_k(K.value, C.value))
return layout.MultiVector(val_annihilate_k(K.value, C.value))


@numba.njit
Expand Down Expand Up @@ -1133,7 +1133,7 @@ def pos_twiddle_root(C):
Leo Dorst and Robert Valkenburg
"""
output = pos_twiddle_root_val(C.value)
return [cf.MultiVector(layout, output[0, :]), cf.MultiVector(layout, output[1, :])]
return [layout.MultiVector(output[0, :]), layout.MultiVector(output[1, :])]


def neg_twiddle_root(C):
Expand All @@ -1144,7 +1144,7 @@ def neg_twiddle_root(C):
Leo Dorst and Robert Valkenburg
"""
output = neg_twiddle_root_val(C.value)
return [cf.MultiVector(layout, output[0, :]), cf.MultiVector(layout, output[1, :])]
return [layout.MultiVector(output[0, :]), layout.MultiVector(output[1, :])]


def square_roots_of_rotor(R):
Expand Down Expand Up @@ -1275,7 +1275,7 @@ def motor_between_rounds(X1, X2):
Calculate the motor between any pair of rounds of the same grade
Line up the carriers, then line up the centers
"""
return layout.MultiVector(value=val_motor_between_rounds(X1.value, X2.value))
return layout.MultiVector(val_motor_between_rounds(X1.value, X2.value))


@numba.njit
Expand Down Expand Up @@ -1333,7 +1333,7 @@ def motor_between_objects(X1, X2):
"""
Calculates a motor that takes X1 to X2
"""
return layout.MultiVector(value=val_motor_between_objects(X1.value, X2.value))
return layout.MultiVector(val_motor_between_objects(X1.value, X2.value))


def calculate_S_over_mu(X1, X2):
Expand Down Expand Up @@ -1492,7 +1492,7 @@ def val_normalised(mv_val):

def normalised(mv):
""" fast version of the normal() function """
return cf.MultiVector(layout, val_normalised(mv.value))
return layout.MultiVector(val_normalised(mv.value))


@numba.njit
Expand All @@ -1517,12 +1517,12 @@ def val_rotor_between_lines(L1_val, L2_val):

def rotor_between_lines(L1, L2):
""" return the rotor between two lines """
return cf.MultiVector(layout, val_rotor_between_lines(L1.value, L2.value))
return layout.MultiVector(val_rotor_between_lines(L1.value, L2.value))


def rotor_between_planes(P1, P2):
""" return the rotor between two planes """
return cf.MultiVector(layout, val_rotor_rotor_between_planes(P1.value, P2.value))
return layout.MultiVector(val_rotor_rotor_between_planes(P1.value, P2.value))


@numba.njit
Expand Down Expand Up @@ -1614,7 +1614,7 @@ def random_circle():
mv_a = val_random_euc_mv()
mv_b = val_random_euc_mv()
mv_c = val_random_euc_mv()
return layout.MultiVector(value=val_normalised(omt_func(omt_func(val_up(mv_a), val_up(mv_b)), val_up(mv_c))))
return layout.MultiVector(val_normalised(omt_func(omt_func(val_up(mv_a), val_up(mv_b)), val_up(mv_c))))


def random_sphere_at_origin():
Expand Down Expand Up @@ -1664,7 +1664,7 @@ def val_apply_rotor(mv_val, rotor_val):

def apply_rotor(mv_in, rotor):
""" Applies rotor to multivector in a fast way """
return cf.MultiVector(layout, val_apply_rotor(mv_in.value, rotor.value))
return layout.MultiVector(val_apply_rotor(mv_in.value, rotor.value))


@numba.njit
Expand All @@ -1675,7 +1675,7 @@ def val_apply_rotor_inv(mv_val, rotor_val, rotor_val_inv):

def apply_rotor_inv(mv_in, rotor, rotor_inv):
""" Applies rotor to multivector in a fast way takes pre computed adjoint"""
return cf.MultiVector(layout, val_apply_rotor_inv(mv_in.value, rotor.value, rotor_inv.value))
return layout.MultiVector(val_apply_rotor_inv(mv_in.value, rotor.value, rotor_inv.value))


@numba.njit
Expand All @@ -1698,14 +1698,14 @@ def val_convert_2D_polar_line_to_conformal_line(rho, theta):
p1_val = val_convert_2D_point_to_conformal(x1, y1)
p2_val = val_convert_2D_point_to_conformal(x2, y2)
line_val = omt_func(omt_func(p1_val, p2_val), ninf_val)
line_val = line_val/abs(cf.MultiVector(layout, line_val))
line_val = line_val/abs(layout.MultiVector(line_val))
return line_val


def convert_2D_polar_line_to_conformal_line(rho, theta):
""" Converts a 2D polar line to a conformal line """
line_val = val_convert_2D_polar_line_to_conformal_line(rho, theta)
return cf.MultiVector(layout, line_val)
return layout.MultiVector(line_val)


@numba.njit
Expand All @@ -1718,7 +1718,7 @@ def val_up(mv_val):

def fast_up(mv):
""" Fast up mapping """
return cf.MultiVector(layout, val_up(mv.value))
return layout.MultiVector(val_up(mv.value))


@numba.njit
Expand All @@ -1743,14 +1743,14 @@ def val_down(mv_val):

def fast_down(mv):
""" A fast version of down() """
return cf.MultiVector(layout, val_down(mv.value))
return layout.MultiVector(val_down(mv.value))


def val_distance_point_to_line(point, line):
"""
Returns the euclidean distance between a point and a line
"""
return float(abs(cf.MultiVector(layout, omt_func(point, line))))
return float(abs(layout.MultiVector(omt_func(point, line))))


@numba.njit
Expand All @@ -1764,7 +1764,7 @@ def val_convert_2D_point_to_conformal(x, y):

def convert_2D_point_to_conformal(x, y):
""" Convert a 2D point to conformal """
return cf.MultiVector(layout, val_convert_2D_point_to_conformal(x, y))
return layout.MultiVector(val_convert_2D_point_to_conformal(x, y))


def distance_polar_line_to_euc_point_2d(rho, theta, x, y):
Expand All @@ -1789,7 +1789,7 @@ def fast_dual(a):
"""
Fast dual
"""
return cf.MultiVector(layout, dual_func(a.value))
return layout.MultiVector(dual_func(a.value))


class ConformalMVArray(cf.MVArray):
Expand Down Expand Up @@ -1854,7 +1854,7 @@ def from_value_array(value_array):


v_dual = np.vectorize(fast_dual, otypes=[ConformalMVArray])
v_new_mv = np.vectorize(lambda v: cf.MultiVector(layout, v), otypes=[ConformalMVArray], signature='(n)->()')
v_new_mv = np.vectorize(lambda v: layout.MultiVector(v), otypes=[ConformalMVArray], signature='(n)->()')
v_up = np.vectorize(fast_up, otypes=[ConformalMVArray])
v_down = np.vectorize(fast_down, otypes=[ConformalMVArray])
v_apply_rotor_inv = np.vectorize(apply_rotor_inv, otypes=[ConformalMVArray])
Expand Down
8 changes: 4 additions & 4 deletions clifford/tools/g3c/cost_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def midpoint_and_error_of_line_cluster_eig(line_cluster):

point_val = np.zeros(32)
point_val[1:6] = np.matmul(mat2solve, start)
new_mv = layout.MultiVector(value=point_val)
new_mv = layout.MultiVector(point_val)
new_mv = normalise_n_minus_1((new_mv * einf * new_mv)(1))
return new_mv, val_point_to_line_cluster_distance(new_mv.value, line_cluster_array)

Expand All @@ -84,7 +84,7 @@ def midpoint_and_error_of_line_cluster_svd(line_cluster):

point_val = np.zeros(32)
point_val[np.array(layout.gradeList) == grade_val] = v[:, 1]
new_mv = layout.MultiVector(value=point_val)
new_mv = layout.MultiVector(point_val)
# new_mv = normalise_n_minus_1(new_mv * einf * new_mv)
new_point = normalise_n_minus_1(new_mv) # up(down(new_mv) / 2)
return new_point, val_point_to_line_cluster_distance(new_point.value, line_cluster_array)
Expand All @@ -98,7 +98,7 @@ def midpoint_and_error_of_line_cluster(line_cluster):
"""
line_cluster_array = np.array([l.value for l in line_cluster])
cp_val = val_midpoint_of_line_cluster(line_cluster_array)
return layout.MultiVector(value=cp_val), val_point_to_line_cluster_distance(cp_val, line_cluster_array)
return layout.MultiVector(cp_val), val_point_to_line_cluster_distance(cp_val, line_cluster_array)


def midpoint_and_error_of_line_cluster_grad(line_cluster):
Expand All @@ -109,7 +109,7 @@ def midpoint_and_error_of_line_cluster_grad(line_cluster):
"""
line_cluster_array = np.array([l.value for l in line_cluster])
cp_val = val_midpoint_of_line_cluster_grad(line_cluster_array)
return layout.MultiVector(value=cp_val), val_point_to_line_cluster_distance(cp_val, line_cluster_array)
return layout.MultiVector(cp_val), val_point_to_line_cluster_distance(cp_val, line_cluster_array)


def line_plane_cost(line, plane):
Expand Down
8 changes: 4 additions & 4 deletions clifford/tools/g3c/object_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def fit_circle(point_list):
"""
Performs Leo Dorsts circle fitting technique
"""
return layout.MultiVector(value=val_fit_circle(np.array([p.value for p in point_list])))
return layout.MultiVector(val_fit_circle(np.array([p.value for p in point_list])))


@numba.njit
Expand Down Expand Up @@ -85,7 +85,7 @@ def fit_line(point_list):
"""
Does line fitting with combo J.Lasenbys method and L. Dorsts
"""
return layout.MultiVector(value=val_fit_line(np.array([p.value for p in point_list])))
return layout.MultiVector(val_fit_line(np.array([p.value for p in point_list])))


@numba.njit
Expand Down Expand Up @@ -124,7 +124,7 @@ def fit_sphere(point_list):
"""
Performs Leo Dorsts sphere fitting technique
"""
return layout.MultiVector(value=val_fit_sphere(np.array([p.value for p in point_list])))
return layout.MultiVector(val_fit_sphere(np.array([p.value for p in point_list])))


@numba.njit
Expand Down Expand Up @@ -158,4 +158,4 @@ def fit_plane(point_list):
"""
Does plane fitting with combo J.Lasenbys method and L. Dorsts
"""
return layout.MultiVector(value=val_fit_plane(np.array([p.value for p in point_list])))
return layout.MultiVector(val_fit_plane(np.array([p.value for p in point_list])))

0 comments on commit a5c9431

Please sign in to comment.