-
Notifications
You must be signed in to change notification settings - Fork 35
/
Dist.pas
782 lines (659 loc) · 23.8 KB
/
Dist.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2018, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
unit Dist;
interface
uses Matrix, BaseMathPersistence;
// ###################################################################
// #### different distance measures
// -> eukledian distance from mean
// -> abs distance from geometric median
// -> mahalonobis distance
// note that this class provides the possibility to calculate the distances from
// different initialization routins too (e.g use initEuclid and the L1 measure)
type
TDistance = class(TMatrixClass)
private
fRx : Integer;
fMean : IMatrix;
fR : IMatrix;
fNumIter : integer;
protected
class function ClassIdentifier : String; override;
procedure DefineProps; override;
function PropTypeOfName(const Name : string) : TPropType; override;
procedure OnLoadIntProperty(const Name : String; Value : integer); override;
function OnLoadObject(const Name : String; Obj : TBaseMathPersistence) : boolean; override;
public
// only valid for L1 distances: shows how many iterations were needed for the geometric median
property NumIter : integer read fNumIter;
// init the distance fields from "outside"; use aMean, aR, dimx for mahalonobis and the other one for L1, euclid
procedure Init(aMean, aCov : IMatrix); overload;
procedure Init(aMean : IMatrix); overload;
// Returns the eucledian distance (L2) in square units. Return value is a vector of width Y
// euclid(i) = sum( (Y(i, :) - MU).*((Y(i, :) - MU))
procedure InitEuclid(X : IMatrix);
function EuclidDist(Y : IMatrix) : IMatrix;
class function Euclid(X, Y : IMatrix) : IMatrix;
procedure InitMahal(X : IMatrix; doTryFastInf : boolean = True);
function MahalDist(Y : IMatrix) : IMatrix;
// regularized weiszfeld according to: "Robust L1 approaches to computing the geometric median and principal and independent compoents"
// Keeling, Kunisch
procedure InitL1DistReg(X : IMatrix; tau : double = 10; maxIter : integer = 200; relTol : double = 1e-6);
// calculate geometric median using the weiszfeld algorithm
procedure InitL1Dist(X : IMatrix; maxIter : integer = 200; relTol : double = 1e-6; beta : double = 1e-5);
function L1Dist(y : IMatrix) : IMatrix;
class function L1(x, y : IMatrix) : IMatrix; overload;
class function L1(x, y : IMatrix; maxIter : integer; relTol : double; beta : double) : IMatrix; overload;
class function L1Reg(x, y : IMatrix) : IMatrix; overload;
class function L1Reg(x, y : IMatrix; maxIter : integer; relTol : double; tau : double) : IMatrix; overload;
// Returns the Mahalonobis distance in squared units.
// Return value is a vector of width Y.
// mahal = (Y( i, :) - MU)* sigma^-1 *y(i, :) - MU)
class function Mahalanobis(X, Y : IMatrix) : IMatrix; overload;
// calculates the mahalonobis disance from given covariance matrix R and mean M
class function Mahalanobis(Y, aCov, aMean : IMatrix) : IMatrix; overload;
// ###########################################
// #### Pairwise distance functions
// #### as input is assumed: columns are coordinates, rows are elements
// #### Result is a vector of length (Height)*(Height - 1) div 2
// ###########################################
class function EuclidPairDist( X : IMatrix ) : IMatrix; overload;
class function NormEuclidPairDist( X : IMatrix ) : IMatrix; overload; // normalized euclid: rows are scaled by inverse of variance
class function AbsPairDist( X : IMatrix ) : IMatrix; overload;
class function MinkowskyPairDist( X : IMatrix; exponent : Double = 2 ) : IMatrix; overload;
class function ChebychevPairDist( X : IMatrix ) : IMatrix; overload;
class function MahalanobisPairDist( X : IMatrix ) : IMatrix; overload;
class function EuclidPairDist( X : TDoubleMatrix ) : TDoubleMatrix; overload;
class function NormEuclidPairDist( X : TDoubleMatrix ) : TDoubleMatrix; overload; // normalized euclid: rows are scaled by inverse of variance
class function AbsPairDist( X : TDoubleMatrix ) : TDoubleMatrix; overload;
class function MinkowskyPairDist( X : TDoubleMatrix; exponent : Double = 2 ) : TDoubleMatrix; overload;
class function ChebychevPairDist( X : TDoubleMatrix ) : TDoubleMatrix; overload;
class function MahalanobisPairDist( X : TDoubleMatrix ) : TDoubleMatrix; overload;
end;
implementation
uses SysUtils, MatrixConst, MatrixASMStubSwitch, Math, Corr, MathUtilFunc,
LinAlgCholesky, Types;
{ TDistance }
procedure TDistance.InitMahal(X: IMatrix; doTryFastInf : boolean = True);
var C : IMatrix;
R : IMatrix;
begin
fRx := x.Height;
fMean := X.Mean(False);
C := X.SubVec(fMean, True);
C.QR(R);
R.TransposeInPlace;
if not doTryFastInf or (R.Invert(fR) <> leOk) then
if R.PseudoInversion(fR) = srNoConvergence then
raise Exception.Create('Error could not invert matrix');
end;
function TDistance.MahalDist(Y: IMatrix): IMatrix;
var M : IMatrix;
ri : IMatrix;
i : integer;
Q : IMatrix;
begin
if not Assigned(fMean) or not Assigned(fR) then
raise Exception.Create('Error call InitMahal first');
M := Y.SubVec(fMean, True);
if fRx < 0 then
begin
// from init with the cholesky decomposition of the covariance matrix
Q := TDoubleMatrix.Create( Y.Width, 1 );
Result := MatrixClass.Create( 1, Y.Height );
for i := 0 to M.Height - 1 do
begin
M.SetSubMatrix(0, i, M.Width, 1);
MatrixCholeskyBackSolve( fR.StartElement, fR.LineWidth, fR.Width, M.StartElement, sizeof(double), Q.StartElement, sizeof(double) );
Result[0, i] := Q.ElementwiseNorm2(False);
end;
end
else
begin
ri := fR.MultT2(M);
ri.ElementWiseMultInPlace(ri);
ri.SumInPlace(False);
Result := ri.Transpose;
Result.ScaleInPlace(fRX - 1);
end;
end;
// ###############################################
// #### One time used short methods
class function TDistance.Mahalanobis(X, Y: IMatrix): IMatrix;
begin
with TDistance.Create do
try
InitMahal(X);
Result := MahalDist(Y);
finally
Free;
end;
end;
class function TDistance.Mahalanobis(Y, aCov, aMean: IMatrix): IMatrix;
begin
with TDistance.Create do
try
Init( aMean, aCov );
Result := MahalDist(Y);
finally
Free;
end;
end;
procedure TDistance.InitEuclid(X: IMatrix);
begin
fMean := X.Mean(False);
end;
function TDistance.EuclidDist(Y: IMatrix): IMatrix;
var dist : IMatrix;
begin
if not Assigned(fMean) then
raise Exception.Create('Error call InitEuclid first');
dist := Y.SubVec(fMean, True);
dist.ElementWiseMultInPlace(dist);
dist.SumInPlace(True);
Result := dist;
end;
class function TDistance.Euclid(X, Y: IMatrix): IMatrix;
begin
with TDistance.Create do
try
InitEuclid(X);
Result := EuclidDist(Y);
finally
Free;
end;
end;
procedure sqrtFunc(var Value : double);
begin
Value := 1/sqrt(Value);
end;
procedure TDistance.InitL1Dist(X: IMatrix; maxIter : integer = 200; relTol : double = 1e-6; beta : double = 1e-5);
var eps : double;
counter : integer;
weights, data : IMatrix;
i : integer;
oldMean : IMatrix;
meanSub : IMatrix;
begin
// initialize with geometric median using the standard Weiszfeld algorithm
// check out: https://de.mathworks.com/matlabcentral/fileexchange/64781-weiszfeld-input-structure-
fNumIter := 0;
fMean := X.Mean(False);
oldMean := fMean.Clone;
data := MatrixClass.Create;
meanSub := MatrixClass.Create;
for counter := 0 to maxIter - 1 do
begin
inc(fNumIter);
// copy data
data.Assign(X);
data.SubVecInPlace(fMean, True);
data.ElementWiseMultInPlace(data);
weights := data.Sum(True);
// check for regularization
// extension to the original algorithm:
// add beat for regularization and then reduce beta dividing it by 2 in each iteration
// until divide by 2^10 is met
if weights.Min < beta then
weights.AddInPlace( beta );
weights.ElementwiseFuncInPlace( {$IFDEF FPC}@{$ENDIF}sqrtFunc );
data := X.Clone;
for i := 0 to X.Width - 1 do
begin
data.SetSubMatrix(i, 0, 1, X.Height);
data.ElementWiseMultInPlace(weights);
end;
data.UseFullMatrix;
data.SumInPlace(False);
weights.SumInPlace(False);
fMean.Assign(data);
fMean.ScaleInPlace(1/weights.Vec[0]);
meanSub.Assign(fMean);
meanSub.SubInPlace(oldMean);
eps := meanSub.ElementwiseNorm2(True);
if eps < relTol then
break;
oldMean.Assign(fMean);
if counter < 10 then
beta := beta/2;
end;
end;
function TDistance.L1Dist(y: IMatrix): IMatrix;
var dist : IMatrix;
begin
if not Assigned(fMean) then
raise Exception.Create('Error call InitAbs first');
dist := Y.SubVec(fMean, True);
dist.AbsInPlace;
dist.SumInPlace(True);
Result := dist;
end;
class function TDistance.L1Reg(x, y: IMatrix): IMatrix;
begin
Result := L1Reg(x, y, 200, 1e-6, 10);
end;
class function TDistance.L1Reg(x, y: IMatrix; maxIter: integer; relTol,
tau: double): IMatrix;
begin
with TDistance.Create do
try
InitL1DistReg(X, tau, maxIter, relTol);
Result := L1Dist(Y);
finally
Free;
end;
end;
class function TDistance.L1(x, y: IMatrix): IMatrix;
begin
with TDistance.Create do
try
InitL1Dist(X);
Result := L1Dist(Y);
finally
Free;
end;
end;
class function TDistance.L1(x, y: IMatrix; maxIter: integer; relTol,
beta: double): IMatrix;
begin
with TDistance.Create do
try
InitL1Dist(X, maxIter, relTol, beta);
Result := L1Dist(Y);
finally
Free;
end;
end;
procedure TDistance.Init(aMean: IMatrix);
begin
fMean := aMean.Clone;
fR := nil;
fRx := 0;
end;
procedure TDistance.Init(aMean, aCov: IMatrix);
begin
if aCov.Cholesky(fR) <> crOk then
raise Exception.Create('Cannot init mahalanobis');
fMean := aMean.Clone;
fRx := -1;
end;
// ######################################################################
// #### persistence functionality
// ######################################################################
const cDistIdentifier = 'Distance';
cDistR = 'R';
cDistRx = 'Rx';
cDistMean = 'DistMean';
class function TDistance.ClassIdentifier: String;
begin
Result := cDistIdentifier;
end;
procedure TDistance.DefineProps;
begin
if Assigned(fR) then
begin
AddObject(cDistR, fR.GetObjRef);
AddIntProperty(cDistRx, fRx);
end;
if Assigned(fMean) then
AddObject(cDistMean, fMean.GetObjRef);
end;
function TDistance.PropTypeOfName(const Name: string): TPropType;
begin
if SameText(Name, cDistR) or SameText(Name, cDistMean)
then
Result := ptObject
else if SameText(Name, cDistRx)
then
Result := ptInteger
else
Result := inherited PropTypeOfName(Name);
end;
function TDistance.OnLoadObject(const Name: String;
Obj: TBaseMathPersistence): boolean;
begin
Result := True;
if SameText(Name, cDistR)
then
fR := (obj as TDoubleMatrix) as IMatrix
else if SameText(Name, cDistMean)
then
fMean := (obj as TDoubleMatrix) as IMatrix
else
Result := inherited OnLoadObject(Name, Obj);
end;
procedure TDistance.OnLoadIntProperty(const Name: String; Value: integer);
begin
if SameText(Name, cDistRx)
then
fRx := Value
else
inherited;
end;
procedure TDistance.InitL1DistReg(X: IMatrix; tau: double; maxIter: integer;
relTol: double);
var eps : double;
counter : integer;
data : IMatrix;
dataP1 : IMatrix;
i : integer;
newMean : IMatrix;
meanSub : IMatrix;
denom : IMatrix;
sumDenom : double;
row : IMatrix;
tmp : IMatrix;
procedure switchMtx(var x1, x2 : IMatrix);
var tmp : IMatrix;
begin
tmp := x1;
x1 := x2;
x2 := tmp;
end;
begin
// regularized geometric median
fMean := X.Mean(False);
fNumIter := 0;
newMean := fMean.Clone;
data := MatrixClass.Create;
dataP1 := MatrixClass.Create(x.Width, x.Height);
data.Assign(X);
row := MatrixClass.Create(X.Width, 1);
meanSub := MatrixClass.Create;
denom := MatrixClass.Create;
tmp := MatrixClass.Create( 1, x.Height );
for counter := 0 to maxIter - 1 do
begin
inc(fNumIter);
// 1 + ||ul - Y*e_j||
denom.Assign(X);
denom.SubVecInPlace(fMean, True);
denom.ElementWiseMultInPlace(denom);
denom.SumInPlace(True, True);
denom.SQRTInPlace;
denom.ScaleAndAddInPlace(1, tau);
tmp.UseFullMatrix;
tmp.SetValue( -Tau );
tmp.ElementWiseDivInPlace(denom);
tmp.SumInPlace(False, True);
sumDenom := 1/(tmp.Vec[0]);
// calculate new D = (D*e_j + tau*(u - X*e_j))/(1 + tau*||u - X*e_j]]_l2);
// e_j is the Kroneker Delta function (is 0 except for j e_j=1)
for i := 0 to X.Height - 1 do
begin
data.SetSubMatrix(0, i, data.Width, 1);
X.SetSubMatrix(0, i, data.Width, 1);
dataP1.SetSubMatrix(0, i, data.Width, 1);
dataP1.SetRow(0, X );
dataP1.SubInPlace(fMean);
dataP1.ScaleInPlace(-tau);
dataP1.AddInplace(data);
dataP1.ScaleInPlace(1/denom.Vec[i]);
end;
data.UseFullMatrix;
dataP1.UseFullMatrix;
X.UseFullMatrix;
// calculate new mean
// u_new = sum_i=0_n ( (D - tau*Y)*e_j)/(1 + tau*||u - X*e_j]]_l2) ) /
// sum_i=0_n( -tau/(1 + tau*||u - X*e_j]]_l2) )
newMean.SetValue(0);
for i := 0 to Data.Height - 1 do
begin
data.SetSubMatrix(0, i, data.Width, 1);
row.SetRow(0, X, i);
row.ScaleInPlace(-tau);
row.AddInplace(data);
row.ScaleInPlace(1/denom.Vec[i]);
newMean.AddInplace(row);
end;
newMean.ScaleInPlace(sumDenom);
meanSub.Assign(fMean);
meanSub.SubInPlace(newMean);
// fast switch
switchMtx(newMean, fMean);
switchMtx(data, dataP1);
// check for exit criteria
eps := meanSub.ElementwiseNorm2(True);
if eps < relTol then
break;
end;
end;
class function TDistance.EuclidPairDist(X: TDoubleMatrix): TDoubleMatrix;
var y1, y2 : Integer;
h : integer;
p1, p2 : PDouble;
destLine : IMatrix;
idx : integer;
begin
Result := DefMatrixClass.Create( X.Height*(X.Height - 1) div 2, 1);
destLine := TDoubleMatrix.Create( X.Width, 1 );
h := X.Height;
idx := 0;
for y1 := 0 to h - 2 do
begin
X.SetSubMatrix(0, y1, X.Width, 1);
p1 := X.StartElement;
for y2 := y1 + 1 to h - 1 do
begin
X.SetSubMatrix(0, y2, X.Width, 1);
p2 := X.StartElement;
MatrixSub( destLine.StartElement, destLine.LineWidth, p1, p2, X.Width, 1, X.LineWidth, X.LineWidth);
Result.Vec[idx] := destLine.ElementwiseNorm2( True );
inc(idx);
end;
end;
X.UseFullMatrix;
end;
class function TDistance.NormEuclidPairDist(X: TDoubleMatrix): TDoubleMatrix;
var varX : IMatrix;
y1, y2 : Integer;
h : integer;
p1, p2 : PDouble;
destLine : IMatrix;
idx : integer;
begin
varX := X.Variance(False, True);
for y1 := 0 to VarX.VecLen - 1 do
varX.Vec[y1] := 1/varX.Vec[y1];
Result := DefMatrixClass.Create( X.Height*(X.Height - 1) div 2, 1);
destLine := TDoubleMatrix.Create( X.Width, 1 );
h := X.Height;
idx := 0;
for y1 := 0 to h - 2 do
begin
X.SetSubMatrix(0, y1, X.Width, 1);
p1 := X.StartElement;
for y2 := y1 + 1 to h - 1 do
begin
X.SetSubMatrix(0, y2, X.Width, 1);
p2 := X.StartElement;
MatrixSub( destLine.StartElement, destLine.LineWidth, p1, p2, X.Width, 1, X.LineWidth, X.LineWidth);
destLine.ElementWiseMultInPlace(destLine);
destLine.ElementWiseMultInPlace( varX );
destLine.SumInPlace(True, True);
Result.Vec[idx] := sqrt(destLine[0, 0]);
destLine.UseFullMatrix;
inc(idx);
end;
end;
X.UseFullMatrix;
end;
class function TDistance.AbsPairDist(X: TDoubleMatrix): TDoubleMatrix;
var y1, y2 : Integer;
h : integer;
p1, p2 : PDouble;
destLine : IMatrix;
sumVal : double;
idx : integer;
begin
Result := DefMatrixClass.Create( X.Height*(X.Height - 1) div 2, 1);
destLine := TDoubleMatrix.Create( X.Width, 1 );
h := X.Height;
idx := 0;
for y1 := 0 to h - 2 do
begin
X.SetSubMatrix(0, y1, X.Width, 1);
p1 := X.StartElement;
for y2 := y1 + 1 to h - 1 do
begin
X.SetSubMatrix(0, y2, X.Width, 1);
p2 := X.StartElement;
MatrixSub( destLine.StartElement, destLine.LineWidth, p1, p2, X.Width, 1, X.LineWidth, X.LineWidth);
destLine.AbsInPlace;
MatrixSum( @sumVal, sizeof(double), destLine.StartElement, destLine.LineWidth, destLine.Width, 1, True );
Result.Vec[idx] := sumVal;
inc(idx);
end;
end;
X.UseFullMatrix;
end;
class function TDistance.MinkowskyPairDist(X: TDoubleMatrix; exponent : Double = 2): TDoubleMatrix;
var y1, y2 : Integer;
h : integer;
p1, p2 : PDouble;
destLine : IMatrix;
sumVal : double;
idx : integer;
pDest : PConstDoubleArr;
i : Integer;
begin
Result := DefMatrixClass.Create( X.Height*(X.Height - 1) div 2, 1);
destLine := TDoubleMatrix.Create( X.Width, 1 );
h := X.Height;
idx := 0;
for y1 := 0 to h - 2 do
begin
X.SetSubMatrix(0, y1, X.Width, 1);
p1 := X.StartElement;
for y2 := y1 + 1 to h - 1 do
begin
X.SetSubMatrix(0, y2, X.Width, 1);
p2 := X.StartElement;
pDest := PConstDoubleArr( destLine.StartElement );
MatrixSub( PDouble(pDest), destLine.LineWidth, p1, p2, X.Width, 1, X.LineWidth, X.LineWidth);
for i := 0 to destLine.Width - 1 do
pDest^[i] := Power(abs(pDest^[i]), exponent);
MatrixSum( @sumVal, sizeof(double), destLine.StartElement, destLine.LineWidth, destLine.Width, 1, True );
Result.Vec[idx] := sumVal;
inc(idx);
end;
end;
X.UseFullMatrix;
end;
class function TDistance.MahalanobisPairDist(X: TDoubleMatrix): TDoubleMatrix;
// from https://stats.stackexchange.com/questions/65705/pairwise-mahalanobis-distances
var y1, y2 : Integer;
h : integer;
p1, p2 : PDouble;
destLine : IMatrix;
idx : integer;
help : IMatrix;
Q : IMatrix;
i : integer;
chol : IMatrix;
pQ : PDouble;
pX : PDouble;
begin
// ###########################################
// #### Init mahalonobis:
help := TCorrelation.Covariance(X);
if help.Cholesky( chol ) <> crOk then
raise Exception.Create('Cannot calculate Mahalanobis distance');
Q := TDoubleMatrix.Create( chol.Width, X.Height );
for i := 0 to x.Height - 1 do
begin
Q.SetSubMatrix(0, i, Q.Width, 1);
X.SetSubMatrix(0, i, X.Width, 1);
pQ := Q.StartElement;
pX := X.StartElement;
MatrixCholeskyBackSolve( chol.StartElement, chol.LineWidth, chol.Width, pX, sizeof(double), pQ, sizeof(double) );
end;
Q.UseFullMatrix;
X.UseFullMatrix;
Result := DefMatrixClass.Create( X.Height*(X.Height - 1) div 2, 1);
destLine := TDoubleMatrix.Create( X.Width, 1 );
// ###########################################
// #### Pairwise eucledian distance from here:
h := Q.Height;
idx := 0;
for y1 := 0 to h - 2 do
begin
Q.SetSubMatrix(0, y1, Q.Width, 1);
p1 := Q.StartElement;
for y2 := y1 + 1 to h - 1 do
begin
Q.SetSubMatrix(0, y2, Q.Width, 1);
p2 := Q.StartElement;
MatrixSub( destLine.StartElement, destLine.LineWidth, p1, p2, Q.Width, 1, Q.LineWidth, Q.LineWidth);
Result.Vec[idx] := destLine.ElementwiseNorm2(True);
inc(idx);
end;
end;
X.UseFullMatrix;
end;
class function TDistance.ChebychevPairDist(X: TDoubleMatrix): TDoubleMatrix;
var y1, y2 : Integer;
h : integer;
p1, p2 : PDouble;
destLine : IMatrix;
idx : integer;
begin
Result := DefMatrixClass.Create( X.Height*(X.Height - 1) div 2, 1);
destLine := TDoubleMatrix.Create( X.Width, 1 );
h := X.Height;
idx := 0;
for y1 := 0 to h - 2 do
begin
X.SetSubMatrix(0, y1, X.Width, 1);
p1 := X.StartElement;
for y2 := y1 + 1 to h - 1 do
begin
X.SetSubMatrix(0, y2, X.Width, 1);
p2 := X.StartElement;
MatrixSub( destLine.StartElement, destLine.LineWidth, p1, p2, X.Width, 1, X.LineWidth, X.LineWidth);
destLine.AbsInPlace;
Result.Vec[idx] := destLine.Max;
inc(idx);
end;
end;
X.UseFullMatrix;
end;
class function TDistance.EuclidPairDist(X: IMatrix): IMatrix;
begin
Result := EuclidPairDist(x.GetObjRef);
end;
class function TDistance.NormEuclidPairDist(X: IMatrix): IMatrix;
begin
Result := NormEuclidPairDist(X.GetObjRef);
end;
class function TDistance.AbsPairDist(X: IMatrix): IMatrix;
begin
Result := AbsPairDist(X.GetObjRef);
end;
class function TDistance.MinkowskyPairDist(X: IMatrix;
exponent: Double): IMatrix;
begin
Result := MinkowskyPairDist(X.GetObjRef, exponent);
end;
class function TDistance.ChebychevPairDist(X: IMatrix): IMatrix;
begin
Result := ChebychevPairDist(X.GetObjRef);
end;
class function TDistance.MahalanobisPairDist(X: IMatrix): IMatrix;
begin
Result := MahalanobisPairDist(X.GetObjRef);
end;
initialization
RegisterMathIO(TDistance);
end.