4

Given an array T of integers, if we want to multiply each item by 2, in Java we will do it via a for-loop (Known as scalar operation). With python 's numpy array, we can just multiply array by 2 (Known as vectorized operation).

My question is: Is there a way in Java that allows us to perform that multiply operation in a vectorized way?

And if Java doesn't support that, is there a reason why?

Update: I'm not asking for sugar syntax. It is about CPU instruction vectorization like SIMD What is "vectorization"?

Thank you.

Xitrum
  • 7,765
  • 26
  • 90
  • 126
  • You can try Streams in java to do so. – Ankit Apr 02 '19 at 08:34
  • 1
    You could use `Arrays.setAll(array, i -> array[i] << 1);`, but that may be slower than just using a loop – Lino Apr 02 '19 at 08:34
  • Java allows defining classes. You can define a Vector class wrapping an array, and add a multiply method to it. I'm sure libraries dealing with matrices and vectors exist, but it's not part of the JDK, because... not everything needs to be provided by the JDK. – JB Nizet Apr 02 '19 at 08:36
  • The term array is used differently in programming languages - thus there is not defined API for arrays. – Florian Salihovic Apr 02 '19 at 08:44
  • 1
    I don't see how a question about vectorized operations is a duplicate of a question that asks about operator overloading... – sloth Apr 02 '19 at 08:47
  • 1
    @Xitrum If you ask about vectorized operations, are you just asking about syntactic sugar or "real" vectorization like SIMD? In the former case, there's at least Java's stream API which offers a `map` function; in the latter case, there's [Project Panama](http://openjdk.java.net/projects/panama/), which aims to add vectorization support to Java/the JVM. – sloth Apr 02 '19 at 08:52
  • @sloth I have updated my question. Thank you – Xitrum Apr 02 '19 at 09:18
  • Because that's the way they designed it. Unclear what other answer could possibly resolve this question. – user207421 Apr 02 '19 at 09:34
  • 1
    @Xitrum Sadly, I don't think your question will be reopened (I think it's not a duplicate), but the answer is: No, currently no support, but will maybe be added in the future (see Project Panama). As to why there's no support yet: the same answer as to almost all 'why does language not have XYZ': somebody has to design, implement, test, document and support it, so it takes a lot of work and money. – sloth Apr 02 '19 at 09:52
  • @sloth can you post it as an answer? so I can accept it. Thank you :) – Xitrum Apr 02 '19 at 10:14
  • @Xitrum I can't since this question is closed.... – sloth Apr 02 '19 at 11:27
  • Voted. Need another vote only. – LppEdd Apr 02 '19 at 16:25
  • There *is* some support, but not direct support. See: https://stackoverflow.com/questions/10784951/do-any-jvms-jit-compilers-generate-code-that-uses-vectorized-floating-point-ins – Thomas Bitonti Apr 02 '19 at 19:08

0 Answers0