Categories
askquestion

Best base type to deal with linear algebra

Best base type to deal with linear algebra

Ask Question

Asked
11 years, 1 month ago

Active
11 years, 1 month ago

Viewed
600 times

.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}

5

2

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?

c++ linear-algebra double-precision

share|improve this question

edited Dec 24 ’08 at 3:30

Jason S

159k144144 gold badges520520 silver badges860860 bronze badges

asked Dec 19 ’08 at 17:56

tunnuztunnuz

19.8k2727 gold badges8181 silver badges122122 bronze badges

add a comment
 | 

6 Answers
6

active

oldest

votes

4

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!)

share|improve this answer

edited Dec 19 ’08 at 18:08

answered Dec 19 ’08 at 18:00

John D. CookJohn D. Cook

27.2k1010 gold badges6262 silver badges9393 bronze badges

Well, it’s been seven years. But maybe I’ll need it some day. Better go ahead and write it now just in case. 🙂

– John D. Cook
Dec 19 ’08 at 18:13

add a comment
 | 

6

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.

share|improve this answer

edited Dec 19 ’08 at 18:09

answered Dec 19 ’08 at 18:00

Frank KruegerFrank Krueger

61.3k4242 gold badges149149 silver badges200200 bronze badges

add a comment
 | 

4

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.

share|improve this answer

answered Dec 19 ’08 at 18:49

Jason SJason S

159k144144 gold badges520520 silver badges860860 bronze badges

It really depends on whether the point is to implement the library, or whether a library’s needed and Tommaso Urli just didn’t know about it. If the point is to implement one, check with your Numerical Analyst books. There are dangerous ops with extremely large/small floating points.

– Calyth
Dec 19 ’08 at 19:36

add a comment
 | 

2

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.

share|improve this answer

answered Dec 19 ’08 at 18:03

Johannes Schaub – litbJohannes Schaub – litb

440k107107 gold badges813813 silver badges11301130 bronze badges

add a comment
 | 

1

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.

share|improve this answer

answered Dec 19 ’08 at 18:11

David NormanDavid Norman

17.2k1010 gold badges5757 silver badges5252 bronze badges

add a comment
 | 

0

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.

share|improve this answer

edited Dec 19 ’08 at 18:10

answered Dec 19 ’08 at 18:00

Vilx-Vilx-

91.3k7575 gold badges247247 silver badges386386 bronze badges

add a comment
 | 

Your Answer

Thanks for contributing an answer to Stack Overflow!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Submit

Post as a guest

Name

Email
Required, but never shown

Post Your Answer

Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you’re looking for? Browse other questions tagged c++ linear-algebra double-precision or ask your own question.

Blog

When laziness is efficient: Make the most of your command line

Ben Popper is the worst coder in the world: Something awry with my array

Featured on Meta

TLS 1.0 and TLS 1.1 removal for Stack Exchange services

Did Stack Exchange cut the number of negative comments nearly in half between…

An account of my meeting with the Stack Overflow management team

Related

210Why is the C++ STL is so heavily based on templates? (and not on *interfaces*)1Sparse linear algebra solvers for C#474How do I use arrays in C++?1615Image Processing: Algorithm Improvement for ‘Coca-Cola Can’ Recognition7Finite Field (Galois Field) Linear Algebra Library for C (not C++)1Fixed-Dimension Specialization for Linear-Algebra Packages in D0CPU instruction sets for linear algebra?4arbitrary precision linear algebra c/c++ library with complex numbers1396Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations with _mm_popcnt_u64 on Intel CPUs

Hot Network Questions

How can I generate a sine wave with time varying frequency that is continuous, in C?

One-click: Open terminal, run commands, keep using the terminal

Is it legal to photograph and test ATMs?

Why is the asteroid belt shaped like a triangle?

If sound passes through material, vibration is produced. So are electromagnetic waves produced too?

I like my new job but I’m always late and I skip work often. How to avoid this?

Why I get “tar: Cowardly refusing to create an empty archive”? when trying to backup the whole server

Believable way to make missiles and drones inferior to analog starfighters?

Would allowing the Rogue sub-class (Assassin) to benefit from Supreme Sneak be unbalanced?

Count regex matches

Is there any dangerous command in LaTeX?

7zip : Why does encrypting the same file with AES-256 not give the same output?

How can I get a very thick or viscous paste (e.g. caramel, ganache, thick mayonnaise) into small-necked squeeze bottles without heating it up?

Using two tires on one wheel to prevent punctures?

Corner modelling technique

What purpose are the transistors in this diagram serving?

Why so many STS-61’s?

When was Alice Born?

Did Voldemort know that his Muggle father was actually a victim?

How to explain that there can only be one magic portal between a planet and another

How to remove spare tire / wheel?

Absence of て in お気をつけください

How do helicopters avoid detection from enemies by using IR suppressors?

Ambiguous Overload Templates

more hot questions

Question feed

Subscribe to RSS

Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-cpp

Leave a Reply

Your email address will not be published. Required fields are marked *