1

Possible Duplicate:
Open source C++ library for vector mathematics

I've to do a very simple question: how can I do basic operations like sum, difference or product on two diffent int vector, like in matlab, using c++? does exist any function that can do it? Thanks in advance.

Community
  • 1
  • 1
tiavec88
  • 345
  • 1
  • 4
  • 12
  • The C++ standard library have _many_ [algorithm functions](http://en.cppreference.com/w/cpp/algorithm) that can be used for a multitude of things. – Some programmer dude Nov 23 '12 at 10:49
  • For vector-to-vector math ops, there is nothing that does it for you in the standard library, but things that can get you near halfway for many operations. Review the standard algorithms and see which ones you may be able to adapt. – WhozCraig Nov 23 '12 at 10:52

5 Answers5

1

Not in the standard library, you will have to use a third party library, I don't know what your requirements are, but you could take a look at something like boost::ublas.

Nim
  • 33,299
  • 2
  • 62
  • 101
1

Take a look at the standard algorithms: http://en.cppreference.com/w/cpp/algorithm

Ron Kluth
  • 51
  • 3
0

Depending on your requirements (which you didn't really elaborate on), you might be looking for anything from std::for_each to Boost::uBLAS...

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • I simply like to initialize vector and do maths operations like: v1={0,1,2} v2={2,3,4} v1+v2=???? I know that should be very very simple. – tiavec88 Nov 23 '12 at 10:59
  • 3
    @tiavec88: there is a standard template `valarray` that does addition like a mathematical vector, but it doesn't have a great reputation and by no means is it a complete set of operations for matrices. `std::vector` has less to do with the mathematical concept of a vector than you might think from the name. It does not in fact define a vector space. Matlab is *for* mathematics, there are a lot of things that are very simple in Matlab but aren't provided in the standard C++ libraries. – Steve Jessop Nov 23 '12 at 11:12
0

Use std::accumulate to accumulate a single value e.g. the total sum or total product.

Use std::inner_product to generate a value which is the result of a binary operator between values in 2 vectors, and a binary operation between successive results. This is a very useful function, if you can formulate your problem correctly. This is related to MapReduce.

Although, probably what you really want is std::transform which can operate on two inputs and write into a third output.

Community
  • 1
  • 1
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
0

You can write you own class, like class Vector, class Matrix, where you overload the operators like +, -, *. Or you can use libraries like LAPACK, boost ublas, ...

ISTB
  • 1,799
  • 3
  • 22
  • 31