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

Kotlin + Maths

Dinorah Tovar
February 05, 2022

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

February 05, 2022
Tweet

More Decks by Dinorah Tovar

Other Decks in Technology

Transcript

  1. 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
  2. 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 } } }
  3. 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()
  4. Left Aligned Title 2x + 10 First degree equation fun

    calc(x: Int) : Int { return 2*x + 10 }
  5. 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()
  6. Left Aligned Title 3 ∏ i=1 i Product fun f()

    = (1..3).fold( 1L, Long::times)
  7. Simple quote or statement goes here. Ideally limit to four

    or five lines max. ADT are only Class Sealed sealed class Tree<out T> data class Node<T>( val value: T, val left: Tree<T> = Empty, val right: Tree<T> = Empty ) : Tree<T>()
  8. 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 }
  9. Left Aligned Title {1,2,4,10} Average Max - Min fun f()

    { val seq = fl oatArrayOf( 1F, 2F, 4F, 10F ) val avg = seq.average() val max = seq.max() val min = seq.min() }
  10. Left Aligned Title ( 222 222 222 ) Multi- dimensional

    Vectors fun f() { val mx = arrayOf( arrayOf( fl oatArrayOf(2F, 2F, 2F)), arrayOf( fl oatArrayOf(2F, 2F, 2F)), arrayOf( fl oatArrayOf(2F, 2F, 2F)) ) val normArray = mx.map { column -> column.map { row -> row.map { element -> element / 255F } } } }
  11. 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
  12. Just rainy probability val weatherMexicoCity = arrayOf( "Partially Sunny", "Rainy"

    , "Partially Sunny" , "Cloudy", "Rainy" ) val rainyProbability = weatherMexicoCity.count { it == "Rainy" } / weatherMexicoCity.size
  13. 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 }