Java 中有没有支持 Array、List 进行算术运算的 Library?

2022-10-26 12:11:32 +08:00
 codingbody

类似下面这种运算:

[1,2,3,4,5]
+
[2,1,2,1,3]
-
[0,1,3,0,2]
=
[3,2,2,5,6]

在 Python 中,有 numpy 可以支持,不知道 Java 中有没有类似的 Library 。

加法

>>> a=numpy.array([2,4,5])
>>> b=numpy.array([1,1,1])
>>> a+b
array([3, 5, 6])

乘法

>>> a*b
array([2, 4, 5])
2657 次点击
所在节点    Java
16 条回复
xiaopanzi
2022-10-26 12:15:07 +08:00
如果只是简单的向量计算,自己简单封装一个?当然,如果对性能有要求,需要 SIMD 。
debuggerx
2022-10-26 12:20:09 +08:00
Java 不支持用户定义的运算符重载
hanhuoer
2022-10-26 12:28:09 +08:00
stream
thinkershare
2022-10-26 12:28:29 +08:00
C#有类似 numpy 的 NumSharp, Java 运算符重载和自定义值类型的限制,导致写出来也不会多简洁直观。C#如果未来添加对 Slice 完全支持,C#应该可以写出和 numpy 一模一样的代码。
jeesk
2022-10-26 12:34:29 +08:00
WispZhan
2022-10-26 13:01:30 +08:00
shishiyi
2022-10-26 14:10:04 +08:00
据我所知,jdk11 的 stream 没有直接方法实现该方式,但是 spring 的 Reactor 中 zip 方法就是干这种事情的
XXWHCA
2022-10-26 14:12:21 +08:00
2 楼已经回复你了,java 不支持运算符重载,只能通过封装工具方法来实现
shishiyi
2022-10-26 14:25:05 +08:00
public static void main(String[] args) {
Flux.zip(Flux.just(1,2,3), Flux.just(0, 1, 1))
.map(tuple -> tuple.getT1() + tuple.getT2())
.subscribe(System.out::println);
}



打印结果:1 ,3 ,4
ql562482472
2022-10-26 14:32:08 +08:00
这不是矩阵运算吗 apache-common-math
impossibleshen
2022-10-26 16:04:03 +08:00
apache-common-math 或者 apache-common-math3 找找
aguesuka
2022-10-26 17:50:20 +08:00
vector api https://openjdk.org/jeps/338 正在孵化中
knives
2022-10-26 19:39:18 +08:00
https://www.hipparchus.org/

Hipparchus: a mathematics Library
Hipparchus is a library of lightweight, self-contained mathematics and statistics components addressing the most common problems not available in the Java programming language.
hingbong
2022-10-27 01:20:14 +08:00
@aguesuka 我记得之前看到过的也是这个
learningman
2022-10-27 08:05:11 +08:00
Java 还在 s1.equals(s2)呢。。。
buliugu
2022-11-04 13:42:22 +08:00
DJL 的 NDArray ,几乎相当于 NumPy

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/889975

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX