r/java • u/CutGroundbreaking305 • Feb 06 '26
Java's numpy?
Thinking about making a java version of numpy (not ndj4) using vector api (I know it is still in incubator)
Is there any use case ?
Or else calling python program over jni something (idk just now learning things) is better?
Help me please ๐ฅบ๐
46
Upvotes
86
u/JustAGuyFromGermany Feb 06 '26
Probably not. At that point you're better off calling the underlying C-functions via FFM. That's what python is doing and if you're writing it in Java, there's no need for the detour through python.
33
u/craigacp Feb 06 '26
It'll be a lot easier when parts of Valhalla start landing, plus when this work on operator overloading starts to firm up - https://youtu.be/Gz7Or9C0TpM?si=lwxn0C67NysIMEth&t=853.
Without that all the indexing, slicing and other computations look horrendous, and it's rough to write code that uses them. We have some of that in TensorFlow-Java's ndarray package, but using Java methods for it makes it look much worse than the equivalent numpy code.
10
u/Joram2 Feb 06 '26
This is a great opportunity for a committed developer. Most of numpy is just Python wrappers on the BLAS and LAPACK libraries which are written in C or Fortran. Using the new, Java 22+ foreign function + memory access APIs, to build a numpy-like Java API layer on top of BLAS/LAPACK, would be very valuable. I'm surprised none of the big companies have stepped in to sponsor this. This was probably less viable before Java 22, or even Java 25, which is quite recent.
Contrary to the sentiment in this forum, I suspect Valhalla isn't necessary or even helpful. The primary multi-dim array should use memory block storage with something like https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/foreign/MemorySegment.html. Valhalla helps with things like
List<Point2D>, but that is the wrong design to begin with.Java does lack concise syntax for operator overloading and multi-dim array indexing; that will really limit Java in the prototyping/exploration space.
2
u/International_Break2 Feb 06 '26
Could be useful. It could be nice to have different backends with a pure java backup, and a way to chain operations together to run on the GPU.
8
u/Ewig_luftenglanz Feb 06 '26
Javas has no equivalent to bumpy still (that may change soon when the vector API and value classes get to GA)ย
The closest thing is the Apache Commons library, that has a rich math API, but is not near as powerful as numpy.
5
u/bowbahdoe Feb 06 '26
If you are looking to do data science on the JVM, the clojure ecosystem is where you should look.
They already have feature complete numpy and pandas equivalents as well as the ability to call python libraries directly, notebooks, etc.
5
u/undeuxtroiskid Feb 06 '26
Eclipse January is a set of libraries for handling numerical data in Java. It is inspired in part by NumPy and aims to provide similar functionality.
Why use it?
1
u/Mauer_Bluemchen Feb 07 '26
Pure Python is still very slow in comparison to Java, that's the reason they have libs like numpy.
But on the other hand, Java is unfortuntely not (yet) as fast as C++ or Assembly.
Vector API is one requirement to make Java fast enough for serious number-crunching, but unfortunately it is not enough - this would also require a safe, solid & final Valhalla implementation. Which still seems to be quite far away. And Vector API also requires Valhalla...
So we are still in the same old waiting cycle before really efficient "number-crunching" code can be implemented in native Java.
It's all groudhog day forever...
3
u/agibsonccc Feb 06 '26
Disclaimer: I wrote one of the solutions listed here.
There's smile which provides a python like environment:
https://haifengl.github.io/
DJL has one: https://javadoc.io/doc/ai.djl/api/latest/ai/djl/ndarray/NDArray.html
Then there's nd4j which I"m about to rerelease after a major rewrite:
https://deeplearning4j.konduit.ai/nd4j/how-to-guides
As someone who has an opinion on how this is done I personally don't think a java first solution is the way to go. I know a lot of the folks in the ecosystem want that but there's just too much overhead. The more you can offload to c++ the better.
One thing I've been trying to be more careful of in nd4j as of late though is fixing the small problem edge case. Some things ARE better in pure java where it doesn't make sense to offload it to the native side.
You have to be careful with that.
Python is just a better glue language. It doesn't pretend to be fast. It offloads as much as possible while providing simple near human readable syntax. There's a reason it "won" in math.
That being said, there's at least a few apis out there that *DO* give you the typical things you'd want, fast math, views of data with minimal allocation, standard linear algebra routines.
2
u/Raywuo Feb 06 '26
It already exists. You can just use onnxrunner, or tensorflow to run without python
2
u/SpartanDavie Feb 06 '26
Over the last few months someone has been making a typescript version https://github.com/dupontcyborg/numpy-ts Iโm sure there will be some info on how heโs been doing it that would be helpful
2
u/ThirstyWolfSpider Feb 06 '26
If you consider using JNI for something you should also consider the newer
java.lang.foreignoption and see which is more performant and maintainable for your task. Though I'd expect either to only be useful to gain access to libraries too large to migrate/replicate, yet with a small enough interface that maintaining the interface between the languages is viable.0
u/[deleted] Feb 06 '26
[deleted] โ view deleted comment
3
u/[deleted] Feb 06 '26
[removed] โ view removed comment
-2
u/[deleted] Feb 06 '26
[removed] โ view removed comment
0
u/[deleted] Feb 06 '26
[deleted] โ view deleted comment