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 full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

  6. 9 times
    At least
    more efficient

    View full-size slide

  7. Mathematical expressions are pretty much
    pseudocode

    View full-size slide

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


    return 2*x + 10


    }

    View full-size 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 full-size slide

  10. Left Aligned Title
    3

    i=1
    i
    Product
    fun f() =


    (1..3).fold(


    1L, Long::times)

    View full-size slide

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

    View full-size 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 full-size 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 full-size slide

  14. Algebraic operations

    View full-size 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 full-size 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 full-size slide

  17. Probability and Statistics

    View full-size 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 full-size slide

  19. Just rainy probability
    val weatherMexicoCity = arrayOf(


    "Partially Sunny",


    "Rainy" ,


    "Partially Sunny" ,


    "Cloudy",


    "Rainy"


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

    View full-size 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 full-size slide

  21. Charts + Statistics

    View full-size slide

  22. Kotlin has integration for Notebooks -
    specifically for Jupiter

    View full-size slide

  23. Kotlin + Maths
    Dinorah Tovar @ddinorahtovar

    Google Developer Expert - Android
    Santo Domingo

    View full-size slide