SciProgCentre / kmath

Kotlin mathematics extensions library
656 stars 55 forks source link

Choice of Buffer/NDStructure realization in function producing new object #51

Open Zelenyy opened 5 years ago

Zelenyy commented 5 years ago

For example, we have some function:

fun <T> someFunction1(input1: Buffer<T>, input2 : Buffer<T>) : Buffer<T>

How choice output Buffer sub-class for function? If always return a generic buffer, this could be slow, but creating specific realization of someFunction request more boilerplate code.

altavir commented 5 years ago

Sorry for the late reply. Missed the issue.

That is what contexts are for. For optimized operations you need to use dedicated context. Specifically if you use more than one operations. On first operation context will reinterpret or transform the buffer into the form it likes, but on subsequent operations it will use optimized buffer form. Operations could be added as extensions for existing contexts.

If you use a single buffer operation on numbers, you can always use Buffer.auto factory. It does not guarantee optimal performance for custom classes, but for simple elements it should work just fine.

altavir commented 5 years ago

Should be part of #61