DiscreteMathToolkit


Source link: https://github.com/MarcinMoskala/KotlinDiscreteMathToolkit

DiscreteMathToolkit

Set of extensions for Kotlin that provides Discrete Math functionalities as an Kotlin extension functions.

To stay current with news about library

Permutations

setOf(1, 2, 3).permutations() // {
[1, 2, 3], [2, 1, 3], [3, 2, 1], [1, 3, 2], [2, 3, 1], [3, 1, 2]
}
 setOf(1, 2, 3).permutationsNumber() // 6 listOf(1, 2, 2).permutations() // {
[1, 2, 2], [2, 1, 2], [2, 2, 1]
}
 listOf(1, 2, 2).permutationsNumber() // 3

More examples here

Combinations

setOf(1, 2, 3, 4).combinations(3) // {
 {
1, 2, 3
}
, {
1, 2, 4
}
, {
1, 4, 3
}
, {
4, 2, 3
}
 
}
 setOf(1, 2, 3, 4).combinationNumber(3) // 4  setOf(1, 2, 3, 4).combinationsWithRepetitions(2) // [{
1=2
}
, {
1=1, 2=1
}
, {
1=1, 3=1
}
, {
1=1, 4=1
}
, {
2=2
}
, {
2=1, 3=1
}
, {
2=1, 4=1
}
, {
3=2
}
, {
3=1, 4=1
}
, {
4=2
}
] setOf(1, 2, 3, 4).combinationsWithRepetitionsNumber(2) // 10

More examples here and here

Powerset

Powerset of any set S is the set of all subsets of S, including the empty set and S itself.

setOf(1, 2, 3).powerset() // {
 {

}
, {
1
}
, {
2
}
, {
3
}
, {
1, 2
}
, {
1, 3
}
, {
2, 3
}
, {
1, 2, 3
}
 
}
 setOf(1, 2, 3).powersetSize() // 8

Product

Product is the result of multiplying.

(3..4).product() // 12 listOf(10, 10, 10).product() // 1000

More examples here.

Factorial

Factorian of n (n!) is a product of all positive integers less than or equal to n.

3.factorial() // 6L 10.factorial() // 3628800L 20.factorial() // 2432902008176640000L

More examples here.

Numbers divisible and non-divisible by

(1..1000).countNonDivisiveBy(2) // 500 (1..1000).countNonDivisiveBy(3) // 777 (1..1000).countNonDivisiveBy(2, 6, 13) // 462 (1..1000).countNonDivisiveBy(3, 7, 11) // 520  (1..1000).countDivisiveBy(2) // 500 (1..1000).countDivisiveBy(3) // 333 (1..1000).countDivisiveBy(2, 6, 13) // 538 (1..1000).countDivisiveBy(3, 7, 11) // 480

More examples here.

Splits of sets and numbers

In Descrete Math there are two functions used to count number of splits: S(n, k) - Stirling function - number of splits of n different elements to k groups P(n, k) - number of splits of n identical elements to k groups

(1..n).toSet().splitsNumber(1) // 1 (1..n).toSet().splitsNumber(n) // 1 setOf(1, 2, 3).splitsNumber(2) // 3 setOf(1, 2, 3, 4).splitsNumber(2) // 7 setOf(1, 2, 3, 4, 5).splitsNumber(3) // 25 setOf(1, 2, 3, 4, 5, 6, 7).splitsNumber(4) // 350 setOf(1, 2, 3).splits(2) // {
 {
 {
1, 2
}
, {
3
}
 
}
,{
 {
1, 3
}
, {
2
}
 
}
,{
 {
3, 2
}
, {
1
}
 
}
 
}

More examples here

n.splitsNumber(1) // 1 n.splitsNumber(n) // 1 7.splitsNumber(4) // 3 11.splitsNumber(4) // 11 9.splitsNumber(5) // 5 13.splitsNumber(8) // 7

More examples here

Iterable multiplication

Multiplication of iterables returns iterable with pairs of each possible connections of elements from first and iterable:

listOf(1, 2) * listOf("A", "B") // returns List<Pair<Int, String>> // [(1, "A"), (1, "B"), (2, "A"), (2, "B")]  listOf('a', 'b') * listOf(1, 2) * listOf("A", "B") // returns List<Triple<Char, Int, String>> // [ //
 ('a', 1, "A"), ('a', 1, "B"),  //
 ('a', 2, "A"), ('a', 2, "B"),  //
 ('b', 1, "A"), ('b', 1, "B"),  //
 ('b', b, "A"), ('b', 2, "B") // ] 

More examples here.

Java support

Library is fully supporting usage from Java. All functions can be used as static function of DiscreteMath. For example:

DiscreteMath.permutationsNumber(set) DiscreteMath.permutationsNumber(list) DiscreteMath.factorial(10) // 3628800L

Returned list and sets are Java standard lists and sets. More examples of Java usage here.

Install

Gradle:

compile "com.marcinmoskala:DiscreteMathToolkit:1.0.3"

Maven:

<dependency>
<groupId>com.marcinmoskala</groupId>
<artifactId>DiscreteMathToolkit</artifactId>
<version>1.0.3</version> </dependency> 

Jar to download together with sources and javadoc can be found on Maven Central.

License

Copyright 2017 Marcin Moska?a  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

Uses JavaScript calls to automate interaction with web pages displayed in Android WebViews. Could be especially helpful in testing.

The easy asynchronous library for Kotlin. With extensions for Android, RxJava, JavaFX and much more.

KerningViews provides a set of views which lets you adjust the spacing between the characters in the text of that view also know as Kerning.

Wow, Android View Animation!

Lettrine is a library that let you display TextView with an initial first letter.

An Android time picker library.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes