#include <math.h>
#include <stdio.h>
double pnorm_i(int n, double *a, double p) {
return (n
< 0) ? 0.0 : pow(fabs(a
[n
]), p
) + pnorm_i
(n
- 1, a
, p
); }
double pnorm(double p, int n, double *a) {
return pow(pnorm_i
(n
- 1, a
, p
), 1.0 / p
); }
int main() {
double a[] = { 0.5, -0.5 };
double b[] = { 1.0, 1.0, 1.0, 1.0, 1.0 };
double c[] = { -1.0, 2.0, 0.0, 3.0 };
printf("%f\n", pnorm
(1.0, sizeof(a
) / sizeof(double), a
)); printf("%f\n", pnorm
(1.0, sizeof(b
) / sizeof(double), b
)); printf("%f\n", pnorm
(1.0, sizeof(c
) / sizeof(double), c
)); printf("%f\n", pnorm
(2.0, sizeof(a
) / sizeof(double), a
)); printf("%f\n", pnorm
(2.0, sizeof(b
) / sizeof(double), b
)); printf("%f\n", pnorm
(2.0, sizeof(c
) / sizeof(double), c
)); printf("%f\n", pnorm
(3.0, sizeof(a
) / sizeof(double), a
)); printf("%f\n", pnorm
(3.0, sizeof(b
) / sizeof(double), b
)); printf("%f\n", pnorm
(3.0, sizeof(c
) / sizeof(double), c
)); return 0;
}
/* end */
I2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPHN0ZGlvLmg+Cgpkb3VibGUgcG5vcm1faShpbnQgbiwgZG91YmxlICphLCBkb3VibGUgcCkgewogIHJldHVybiAobiA8IDApID8gMC4wIDogcG93KGZhYnMoYVtuXSksIHApICsgcG5vcm1faShuIC0gMSwgYSwgcCk7Cn0KZG91YmxlIHBub3JtKGRvdWJsZSBwLCBpbnQgbiwgZG91YmxlICphKSB7CiAgcmV0dXJuIHBvdyhwbm9ybV9pKG4gLSAxLCBhLCBwKSwgMS4wIC8gcCk7Cn0KCmludCBtYWluKCkgewogIGRvdWJsZSBhW10gPSB7IDAuNSwgLTAuNSB9OwogIGRvdWJsZSBiW10gPSB7IDEuMCwgMS4wLCAxLjAsIDEuMCwgMS4wIH07CiAgZG91YmxlIGNbXSA9IHsgLTEuMCwg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