处理线性代数的最佳基本类型

发布于 2024-07-10 05:31:43 字数 95 浏览 13 评论 0原文

我正在为一个项目用 C++ 编写一个小型且不充分的线性代数库(抱歉)。 我正在使用双精度数字实现矩阵和运算。 我做的对吗? 我应该实现一个模板类吗? 有更精确的类型吗?

I'm writing a small and inadequate linear algebra library in C++ for a project (I'm sorry). I'm implementing matrices and operations using double precision numbers. I'm doing right? Should I implement a template class instead? Is there a more precise type around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我将使用模板来实现类/结构。 一开始,您很可能会对 double 感到满意,但我发现在每个我没有将矩阵实现为模板的项目中,我后来都后悔了。

此外,它还使您有机会使用更有趣的元素代数 - 区间算术、概率分布、复杂数学、定点匹配、子矩阵、简单数学:-)等。

I would implement the class/struct using a template. In the beginning, you will most likely be satisfied with just double, but I have found that in every project where I didn't implement matrices as templates, I later regretted it.

Also, it gives you an opportunity to use more interesting element-algebras - interval arithmetic, probability distributions, complex math, fixed-point match, sub-matrices, simple math :-), etc.

万水千山粽是情ミ 2024-07-17 05:31:43

我使用模板编写了一个 C++ 线性代数库。 我的想法是,有一天我们可能想要使用复数或扩展精度数。 那可能是七年前的事了,我们还没有做到。 我们几乎总是使用双精度作为模板类型,并且我们有 typedef 来使这变得简单。

有几次我们反其道而行之,使用比 double 更小的类型。 例如,我们在 此处< 描述的内存绑定应用程序中使用了 float 而不是 double /a>. 但 99.9% 的时间我们都使用双打。

如果您确实使用模板参数,请注意使用整数类型但隐式需要浮点类型。 例如,假设您有一个矩阵,其条目都是整数,因此您使用矩阵来表示。 班级。 但随后你将其传递给线性求解器。 现在你的算术是使用整数除法完成的,你的结果是错误的。 (我已经做到了!)

I've written a C++ linear algebra library using templates. My thought was that we might want to use complex numbers or extended precision numbers some day. That was maybe seven years ago, and we haven't done it yet. We almost always use doubles as the template type, and we have typedefs to make that easy.

A few times we've gone the other way, using types smaller than a double. For example, we've used float rather than double in a memory-bound application described here. But 99.9 percent of the time we use doubles.

If you do use a template argument, watch out for using an integer type but implicitly requiring a floating point type. For example, say you have a matrix whose entries are all integers and so you use a matrix<int> class. But then you pass that to a linear solver. Now your arithmetic is done using integer division, and your results are wrong. (I've done that!)

一袭水袖舞倾城 2024-07-17 05:31:43

我正在写一篇小而不足的文章
C++ 中的线性代数库
项目(对不起)

哎呀! 小心,非常非常小心...查看JAMA/TNT - 这是获得了 NIST 的批准,并且他们已经处理了一些“更简单”的线性代数数学,例如各种因式分解算法。 线性代数涉及许多数值精度方面的棘手问题(例如 希尔伯特矩阵),并且与我一样多就像做我自己的事情一样,这是您可能想要使用已经经过充分测试的良好坚实基础的领域之一。

您应该能够将 long double 与它一起使用(不太确定),但算法本身可能比矩阵的精度更重要。

I'm writing a small and inadequate
linear algebra library in C++ for a
project (I'm sorry)

OUCH! Be careful, be very very careful... Check out JAMA/TNT -- it's got the NIST stamp-of-approval on it and they've already handled some of the "simpler" linear algebra math e.g. various factoring algorithms. Linear algebra involves lots of tricky issues with numerical precision (e.g. Hilbert matrices) and as much as I like doing my own thing this is one of those areas where you might want to use a good solid foundation that's already been well-tested.

You should be able to use long double with it (not quite sure of that) but the algorithms themselves are probably more critical than the precision of the matrices.

乜一 2024-07-17 05:31:43

最后的问题答案:是的,有,它被称为long double,并且至少与double一样精确。 至于是否使用模板,是的,我会使用模板。 这对他们来说是一个很好的用例,我认为这将使移植到其他标量数字类型变得更容易。 然后,您还可以键入定义浮点数和/或双精度矩阵,具体取决于您正在运行的系统以及哪个系统运行得更快/更好。

Final question answer: Yes, there is, it's called long double and is at least as precise as double. For whether to use templates or not, yes i would use templates. That's a great usecase for them and i think it will make porting to some other scalar number type easier. You can then also just typedef a float and/or a double matrix, depending on the system you are running on and which one works faster/better there.

画骨成沙 2024-07-17 05:31:43

不要为自己做任何额外的工作。 如果你能用双精度(或长双精度)就可以了。

听起来这只是一个小项目,在这种情况下,模板就可以为您服务。

尚未讨论的另一个选项是使用模板来定义元素类型。 这不会导致太多(如果有的话)额外工作,但允许稍后进行一些更改。

Don't make any extra work for yourself. If you can get by with double (or long double) go with that.

It sounds like this is just a little project, in which case the template thing will just make work for you.

Another option that hasn't been discussed is using a template to define your element type. That doesn't cause much, if any, extra work, but allows some changes later on.

花间憩 2024-07-17 05:31:43

没有比 long double 更精确的类型了,而且它也有硬件支持。 但如果您觉得需要更精确,您可以自由地创建自己的类型。 然而,即使进行了广泛的优化,它们也会比本机双精度类型慢得多。

There is no other type more precise than long double which also has hardware support. But you are free to make your own types if you feel the need for more precision. They will however be quite slower than the native double type, even with extensive optimization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文