Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Toolbox/LinearAlgebra.cpp @ 1890:6ce81914f7e4
added classes BucketAccumulator1D/2D
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 18 Jan 2022 22:08:55 +0100 |
parents | 7053b8a0aaec |
children | 187a261d7ae2 |
comparison
equal
deleted
inserted
replaced
1889:fe4befc9c2b0 | 1890:6ce81914f7e4 |
---|---|
715 m(1,2) = -shear(1,2); | 715 m(1,2) = -shear(1,2); |
716 m(3,2) = -shear(3,2); | 716 m(3,2) = -shear(3,2); |
717 | 717 |
718 return m; | 718 return m; |
719 } | 719 } |
720 | |
721 | |
722 template <typename T> | |
723 static T ComputeMedianInternal(std::vector<T>& v) | |
724 { | |
725 if (v.size() == 0) | |
726 { | |
727 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange, "Empty vector"); | |
728 } | |
729 | |
730 const size_t middle = v.size() / 2; | |
731 nth_element(v.begin(), v.begin() + middle, v.end()); | |
732 | |
733 T median = v[middle]; | |
734 | |
735 if (v.size() % 2 == 1) | |
736 { | |
737 return median; | |
738 } | |
739 else | |
740 { | |
741 /** | |
742 * Side-effect of "nth_element()": "All of the elements before | |
743 * this new nth element are less than or equal to the elements | |
744 * after the new nth element." | |
745 * https://en.cppreference.com/w/cpp/algorithm/nth_element | |
746 **/ | |
747 | |
748 typename std::vector<T>::const_iterator m = std::max_element(v.begin(), v.begin() + middle); | |
749 | |
750 return (*m + median) / static_cast<T>(2); | |
751 } | |
752 } | |
753 | |
754 double ComputeMedian(std::vector<double>& v) | |
755 { | |
756 return ComputeMedianInternal<double>(v); | |
757 } | |
758 | |
759 float ComputeMedian(std::vector<float>& v) | |
760 { | |
761 return ComputeMedianInternal<float>(v); | |
762 } | |
720 } | 763 } |
721 | 764 |
722 std::ostream& operator<<(std::ostream& s, const Vector& vec) | 765 std::ostream& operator<<(std::ostream& s, const Vector& vec) |
723 { | 766 { |
724 s << "("; | 767 s << "("; |