最广泛使用的 C++ 向量/矩阵数学/线性代数库是什么,以及它们的成本和收益权衡?

2021-12-17 00:00:00 matrix linear-algebra math c++

似乎许多项目慢慢地需要做矩阵数学,并陷入首先构建一些向量类然后慢慢添加功能的陷阱,直到他们被发现构建一个半途而废的自定义线性代数库,并取决于在上面.

It seems that many projects slowly come upon a need to do matrix math, and fall into the trap of first building some vector classes and slowly adding in functionality until they get caught building a half-assed custom linear algebra library, and depending on it.

我想避免这种情况,同时不依赖于一些切线相关的库(例如 OpenCV、OpenSceneGraph).

I'd like to avoid that while not building in a dependence on some tangentially related library (e.g. OpenCV, OpenSceneGraph).

什么是常用的矩阵数学/线性代数库,为什么要决定使用一个而不是另一个?是否有任何人因某种原因被建议不要使用?我专门在几何/时间上下文*(2,3,4 Dim)* 中使用它,但将来可能会使用更高维度的数据.

What are the commonly used matrix math/linear algebra libraries out there, and why would decide to use one over another? Are there any that would be advised against using for some reason? I am specifically using this in a geometric/time context*(2,3,4 Dim)* but may be using higher dimensional data in the future.

我正在寻找以下方面的差异:API、速度、内存使用、广度/完整性、狭窄性/特异性、可扩展性和/或成熟度/稳定性.

I'm looking for differences with respect to any of: API, speed, memory use, breadth/completeness, narrowness/specificness, extensibility, and/or maturity/stability.

我最终使用了 Eigen3,对此我非常满意.

I ended up using Eigen3 which I am extremely happy with.

推荐答案

有不少项目已经在 通用图形工具包.那里的 GMTL 很好 - 它非常小,非常实用,并且被广泛使用,非常可靠.OpenSG、VRJuggler 和其他项目都已转而使用它,而不是他们自己的手卷垂直器/矩阵数学.

There are quite a few projects that have settled on the Generic Graphics Toolkit for this. The GMTL in there is nice - it's quite small, very functional, and been used widely enough to be very reliable. OpenSG, VRJuggler, and other projects have all switched to using this instead of their own hand-rolled vertor/matrix math.

我发现它非常好 - 它通过模板完成所有工作,因此非常灵活且速度非常快.

I've found it quite nice - it does everything via templates, so it's very flexible, and very fast.

在评论讨论和编辑之后,我想我会提供更多关于特定实现的优点和缺点的信息,以及为什么你可以根据你的情况选择另一个.

After the comments discussion, and edits, I thought I'd throw out some more information about the benefits and downsides to specific implementations, and why you might choose one over the other, given your situation.

GMTL -

优点:简单的 API,专为图形引擎设计.包括许多其他包中没有的面向渲染的基本类型(例如平面、AABB、具有多重插值的四元数等).极低的内存开销,相当快,易于使用.

Benefits: Simple API, specifically designed for graphics engines. Includes many primitive types geared towards rendering (such as planes, AABB, quatenrions with multiple interpolation, etc) that aren't in any other packages. Very low memory overhead, quite fast, easy to use.

缺点:API 非常专注于渲染和图形.不包括通用 (NxM) 矩阵、矩阵分解和求解等,因为这些超出了传统图形/几何应用程序的范围.

Downsides: API is very focused specifically on rendering and graphics. Doesn't include general purpose (NxM) matrices, matrix decomposition and solving, etc, since these are outside the realm of traditional graphics/geometry applications.

本征 -

优点:干净的 API,相当容易使用.包括带有四元数和几何变换的几何模块.低内存开销.高性能大型 NxN 矩阵和其他通用数学例程的完整求解.

Benefits: Clean API, fairly easy to use. Includes a Geometry module with quaternions and geometric transforms. Low memory overhead. Full, highly performant solving of large NxN matrices and other general purpose mathematical routines.

缺点:可能比您想要的范围更大(?).与 GMTL 相比,几何/渲染特定例程更少(即:欧拉角定义等).

Downsides: May be a bit larger scope than you are wanting (?). Fewer geometric/rendering specific routines when compared to GMTL (ie: Euler angle definitions, etc).

IMSL -

优点:非常完整的数字库.非常非常快(据说是最快的求解器).迄今为止最大、最完整的数学 API.商业支持、成熟且稳定.

Benefits: Very complete numeric library. Very, very fast (supposedly the fastest solver). By far the largest, most complete mathematical API. Commercially supported, mature, and stable.

缺点:成本 - 不便宜.很少有几何/渲染特定的方法,因此您需要在其线性代数类之上推出自己的方法.

Downsides: Cost - not inexpensive. Very few geometric/rendering specific methods, so you'll need to roll your own on top of their linear algebra classes.

NT2 -

优点:如果您习惯了 MATLAB,则提供更熟悉的语法.为大型矩阵等提供完整的分解和求解.

Benefits: Provides syntax that is more familiar if you're used to MATLAB. Provides full decomposition and solving for large matrices, etc.

缺点:数学,不专注于渲染.性能可能不如 Eigen.

Downsides: Mathematical, not rendering focused. Probably not as performant as Eigen.

LAPACK -

优点:非常稳定、经过验证的算法.好久没来了完整的矩阵求解等.很多晦涩数学的选项.

Benefits: Very stable, proven algorithms. Been around for a long time. Complete matrix solving, etc. Many options for obscure mathematics.

缺点:在某些情况下性能不高.从 Fortran 移植,使用奇怪的 API.

Downsides: Not as highly performant in some cases. Ported from Fortran, with odd API for usage.

就我个人而言,这归结为一个问题 - 您打算如何使用它.如果您只关注渲染和图形,我喜欢 Generic Graphics Toolkit,因为它表现良好,并且支持许多开箱即用的有用渲染操作,而无需您自己实现.如果您需要通用矩阵求解(即:大矩阵的 SVD 或 LU 分解),我会选择 Eigen,因为它处理了这个问题,提供了一些几何运算,并且在处理大型矩阵解决方案时非常高效.您可能需要编写更多自己的图形/几何操作(在其矩阵/向量之上),但这并不可怕.

Personally, for me, it comes down to a single question - how are you planning to use this. If you're focus is just on rendering and graphics, I like Generic Graphics Toolkit, since it performs well, and supports many useful rendering operations out of the box without having to implement your own. If you need general purpose matrix solving (ie: SVD or LU decomposition of large matrices), I'd go with Eigen, since it handles that, provides some geometric operations, and is very performant with large matrix solutions. You may need to write more of your own graphics/geometric operations (on top of their matrices/vectors), but that's not horrible.

相关文章