All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.inforion.lab403.common.extensions.math.kt Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
@file:Suppress("unused")

package ru.inforion.lab403.common.extensions

import java.util.*

object math {
    data class Bin(val value: T, val count: Int)

    fun histogram(data: ByteArray): List> {
        val values = data.toSet()
        val result = ArrayList>(values.size)
        values.forEach { byte -> result.add(Bin(byte, data.count { it == byte })) }
        return result
    }

    fun histogram(data: ByteArray, mask: BooleanArray): List> {
        val values = data.toSet()
        val result = ArrayList>(values.size)
        val filtered = data.filterIndexed { i, _ -> mask[i] }
        values.forEach { byte ->
            val bin = Bin(byte, filtered.count { it == byte })
            result.add(bin)
        }
        return result
    }

    fun histogram(values: Set, data: ByteArray, mask: BooleanArray): IntArray {
        val result = IntArray(256)
        val filtered = data.filterIndexed { i, _ -> mask[i] }
        values.forEach { byte -> result[byte.toUInt()] = filtered.count { it == byte } }
        return result
    }

    fun logRandomBase(index: Double, base: Double): Double =  Math.log(index) / Math.log(base)

    fun loge(index: Double): Double = Math.log(index)
    fun log2(index: Double): Double = logRandomBase(index, 2.0)
    fun log10(index: Double): Double = Math.log10(index)
}

fun Double.round(precision: Double = 1.0): Double = Math.round(this / precision) * precision
fun Float.round(precision: Float = 1.0f): Float = Math.round(this / precision) * precision




© 2015 - 2024 Weber Informatics LLC | Privacy Policy