Returns the p-norm of this vector. @param p the norm type. 2 is a common value @return the p-norm of this vector
(double p)
| 799 | * @return the p-norm of this vector |
| 800 | */ |
| 801 | public double pNorm(double p) |
| 802 | { |
| 803 | if (p <= 0) |
| 804 | throw new IllegalArgumentException("norm must be a positive value, not " + p); |
| 805 | double result = 0; |
| 806 | if (p == 1) |
| 807 | { |
| 808 | for (IndexValue iv : this) |
| 809 | result += abs(iv.getValue()); |
| 810 | } |
| 811 | else if (p == 2) |
| 812 | { |
| 813 | for (IndexValue iv : this) |
| 814 | result += iv.getValue() * iv.getValue(); |
| 815 | result = Math.sqrt(result); |
| 816 | } |
| 817 | else if (Double.isInfinite(p)) |
| 818 | { |
| 819 | for (IndexValue iv : this) |
| 820 | result = Math.max(result, abs(iv.getValue())); |
| 821 | } |
| 822 | else |
| 823 | { |
| 824 | for (IndexValue iv : this) |
| 825 | result += pow(abs(iv.getValue()), p); |
| 826 | result = pow(result, 1 / p); |
| 827 | } |
| 828 | return result; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Computes the dot product between two vectors, which is equivalent to<br> |