@hpaulj让我清楚地看到了更多的功能和机会,从而帮助我进一步探索这个问题。这有助于我更清楚地界定我的竞争目标,也有助于我开始将优先权赋予它们。
-
-
-
速度慢5%,但各方面都好,这是可以接受的
-
100%的速度慢可能是不可接受的
-
实现应该尽可能不依赖于数据类型
这让我想到
目前
def scale_unit_cube_to_unit_sphere(*values):
"""
Scales all the inputs (on a row basis for array_line types) such that when
treated as n-dimensional vectors, their scale is always 1.
(Divides the vector represented by each row of inputs by that row's
root-of-sum-of-squares, so as to normalise to a unit magnitude.)
Examples - Scalar Inputs
--------
>>> scale_unit_cube_to_unit_sphere(1, 1, 1)
[0.5773502691896258, 0.5773502691896258, 0.5773502691896258]
Examples - Array Like Inputs
--------
>>> x = [ 1, 2, 3]
>>> y = [ 1, 4, 3]
>>> z = [ 1,-3,-1]
>>> scale_unit_cube_to_unit_sphere(x, y, z)
[array([0.57735027, 0.37139068, 0.6882472 ]),
array([0.57735027, 0.74278135, 0.6882472 ]),
array([ 0.57735027, -0.55708601, -0.22941573])]
>>> a = np.array([x, y, z])
>>> scale_unit_cube_to_unit_sphere(*a)
[array([0.57735027, 0.37139068, 0.6882472 ]),
array([0.57735027, 0.74278135, 0.6882472 ]),
array([ 0.57735027, -0.55708601, -0.22941573])]
scale_unit_cube_to_unit_sphere(*t)
>>> t = (x, y, z)
>>> scale_unit_cube_to_unit_sphere(*t)
[array([0.57735027, 0.37139068, 0.6882472 ]),
array([0.57735027, 0.74278135, 0.6882472 ]),
array([ 0.57735027, -0.55708601, -0.22941573])]
>>> df = pd.DataFrame(data={'x':x,'y':y,'z':z})
>>> scale_unit_cube_to_unit_sphere(df['x'], df['y'], df['z'])
[0 0.577350
1 0.371391
2 0.688247
dtype: float64,
0 0.577350
1 0.742781
2 0.688247
dtype: float64,
0 0.577350
1 -0.557086
2 -0.229416
dtype: float64]
For all array_like inputs, the results can then be utilised in similar
ways, such as writing them to an existing DataFrame as follows:
>>> transform = scale_unit_cube_to_unit_sphere(df['x'], df['y'], df['z'])
>> df['i'], df['j'], df['k'] = transform
"""
# Scale the position in space to be a unit vector, as on the surface of a sphere
################################################################################
scaler = np.sqrt(sum([np.multiply(v, v) for v in values]))
return [np.divide(v, scaler) for v in values]
根据doc字符串,这适用于标量、数组、序列等,无论是否提供一个标量、三个标量、n个标量、n个数组等。
它们也在“链”中工作,例如下面的示例(函数的实现不相关,只是将输入链接到输出的模式)。。。
cube, ix = generate_index_cube(vertices_per_edge)
df = pd.DataFrame(
data = {
'x': cube[0],
'y': cube[1],
'z': cube[2],
},
index = ix,
)
unit = scale_index_to_unit(vertices_per_edge, *cube)
distortion = scale_unit_to_distortion(distortion_factor, *unit)
df['a'], df['b'], df['c'] = distortion
sphere = scale_unit_cube_to_unit_sphere(*distortion)
df['i'], df['j'], df['k'] = sphere
recovered_distortion = scale_unit_sphere_to_unit_cube(*sphere)
df['a_'], df['b_'], df['c_'] = recovered_distortion
recovered_cube = scale_unit_to_index(
vertices_per_edge,
*scale_distortion_to_unit(
distortion_factor,
*recovered_distortion,
),
)
df['x_'], df['y_'], df['z_'] = recovered_cube
print(len(df[np.logical_not(np.isclose(df['a'], df['a_']))])) # No Differences
print(len(df[np.logical_not(np.isclose(df['b'], df['b_']))])) # No Differences
print(len(df[np.logical_not(np.isclose(df['c'], df['c_']))])) # No Differences
print(len(df[np.logical_not(np.isclose(df['x'], df['x_']))])) # No Differences
print(len(df[np.logical_not(np.isclose(df['y'], df['y_']))])) # No Differences
print(len(df[np.logical_not(np.isclose(df['z'], df['z_']))])) # No Differences
请做评论或评论。