• Stars
    star
    183
  • Rank 210,154 (Top 5 %)
  • Language
    Kotlin
  • Created over 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Set of extensions for Kotlin that provides Discrete math functionalities

DiscreteMathToolkit

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

Maven Central Build Status codecov codebeat badge Analytics

To stay current with news about library Twitter URL

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.

Cartesian product of lists

Similar to iterable multiplication but produces sequence of lists:

listOf('A', 'B', 'C', D).cartesianProduct(listOf('x', 'y')) // returns List<List<Char>>
// [
//     ['A', 'x'],
//     ['A', 'y'],
//     ['B', 'x'],
//     ['B', 'y'],
//     ['C', 'x'],
//     ['C', 'y'],
//     ['D', 'x'],
//     ['D', 'y']
// ]
listOf(0, 1).cartesianProduct(repeat = 2) // returns List<List<Int>>
// [
//     [0, 0],
//     [0, 1],
//     [1, 0],
//     [1, 1]
// ]
listOf(1, 2).cartesianProduct(listOf("ABC")) // returns List<List<Any>>
// [
//     [1, "ABC"],
//     [2, "ABC"]
// ]

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.

More Repositories

1

ArcSeekBar

Good looking curved Android SeekBar
Kotlin
514
star
2

KtAcademyPortal

Multiplatform Kotlin application of KotlinAcademy
CSS
443
star
3

ActivityStarter

Simple Android Library, that provides easy way to start the Activities with arguments.
Kotlin
428
star
4

PreferenceHolder

SharedPreference usage made fun in Kotlin
Kotlin
155
star
5

KotlinAndroidViewBindings

Bindings for properties with simple Kotlin types (Boolean, String) to layout traits (visibility, text).
Kotlin
126
star
6

kotlin-coroutines-recipes

Kotlin
113
star
7

VideoPlayView

Custom Android view with video player, loader and placeholder image
Kotlin
94
star
8

coroutines_sources

Kotlin
80
star
9

KotlinPreferences

Android Library to make SharedPreferences usage easier.
Kotlin
50
star
10

MarvelGallery

Example of Kotlin Android application
Kotlin
47
star
11

KotlinMultiplatformExample

Kotlin
45
star
12

kotlin-coroutines-workshop

Kotlin
38
star
13

MVTest

Code created as an examples for presentation about MVC vs MVP vs MVVM.
Kotlin
34
star
14

kotlin-exercises

Kotlin
29
star
15

kotlin_essentials_sources

Kotlin
20
star
16

SimpleKotlinMvpBoilerplate

Presentation of simple Kotlin Android MVP
Kotlin
19
star
17

effective-kotlin-book

Some open-sourced chapters of the Effective Kotlin
16
star
18

effective-kotlin-tests

JavaScript
13
star
19

functional_kotlin_sources

Kotlin
10
star
20

advanced-kotlin-workshop-tasks

Kotlin
10
star
21

caffeine-suspending

Kotlin
9
star
22

effective-kotlin-workshop

Kotlin
7
star
23

kotlin-android-workshop

Project with exercises for Kotlin Android workshop
Kotlin
7
star
24

measured-wrapper-ksp

Kotlin
6
star
25

WorkoutMPP

Kotlin
6
star
26

testing-viewmodel

Kotlin
6
star
27

Website

CSS
6
star
28

coroutines-benchmarks

Kotlin
6
star
29

AnkiMarkdown

Kotlin
5
star
30

coroutines-short-workshop-tasks

Kotlin
5
star
31

advanced_kotlin_sources

Kotlin
5
star
32

advent-2021

Kotlin
4
star
33

pacmanFunctional

Kotlin
3
star
34

generate-builder

Kotlin
3
star
35

pong

HTML
3
star
36

FindMyPhone

Kotlin
3
star
37

UserManagementApp

An example project using Jetpack Compose, Kotlin Coroutines, MVVM, proper unit testing
Kotlin
3
star
38

Minions

This is sink of different ideas for Kotlin usage to improve Android Development
Kotlin
3
star
39

StackTester

Simple App for testing different Activity Stack flags
Kotlin
3
star
40

effectivekotlin_sources

Kotlin
2
star
41

generateinterface-ksp

Kotlin
2
star
42

kotlin_programmer_dictionary

2
star
43

DependencyInjection-KSP

Simple example showing KSP processing with multiple rounds of execution.
Kotlin
2
star
44

marcinmoskala.github.io

CSS
2
star
45

testing-android-exercise

Kotlin
2
star
46

coroutines-android-testing-example

Kotlin
2
star
47

sudoku-generator-exercise

Kotlin
2
star
48

snake

Snake game written in Python
Python
1
star
49

measured-wrapper-ap

Kotlin
1
star
50

JSCoroutinesRecipes

Set of recipes for useful Kotlin/JS coroutines functions
1
star
51

effective-kotlin-website

HTML
1
star
52

kotlin-workshop

Kotlin
1
star
53

MorningWorkout

Kotlin
1
star
54

workshop-android-16-18.4.19

Kotlin
1
star
55

cde-droidcon-it

This is project made during Maciej Górski workshops about Continuous Delivery
Java
1
star
56

kotlin_essentials_book_pl

Kotlin
1
star
57

ktor-playground

Kotlin
1
star
58

NetworkClientTests

Kotlin
1
star
59

DeckMarkdown

Kotlin
1
star
60

ksp-template

Kotlin
1
star
61

next-starter-jamstack

JavaScript
1
star