Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin + Maths

Dinorah Tovar
November 13, 2021

Kotlin + Maths

Grab your notebook cause in this talk, we are gonna talk about Math and Kotlin! Especially in the power that Kotlin has to run long operations and the power to perform some calculations like computing min, max, an average of numbers stored in a list! - from sumOf {} to multi-dimensional arrays to charts!

Dinorah Tovar

November 13, 2021
Tweet

More Decks by Dinorah Tovar

Other Decks in Science

Transcript

  1. Kotlin + Maths
    Dinorah Tovar @ddinorahtovar

    Google Developer Expert - Android
    Santo Domingo

    View Slide

  2. Disclaimer - I’m
    not a data science
    engineer

    View Slide

  3. Left Aligned Title
    Kotlin help us to solve many problems - but specially in maths!
    Math’s are coolest thing!
    • A fast operational language
    • Mathematical expressions
    • Algebraic data types (sealed class)
    • We can do simple functions - from basic to complex

    View Slide

  4. Simple quote or
    statement goes
    here. Ideally limit
    to four or five
    lines max.
    This function takes
    mseconds
    258
    fun main() {


    val y = complex(2, 1)


    var x = complex(0, 0)


    val time = measureTimeMillis {


    repeat(10000000) {


    x += y


    x *= y


    x -= y


    x /= y


    }


    }


    }

    View Slide

  5. Simple quote or
    statement goes
    here. Ideally limit
    to four or five
    lines max.
    This function takes
    seconds
    2.76
    def main():


    z = complex(2, 1)


    w = complex(0, 0)


    start = time.time()


    for i in range(0, 10000000):


    w += z


    w *= z


    w -= z


    w /= z


    ende = time.time()

    View Slide

  6. 9 times
    At least
    more efficient

    View Slide

  7. Mathematical expressions are pretty much
    pseudocode

    View Slide

  8. Left Aligned Title
    2x + 10
    First degree
    equation
    fun calc(x: Int) : Int {


    return 2*x + 10


    }

    View Slide

  9. Left Aligned Title
    10

    i=1
    1
    i + 1
    Summation
    val sum = 0


    for (i in 1..10) {


    sum += 1/(i+1)


    }
    fun f() =


    (1..10).map { i ->


    1 / (i + 1)


    }.sum()

    View Slide

  10. Left Aligned Title
    3

    i=1
    i
    Product
    fun f() =


    (1..3).fold(


    1L, Long::times)

    View Slide

  11. ADT is a type which is represented by
    several other subtypes

    View Slide

  12. Simple quote or
    statement goes
    here. Ideally limit
    to four or five
    lines max.
    ADT are only
    Class
    Sealed
    sealed class Tree


    data class Node(


    val value: T,


    val left: Tree = Empty,


    val right: Tree = Empty


    ) : Tree()

    View Slide

  13. Simple quote or
    statement goes
    here. Ideally limit
    to four or five
    lines max.
    ADT can create
    Sequences
    Nodes
    fun f() {


    val tree = Node(


    10,


    20,


    Node(40, 20, 10)


    )


    // we can create ext function


    // and get the result


    }

    View Slide

  14. Algebraic operations

    View Slide

  15. Left Aligned Title
    {1,2,4,10}
    Average


    Max - Min
    fun f() {


    val seq = floatArrayOf(


    1F,


    2F,


    4F,


    10F


    )


    val avg = seq.average()


    val max = seq.max()


    val min = seq.min()


    }

    View Slide

  16. Left Aligned Title
    (
    222
    222
    222
    )
    Multi-
    dimensional
    Vectors
    fun f() {


    val mx = arrayOf(


    arrayOf(floatArrayOf(2F, 2F, 2F)),


    arrayOf(floatArrayOf(2F, 2F, 2F)),


    arrayOf(floatArrayOf(2F, 2F, 2F))


    )


    val normArray = mx.map {


    column -> column.map {


    row -> row.map { element ->


    element / 255F


    }


    }


    }


    }

    View Slide

  17. Probability and Statistics

    View Slide

  18. Let’s solve a problem
    Let’s calculate the probability of
    the weather in the last week in
    Mexico City!
    Last week weather
    Partially Sunny
    Rainy
    Rainy
    Cloudy
    Partially Sunny

    View Slide

  19. Just rainy probability
    val weatherMexicoCity = arrayOf(


    "Partially Sunny",


    "Rainy" ,


    "Partially Sunny" ,


    "Cloudy",


    "Rainy"


    )
    val rainyProbability = weatherMexicoCity.count { it == "Rainy" } /
    weatherMexicoCity.size

    View Slide

  20. How about all the weathers
    val weatherMexicoCity = arrayOf(


    "Partially Sunny",


    "Rainy" ,


    "Partially Sunny" ,


    "Cloudy",


    "Rainy"


    )
    val generalProbability = weatherMexicoCity.map {


    label -> labels.count { it == label } / labels.size


    }

    View Slide

  21. Charts + Statistics

    View Slide

  22. Kotlin has integration for Notebooks -
    specifically for Jupiter

    View Slide

  23. View Slide

  24. Kotlin + Maths
    Dinorah Tovar @ddinorahtovar

    Google Developer Expert - Android
    Santo Domingo

    View Slide