MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlLinearAlgebraTools.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_LINEAR_ALGEBRA_TOOLS_H
14#define ML_LINEAR_ALGEBRA_TOOLS_H
15
17
18#include "mlTypeDefTraits.h"
19
20// Include system independent file and project settings.
23#include <MeVisLabVersion.h>
24#include <sstream>
25
26
27// All declarations of this header will be in the ML_LA_NAMESPACE namespace.
29
30#if ML_DEPRECATED_SINCE(4, 2, 0)
31template <typename T> [[deprecated("Use std::swap instead of MLSwap")]]
32void MLSwap(T& obj1, T& obj2)
33{
34 T buf = obj1;
35 obj1 = obj2;
36 obj2 = buf;
37}
38#else
39template <typename T> [[deprecated("Please use std::swap instead")]]
40void MLSwap(T&, T&) = delete;
41#endif
42
43//-------------------------------------------------------------------
61//-------------------------------------------------------------------
62template <class BASE_TYPE> BASE_TYPE MLInverseMatHelper(const BASE_TYPE& origMat, bool* isInvertible,
63 const typename BASE_TYPE::ComponentType /*ZeroEpsilon*/,
64 const char* const ZeroDetErrString, const BASE_TYPE& Identity,
65 const size_t Dim)
66{
67 BASE_TYPE a(origMat); // As a evolves from original mat into identity
68 BASE_TYPE b(Identity); // b evolves from identity into inverse(a)
69 size_t i = 0, j = 0, i1 = 0; // Index counters.
70
71 // Loop over columns of a from left to right, eliminating above and below diagonal
72 for (j = 0; j < Dim; j++) {
73 // Find largest pivot in column j among rows j..DIM-1
74 i1 = j; // Row with largest pivot candidate
75 for (i = j + 1; i < Dim; i++) {
76 if (std::abs(a[i][j]) > std::abs(a[i1][j])) {
77 i1 = i;
78 }
79 }
80 // Swap rows i1 and j in a and b to put pivot on diagonal
81 std::swap(a[i1], a[j]);
82 std::swap(b[i1], b[j]);
83
84 // Scale row j to have a identity diagonal
85 const typename BASE_TYPE::ComponentType avjj = a[j][j];
86 if (MLValueIs0WOM(avjj)) {
87 if (isInvertible == nullptr) {
88 std::stringstream sstream;
89 sstream.precision(4);
90 sstream << "Returning identity, matrix was:" << std::endl;
91 for (size_t y = 0; y < Dim; ++y) {
92 for (size_t x = 0; x < Dim; ++x) {
93 sstream << std::fixed << origMat[x][y] << "\t";
94 }
95 sstream << std::endl;
96 }
98 }
99 else {
100 *isInvertible = false;
101 }
102 return Identity;
103 }
104 b[j] /= avjj;
105 a[j] /= avjj;
106
107 // Eliminate off-diagonal elements in col j of a,
108 // applying identical operations to b
109 for (i = 0; i < Dim; i++) {
110 if (i != j) {
111 b[i] -= a[i][j] * b[j];
112 a[i] -= a[i][j] * a[j];
113 }
114 }
115 }
116
117 // Set state and return inverse.
118 if (isInvertible != nullptr) {
119 *isInvertible = true;
120 }
121 return b;
122}
123
125
126#endif // __mlLinearAlgebraTools_H
@ T
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0; otherwise, it returns false.
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm,...
Definition mlTypeDefs.h:879
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)
void MLSwap(T &, T &)=delete
BASE_TYPE MLInverseMatHelper(const BASE_TYPE &origMat, bool *isInvertible, const typename BASE_TYPE::ComponentType, const char *const ZeroDetErrString, const BASE_TYPE &Identity, const size_t Dim)
Computes an N dimensional inverse from given default parameters.