-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More features for NDArray #74
Comments
Yes this makes sense. Those functions being limited to Matrix are historical as NDArray is a relatively new addition. Feel free to make PRs and we can sort through any issues that come up. Note that many of the top-level functions such as those in |
Opened #75 to capture my above note as a separate ticket. It may be possible to close both this issue and that one at once. |
I think I've come up with a strategy that will allow all the different versions to be defined without needing code generation. I first create a function inline fun <reified T> NDArray<T>.mapIfNumeric(crossinline f: (Double) -> Double): NDArray<Double> {
when (T::class) {
Double::class -> return (this as NDArray<Double>).map { f(it) }
Float::class -> return (this as NDArray<Float>).map { f(it.toDouble()) }
Long::class -> return (this as NDArray<Long>).map { f(it.toDouble()) }
Int::class -> return (this as NDArray<Int>).map { f(it.toDouble()) }
Short::class -> return (this as NDArray<Short>).map { f(it.toDouble()) }
Byte::class -> return (this as NDArray<Byte>).map { f(it.toDouble()) }
else -> throw IllegalArgumentException("This NDArray does not contain numeric data.")
}
} Lots of functions can easily be defined by using that. For example, inline fun <reified T> acos(arr: NDArray<T>): NDArray<Double> {
return arr.mapIfNumeric { kotlin.math.acos(it) }
} I decompiled it and verified it wasn't doing any boxing. |
I like that idea. Looks good to me for functions that map through to an underlying function which expects a scalar |
The one issue with that implementation is that the return type is always Double. If the input array has type Float, I would expect it to return an array of Floats. I need to think about possibilities. Some functions will just need specialized implementations, due to their quirks. |
You know, I think I'm trying to hard to be clever. It's simpler to just write two functions, fun acos(arr: NDArray<Float>): NDArray<Float> {
return arr.map { kotlin.math.acos(it) }
}
fun acos(arr: NDArray<Double>): NDArray<Double> {
return arr.map { kotlin.math.acos(it) }
} The amount of code isn't that much more, it's easier to understand, and it avoids complications about return types. |
I have a question about code generation. Some of the extension functions I'll be adding only apply to certain types. For example, |
I'm traveling today but I'll look into this soon. Given the apparent recent changes to how inlining works, I'd like to explore the available options and see if we can find a way to clean up the codegen as you were attempting to do here. |
I played with this a bit and regrettably couldn't come up with something I hated less than codegen, so we might need to stick with that for now.
Take a look at the convertGetter and floatersOnly. The former inserts different strings depending on whether or not Alternatively, you could also just add another Also, feel free to just PR the extension functions directly if you'd rather not deal with it, and I can update the codegen afterwards. |
One option to consider is switching to a more powerful templating engine. The one built into Gradle is pretty limited. A lot of other ones can handle conditional blocks, loops, etc., which would make cases like this easy. It also would let you put all the logic into the template, rather than splitting it between the template and the build script. |
Fair enough. The only reason for using gradle's is to avoid another dependency. However, we could probably use something on the jvm like pebble and grab it directly from maven in the |
Opened #76 to track that idea. |
Closed by #78. |
Matrix has a lot of features that would be equally appropriate for NDArray. This includes methods like
min()
,max()
,mean()
,toString()
, etc. Also a lot of the math functions defined in matrixfuncs.kt. If you agree, I'd like to start implementing some of them.The text was updated successfully, but these errors were encountered: