Skip to content

Commit d49c204

Browse files
dance858claude
andcommitted
power: avoid pow for the exponents of x^2 and x^(-1)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012aJMguJbVnytNNJEuvumcb
1 parent a13cf6b commit d49c204

3 files changed

Lines changed: 74 additions & 9 deletions

File tree

‎src/atoms/elementwise_full_dom/power.c‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,40 @@
2121
#include <math.h>
2222
#include <stdlib.h>
2323

24+
/* out[i] = c * x[i]^e. The exponents that arise from x^2 and x^(-1) and their
25+
derivatives avoid pow, which is slow. */
26+
static void scaled_pow(double *out, const double *x, int n, double c, double e)
27+
{
28+
if (e == 0.0)
29+
{
30+
for (int i = 0; i < n; i++) out[i] = c;
31+
}
32+
else if (e == 1.0)
33+
{
34+
for (int i = 0; i < n; i++) out[i] = c * x[i];
35+
}
36+
else if (e == 2.0)
37+
{
38+
for (int i = 0; i < n; i++) out[i] = c * (x[i] * x[i]);
39+
}
40+
else if (e == -1.0)
41+
{
42+
for (int i = 0; i < n; i++) out[i] = c / x[i];
43+
}
44+
else if (e == -2.0)
45+
{
46+
for (int i = 0; i < n; i++) out[i] = c / (x[i] * x[i]);
47+
}
48+
else if (e == -3.0)
49+
{
50+
for (int i = 0; i < n; i++) out[i] = c / (x[i] * x[i] * x[i]);
51+
}
52+
else
53+
{
54+
for (int i = 0; i < n; i++) out[i] = c * pow(x[i], e);
55+
}
56+
}
57+
2458
static void forward(expr *node, const double *u)
2559
{
2660

@@ -30,31 +64,26 @@ static void forward(expr *node, const double *u)
3064
/* local forward pass */
3165
double *x = node->left->value;
3266
double p = ((power_expr *) node)->p;
33-
for (int i = 0; i < node->size; i++)
34-
{
35-
node->value[i] = pow(x[i], p);
36-
}
67+
scaled_pow(node->value, x, node->size, 1.0, p);
3768
}
3869

3970
static void local_jacobian(expr *node, double *vals)
4071
{
4172
double *x = node->left->value;
4273
double p = ((power_expr *) node)->p;
4374

44-
for (int j = 0; j < node->size; j++)
45-
{
46-
vals[j] = p * pow(x[j], p - 1);
47-
}
75+
scaled_pow(vals, x, node->size, p, p - 1);
4876
}
4977

5078
static void local_wsum_hess(expr *node, double *out, const double *w)
5179
{
5280
double *x = node->left->value;
5381
double p = ((power_expr *) node)->p;
5482

83+
scaled_pow(out, x, node->size, p * (p - 1), p - 2);
5584
for (int j = 0; j < node->size; j++)
5685
{
57-
out[j] = w[j] * p * (p - 1) * pow(x[j], p - 2);
86+
out[j] *= w[j];
5887
}
5988
}
6089

‎tests/all_tests.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ int main(void)
290290
mu_run_test(test_wsum_hess_entr, tests_run);
291291
mu_run_test(test_wsum_hess_logistic, tests_run);
292292
mu_run_test(test_wsum_hess_power, tests_run);
293+
mu_run_test(test_power_special_exponents, tests_run);
293294
mu_run_test(test_wsum_hess_xexp, tests_run);
294295
mu_run_test(test_wsum_hess_sin, tests_run);
295296
mu_run_test(test_wsum_hess_cos, tests_run);

‎tests/wsum_hess/elementwise_full_dom/test_power.h‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,38 @@ const char *test_wsum_hess_power(void)
3535

3636
return 0;
3737
}
38+
39+
/* p = 2 and p = -1 use every special case in power.c, p = 0.5 uses pow */
40+
const char *test_power_special_exponents(void)
41+
{
42+
double u_vals[3] = {0.5, 1.5, 3.0};
43+
double w[3] = {1.0, -2.0, 3.0};
44+
double exponents[3] = {2.0, -1.0, 0.5};
45+
46+
for (int k = 0; k < 3; k++)
47+
{
48+
double p = exponents[k];
49+
double f[3], df[3], d2f[3];
50+
for (int i = 0; i < 3; i++)
51+
{
52+
f[i] = pow(u_vals[i], p);
53+
df[i] = p * pow(u_vals[i], p - 1);
54+
d2f[i] = w[i] * p * (p - 1) * pow(u_vals[i], p - 2);
55+
}
56+
57+
expr *node = new_power(new_variable(3, 1, 0, 3), p);
58+
node->forward(node, u_vals);
59+
jacobian_init(node);
60+
eval_jacobian(node);
61+
wsum_hess_init(node);
62+
eval_wsum_hess(node, w);
63+
64+
mu_assert("forward fail", cmp_double_array(node->value, f, 3));
65+
mu_assert("jacobian fail", cmp_values(node->jacobian, df, 3));
66+
mu_assert("hessian fail", cmp_values(node->wsum_hess, d2f, 3));
67+
68+
free_expr(node);
69+
}
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)