diff --git a/doc/changelog.dox b/doc/changelog.dox index 7d19894ff..c2cd2d4c4 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -64,6 +64,7 @@ See also: @subsubsection changelog-latest-new-math Math library - Added @ref Math::fmod() (see [mosra/magnum#454](https://github.com/mosra/magnum/pull/454)) +- Added @ref Math::binomialCoefficient() (see [mosra/magnum#461](https://github.com/mosra/magnum/pull/461)) @subsection changelog-latest-changes Changes and improvements @@ -139,7 +140,7 @@ See also: @ref Corrade::Containers::ArrayView are now removed. This should have a significant positive effect on compile times of code using the @ref GL, @ref Audio, @ref Trade and @ref Text librariess -- @ref SceneGraph::Object:addChild() no longer requires the type constructor +- @ref SceneGraph::Object::addChild() no longer requires the type constructor to have the last parameter a parent object object pointer, as that was quite limiting. Instead it's calling @ref SceneGraph::Object::setParent() afterwards. This can cause compilation breakages in case the type diff --git a/doc/credits.dox b/doc/credits.dox index 9b4cf720a..8cc1f53a4 100644 --- a/doc/credits.dox +++ b/doc/credits.dox @@ -143,6 +143,7 @@ Are the below lists missing your name or something's wrong? bootstrap project - **Jan Dupal** ([\@JanDupal](https://github.com/JanDupal)) --- Homebrew package for `magnum-bindings` +- **[\@Janos95](https://github.com/Janos95)** -- @ref Math additions - **Jonathan Hale** ([\@Squareys](https://github.com/Squareys)) --- @ref Audio and @ref Trade library enhancements, @ref Platform::GlfwApplication and @ref Platform::EmscriptenApplication diff --git a/src/Magnum/Math/Functions.cpp b/src/Magnum/Math/Functions.cpp index a52819446..72969c58c 100644 --- a/src/Magnum/Math/Functions.cpp +++ b/src/Magnum/Math/Functions.cpp @@ -3,6 +3,7 @@ Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Vladimír Vondruš + Copyright © 2020 janos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), @@ -43,19 +44,23 @@ UnsignedInt log2(UnsignedInt number) { return log; } +UnsignedLong binomialCoefficient(const UnsignedInt n, UnsignedInt k) { + if(k > n) return 0; -UnsignedLong binomialCoefficient(UnsignedInt n, UnsignedInt k) { - if (k > n) return 0; - if (k * 2 > n) - k = n-k; - if (k == 0) return 1; + /* k and n - k gives the same value, optimize the calculation to do fewer + steps */ + if(k*2 > n) k = n - k; + + if(k == 0) return 1; UnsignedLong result = n; - for(UnsignedInt i = 2; i <= k; ++i ) { - CORRADE_ASSERT(result < ~UnsignedLong{} / (n-i+1), "Math::binomialCoefficient(): overflow for (" << Corrade::Utility::Debug::nospace << n << "choose" << k << Corrade::Utility::Debug::nospace << ")", 0ul); - result *= (n-i+1); + for(UnsignedInt i = 2; i <= k; ++i) { + CORRADE_ASSERT(result < ~UnsignedLong{} / (n-i+1), "Math::binomialCoefficient(): overflow for (" << Corrade::Utility::Debug::nospace << n << "choose" << k << Corrade::Utility::Debug::nospace << ")", {}); + + result *= n - i + 1; result /= i; } + return result; } diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index 2fe2cf1fc..4503d60c8 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -7,6 +7,7 @@ 2020 Vladimír Vondruš Copyright © 2020 Nghia Truong Copyright © 2020 Pablo Escobar + Copyright © 2020 janos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), @@ -410,14 +411,16 @@ template inline Vector ceil(const Vector + Copyright © 2020 janos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),