Returns the p-norm distance between this and another vector y. @param p the distance type. 2 is the common value @param y the other vector to compare against @return the p-norm distance
(double p, Vec y)
| 734 | * @return the p-norm distance |
| 735 | */ |
| 736 | public double pNormDist(double p, Vec y) |
| 737 | { |
| 738 | Iterator<IndexValue> thisIter = this.iterator(); |
| 739 | Iterator<IndexValue> otherIter = y.iterator(); |
| 740 | if (!thisIter.hasNext()) |
| 741 | if (!otherIter.hasNext()) |
| 742 | return 0; |
| 743 | else |
| 744 | return y.pNorm(p); |
| 745 | else if (!otherIter.hasNext()) |
| 746 | return this.pNorm(p); |
| 747 | |
| 748 | double result = 0; |
| 749 | |
| 750 | IndexValue av = thisIter.next(); |
| 751 | IndexValue bv = otherIter.next(); |
| 752 | |
| 753 | do |
| 754 | { |
| 755 | boolean nextA = false, nextB = false; |
| 756 | if (av.getIndex() == bv.getIndex()) |
| 757 | { |
| 758 | result += pow(abs(av.getValue() - bv.getValue()), p); |
| 759 | nextA = nextB = true; |
| 760 | } |
| 761 | else if(av.getIndex() < bv.getIndex()) |
| 762 | { |
| 763 | result += pow(abs(av.getValue()), p); |
| 764 | nextA = true; |
| 765 | } |
| 766 | else if(av.getIndex() > bv.getIndex()) |
| 767 | { |
| 768 | result += pow(abs(bv.getValue()), p); |
| 769 | nextB = true; |
| 770 | } |
| 771 | |
| 772 | if(nextA) |
| 773 | av = thisIter.hasNext() ? thisIter.next() : null; |
| 774 | if(nextB) |
| 775 | bv = otherIter.hasNext() ? otherIter.next() : null; |
| 776 | } |
| 777 | while (av != null && bv != null); |
| 778 | |
| 779 | //accumulate left overs |
| 780 | while(av != null) |
| 781 | { |
| 782 | result += pow(abs(av.getValue()), p); |
| 783 | av = thisIter.hasNext() ? thisIter.next() : null; |
| 784 | } |
| 785 | |
| 786 | while(bv != null) |
| 787 | { |
| 788 | result += pow(abs(bv.getValue()), p); |
| 789 | bv = otherIter.hasNext() ? otherIter.next() : null; |
| 790 | } |
| 791 | |
| 792 | |
| 793 | return pow(result, 1/p); |