kyonifer / koma

A scientific computing library for Kotlin. https://kyonifer.github.io/koma
Other
270 stars 23 forks source link

Proposal: Invokable NDArray.Companion #51

Closed drmoose closed 6 years ago

drmoose commented 6 years ago

What It Does This PR enables a syntax on NDArray similar to Kotlin's List(5) { "init" }, using inlines to avoid boxing primitive values.

With this patch, the following code

val before = 5431
val nd = NDArray(1,3) { 0.5 }
val after = 1234

disassebles to this on the JVM:

    if (Intrinsics.areEqual(var2, Reflection.getOrCreateKotlinClass(Double.TYPE))) {
         $receiver$iv$iv = this_$iv.getDoubleFactory().alloc(dims$iv);
         $receiver$iv$iv = $receiver$iv$iv;
         var6 = $receiver$iv$iv.iterateIndices().iterator();

         while(var6.hasNext()) {
            index$iv$iv = (IndexIterator)var6.next();
            var10001 = index$iv$iv.getLinear();
            it$iv = index$iv$iv.getNd();
            var9 = var10001;
            double var13 = 0.5D;
            $receiver$iv$iv.setDouble(var9, var13);
         }

         var10000 = $receiver$iv$iv;
    } else if /* more branches */

Positive Side Effects

Awkward Side-Effects

Mixed Bag