<jupyter_start><jupyter_text>Automatic Multivector Differentiation<jupyter_code>results = []
num_repetitions = 2
results.append(benchmark_results('Adept', 'Forward', 'Matrix', (3,1), 'BM_AdeptRotorMatrixJacobianForward', 
                                 num_repetitions,
                                 ad.diff_adept_rotor_matrix_forward(0.5, vsr.Vec(1,0,0))[-1]))
results.append(benchmark_results('Adept', 'Reverse', 'Matrix',(3,1), 'BM_AdeptRotorMatrixJacobianReverse', 
                                 num_repetitions,
                                 ad.diff_adept_rotor_matrix_reverse(0.5, vsr.Vec(1,0,0))[-1]
                                ))
results.append(benchmark_results('Adept', 'Forward', 'Versor',(3,1), 'BM_AdeptRotorVersorJacobianForward', 
                                 num_repetitions,
                                 ad.diff_adept_rotor_versor_forward(0.5, vsr.Vec(1,0,0))[-1]))
results.append(benchmark_results('Adept', 'Reverse', 'Versor',(3,1), 'BM_AdeptRotorVersorJacobianReverse',
                                 num_repetitions,
                                 ad.diff_adept_rotor_versor_reverse(0.5, vsr.Vec(1,0,0))[-1]
                                ))
results.append(benchmark_results('Adept', 'Forward', 'HepGA',(3,1), 'BM_AdeptRotorHepGAJacobianForward', 
                                 num_repetitions,
                                 ad.diff_adept_rotor_hepga_forward(0.5, vsr.Vec(1,0,0))[-1]))
results.append(benchmark_results('Adept', 'Reverse', 'HepGA',(3,1), 'BM_AdeptRotorHepGAJacobianReverse', 
                                 num_repetitions,
                                 ad.diff_adept_rotor_hepga_reverse(0.5, vsr.Vec(1,0,0))[-1]))
results.append(benchmark_results('Ceres', 'Forward', 'HepGA',(3,1), 'BM_CeresRotorHepGAJacobian', num_repetitions))
results.append(benchmark_results('Ceres', 'Forward', 'Versor',(3,1), 'BM_CeresRotorVersorJacobian', num_repetitions))
results.append(benchmark_results('Ceres', 'Forward', 'Matrix',(3,1),'BM_CeresRotorMatrixJacobian', num_repetitions))
results.append(benchmark_results('Ceres', 'Forward', 'Versor',(5,8),'BM_CeresMotorSpinPointJacobian', num_repetitions))
results.append(benchmark_results('Adept', 'Forward', 'Versor',(5,8),'BM_AdeptMotorSpinPointJacobianForward', num_repetitions))
results.append(benchmark_results('Ceres', 'Forward', 'Versor',(3,4),'BM_CeresRotorSpinPointJacobian', num_repetitions))
results.append(benchmark_results('Adept', 'Forward', 'Versor',(3,4),'BM_AdeptRotorSpinPointJacobianForward', num_repetitions))
results.append(benchmark_results('Ceres', 'Forward', 'HepGA',(3,1), 'BM_CeresRotorGaalopJacobian', num_repetitions))
df_ad = pd.DataFrame(results)
df_ad.transpose()
df_ad.plot(y='real_time_mean', yerr='real_time_stddev', kind='bar', color='red')
# df_ad.plot(y='cpu_time_mean', yerr='cpu_time_stddev', kind='bar')<jupyter_output><empty_output><jupyter_text>Motor Estimation Performance<jupyter_code>motor = vsr.Vec(1,1,1).trs() * vsr.Rot(vsr.Biv(0,1,0) * np.pi/6.0)
print(motor)
def benchmark_motor_estimation(n_points, num_linear_solver_threads, num_threads, 
                               parameterization='BIVECTOR_GENERATOR'):
    sigma = 0.09
    points_a = [vsr.Vec(*np.random.normal(0.0, 0.8, 3)).null() 
                for i in range(n_points)]
    points_b = [point.spin(motor) for point in points_a]
    points_b_noisy = [vsr.Vec(*(np.array(point)[:3] 
                                + sigma * np.random.randn(3))).null() 
                      for point in points_b]
    initial_motor = vsr.Mot(1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
    mes = MotorEstimationSolver(initial_motor)
    for a, b in zip(points_a, points_b_noisy):
        mes.add_point_correspondences_residual_block(a,b)
    mes.set_parameterization(parameterization)
    mes.linear_solver_type = 'DENSE_QR'
    mes.num_linear_solver_threads = num_linear_solver_threads
    mes.num_threads = num_threads
    (estimated_motor, summary, motors) = mes.solve()
    results = {}
    results['initial_cost'] = summary['initial_cost']
    results['final_cost'] = summary['final_cost']
    results['num_iterations'] = len(summary['iterations'])
    results['num_residual_blocks'] = summary['num_residual_blocks']
    results['num_linear_solver_threads_given'] = summary['num_linear_solver_threads_given']
    results['num_threads_given'] = summary['num_threads_given']
    results['num_linear_solver_threads_used'] = summary['num_linear_solver_threads_used']
    results['num_threads_used'] = summary['num_threads_used']
    results['num_parameters'] = summary['num_parameters']
    results['num_effective_parameters'] = summary['num_effective_parameters']
    results['num_residuals'] = summary['num_residuals']
    results['jacobian_size'] = (results['num_residuals'], results['num_effective_parameters'])
    results['preprocessor_time'] = summary['preprocessor_time_in_seconds']
    results['residual_evaluation_time'] = summary['residual_evaluation_time_in_seconds']
    results['jacobian_evaluation_time'] = summary['jacobian_evaluation_time_in_seconds']
    results['linear_solver_time'] = summary['linear_solver_time_in_seconds']
    results['minimizer_time'] = summary['minimizer_time_in_seconds']
    results['postprocessor_time'] = summary['postprocessor_time_in_seconds']

    results['total_time'] = summary['total_time_in_seconds']
    return results
results = []
results.append(benchmark_motor_estimation(10,1,1))
results.append(benchmark_motor_estimation(10,1,1,'POLAR_DECOMPOSITION'))
results.append(benchmark_motor_estimation(1000,256,256))
results.append(benchmark_motor_estimation(1000,256,256,'POLAR_DECOMPOSITION'))
results.append(benchmark_motor_estimation(100000,1024,1024))
results.append(benchmark_motor_estimation(100000,1024,1024, 'POLAR_DECOMPOSITION'))

df = pd.DataFrame(results)
df.transpose()
results = [benchmark_motor_estimation(1000000, 1024, 1024) for i in range(1)]
df = pd.DataFrame(results)
df.transpose()
results = [benchmark_motor_estimation(1000000, 2048, 2048, 'POLAR_DECOMPOSITION') for i in range(1)]
df = pd.DataFrame(results)
df.transpose()
(df['total_time'].mean(), df['total_time'].std())
results = [benchmark_motor_estimation(100000, 1024,1024, 'POLAR_DECOMPOSITION') for i in range(10)]
df = pd.DataFrame(results)
df.transpose()
(df['total_time'].mean(), df['total_time'].std())
df
df.plot(x='num_residual_blocks', y='total_time', kind='bar')
n_points=10
sigma = 0.09
points_a = [vsr.Vec(*np.random.normal(0.0, 0.8, 3)).null() 
            for i in range(n_points)]
points_b = [point.spin(motor) for point in points_a]
points_b_noisy = [vsr.Vec(*(np.array(point)[:3] 
                            + sigma * np.random.randn(3))).null() 
                  for point in points_b]
initial_motor = vsr.Mot(1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
mes = MotorEstimationSolver(initial_motor)
for a, b in zip(points_a, points_b_noisy):
    mes.add_point_correspondences_residual_block(a,b)
mes.set_parameterization('BIVECTOR_GENERATOR')
mes.linear_solver_type = 'DENSE_QR'
mes.num_linear_solver_threads = 1
mes.num_threads = 1
(estimated_motor, summary, motors) = mes.solve()
print(summary['full_report'])
pd.DataFrame(summary).transpose()
time_10_1_1 = [benchmark_motor_estimation(10, 1,1) for i in range(1000)]
print(np.mean(time_10_1_1))
print(np.std(time_10_1_1))
time_100_4_4 = [benchmark_motor_estimation(10000, 128,32) for i in range(100)]
print(np.mean(time_100_4_4))
print(np.std(time_100_4_4))
# n_points = [10, 100, 1000, 10000, 100000]
n_points = [10]
n_linear_solver_threads = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]
n_threads = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]
res = {}
for i in n_points:
    for j in n_linear_solver_threads:
        for k in n_threads:
            total_time_in_seconds = benchmark_motor_estimation(i, j, k)
            res['{}_{}_{}'.format(i,j,k)] = total_time_in_seconds
print(res)
summary['jacobian_evaluation_solver_time_in_seconds']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14382360310699072, 'fraction_numerical': 0.04472563267351541, 'mean_word_length': 4.702142857142857, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '380226', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3146, 'n_tokens_neox': 2846, 'n_words': 381, 'path': 'python/1-Automatic-Multivector-Differentiation-and-Optimization.ipynb', 'repo_name': 'tingelst/game', 'size': '7982'}