sum
Returns the sum of all elements in the array. sumBy variants allow mapping each element through a selector function before summing, supporting Int, Double, ComplexFloat, and ComplexDouble result types.
Signatures
fun <T : Number, D : Dimension> MultiArray<T, D>.sum(): T
inline fun <T : Number, D : Dimension> MultiArray<T, D>.sumBy(
selector: (T) -> Int
): Int
inline fun <T : Number, D : Dimension> MultiArray<T, D>.sumBy(
selector: (T) -> Double
): Double
Parameters
Parameter | Type | Description |
|---|---|---|
|
| Maps each element to an |
|
| Maps each element to a |
Returns: The sum of all elements (or transformed elements).
Example
val a = mk.ndarray(mk[1, 2, 3, 4, 5])
a.sum() // 15
a.sumBy { it * it } // 55 (sum of squares)
val b = mk.ndarray(mk[1.5, 2.5, 3.0])
b.sum() // 7.0
28 February 2026