Dinorah Tovar
February 05, 2022
110

# 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

## Transcript

1. Kotlin + Maths
Dinorah Tovar
@ddinorahtovar
@ddinorahtovar

2. I’m not a data
Science engineer
Disclaimer
I just like math

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

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
}
}
}

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()

6. 9 times
At least
more efficient

7. Mathematical expressions are pretty much pseudocode

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

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()

10. Left Aligned Title
3

i=1
i
Product
fun f() =
(1..3).fold(
1L, Long::times)

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

12. Simple quote or
statement goes here.
Ideally limit to four or five
lines max.
Class
Sealed
sealed class Tree
data class Node(
val value: T,
val left: Tree = Empty,
val right: Tree = Empty
) : Tree()

13. Simple quote or
statement goes here.
Ideally limit to four or five
lines max.
Sequences
Nodes
fun f() {
val tree = Node(
10,
20,
Node(40, 20, 10)
)
// we can create ext function
// and get the result
}

14. Algebraic operations

15. 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()
}

16. 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
}
}
}
}

17. Probability and Statistics

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

19. Just rainy probability
val weatherMexicoCity = arrayOf(
"Partially Sunny",
"Rainy" ,
"Partially Sunny" ,
"Cloudy",
"Rainy"
)
val rainyProbability = weatherMexicoCity.count { it == "Rainy" } / weatherMexicoCity.size

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
}

21. Charts + Statistics
Data science stuff

22. Kotlin has integration for Notebooks - specifically for
Jupiter

23. Kotlin + Maths
Dinorah Tovar