| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public double pNormDist(double p, Vec y) |
| 336 | { |
| 337 | if(this.length() != y.length()) |
| 338 | throw new ArithmeticException("Vectors must be of the same length"); |
| 339 | |
| 340 | double norm = 0; |
| 341 | if(y.isSparse()) |
| 342 | { |
| 343 | int lastIndx = -1; |
| 344 | for(IndexValue iv : y) |
| 345 | { |
| 346 | for(int i = lastIndx+1; i < iv.getIndex(); i++)//add all the indecies we skipped |
| 347 | norm += Math.pow(Math.abs(array[i]), p); |
| 348 | lastIndx = iv.getIndex(); |
| 349 | //add current |
| 350 | norm += Math.pow(Math.abs(array[iv.getIndex()]-iv.getValue()), p); |
| 351 | } |
| 352 | |
| 353 | //Tailing zeros |
| 354 | for(int i = lastIndx+1; i < y.length(); i++) |
| 355 | norm += Math.pow(Math.abs(array[i]), p); |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | for(int i = startIndex; i < endIndex; i++) |
| 360 | norm += Math.pow(Math.abs(array[i]-y.get(i)), p); |
| 361 | } |
| 362 | return Math.pow(norm, 1.0/p); |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public double pNorm(double p) |