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

fuookami.ospf.kotlin.utils.math.geometry.Distance.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
package fuookami.ospf.kotlin.utils.math.geometry

import fuookami.ospf.kotlin.utils.math.*
import fuookami.ospf.kotlin.utils.functional.*

sealed interface Distance {
    operator fun  invoke(lhs: Point, rhs: Point): Flt64

    data object Euclidean : Distance {
        override operator fun  invoke(lhs: Point, rhs: Point): Flt64 {
            return (lhs.indices.sumOf { (lhs[it] - rhs[it]).sqr() }).sqrt()
        }
    }

    data object Manhattan : Distance {
        override operator fun  invoke(lhs: Point, rhs: Point): Flt64 {
            return lhs.indices.sumOf { (lhs[it] - rhs[it]).abs() }
        }
    }

    class Minkowski(val p: Int) : Distance {
        override operator fun  invoke(lhs: Point, rhs: Point): Flt64 {
            return (lhs.indices.sumOf { (lhs[it] - rhs[it]).pow(p) }).pow(Flt64(1.0 / p.toDouble())) as Flt64
        }
    }

    data object Chebyshev : Distance {
        override operator fun  invoke(lhs: Point, rhs: Point): Flt64 {
            return (lhs.indices.maxOf { (lhs[it] - rhs[it]).abs() })
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy