| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public double pNorm(double p) |
| 367 | { |
| 368 | if (p <= 0) |
| 369 | throw new IllegalArgumentException("norm must be a positive value, not " + p); |
| 370 | double result = 0; |
| 371 | if (p == 1) |
| 372 | { |
| 373 | for (int i = startIndex; i < endIndex; i++) |
| 374 | result += abs(array[i]); |
| 375 | } |
| 376 | else if (p == 2) |
| 377 | { |
| 378 | for(int i = startIndex; i < endIndex; i++) |
| 379 | result += array[i] * array[i]; |
| 380 | result = Math.sqrt(result); |
| 381 | } |
| 382 | else if (Double.isInfinite(p)) |
| 383 | { |
| 384 | for(int i = startIndex; i < endIndex; i++) |
| 385 | result = Math.max(result, abs(array[i])); |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | for(int i = startIndex; i < endIndex; i++) |
| 390 | result += Math.pow(Math.abs(array[i]), p); |
| 391 | result = pow(result, 1 / p); |
| 392 | } |
| 393 | return result; |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public Vec clone() |