0
# Mathematical Operations
1
2
ITK provides comprehensive mathematical algorithms including optimization, statistics, numerical analysis, and linear algebra operations. These components support advanced image analysis, registration, and machine learning applications.
3
4
## Optimization Framework
5
6
### Optimizer
7
8
Base class for all optimization algorithms.
9
10
```cpp { .api }
11
class Optimizer : public Object
12
{
13
public:
14
typedef Optimizer Self;
15
typedef Object Superclass;
16
typedef SmartPointer<Self> Pointer;
17
typedef SmartPointer<const Self> ConstPointer;
18
19
// Parameters
20
typedef Array<double> ParametersType;
21
typedef Array<double> ScalesType;
22
23
// Optimization control
24
virtual void StartOptimization() = 0;
25
virtual void StopOptimization();
26
virtual bool CanUseScales() const;
27
28
// Parameters
29
virtual void SetInitialPosition(const ParametersType & param);
30
itkGetConstReferenceMacro(InitialPosition, ParametersType);
31
virtual const ParametersType & GetCurrentPosition() const;
32
33
// Scaling
34
void SetScales(const ScalesType & scales);
35
const ScalesType & GetScales() const;
36
const ScalesType & GetInverseScales() const;
37
38
// Observer pattern
39
void AddObserver(const EventObject & event, Command * command);
40
41
protected:
42
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
43
};
44
```
45
46
### SingleValuedNonLinearOptimizer
47
48
Base class for single-valued cost function optimizers.
49
50
```cpp { .api }
51
class SingleValuedNonLinearOptimizer : public Optimizer
52
{
53
public:
54
typedef SingleValuedNonLinearOptimizer Self;
55
typedef Optimizer Superclass;
56
typedef SmartPointer<Self> Pointer;
57
typedef SmartPointer<const Self> ConstPointer;
58
59
// Cost function types
60
typedef SingleValuedCostFunction CostFunctionType;
61
typedef CostFunctionType::Pointer CostFunctionPointer;
62
typedef CostFunctionType::MeasureType MeasureType;
63
typedef CostFunctionType::DerivativeType DerivativeType;
64
65
// Cost function
66
virtual void SetCostFunction(CostFunctionType * costFunction);
67
itkGetObjectMacro(CostFunction, CostFunctionType);
68
69
// Results
70
itkGetConstReferenceMacro(Value, MeasureType);
71
itkGetConstReferenceMacro(Gradient, DerivativeType);
72
73
// Optimization status
74
virtual const std::string GetStopConditionDescription() const;
75
76
protected:
77
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
78
};
79
```
80
81
### GradientDescentOptimizer
82
83
Gradient descent optimization algorithm.
84
85
```cpp { .api }
86
class GradientDescentOptimizer : public SingleValuedNonLinearOptimizer
87
{
88
public:
89
typedef GradientDescentOptimizer Self;
90
typedef SingleValuedNonLinearOptimizer Superclass;
91
typedef SmartPointer<Self> Pointer;
92
typedef SmartPointer<const Self> ConstPointer;
93
94
static Pointer New();
95
96
// Optimization parameters
97
void SetLearningRate(double learningRate);
98
itkGetConstMacro(LearningRate, double);
99
100
void SetNumberOfIterations(unsigned long numberOfIterations);
101
itkGetConstMacro(NumberOfIterations, unsigned long);
102
103
// Current iteration
104
itkGetConstMacro(CurrentIteration, unsigned int);
105
106
// Convergence monitoring
107
void SetMaximize(bool maximize);
108
itkGetConstMacro(Maximize, bool);
109
void MaximizeOn() { SetMaximize(true); }
110
void MaximizeOff() { SetMaximize(false); }
111
112
// Optimization execution
113
virtual void StartOptimization() ITK_OVERRIDE;
114
virtual void ResumeOptimization();
115
virtual void StopOptimization() ITK_OVERRIDE;
116
117
// Results
118
virtual const std::string GetStopConditionDescription() const ITK_OVERRIDE;
119
120
protected:
121
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
122
virtual void AdvanceOneStep();
123
};
124
```
125
126
### RegularStepGradientDescentOptimizer
127
128
Gradient descent with adaptive step size.
129
130
```cpp { .api }
131
class RegularStepGradientDescentOptimizer : public SingleValuedNonLinearOptimizer
132
{
133
public:
134
typedef RegularStepGradientDescentOptimizer Self;
135
typedef SingleValuedNonLinearOptimizer Superclass;
136
typedef SmartPointer<Self> Pointer;
137
typedef SmartPointer<const Self> ConstPointer;
138
139
static Pointer New();
140
141
// Step size control
142
void SetMaximumStepLength(double maximumStepLength);
143
void SetMinimumStepLength(double minimumStepLength);
144
itkGetConstMacro(MaximumStepLength, double);
145
itkGetConstMacro(MinimumStepLength, double);
146
147
// Current step length
148
itkGetConstMacro(CurrentStepLength, double);
149
150
// Relaxation factor
151
void SetRelaxationFactor(double relaxationFactor);
152
itkGetConstMacro(RelaxationFactor, double);
153
154
// Gradient magnitude tolerance
155
void SetGradientMagnitudeTolerance(double gradientMagnitudeTolerance);
156
itkGetConstMacro(GradientMagnitudeTolerance, double);
157
158
// Iteration control
159
void SetNumberOfIterations(unsigned long numberOfIterations);
160
itkGetConstMacro(NumberOfIterations, unsigned long);
161
itkGetConstMacro(CurrentIteration, unsigned int);
162
163
// Convergence value
164
void SetValue(double value);
165
itkGetConstMacro(Value, double);
166
167
// Maximize/minimize
168
void SetMaximize(bool maximize);
169
itkGetConstMacro(Maximize, bool);
170
void MaximizeOn() { SetMaximize(true); }
171
void MaximizeOff() { SetMaximize(false); }
172
173
// Optimization execution
174
virtual void StartOptimization() ITK_OVERRIDE;
175
virtual void ResumeOptimization();
176
virtual void StopOptimization() ITK_OVERRIDE;
177
178
// Results
179
virtual const std::string GetStopConditionDescription() const ITK_OVERRIDE;
180
181
protected:
182
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
183
};
184
```
185
186
### LBFGSOptimizer
187
188
Limited-memory BFGS quasi-Newton optimization.
189
190
```cpp { .api }
191
class LBFGSOptimizer : public SingleValuedNonLinearOptimizer
192
{
193
public:
194
typedef LBFGSOptimizer Self;
195
typedef SingleValuedNonLinearOptimizer Superclass;
196
typedef SmartPointer<Self> Pointer;
197
typedef SmartPointer<const Self> ConstPointer;
198
199
static Pointer New();
200
201
// LBFGS parameters
202
void SetTrace(bool trace);
203
itkGetConstMacro(Trace, bool);
204
205
void SetMaximumNumberOfFunctionEvaluations(unsigned int maximumNumberOfFunctionEvaluations);
206
itkGetConstMacro(MaximumNumberOfFunctionEvaluations, unsigned int);
207
208
void SetGradientConvergenceTolerance(double gradientConvergenceTolerance);
209
itkGetConstMacro(GradientConvergenceTolerance, double);
210
211
void SetLineSearchAccuracy(double lineSearchAccuracy);
212
itkGetConstMacro(LineSearchAccuracy, double);
213
214
void SetDefaultStepLength(double defaultStepLength);
215
itkGetConstMacro(DefaultStepLength, double);
216
217
// Optimization execution
218
virtual void StartOptimization() ITK_OVERRIDE;
219
220
// Results
221
virtual const std::string GetStopConditionDescription() const ITK_OVERRIDE;
222
223
// Memory size
224
void SetMaximumNumberOfCorrections(unsigned int maximumNumberOfCorrections);
225
itkGetConstMacro(MaximumNumberOfCorrections, unsigned int);
226
227
protected:
228
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
229
};
230
```
231
232
### AmoebaOptimizer
233
234
Nelder-Mead simplex (Amoeba) optimization.
235
236
```cpp { .api }
237
class AmoebaOptimizer : public SingleValuedNonLinearOptimizer
238
{
239
public:
240
typedef AmoebaOptimizer Self;
241
typedef SingleValuedNonLinearOptimizer Superclass;
242
typedef SmartPointer<Self> Pointer;
243
typedef SmartPointer<const Self> ConstPointer;
244
245
static Pointer New();
246
247
// Simplex parameters
248
void SetMaximumNumberOfIterations(unsigned int maximumNumberOfIterations);
249
itkGetConstMacro(MaximumNumberOfIterations, unsigned int);
250
251
void SetAutomaticInitialSimplex(bool automaticInitialSimples);
252
itkGetConstMacro(AutomaticInitialSimplex, bool);
253
254
void SetOptimizeWithRestarts(bool optimizeWithRestarts);
255
itkGetConstMacro(OptimizeWithRestarts, bool);
256
257
// Tolerance settings
258
void SetParametersConvergenceTolerance(double parametersConvergenceTolerance);
259
itkGetConstMacro(ParametersConvergenceTolerance, double);
260
261
void SetFunctionConvergenceTolerance(double functionConvergenceTolerance);
262
itkGetConstMacro(FunctionConvergenceTolerance, double);
263
264
// Initial simplex
265
void SetInitialSimplexDelta(ParametersType initialSimplexDelta, bool automaticInitialSimplex = false);
266
itkGetConstMacro(InitialSimplexDelta, ParametersType);
267
268
// Optimization execution
269
virtual void StartOptimization() ITK_OVERRIDE;
270
271
// Results
272
virtual const std::string GetStopConditionDescription() const ITK_OVERRIDE;
273
274
protected:
275
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
276
};
277
```
278
279
## Statistics Framework
280
281
### Sample
282
283
Base interface for statistical samples.
284
285
```cpp { .api }
286
template<typename TMeasurementVector>
287
class Sample : public DataObject
288
{
289
public:
290
typedef Sample Self;
291
typedef DataObject Superclass;
292
typedef SmartPointer<Self> Pointer;
293
typedef SmartPointer<const Self> ConstPointer;
294
295
// Measurement vector types
296
typedef TMeasurementVector MeasurementVectorType;
297
typedef typename MeasurementVectorType::ValueType MeasurementType;
298
299
// Sample size
300
virtual InstanceIdentifier Size() const = 0;
301
virtual unsigned int GetMeasurementVectorSize() const = 0;
302
303
// Data access
304
virtual const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const = 0;
305
virtual MeasurementType GetMeasurement(InstanceIdentifier id, unsigned int dimension);
306
307
// Frequency information
308
virtual AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const = 0;
309
virtual TotalAbsoluteFrequencyType GetTotalFrequency() const = 0;
310
311
// Iteration support
312
virtual void Graft(const DataObject * thatObject) ITK_OVERRIDE;
313
314
protected:
315
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
316
};
317
```
318
319
### ListSample
320
321
Sample implementation using STL list container.
322
323
```cpp { .api }
324
template<typename TMeasurementVector>
325
class ListSample : public Sample<TMeasurementVector>
326
{
327
public:
328
typedef ListSample Self;
329
typedef Sample<TMeasurementVector> Superclass;
330
typedef SmartPointer<Self> Pointer;
331
typedef SmartPointer<const Self> ConstPointer;
332
333
static Pointer New();
334
335
// Container types
336
typedef std::vector<MeasurementVectorType> InternalDataContainerType;
337
typedef std::vector<AbsoluteFrequencyType> AbsoluteFrequencyContainerType;
338
339
// Sample modification
340
void PushBack(const MeasurementVectorType & measurementVector);
341
void Resize(InstanceIdentifier newSize);
342
void Clear();
343
344
// Sample size
345
virtual InstanceIdentifier Size() const ITK_OVERRIDE;
346
virtual unsigned int GetMeasurementVectorSize() const ITK_OVERRIDE;
347
void SetMeasurementVectorSize(unsigned int s);
348
349
// Data access
350
virtual const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const ITK_OVERRIDE;
351
void SetMeasurementVector(InstanceIdentifier id, const MeasurementVectorType & measurementVector);
352
void SetMeasurement(InstanceIdentifier id, unsigned int dimension, const MeasurementType & value);
353
354
// Frequency information
355
virtual AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const ITK_OVERRIDE;
356
void SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType frequency);
357
virtual TotalAbsoluteFrequencyType GetTotalFrequency() const ITK_OVERRIDE;
358
359
// Iteration
360
class Iterator;
361
class ConstIterator;
362
Iterator Begin();
363
Iterator End();
364
ConstIterator Begin() const;
365
ConstIterator End() const;
366
367
protected:
368
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
369
};
370
```
371
372
### Histogram
373
374
Multi-dimensional histogram for statistical analysis.
375
376
```cpp { .api }
377
template<typename TMeasurement, unsigned int VMeasurementVectorSize>
378
class Histogram : public Sample<FixedArray<TMeasurement, VMeasurementVectorSize>>
379
{
380
public:
381
typedef Histogram Self;
382
typedef Sample<FixedArray<TMeasurement, VMeasurementVectorSize>> Superclass;
383
typedef SmartPointer<Self> Pointer;
384
typedef SmartPointer<const Self> ConstPointer;
385
386
static Pointer New();
387
388
// Histogram bin types
389
typedef typename Superclass::MeasurementVectorType MeasurementVectorType;
390
typedef typename Superclass::MeasurementType MeasurementType;
391
typedef typename Superclass::InstanceIdentifier InstanceIdentifier;
392
typedef typename Superclass::AbsoluteFrequencyType AbsoluteFrequencyType;
393
typedef typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
394
395
typedef MeasurementVectorType BinMinVectorType;
396
typedef MeasurementVectorType BinMaxVectorType;
397
typedef std::vector<MeasurementType> BinMinContainerType;
398
typedef std::vector<MeasurementType> BinMaxContainerType;
399
400
// Initialization
401
void Initialize(const SizeType & size);
402
void Initialize(const SizeType & size, const BinMinVectorType & lowerBound, const BinMaxVectorType & upperBound);
403
404
// Size information
405
virtual InstanceIdentifier Size() const ITK_OVERRIDE;
406
SizeType GetSize() const;
407
SizeValueType GetSize(unsigned int dimension) const;
408
409
// Bin boundaries
410
const BinMinVectorType & GetBinMin(unsigned int dimension, InstanceIdentifier binIndex) const;
411
const BinMaxVectorType & GetBinMax(unsigned int dimension, InstanceIdentifier binIndex) const;
412
413
// Frequency access
414
virtual AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const ITK_OVERRIDE;
415
bool SetFrequency(InstanceIdentifier id, AbsoluteFrequencyType frequency);
416
bool IncreaseFrequency(InstanceIdentifier id, AbsoluteFrequencyType frequency);
417
418
// Bin index calculations
419
bool GetIndex(const MeasurementVectorType & measurement, IndexType & index) const;
420
InstanceIdentifier GetInstanceIdentifier(const IndexType & index) const;
421
422
// Statistics
423
double GetMean(unsigned int dimension) const;
424
double GetVariance(unsigned int dimension) const;
425
426
// Quantiles
427
double Quantile(unsigned int dimension, double percent) const;
428
429
// Iteration
430
class Iterator;
431
class ConstIterator;
432
Iterator Begin();
433
Iterator End();
434
ConstIterator Begin() const;
435
ConstIterator End() const;
436
437
protected:
438
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
439
};
440
```
441
442
### MembershipFunction
443
444
Base class for membership functions in statistical classification.
445
446
```cpp { .api }
447
template<typename TVector>
448
class MembershipFunction : public FunctionBase<TVector, double>
449
{
450
public:
451
typedef MembershipFunction Self;
452
typedef FunctionBase<TVector, double> Superclass;
453
typedef SmartPointer<Self> Pointer;
454
typedef SmartPointer<const Self> ConstPointer;
455
456
// Function evaluation
457
virtual double Evaluate(const MeasurementVectorType & measurement) const = 0;
458
459
// Measurement vector dimension
460
void SetMeasurementVectorSize(unsigned int measurementVectorSize);
461
unsigned int GetMeasurementVectorSize() const;
462
463
protected:
464
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
465
};
466
```
467
468
### GaussianMembershipFunction
469
470
Multivariate Gaussian membership function.
471
472
```cpp { .api }
473
template<typename TVector>
474
class GaussianMembershipFunction : public MembershipFunction<TVector>
475
{
476
public:
477
typedef GaussianMembershipFunction Self;
478
typedef MembershipFunction<TVector> Superclass;
479
typedef SmartPointer<Self> Pointer;
480
typedef SmartPointer<const Self> ConstPointer;
481
482
static Pointer New();
483
484
// Parameters
485
typedef typename Superclass::MeasurementVectorType MeasurementVectorType;
486
typedef vnl_vector<double> MeanVectorType;
487
typedef vnl_matrix<double> CovarianceMatrixType;
488
489
// Mean vector
490
void SetMean(const MeanVectorType & mean);
491
void SetMean(const MeasurementVectorType & mean);
492
itkGetConstReferenceMacro(Mean, MeanVectorType);
493
494
// Covariance matrix
495
void SetCovariance(const CovarianceMatrixType & covariance);
496
itkGetConstReferenceMacro(Covariance, CovarianceMatrixType);
497
498
// Inverse covariance matrix
499
void SetInverseCovariance(const CovarianceMatrixType & inverseCovariance);
500
itkGetConstReferenceMacro(InverseCovariance, CovarianceMatrixType);
501
502
// Function evaluation
503
virtual double Evaluate(const MeasurementVectorType & measurement) const ITK_OVERRIDE;
504
505
protected:
506
virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
507
void CalculateInverseCovariance();
508
};
509
```
510
511
## Linear Algebra Operations
512
513
### Matrix
514
515
Basic matrix operations.
516
517
```cpp { .api }
518
template<typename T, unsigned int NRows, unsigned int NColumns>
519
class Matrix : public FixedArray<T, NRows * NColumns>
520
{
521
public:
522
typedef Matrix Self;
523
typedef FixedArray<T, NRows * NColumns> Superclass;
524
525
// Constructors
526
Matrix();
527
Matrix(const T & value);
528
Matrix(const T matrix[NRows][NColumns]);
529
Matrix(const InternalMatrixType & matrix);
530
531
// Element access
532
T & operator()(unsigned int row, unsigned int col);
533
const T & operator()(unsigned int row, unsigned int col) const;
534
T * operator[](unsigned int i);
535
const T * operator[](unsigned int i) const;
536
537
// Matrix operations
538
Self operator+(const Self & matrix) const;
539
Self operator-(const Self & matrix) const;
540
Self & operator+=(const Self & matrix);
541
Self & operator-=(const Self & matrix);
542
Self & operator*=(const T & value);
543
Self & operator/=(const T & value);
544
Self operator*(const T & value) const;
545
Self operator/(const T & value) const;
546
547
// Matrix multiplication
548
template<unsigned int NColumns2>
549
Matrix<T, NRows, NColumns2> operator*(const Matrix<T, NColumns, NColumns2> & matrix) const;
550
551
// Vector multiplication
552
Vector<T, NRows> operator*(const Vector<T, NColumns> & vector) const;
553
554
// Matrix properties
555
vnl_matrix<T> GetVnlMatrix() const;
556
void SetVnlMatrix(const vnl_matrix<T> & matrix);
557
558
// Matrix operations
559
vnl_matrix<T> GetInverse() const;
560
vnl_matrix<T> GetTranspose() const;
561
T GetTrace() const;
562
T GetDeterminant() const;
563
564
// Utilities
565
void SetIdentity();
566
void Fill(const T & value);
567
568
// Dimension information
569
static unsigned int GetNumberOfRows() { return NRows; }
570
static unsigned int GetNumberOfColumns() { return NColumns; }
571
};
572
```
573
574
### SymmetricSecondRankTensor
575
576
Symmetric tensor representation.
577
578
```cpp { .api }
579
template<typename TComponent, unsigned int NDimension>
580
class SymmetricSecondRankTensor : public FixedArray<TComponent, NDimension * (NDimension + 1) / 2>
581
{
582
public:
583
typedef SymmetricSecondRankTensor Self;
584
typedef FixedArray<TComponent, NDimension * (NDimension + 1) / 2> Superclass;
585
586
// Constructors
587
SymmetricSecondRankTensor();
588
SymmetricSecondRankTensor(const ComponentType & r);
589
SymmetricSecondRankTensor(const ComponentArrayType & r);
590
SymmetricSecondRankTensor(const InternalMatrixType & matrix);
591
592
// Element access
593
ComponentType & operator()(unsigned int row, unsigned int col);
594
const ComponentType & operator()(unsigned int row, unsigned int col) const;
595
void SetComponent(unsigned int row, unsigned int col, const ComponentType & value);
596
ComponentType GetComponent(unsigned int row, unsigned int col) const;
597
598
// Matrix conversion
599
void SetVnlMatrix(const vnl_matrix<ComponentType> & matrix);
600
vnl_matrix<ComponentType> GetVnlMatrix() const;
601
602
// Tensor operations
603
Self operator+(const Self & tensor) const;
604
Self operator-(const Self & tensor) const;
605
Self & operator+=(const Self & tensor);
606
Self & operator-=(const Self & tensor);
607
Self operator*(const RealValueType & scalar) const;
608
Self operator/(const RealValueType & scalar) const;
609
Self & operator*=(const RealValueType & scalar);
610
Self & operator/=(const RealValueType & scalar);
611
612
// Eigenvalues and eigenvectors
613
void ComputeEigenValues(EigenValuesArrayType & eigenValues) const;
614
void ComputeEigenAnalysis(EigenValuesArrayType & eigenValues, EigenVectorsMatrixType & eigenVectors) const;
615
616
// Tensor invariants
617
AccumulateType GetTrace() const;
618
RealValueType GetNorm() const;
619
RealValueType GetSquaredNorm() const;
620
621
// Tensor properties
622
void SetIdentity();
623
624
// Dimension information
625
static unsigned int GetNumberOfComponents() { return InternalDimension; }
626
627
protected:
628
void PrintSelf(std::ostream & os, Indent indent) const;
629
};
630
```
631
632
## Numerical Utilities
633
634
### NumericTraits
635
636
Type traits for numerical computations.
637
638
```cpp { .api }
639
template<typename T>
640
class NumericTraits
641
{
642
public:
643
typedef T ValueType;
644
typedef T PrintType;
645
typedef T AbsType;
646
typedef double AccumulateType;
647
typedef double RealType;
648
typedef RealType ScalarRealType;
649
typedef float FloatType;
650
651
// Constants
652
static const T Zero;
653
static const T One;
654
static const T NonpositiveMin();
655
static const T min();
656
static const T max();
657
658
// Type information
659
static T ZeroValue();
660
static T OneValue();
661
static unsigned int GetLength(const T &);
662
static unsigned int GetLength();
663
664
// Comparison operations
665
static bool IsPositive(T val);
666
static bool IsNonpositive(T val);
667
static bool IsNegative(T val);
668
static bool IsNonnegative(T val);
669
670
// Arithmetic operations
671
static T AbsValue(T x);
672
static AccumulateType AccumulateSquare(T x);
673
static AccumulateType Accumulate(T x);
674
static T AssignToValue(const AccumulateType & v);
675
676
// Special values
677
static bool IsInfinite(T val);
678
static bool IsFinite(T val);
679
static bool IsNaN(T val);
680
681
// Limits
682
static T min(T);
683
static T max(T);
684
static T NonpositiveMin(T);
685
static T epsilon();
686
static T infinity();
687
static T quiet_NaN();
688
static T signaling_NaN();
689
};
690
```
691
692
### Math
693
694
Mathematical utilities and constants.
695
696
```cpp { .api }
697
namespace Math
698
{
699
// Mathematical constants
700
static ITK_CONSTEXPR_VAR double pi = 3.14159265358979323846;
701
static ITK_CONSTEXPR_VAR double e = 2.71828182845904523536;
702
static ITK_CONSTEXPR_VAR double log2e = 1.44269504088896340736;
703
static ITK_CONSTEXPR_VAR double log10e = 0.43429448190325182765;
704
static ITK_CONSTEXPR_VAR double ln2 = 0.69314718055994530942;
705
static ITK_CONSTEXPR_VAR double ln10 = 2.30258509299404568402;
706
static ITK_CONSTEXPR_VAR double pi_2 = 1.57079632679489661923;
707
static ITK_CONSTEXPR_VAR double pi_4 = 0.78539816339744830962;
708
static ITK_CONSTEXPR_VAR double one_over_pi = 0.31830988618379067154;
709
static ITK_CONSTEXPR_VAR double one_over_sqrt2pi = 0.39894228040143267794;
710
static ITK_CONSTEXPR_VAR double two_over_pi = 0.63661977236758134308;
711
static ITK_CONSTEXPR_VAR double two_over_sqrtpi = 1.12837916709551257390;
712
static ITK_CONSTEXPR_VAR double sqrt2 = 1.41421356237309504880;
713
static ITK_CONSTEXPR_VAR double sqrt1_2 = 0.70710678118654752440;
714
715
// Utility functions
716
template<typename TReturn, typename TInput>
717
inline TReturn Round(TInput x);
718
719
template<typename TReturn, typename TInput>
720
inline TReturn RoundHalfIntegerToEven(TInput x);
721
722
template<typename TReturn, typename TInput>
723
inline TReturn RoundHalfIntegerUp(TInput x);
724
725
template<typename TReturn, typename TInput>
726
inline TReturn Floor(TInput x);
727
728
template<typename TReturn, typename TInput>
729
inline TReturn Ceil(TInput x);
730
731
template<typename T1, typename T2>
732
inline bool FloatAlmostEqual(T1 x1, T2 x2, typename NumericTraits<T1>::RealType radiusOfTolerance = NumericTraits<T1>::epsilon());
733
734
template<typename T>
735
inline bool FloatDifferenceULP(T1 x1, T2 x2, unsigned int maxUlps = 4);
736
737
// Trigonometric functions
738
inline double DegreesToRadians(double degrees);
739
inline double RadiansToDegrees(double radians);
740
}
741
```
742
743
## Common Usage Patterns
744
745
### Optimization Setup
746
747
```cpp
748
// Define cost function (example: mean squares metric)
749
typedef itk::MeanSquaresImageToImageMetric<FixedImageType, MovingImageType> MetricType;
750
MetricType::Pointer metric = MetricType::New();
751
752
// Configure metric with images and transform
753
metric->SetFixedImage(fixedImage);
754
metric->SetMovingImage(movingImage);
755
metric->SetTransform(transform);
756
metric->SetInterpolator(interpolator);
757
metric->SetFixedImageRegion(fixedImage->GetBufferedRegion());
758
metric->Initialize();
759
760
// Create optimizer
761
typedef itk::RegularStepGradientDescentOptimizer OptimizerType;
762
OptimizerType::Pointer optimizer = OptimizerType::New();
763
764
// Configure optimizer
765
optimizer->SetCostFunction(metric);
766
optimizer->SetMaximumStepLength(4.0);
767
optimizer->SetMinimumStepLength(0.01);
768
optimizer->SetRelaxationFactor(0.9);
769
optimizer->SetNumberOfIterations(200);
770
771
// Set initial parameters
772
OptimizerType::ParametersType initialParameters(transform->GetNumberOfParameters());
773
initialParameters.Fill(0.0);
774
optimizer->SetInitialPosition(initialParameters);
775
776
// Run optimization
777
try {
778
optimizer->StartOptimization();
779
780
OptimizerType::ParametersType finalParameters = optimizer->GetCurrentPosition();
781
double finalValue = optimizer->GetValue();
782
783
std::cout << "Final parameters: " << finalParameters << std::endl;
784
std::cout << "Final value: " << finalValue << std::endl;
785
786
} catch (itk::ExceptionObject & error) {
787
std::cerr << "Optimization failed: " << error << std::endl;
788
}
789
```
790
791
### Statistical Analysis
792
793
```cpp
794
// Create sample data
795
typedef itk::Vector<double, 2> MeasurementVectorType;
796
typedef itk::Statistics::ListSample<MeasurementVectorType> SampleType;
797
SampleType::Pointer sample = SampleType::New();
798
799
// Add measurement vectors
800
MeasurementVectorType measurement;
801
measurement[0] = 1.0; measurement[1] = 2.0;
802
sample->PushBack(measurement);
803
804
measurement[0] = 2.0; measurement[1] = 4.0;
805
sample->PushBack(measurement);
806
807
measurement[0] = 3.0; measurement[1] = 6.0;
808
sample->PushBack(measurement);
809
810
// Create histogram
811
typedef itk::Statistics::Histogram<double, itk::Statistics::DenseFrequencyContainer2> HistogramType;
812
HistogramType::Pointer histogram = HistogramType::New();
813
814
// Initialize histogram
815
HistogramType::SizeType size(2);
816
size[0] = 10; // 10 bins in first dimension
817
size[1] = 10; // 10 bins in second dimension
818
819
HistogramType::MeasurementVectorType lowerBound(2);
820
HistogramType::MeasurementVectorType upperBound(2);
821
lowerBound[0] = 0.0; lowerBound[1] = 0.0;
822
upperBound[0] = 10.0; upperBound[1] = 10.0;
823
824
histogram->Initialize(size, lowerBound, upperBound);
825
826
// Fill histogram from sample
827
for (SampleType::Iterator iter = sample->Begin(); iter != sample->End(); ++iter) {
828
histogram->IncreaseFrequency(iter.GetMeasurementVector(), 1);
829
}
830
831
// Calculate statistics
832
double mean0 = histogram->GetMean(0);
833
double variance0 = histogram->GetVariance(0);
834
double quantile95_0 = histogram->Quantile(0, 0.95);
835
836
std::cout << "Mean (dimension 0): " << mean0 << std::endl;
837
std::cout << "Variance (dimension 0): " << variance0 << std::endl;
838
std::cout << "95th percentile (dimension 0): " << quantile95_0 << std::endl;
839
```
840
841
## Type Definitions
842
843
```cpp { .api }
844
// Common optimizer instantiations
845
typedef RegularStepGradientDescentOptimizer RegularStepGDOptimizer;
846
typedef GradientDescentOptimizer GDOptimizer;
847
typedef LBFGSOptimizer LBFGSOptimizer;
848
typedef AmoebaOptimizer AmoebaOptimizer;
849
850
// Common statistical types
851
typedef Statistics::ListSample<Vector<double, 2>> ListSample2D;
852
typedef Statistics::ListSample<Vector<double, 3>> ListSample3D;
853
typedef Statistics::ListSample<Vector<float, 2>> FloatListSample2D;
854
typedef Statistics::ListSample<Vector<float, 3>> FloatListSample3D;
855
856
typedef Statistics::Histogram<double> Histogram1D;
857
typedef Statistics::Histogram<double, 2> Histogram2D;
858
typedef Statistics::Histogram<double, 3> Histogram3D;
859
860
typedef Statistics::GaussianMembershipFunction<Vector<double, 2>> GaussianMembership2D;
861
typedef Statistics::GaussianMembershipFunction<Vector<double, 3>> GaussianMembership3D;
862
863
// Matrix types
864
typedef Matrix<double, 2, 2> Matrix2x2;
865
typedef Matrix<double, 3, 3> Matrix3x3;
866
typedef Matrix<double, 4, 4> Matrix4x4;
867
typedef Matrix<float, 2, 2> FloatMatrix2x2;
868
typedef Matrix<float, 3, 3> FloatMatrix3x3;
869
typedef Matrix<float, 4, 4> FloatMatrix4x4;
870
871
// Tensor types
872
typedef SymmetricSecondRankTensor<double, 2> Tensor2D;
873
typedef SymmetricSecondRankTensor<double, 3> Tensor3D;
874
typedef SymmetricSecondRankTensor<float, 2> FloatTensor2D;
875
typedef SymmetricSecondRankTensor<float, 3> FloatTensor3D;
876
877
// Common measurement vector types
878
typedef Vector<double, 2> MeasurementVector2D;
879
typedef Vector<double, 3> MeasurementVector3D;
880
typedef Vector<float, 2> FloatMeasurementVector2D;
881
typedef Vector<float, 3> FloatMeasurementVector3D;
882
883
// Statistical identifiers
884
typedef unsigned long InstanceIdentifier;
885
typedef unsigned long AbsoluteFrequencyType;
886
typedef double RelativeFrequencyType;
887
typedef double TotalAbsoluteFrequencyType;
888
typedef double TotalRelativeFrequencyType;
889
890
// Optimization types
891
typedef Array<double> ParametersType;
892
typedef Array<double> DerivativeType;
893
typedef Array<double> ScalesType;
894
typedef double MeasureType;
895
```
896
897
ITK's mathematical operations framework provides comprehensive support for optimization, statistics, and numerical analysis, enabling sophisticated quantitative analysis in medical imaging applications.