-
Notifications
You must be signed in to change notification settings - Fork 2
/
matio.pas
784 lines (696 loc) · 23.5 KB
/
matio.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
783
784
unit matio;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/matio
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
System.Classes, System.SysUtils, System.IOUtils, System.Types, PropSet, ArrayHlp, ArrayVal;
Type
TFloatType = (ftFloat16,ftFloat32,ftFloat64);
TFloat32MatrixRow = TArray<Float32>;
TFloat64MatrixRow = TArray<Float64>;
TMatrixRow = TFloat64MatrixRow;
TVirtualMatrixRow = Class
// Read only matrix row
private
FSize: Integer;
strict protected
Procedure Init(Size: Integer);
Function GetValues(Column: Integer): Float64; virtual; abstract;
public
Function Total: Float64; overload;
public
Property Size: Integer read FSize;
Property Values[Column: Integer]: Float64 read GetValues; default;
end;
TDelegatedMatrixRow = Class(TVirtualMatrixRow)
private
FValues: TFunc<Integer,Float64>;
strict protected
Function GetValues(Column: Integer): Float64; override; final;
public
Constructor Create(const Size: Integer; const Values: TFunc<Integer,Float64>);
end;
TVirtualMatrixRows = Class
// Read only matrix rows
private
FCount,FSize: Integer;
RoundToZeroThreshold: Float64;
Function DoGetValues(Matrix,Column: Integer): Float64; inline;
strict protected
Procedure Init(Count,Size: Integer);
Function GetValues(Matrix,Column: Integer): Float64; virtual; abstract;
public
Procedure GetRow(Matrix: Integer; var Row: TFloat32MatrixRow); overload;
Procedure GetRow(Matrix: Integer; var Row: TFloat64MatrixRow); overload;
Function Total: Float64; overload;
Function Total(Matrix: Integer): Float64; overload;
public
Property Count: Integer read FCount;
Property Size: Integer read FSize;
Property Values[Matrix,Column: Integer]: Float64 read DoGetValues; default;
end;
TCustomMatrixRows = Class(TVirtualMatrixRows)
// Base class for matrix rows with read and write access. Descendents must implement
// the in memory storage for matrix values.
private
TargetMatrices: TArray<Integer>;
Procedure DoSetValues(Matrix,Column: Integer; Value: Float64); inline;
strict protected
Procedure SetValues(Matrix,Column: Integer; Value: Float64); virtual; abstract;
public
Procedure Initialize(Value: Float64 = 0.0);
public
Property Values[Matrix,Column: Integer]: Float64 read DoGetValues write DoSetValues; default;
end;
TFloat64MatrixRows = Class(TCustomMatrixRows)
private
FValues: array of TFloat64MatrixRow;
strict protected
Function GetValues(Matrix,Column: Integer): Float64; override;
Procedure SetValues(Matrix,Column: Integer; Value: Float64); override;
public
Constructor Create; overload;
Constructor Create(Count,Size: Integer); overload;
Procedure Allocate(Count,Size: Integer);
Function RowValues(Matrix: Integer): TFloat64ArrayValues;
end;
TFloat32MatrixRows = Class(TCustomMatrixRows)
private
FValues: array of TFloat32MatrixRow;
strict protected
Function GetValues(Matrix,Column: Integer): Float64; override;
Procedure SetValues(Matrix,Column: Integer; Value: Float64); override;
public
Constructor Create; overload;
Constructor Create(Count,Size: Integer); overload;
Procedure Allocate(Count,Size: Integer);
Function RowValues(Matrix: Integer): TFloat32ArrayValues;
end;
TMatrixIterator = Class
private
FCount,FSize,Index: Integer;
Matrices: TFloat64MatrixRows;
strict protected
Procedure Yield(const Row: TMatrixRow);
public
Constructor Create(Count,Size: Integer);
Procedure Iterate; virtual; abstract;
Destructor Destroy; override;
public
Property Count: Integer read FCount;
Property Size: Integer read FSize;
end;
TMatrixFiler = Class
// TMatrixFiler is the abstract base class for all matrix reader and writer objects
private
FFileName: String;
FCount,FSize,CurrentRow: Integer;
strict protected
Const
BufferSize: Integer = 4096;
Var
FileStream: TBufferedFileStream;
Float64MatrixRows: TFloat64MatrixRows;
Float32MatrixRows: TFloat32MatrixRows;
Procedure SetCount(Count: Integer); virtual;
Procedure SetSize(Size: Integer);
public
Constructor Create;
Destructor Destroy; override;
public
Property FileName: String read FFileName;
Property Count: Integer read FCount;
Property Size: Integer read FSize;
end;
TMatrixReader = Class(TMatrixFiler)
// TMatrixReader is the abstract base class for all format specific matrix reader objects
private
FFileLabel: String;
FMatrixLabels: TArray<String>;
Function GetMatrixLabels(Mtrx: Integer): String; inline;
strict protected
FOrdered: Boolean;
Constructor Create(const FileName: String; const CreateStream: Boolean = true); overload;
Procedure SetCount(Count: Integer); override;
Procedure SetFileLabel(const FileLabel: String);
Procedure SetMatrixLabels(const Matrix: Integer; const MatrixLabel: String);
protected
Procedure Read(const CurrentRow: Integer; const Rows: TCustomMatrixRows); overload; virtual; abstract;
public
Function GetMatrix(const MatrixLabel: string): Integer;
Function MatrixLabelsArray: TStringDynArray;
Procedure Read(const Row: TFloat64MatrixRow); overload;
Procedure Read(const Row: TFloat32MatrixRow); overload;
Procedure Read(const Rows: array of TFloat64MatrixRow); overload;
Procedure Read(const Rows: array of TFloat32MatrixRow); overload;
Procedure Read(const Rows: TCustomMatrixRows); overload;
Procedure Read(const Rows: TMatrixIterator); overload;
public
Property Ordered: Boolean read FOrdered;
Property FileLabel: String read FFileLabel;
Property MatrixLabels[Matrix: Integer]: String read GetMatrixLabels;
end;
TMaskedMatrixReader = Class(TMatrixReader)
// Reads a selection of the matrices in a file
// The object takes ownership of the unmasked reader
private
Unmasked: TMatrixReader;
TargetMatrices: TArray<Integer>;
protected
Procedure Read(const CurrentRow: Integer; const Rows: TCustomMatrixRows); overload; override;
public
Constructor Create(const Reader: TMatrixReader; const Selection: array of Integer); overload;
Constructor Create(const Reader: TMatrixReader; const Selection: array of String); overload;
Destructor Destroy; override;
end;
TMatrixWriter = Class(TMatrixFiler)
// TMatrixWriter is the abstract base class for all format specific matrix writer objects
private
Type
TMatrixRows = Class(TVirtualMatrixRows)
private
Values: array of TVirtualMatrixRow;
strict protected
Function GetValues(Matrix,Column: Integer): Float64; override;
end;
Var
MatrixRows: TMatrixRows;
strict protected
Constructor Create(const FileName: String; const Count,Size: Integer; const CreateStream: Boolean = true); overload;
protected
Procedure Write(const CurrentRow: Integer; const Rows: TVirtualMatrixRows); overload; virtual; abstract;
public
Class Var
RoundToZeroThreshold: Float64;
public
Procedure Write(const Row: TVirtualMatrixRow); overload;
Procedure Write(const Row: TFloat64MatrixRow); overload;
Procedure Write(const Row: TFloat32MatrixRow); overload;
Procedure Write(const Rows: array of TVirtualMatrixRow); overload;
Procedure Write(const Rows: array of TFloat64MatrixRow); overload;
Procedure Write(const Rows: array of TFloat32MatrixRow); overload;
Procedure Write(const Rows: TVirtualMatrixRows); overload;
Procedure Write(const Rows: TMatrixIterator); overload;
Destructor Destroy; override;
end;
Const
PrecisionLabels: array[TFloatType] of String = ('float16','float32','float64');
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Procedure TVirtualMatrixRow.Init(Size: Integer);
begin
FSize := Size;
end;
Function TVirtualMatrixRow.Total: Float64;
begin
Result := 0.0;
for var Column := 0 to FSize-1 do Result := Result + GetValues(Column);
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TDelegatedMatrixRow.Create(const Size: Integer; const Values: TFunc<Integer,Float64>);
begin
inherited Create;
Init(Size);
FValues := Values;
end;
Function TDelegatedMatrixRow.GetValues(Column: Integer): Float64;
begin
Result := FValues(Column);
end;
////////////////////////////////////////////////////////////////////////////////
Procedure TVirtualMatrixRows.Init(Count,Size: Integer);
begin
FCount := Count;
FSize := Size;
end;
Function TVirtualMatrixRows.DoGetValues(Matrix,Column: Integer): Float64;
begin
if (Matrix < FCount) and (Column < FSize) then
begin
Result := GetValues(Matrix,Column);
if Abs(Result) < RoundToZeroThreshold then Result := 0.0
end else
Result := 0.0;
end;
Procedure TVirtualMatrixRows.GetRow(Matrix: Integer; var Row: TFloat32MatrixRow);
begin
if FSize < Length(Row) then
begin
for var Column := 0 to FSize-1 do Row[Column] := DoGetValues(Matrix,Column);
for var Column := FSize to Length(Row)-1 do Row[Column] := 0.0;
end else
for var Column := 0 to Length(Row)-1 do Row[Column] := DoGetValues(Matrix,Column);
end;
Procedure TVirtualMatrixRows.GetRow(Matrix: Integer; var Row: TFloat64MatrixRow);
begin
if FSize < Length(Row) then
begin
for var Column := 0 to FSize-1 do Row[Column] := DoGetValues(Matrix,Column);
for var Column := FSize to Length(Row)-1 do Row[Column] := 0.0;
end else
for var Column := 0 to Length(Row)-1 do Row[Column] := DoGetValues(Matrix,Column);
end;
Function TVirtualMatrixRows.Total: Float64;
begin
Result := 0.0;
for var Matrix := 0 to FCount-1 do Result := Result + Total(Matrix);
end;
Function TVirtualMatrixRows.Total(Matrix: Integer): Float64;
begin
Result := 0.0;
for var Column := 0 to FSize-1 do Result := Result + GetValues(Matrix,Column);
end;
////////////////////////////////////////////////////////////////////////////////
Procedure TCustomMatrixRows.DoSetValues(Matrix,Column: Integer; Value: Float64);
begin
if TargetMatrices.Length = 0 then
begin
if (Matrix < FCount) and (Column < FSize) then SetValues(Matrix,Column,Value);
end else
if Matrix < TargetMatrices.Length then
begin
Matrix := TargetMatrices[Matrix];
if (Matrix >= 0) and (Matrix < FCount) and (Column < FSize) then SetValues(Matrix,Column,Value);
end;
end;
Procedure TCustomMatrixRows.Initialize(Value: Float64 = 0.0);
begin
for var Matrix := 0 to FCount-1 do
for var Column := 0 to FSize-1 do
SetValues(Matrix,Column,Value);
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TFloat64MatrixRows.Create;
begin
inherited Create;
end;
Constructor TFloat64MatrixRows.Create(Count,Size: Integer);
begin
inherited Create;
Allocate(Count,Size);
end;
Function TFloat64MatrixRows.GetValues(Matrix,Column: Integer): Float64;
begin
Result := FValues[Matrix,Column];
end;
Procedure TFloat64MatrixRows.SetValues(Matrix,Column: Integer; Value: Float64);
begin
FValues[Matrix,Column] := Value
end;
Procedure TFloat64MatrixRows.Allocate(Count,Size: Integer);
begin
Init(Count,Size);
SetLength(FValues,Count,Size);
end;
Function TFloat64MatrixRows.RowValues(Matrix: Integer): TFloat64ArrayValues;
begin
Result := TFloat64ArrayValues.Create(FValues[Matrix])
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TFloat32MatrixRows.Create;
begin
inherited Create;
end;
Constructor TFloat32MatrixRows.Create(Count,Size: Integer);
begin
inherited Create;
Allocate(Count,Size);
end;
Function TFloat32MatrixRows.GetValues(Matrix,Column: Integer): Float64;
begin
Result := FValues[Matrix,Column];
end;
Procedure TFloat32MatrixRows.SetValues(Matrix,Column: Integer; Value: Float64);
begin
FValues[Matrix,Column] := Value
end;
Procedure TFloat32MatrixRows.Allocate(Count,Size: Integer);
begin
Init(Count,Size);
SetLength(FValues,Count,Size);
end;
Function TFloat32MatrixRows.RowValues(Matrix: Integer): TFloat32ArrayValues;
begin
Result := TFloat32ArrayValues.Create(FValues[Matrix])
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TMatrixIterator.Create(Count,Size: Integer);
begin
inherited Create;
FCount := Count;
FSize := Size;
Matrices := TFloat64MatrixRows.Create(Count,0);
Matrices.FSize := Size;
end;
Procedure TMatrixIterator.Yield(const Row: TMatrixRow);
begin
if Length(Row) = FSize then
begin
Matrices.FValues[Index] := Row;
Inc(Index);
end else
raise Exception.Create('Invalid row size');
end;
Destructor TMatrixIterator.Destroy;
begin
Matrices.Free;
inherited Destroy;
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TMatrixFiler.Create;
begin
inherited Create;
Float64MatrixRows := TFloat64MatrixRows.Create(0,0);
Float32MatrixRows := TFloat32MatrixRows.Create(0,0);
end;
Procedure TMatrixFiler.SetCount(Count: Integer);
begin
FCount := Count;
end;
Procedure TMatrixFiler.SetSize(Size: Integer);
begin
FSize := Size;
end;
Destructor TMatrixFiler.Destroy;
begin
Float64MatrixRows.Free;
Float32MatrixRows.Free;
FileStream.Free;
inherited Destroy;
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TMatrixReader.Create(const FileName: String; const CreateStream: Boolean = true);
begin
inherited Create;
FOrdered := true;
FFileName := ExpandFileName(FileName);
if CreateStream then
FileStream := TBufferedFileStream.Create(FFileName,fmOpenRead or fmShareDenyWrite,BufferSize);
end;
Procedure TMatrixReader.SetCount(Count: Integer);
begin
inherited SetCount(Count);
SetLength(FMatrixLabels,Count);
end;
Procedure TMatrixReader.SetFileLabel(const FileLabel: String);
begin
FFileLabel := FileLabel;
end;
Function TMatrixReader.GetMatrixLabels(Mtrx: Integer): String;
begin
Result := FMatrixLabels[Mtrx];
end;
Procedure TMatrixReader.SetMatrixLabels(const Matrix: Integer; const MatrixLabel: String);
begin
FMatrixLabels[Matrix] := MatrixLabel;
end;
Function TMatrixReader.GetMatrix(const MatrixLabel: string): Integer;
begin
Result := -1;
for var Matrix := 0 to FCount-1 do
if SameText(FMatrixLabels[Matrix],MatrixLabel) then Exit(Matrix);
end;
Function TMatrixReader.MatrixLabelsArray: TStringDynArray;
begin
Result := Copy(FMatrixLabels);
end;
Procedure TMatrixReader.Read(const Row: TFloat64MatrixRow);
begin
Float64MatrixRows.FCount := 1;
Float64MatrixRows.FSize := Length(Row);
SetLength(Float64MatrixRows.FValues,1);
Float64MatrixRows.FValues[0] := Row;
Read(Float64MatrixRows);
end;
Procedure TMatrixReader.Read(const Row: TFloat32MatrixRow);
begin
Float32MatrixRows.FCount := 1;
Float32MatrixRows.FSize := Length(Row);
SetLength(Float32MatrixRows.FValues,1);
Float32MatrixRows.FValues[0] := Row;
Read(Float32MatrixRows);
end;
Procedure TMatrixReader.Read(const Rows: array of TFloat64MatrixRow);
begin
Float64MatrixRows.FCount := Length(Rows);
Float64MatrixRows.FSize := Length(Rows[0]);
SetLength(Float64MatrixRows.FValues,Length(Rows));
for var Matrix := low(Rows) to high(Rows) do
if Length(Rows[Matrix]) = Float64MatrixRows.FSize then
Float64MatrixRows.FValues[Matrix] := Rows[Matrix]
else
raise Exception.Create('Matrix rows must have the same size');
Read(Float64MatrixRows);
end;
Procedure TMatrixReader.Read(const Rows: array of TFloat32MatrixRow);
begin
Float32MatrixRows.FCount := Length(Rows);
Float32MatrixRows.FSize := Length(Rows[0]);
SetLength(Float32MatrixRows.FValues,Length(Rows));
for var Matrix := low(Rows) to high(Rows) do
if Length(Rows[Matrix]) = Float32MatrixRows.FSize then
Float32MatrixRows.FValues[Matrix] := Rows[Matrix]
else
raise Exception.Create('Matrix rows must have the same size');
Read(Float32MatrixRows);
end;
Procedure TMatrixReader.Read(const Rows: TCustomMatrixRows);
begin
if Assigned(Rows) then
begin
try
Read(CurrentRow,Rows);
except
on E: Exception do
raise Exception.Create('Error (' + E.Message + ') reading row ' +
(CurrentRow+1).ToString + ' in ' + FFileName);
end;
// Zeroize values not read from file
if Size < Rows.Size then
for var Matrix := 0 to Rows.Count-1 do
for var Column := Size to Rows.Size-1 do
Rows[Matrix,Column] := 0.0;
if FCount < Rows.Count then
for var Matrix := FCount to Rows.Count-1 do
for var Column := 0 to Rows.Size-1 do
Rows[Matrix,Column] := 0.0;
Inc(CurrentRow);
end else
raise Exception.Create('Rows unassigned');
end;
Procedure TMatrixReader.Read(const Rows: TMatrixIterator);
begin
Rows.Index := 0;
Rows.Iterate;
if Rows.Index = Rows.FCount then
Read(Rows.Matrices)
else
raise Exception.Create('Invalid iteration count');
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TMaskedMatrixReader.Create(const Reader: TMatrixReader; const Selection: array of Integer);
begin
if Length(Selection) > 0 then
begin
inherited Create(Reader.FileName,false);
// Copy file properties
FFileLabel := Reader.FFileLabel;
SetSize(Reader.Size);
// Set target matrices
var Nmatrices := 0;
for var Selected in Selection do
begin
if Selected >= Nmatrices then
begin
TargetMatrices.Length := Selected+1;
for var Matrix := Nmatrices to Selected do TargetMatrices[Matrix] := -1;
Nmatrices := TargetMatrices.Length;
end;
if TargetMatrices[Selected] < 0 then
begin
TargetMatrices[Selected] := FCount;
SetCount(FCount+1);
SetMatrixLabels(FCount-1,Reader.MatrixLabels[Selected]);
end else
raise Exception.Create('Matrix ' + Selected.ToString + ' selected multiple times');
end;
// Set unmasked reader
Unmasked := Reader;
end else
raise Exception.Create('Empty selection');
end;
Constructor TMaskedMatrixReader.Create(const Reader: TMatrixReader; const Selection: array of String);
begin
if Length(Selection) > 0 then
begin
inherited Create(Reader.FileName,false);
// Copy file properties
FFileLabel := Reader.FFileLabel;
SetSize(Reader.Size);
// Set target matrices
var Nmatrices := 0;
for var MatrixLabel in Selection do
begin
var Selected := Reader.GetMatrix(MatrixLabel);
if Selected >= 0 then
begin
if Selected >= Nmatrices then
begin
TargetMatrices.Length := Selected+1;
for var Matrix := Nmatrices to Selected do TargetMatrices[Matrix] := -1;
Nmatrices := TargetMatrices.Length;
end;
if TargetMatrices[Selected] < 0 then
begin
TargetMatrices[Selected] := FCount;
SetCount(FCount+1);
SetMatrixLabels(FCount-1,Reader.MatrixLabels[Selected]);
end else
raise Exception.Create('Matrix ' + Selected.ToString + ' selected multiple times');
end else
raise Exception.Create('Matrix ' + MatrixLabel + ' does not exist');
end;
// Set unmasked reader
Unmasked := Reader;
end else
raise Exception.Create('Empty selection');
end;
Procedure TMaskedMatrixReader.Read(const CurrentRow: Integer; const Rows: TCustomMatrixRows);
begin
Rows.TargetMatrices := TargetMatrices;
try
Unmasked.Read(CurrentRow,Rows);
SetCount(Unmasked.Count);
SetSize(Unmasked.Size);
finally
Rows.TargetMatrices := nil;
end;
end;
Destructor TMaskedMatrixReader.Destroy;
begin
Unmasked.Free;
inherited Destroy;
end;
////////////////////////////////////////////////////////////////////////////////
Function TMatrixWriter.TMatrixRows.GetValues(Matrix: Integer; Column: Integer): Float64;
begin
Result := Values[Matrix].Values[Column]
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TMatrixWriter.Create(const FileName: String;
const Count,Size: Integer;
const CreateStream: Boolean = true);
begin
inherited Create;
FFileName := ExpandFileName(FileName);
MatrixRows := TMatrixRows.Create;
MatrixRows.FSize := Size;
if CreateStream then
FileStream := TBufferedFileStream.Create(FFileName,fmCreate or fmShareDenyWrite,BufferSize);
SetCount(Count);
SetSize(Size);
end;
Procedure TMatrixWriter.Write(const Row: TVirtualMatrixRow);
begin
MatrixRows.FCount := 1;
MatrixRows.FSize := FSize;
SetLength(MatrixRows.Values,1);
MatrixRows.Values[0] := Row;
Write(MatrixRows);
end;
Procedure TMatrixWriter.Write(const Row: TFloat64MatrixRow);
begin
Float64MatrixRows.FCount := 1;
Float64MatrixRows.FSize := Length(Row);
SetLength(Float64MatrixRows.FValues,1);
Float64MatrixRows.FValues[0] := Row;
Write(Float64MatrixRows);
end;
Procedure TMatrixWriter.Write(const Row: TFloat32MatrixRow);
begin
Float32MatrixRows.FCount := 1;
Float32MatrixRows.FSize := Length(Row);
SetLength(Float32MatrixRows.FValues,1);
Float32MatrixRows.FValues[0] := Row;
Write(Float32MatrixRows);
end;
Procedure TMatrixWriter.Write(const Rows: array of TVirtualMatrixRow);
begin
MatrixRows.FCount := Length(Rows);
MatrixRows.FSize := Rows[0].FSize;
SetLength(MatrixRows.Values,Length(Rows));
for var Matrix := low(Rows) to high(Rows) do
if Rows[Matrix].FSize = MatrixRows.FSize then
MatrixRows.Values[Matrix] := Rows[Matrix]
else
raise Exception.Create('Matrix rows must have the same size');
Write(MatrixRows);
end;
Procedure TMatrixWriter.Write(const Rows: array of TFloat64MatrixRow);
begin
Float64MatrixRows.FCount := Length(Rows);
Float64MatrixRows.FSize := Length(Rows[0]);
SetLength(Float64MatrixRows.FValues,Length(Rows));
for var Matrix := low(Rows) to high(Rows) do
if Length(Rows[Matrix]) = Float64MatrixRows.FSize then
Float64MatrixRows.FValues[Matrix] := Rows[Matrix]
else
raise Exception.Create('Matrix rows must have the same size');
Write(Float64MatrixRows);
end;
Procedure TMatrixWriter.Write(const Rows: array of TFloat32MatrixRow);
begin
Float32MatrixRows.FCount := Length(Rows);
Float32MatrixRows.FSize := Length(Rows[0]);
SetLength(Float32MatrixRows.FValues,Length(Rows));
for var Matrix := low(Rows) to high(Rows) do
if Length(Rows[Matrix]) = Float32MatrixRows.FSize then
Float32MatrixRows.FValues[Matrix] := Rows[Matrix]
else
raise Exception.Create('Matrix rows must have the same size');
Write(Float32MatrixRows);
end;
Procedure TMatrixWriter.Write(const Rows: TVirtualMatrixRows);
begin
if Assigned(Rows) then
if CurrentRow < Size then
begin
try
Rows.RoundToZeroThreshold := RoundToZeroThreshold;
try
Write(CurrentRow,Rows);
finally
Rows.RoundToZeroThreshold := 0.0;
end;
except
on E: Exception do
raise Exception.Create('Error (' + E.Message + ') writing row ' +
(CurrentRow+1).ToString + ' in ' + FFileName);
end;
Inc(CurrentRow);
end else
raise Exception.Create('Writing too many rows to matrix file')
else raise Exception.Create('Rows unassigned');
end;
Procedure TMatrixWriter.Write(const Rows: TMatrixIterator);
begin
Rows.Index := 0;
Rows.Iterate;
if Rows.Index = Rows.FCount then
write(Rows.Matrices)
else
raise Exception.Create('Invalid iteration count');
end;
Destructor TMatrixWriter.Destroy;
begin
MatrixRows.Free;
inherited Destroy;
end;
end.