V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
codingbody
V2EX  ›  Java

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

  •  
  •   codingbody · 2022-10-26 12:11:32 +08:00 · 2633 次点击
    这是一个创建于 540 天前的主题,其中的信息可能已经有所发展或是发生改变。

    类似下面这种运算:

    [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])
    
    16 条回复    2022-11-04 13:42:22 +08:00
    xiaopanzi
        1
    xiaopanzi  
       2022-10-26 12:15:07 +08:00
    如果只是简单的向量计算,自己简单封装一个?当然,如果对性能有要求,需要 SIMD 。
    debuggerx
        2
    debuggerx  
       2022-10-26 12:20:09 +08:00
    Java 不支持用户定义的运算符重载
    hanhuoer
        3
    hanhuoer  
       2022-10-26 12:28:09 +08:00 via iPhone
    stream
    thinkershare
        4
    thinkershare  
       2022-10-26 12:28:29 +08:00
    C#有类似 numpy 的 NumSharp, Java 运算符重载和自定义值类型的限制,导致写出来也不会多简洁直观。C#如果未来添加对 Slice 完全支持,C#应该可以写出和 numpy 一模一样的代码。
    jeesk
        5
    jeesk  
       2022-10-26 12:34:29 +08:00
    WispZhan
        6
    WispZhan  
       2022-10-26 13:01:30 +08:00 via Android
    shishiyi
        7
    shishiyi  
       2022-10-26 14:10:04 +08:00
    据我所知,jdk11 的 stream 没有直接方法实现该方式,但是 spring 的 Reactor 中 zip 方法就是干这种事情的
    XXWHCA
        8
    XXWHCA  
       2022-10-26 14:12:21 +08:00
    2 楼已经回复你了,java 不支持运算符重载,只能通过封装工具方法来实现
    shishiyi
        9
    shishiyi  
       2022-10-26 14:25:05 +08:00   ❤️ 1
    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
        10
    ql562482472  
       2022-10-26 14:32:08 +08:00
    这不是矩阵运算吗 apache-common-math
    impossibleshen
        11
    impossibleshen  
       2022-10-26 16:04:03 +08:00
    apache-common-math 或者 apache-common-math3 找找
    aguesuka
        12
    aguesuka  
       2022-10-26 17:50:20 +08:00   ❤️ 1
    vector api https://openjdk.org/jeps/338 正在孵化中
    knives
        13
    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
        14
    hingbong  
       2022-10-27 01:20:14 +08:00 via Android
    @aguesuka 我记得之前看到过的也是这个
    learningman
        15
    learningman  
       2022-10-27 08:05:11 +08:00
    Java 还在 s1.equals(s2)呢。。。
    buliugu
        16
    buliugu  
       2022-11-04 13:42:22 +08:00
    DJL 的 NDArray ,几乎相当于 NumPy
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1314 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 23:37 · PVG 07:37 · LAX 16:37 · JFK 19:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.