(Re)Starting with C++ (for scientific computing) - c++

I have a fair hang of programming in various languages. I have been implementing my codes for research using MATLAB (during the past few months) and for the first time really noticed the difference in execution speed of MATLAB v$ C. (As much as I love the blazingly fast prototyping capabilities).
I am looking to pickup C++ and start using it in my research. I am aware of OOP and have programmed fair bit of Java (relatively long back) and C++ (even longer back). I would like to really get deep into C++ now and hence need suggestions for resources on the same:
What C++ things I need to pick up (STLs and. ) to really make good use of C++?
What is a good tutorial/manual to get started with?
What are the numerical/scientific libraries for C++? GSL? Is there a equivalent (features) of Scipy/Numpy for C++?
I shall be programming on Linux, so I shall be using g++ .
Any pointers to previous SO questions also appreciated.

You'll want to get to grips with parallel programming as quickly as possible. For message-passing I like this book by Karniadakis and Kirby. Of the books on OpenMP, for distributed-memory programming, this one is the best.
If you can get access to them, then Intel's Threading Building Blocks, Maths Kernel Library, and Integrated Performance Primitives are good to have. If not, there are plenty of open source alternatives, start looking at Netlib.
Oh, I almost forgot BOOST, which is a must.

In regards to numerical stuff like Numpy, you should have a look at both:
Blitz++ http://www.oonumerics.org/blitz/
and
Jama/TNT http://math.nist.gov/tnt/download.html

On the library side, check out Armadillo. It almost gives you the full extent of MATLAB's array manipulation syntax and uses LAPACK and BLAS (ATLAS) under the hood.

This tutorial absolutely rocks, but you may not want to tackle it initially.
http://www.parashift.com/c++-faq/
Make sure to read up on the STL (standard template library) and other stuff, using sites like:
http://cplusplus.com/
And, check out the Boost library:
http://www.boost.org/
To make really good use of C++, you need to learn at least the STL, that alone will save you lots of time, but as parashift mentions, C++ OOP is only programming with objects, if you don't use dynamic bindings.

TRNG is a parallel random number generation library. It allows you to create multiple independent streams and was designed for use on clusters.

Related

OpenCV vs IVT - For Beginners

I'm looking to getting into image/video processing and was searching for a good library to start with. I've heard of two, OpenCV and IVT. I'd like to hear your opinion about which one is better to start with, what are the advantages/disadvantages of both and which one is better for possible commercial use later on.
Both offer very similar functionality - about 95% of it overlaps.
Both are BSD(ish) licensed and are widely used in commercial packages.
IVT is a cleaner more modern C++ design, but the new c++ bindings to opencv work well. Opencv has a few more 'C' type macros but it also means it's usable from C. Opencv is also very well supported by python and other languages, don't know about IVT.
There is also CImg. It requires only a single header file and uses C++ type templates so you write code like result = image.blur().sharpen().edge() almost like Mathematica!
It doesn't have the same depth of functionality, especially in things like recognition and machine learning, but is definitely easier to use. It's GPL/LGPL so usable commercially with care.
OpenCV is much more widely used, so has a bigger set of users who might answer questions - but it also has a MUCH bigger set of beginners asking questions !
The decider for me is that openCV is moving to support (almost) all the functionality in CUDA (ie on a parallel GPU) which is fantastic for anything needing realtime video processing.
Other than this I couldn't comment on the performance I didn't really benchmark IVT enough. OpenCV does use custom SSE2 assembler for a lot of the operations and uses TBB to parallel the rest if you have a multicore/hyperthreaded CPU.
I am a beginner like you and I personally say OpenCV. My first learning experience with OpenCV was more effortless than IVT because documentation was so neat and clear, also there are common beginner books, many tutorials and example projects for OpenCV.

replace c++ with go + swig

I recently asked this question https://softwareengineering.stackexchange.com/questions/129076/go-instead-of-c-c-with-cgo and got some very interesting input. However there's a mistake in my question: I assumed cgo could also be used to access c++ code but that's not possible. Instead you need to use SWIG.
The go faq says "The cgo program provides the mechanism for a “foreign function interface” to allow safe calling of C libraries from Go code. SWIG extends this capability to C++ libraries. "
my question:
Is it possible to access high-level c++ frameworks such as QT with SWIG + Go and get productive? I'd like to use Go as a "scripting language" to utilize c++ libraries.
Have you any experience with go and swig? Are there pitfalls I have to be aware of?
Update/Answer: I've asked this over IRC too and I think the question is solved:
SWIG is a rather clean way of interfacing c++ code from other languages. Sadly matching the types of c++ to something like go can be very complex and in most cases you have to specify the mapping yourself. That means that SWIG is a good way to leverage an existing codebase to reuse already written algorithms. However mapping a library like Qt to go will take you ages. Mind it's surely possible but you don't want to do it.
Those of you that came here for gui programming with go might want try go-gtk or the go version of wxWidgets.
Is it possible? Yes.
Can it be done in a reasonably short period of time? No.
If you go back and look at other projects that have taken large frameworks and tried to put an abstraction layer on it, you'll find most are "incomplete". You can probably make a fairly good start and get some initial wrappers in place, but generally even the work to get the simple cases solved takes time when there is a lot of underlying code to wrap, even with automated tools (which help, but are never a complete solution). And then... you get to the nasty remaining 10% that will take you forever (ok, a really really long time at least). And then think about how it's a changing target in the first place. Qt, for example, is about to release the next major rewrite.
Generally, it's safest to stick to the framework language that the framework was designed for. Though many have language extensions within the project itself. For example, for Qt you should check out QML, which provides (among many other things) a javascript binding to Qt. Sort of. But it might meet your "scripting" requirement.
A relevant update on this issue: it is now possible to interact with C++ using cgo with this CL, which is merged for Go 1.2. It is limited, however, to C-like functions calls, and classes, methods and C++ goodies are not supported (yet, I hope).

C or C++ for a Robot?

Greetings,
I am trying to decide between C and C++ for my robot. I am a 5+ year veteran of Visual Basic.NET, however I'm going with Linux (Ubuntu) on this robot, and there is a compatibility problem between Linux and the .NET Framework. I want to stick with whichever language I choose for all of my projects, so I want to make sure that I choose the most appropriate one for the task.
For reference, I will describe my current robot in progress and what I am going to do with it. I am in the process of building a full-sized R4 Astromech (yep, I'm one of those guys). I have incorporated a PC motherboard with an Intel Core 2 2.1 GHz processor, 1 GB RAM. I will be using a scratch-built parallel interface card to control the drive motors, head motor, as well as a secondary parallel interface card (going to a second parallel port) which all of the sensors (IR, Ultrasonic Ranging, Visual Recognition via webcam, etc.) will be going to. Commands will be given using speech recognition (currently have a VB.NET scratch-built recognition program that I will be adapting to the new language).
Given the specifications and desired goals listed above, would I be better off with C or C++? I greatly appreciate any suggestions that you might have.
Thanks!
Thaskalas
What do you mean by a compatibility problem? Have you looked at Mono? It's an open-source implementation of the .NET libraries. It's geared toward C# not VB.NET but if you're more comfortable in a .NET environment use that. Speed isn't really an issue here as a Core2Duo is plenty fast for what you need to do.
If Mono won't work for you, I'd recommend C++. There are a lot more libraries out there for C++ (or at least, I am familiar with more, e.g. Boost), which can use most C libraries too. There's no real speed penalty for using C++. While using C wouldn't be bad per-se, C++ has some benefits and no drawbacks, so it's probably the better choice.
I would recommend using ROS. It will let you get started with a sophisticated Inter-Process Communications manager, as well as a large library of sophisticated robotics code, including multiple implementations of SLAM and other critical robotics algorithms. ROS also lets you program in multiple languages, including C, C++, and Python, so you aren't stuck with one language or another down the road.
I would also recommend C++ and ROS. In our company we're migrating to it, because there's so many people working on it, expanding it, and adding lots of cool features.
With this, you can forget about implementing most of the basic low-level stuff and start working on what you intend to research.
It's really easy to set up and start developing.
Since you're running Linux on it, I'd recommend a split approach, where you do the lower-level (device interface, where you may need fast performance) stuff in C (or C++), and the higher level stuff in a modern language like C# (using Mono) or Java, or even Python.
Python especially is hugely expressive, has a large set of libraries, and has a pretty straightforward C interface.
Writing your high-level control stuff in a low-level language like C/C++ will get old fast (IMHO). Robots should be fun!
Have you considered D? It's a fairly new language, is compiled to native code and can link directly to C. (The entire C standard library is even available from D, and bindings to the POSIX API are included in the standard library.) Basically all you need to do to use any C library from D is compile it with a C compiler and translate the function prototypes, constant declarations, etc. in the header file.
D is low-level enough that an experimental kernel is written in it, but has modern features like garbage collection (though manual memory management is still permitted), builtin strings and arrays, and more advanced/easier to use template metaprogramming facilities than C++. The biggest disadvantage is lack of a mature toolchain and libraries for enterprise-y things, but for your purposes that probably doesn't matter. BTW, if you need to do a bunch of matrix manipulation and stuff, there's the SciD project, which provides nice templated wrappers over LAPACK and BLAS.
Use C++. You have the space. You can use it "as a better C" to start with.
C++ is a bigger tool bag; why would you not want that!? You need not use all the tools, but with C you'd have no choice. Most importantly with C++ you have the choice of using both C and C++ third-party libraries.

C or C++ for pattern recogniton/image processing?

I about to take some courses in Pattern Recognition.
As i have no prior knowledge in either C or C++, my professors told me to learn a bit of one of them before the course, and learn more when doing the course.
Which one should i pick?
The prior knowledge in programming i have is limited to mostly C# but some PHP, SQL and Prolog as well.
The choice of a low-level language like C or C++ probably means you are into performance at the cost of the development time.
If this is your first low-level language, then learn C. It is simple, robust and proven language, and it allows to write the fast code. It has a decades long record of portability. It is much easier to integrate C code with code written in other languages. With C++ it is too easy to make things wrong. C++ requires much greater degree of language mastery and much more programmer's attention to make things right. While it is possible to write fast code in C++, it's more of an art than doing the same thing in C.
If you have only a few months to learn, then at the end you'll be able to write an OK C code, but this time is simply not enough to get enough experience with C++, hence your C++ code written in the first year or two will be awful.
See, for example, severe criticism of C++ from Linus Torvalds: C++ is a horrible language and C++ productivity. Basically, it boils down to C++ being too complicated even for professional programmers, and C++ code being ambiguous with context-dependent behaviour (this may be considered a higher-level language feature, but it makes more difficult to reason about the performance).
One of the major open source libraries for computer vision, OpenCV, is available both for C and C++, but it is also available for Python, which is a much easier language to get the things done quickly (and also to learn as a first language). BTW, I assume if you manage to offload most of the work to the library, which itself is written in C/C++, the performance cost of Python won't be huge (but generally Python is 10x slower than C).
Stroustrup (inventor of C++) argues that C++ is easier to learn than C:
There will be less type errors to catch manually […] fewer tricks to learn […], and better libraries available.
With that in mind, go for C++.
C and C++ are fundamentally different in the way they approach programming. If you have experience with C#, C++ would be a choice since it is object oriented as well. Also, even though they are different, knowing C++ will let you read (and mostly understand) C code as well. Also, check out this question for some information on the differences between these languages.
I would recommend learning C++ as this probably be easiest if you know about classes etc from C#. Also you can write free functions in C++ but is harder to write classes in C.
A standard library you will likely use is opencv.
C# will set you in good stead to master C/C++. You will probably be able to see through the opencv code examples and understand them.
You can likely get by with enough C you pick up from working through the examples and becoming familiar with them. The focus of the course will be on the algorithms and not how fancy your code is.
Sounds like a fun course! Good luck.

Matlab vs. Visual C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm doing a Windows Application the uses lots of charts. Its practically a dataviewer. I started doing Matlab, because its easier, but I realized it's too slow. I wanted to change to another language. Somebody recommended me Visual C++ or Java. But Im not sure. What language should I use??
In my opinion the speed gain from going to another "faster" language is not as much as refining your algorithm.
The "problem" with MATLAB is that it allows you to do some nasty things, such as resizing your matrix in a tight loop. You should really try to pinpoint your bottlenecks using the following command:
profile on
... run your program
profile off
profile report
This will give you nice information about which function takes how long to execute and which line creates the biggest bottleneck. You can also see how many times a function is called and a M-Lint Code Check Report is included.
These measurements and hints can show you the bottlenecks of your algorithm. if your sure there isn't a way to reduce the callcount/speed of a function using a smarter algorithm. Such as do I really need that big 2d matrix where a smart vector would be large enough, or if I found a artifact, why would I still continue searching for artifacts. You could write the functions you're experiencing the most performance problems with in c/c++ and use it as a function in matlab. You can get a big speedup out of correctly choosing which functions to implement in c/c++. There is an amount of overhead with calling a c/c++ function from MATLAB, or more correctly there is a overhead in c/c++ to get the data from MATLAB, so a function which is called 10000 times will not be the best to implement in c/c++, you'd be better of with the function higher up the callstack.
It depends on what your requirements are.
The advantage of using matlab is that it's strong in numerical calculations. If you don't need that, then there is no advantage to using matlab. In this case, all those languages are okay, and many others (Python, C#, ...) as well. It depends on which language you are most comfortable with.
If you do want the advantages of matlab then:
Try optimizing in matlab. Most optimization techniques are language independent.
There are tools to translate matlab to C automatically. You can then try to compile with all optimizations on. I seriously doubt this will help much, however, especially considering the GUI part.
First and foremost, as other answers have mentioned, you need to profile your code to find out where the bottleneck is. I would check out Doug Hull's blog at The MathWorks, specifically this entry about using the profiler. This will help you find out where all the work is being done in your code.
If the source of the slowdown is associated with data processing, there may be a number of ways to speed things up (vectorizing, writing a mex file, etc.).
If the source of the slowdown is your GUI, this may be even easier to solve. There are a number of blog posts, both from Doug and other MathWorkers, which I've seen that deal with GUI design. There have also been a few questions on SO dealing with it (here's one). If you're dealing with displaying very large data sets, this submission from Jiro Doke on The MathWorks File Exchange may help speed things up.
It's hard to give you more specific advice since I don't know how you are designing your GUI, but if that turns out to be the bottleneck in displaying your data there are many resources to turn to for improving its speed before you go through the hassle of switching to a whole other language.
Don't forget that you can create functions in C++ that can be called from Matlab. And TADA, you have access to both environments !
I would use C#. It is easier than C++ and integrates well with the Windows platform. Just find a free graphing library for it and you're good to go.
There are plenty of other options depending on your preference of language. Eg. Qt with Python or C++.
As far as I know, the most common methodology is to first do the proof of concept or just the main algorithm on Matlab, because of its ease of use and convenience for math calculations, and after that to translate it to a "real" programming language in order to improve the performance. Usually C or C++ act as the "real" language, but in your case, aiming to do a Windows application, perhaps C# will be the best option.
I found that GUI programming in MATLAB can get really nasty if your application gets more complex. BTW MATLAB can also be called from Java easily (and vice versa, current versions basically provide an interactive Java console).
Just as a side note, if you still need the math power of Matlab, you may want to check out Scilab. It's open-source and free, and it has examples of how it can be integrated with other C# or C++. I have created projects on which Scilab was running in the background to perform all the data math operations; and displaying them with C#'s ZedGraph library. Worked like magic!
I suggest you using Java and the JFreeChart (http://www.jfree.org/jfreechart/) library. I found very easy (and fast) developing applications with a lot of charts of different typologies. If you don't need particularly fast performances, you can use Java. I suppose that there are similar libraries for C#, but I'm not sure.
An alternative to Matlab and Scilab is another free software: Octave.
I don't know about Scilab, but Octave syntax is nearly the same as matlab so you can import code with minimal effort.
If you need fancy toolboxes though, Scilab and Octave might let you down, so check this.
You can execute Octave functions in a C++ program:
http://en.wikipedia.org/wiki/GNU_Octave
I do not think that you can call your own m-files functions from your C++ program though. In the past, the Matlab compiler would let users run matlab programs without installing Matlab, but not without installing a huge library (250 MB if I remember correctly). Nevermind if your Matlab program took 20 kB, you had to distribute the huge library.
Please someone edit/comment on the situation today!
It has been a while since I used the GUI "ability" of Matlab, but back then (2005) I found it awful. Ugly, hard to use, very hard to maintain, dependent on user settings of windows parameters.
Please comment or edit on that too, they may have made progress!
If they have not, I believe that Matlab is NOT the way to go for a program that you want to deliver to anyone.
If you can use Visual Studio for doing your GUI, do that. I second the earlier opinions: go with what you're comfortable with.
If you need the Matlab functions, go with what you're comfortable with, that supports Matlab libraries.
First of all Visual C++ is not a language is an IDE for developing applications.
Second... Which languages do you know? You can have several options. Take a look to:
C++ + Qt (Mine preferred option, powerful and easy to understand)
C# + .NET or WPF
Java
If you can tell more information we could find a language that matches your needs.