Skip to content

Instantly share code, notes, and snippets.

@enzo-santos
Created June 23, 2020 04:12
Show Gist options
  • Save enzo-santos/66bf1fcf97dce764b45d91ddaa37f40d to your computer and use it in GitHub Desktop.
Save enzo-santos/66bf1fcf97dce764b45d91ddaa37f40d to your computer and use it in GitHub Desktop.
Kotlin snippets
import kotlin.math.*
/**
* Returns [num] evenly spaced samples, calculated over the interval [0, [max]].
*/
fun linspace(num: Int, max: Double): DoubleArray
= DoubleArray(num) { it * max / (num - 1.0) }
/**
* Returns a sequence of numbers starting from [start], incrementing by [stop] and stopping before [stop].
* [stop] must be non negative and [start] > [stop].
*/
fun arange(start: Int, stop: Int, step: Int): IntArray
= IntArray((stop - start - 1)/step + 1) { start + step*it }
/**
* Return the maximum of an array.
*/
fun DoubleArray.amax() = max() ?: .0
/**
* Returns the element in an array [x] in the same position as the maximum of this array.
*/
fun DoubleArray.amax(x: DoubleArray): Pair<Double, Double> {
val peak = amax()
for ((i, v) in x.withIndex()) {
if (this[i] == peak) {
return v to peak
}
}
return x.last() to peak
}
/**
* Return the minimum of an array.
*/
fun DoubleArray.amin() = min() ?: .0
/**
* Returns the difference between the maximum and the minimum values of an array.
*/
fun DoubleArray.range() = amax() - amin()
/**
* Returns the average of this array elements.
*/
fun DoubleArray.mean() = average()
/**
* Returns the median of this array elements.
*/
fun DoubleArray.median() = sorted().let {
if (size % 2 == 1) it[(size-1)/2]
else (it[size/2] + it[(size/2)-1]) / 2.0
}
/**
* Returns the variance of this array elements.
*/
fun DoubleArray.variance(): Double
= fold(.0) { acc, value -> acc + (value - mean()).pow(2) } / size
/**
* Returns the standard deviation of this array elements.
*/
fun DoubleArray.stdev(): Double = sqrt(variance())
/**
* Returns the [n]th percentile rank of this array elements.
*/
fun DoubleArray.percentileRank(n: Double): Double
= count { it <= n } / size
/**
* Returns the coefficient of variation of this array elements.
*/
fun DoubleArray.variation(): Double
= stdev() / mean()
/**
* Returns the skewness of this array elements.
*/
fun DoubleArray.skewness(): Double
= fold(.0) { acc, value ->
acc + ((value - mean()).pow(3) / stdev().pow(3))
} / size
/**
* Returns the kurtosis of this array elements.
*/
fun DoubleArray.kurtosis(): Double
= fold(.0) { acc, value ->
acc + ((value - mean()).pow(4) / stdev().pow(4))
} / size
/**
* Returns the mean square of this array elements.
*/
fun DoubleArray.meanSquare(): Double
= fold(.0) { acc, value -> acc + value.pow(2) } / size
/**
* Returns the root mean square of this array elements.
*/
fun DoubleArray.rms(): Double = sqrt(meanSquare())
/**
* Returns the most common value of this array elements.
*/
fun DoubleArray.mode() : Double
= toSet()
.map { it to count { value -> value == it } }
.maxBy(Pair<Double, Int>::second)?.first ?: this[0]
/**
* Integrate [y] ([x]) using the trapezoidal rule.
*/
fun trapz(x: DoubleArray, y: DoubleArray): Double {
var area = .0
for (i in 1 until x.size) {
area += (x[i] - x[i-1]) * (y[i] + y[i-1])/2
}
return area
}
/**
* Returns the mean frequency of this array elements based on a time-domain signal [x].
*/
fun DoubleArray.meanFrequency(x: DoubleArray): Pair<Double, Double> {
val totalArea = trapz(x, this)
var partialArea: Double
for (i in x.indices) {
val xPartial = x.slice(0 until i).toDoubleArray()
val yPartial = slice(0 until i).toDoubleArray()
partialArea = trapz(xPartial, yPartial)
if (partialArea > totalArea / 2.0)
return Pair(x[i-1], this[i-1])
}
return Pair(x.last(), last())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment