Page 1 of 1

Difference between SimTK::Vector and std::vector

Posted: Sun Oct 11, 2015 12:44 pm
by jp123909
hi,

I have created a SimTK::Vector and a std::vector. I found that the resize() method was different.

if we have two empty vector SimTK::Vector a and std::vector b now,
a.resize(10) will return a vector with ten elements but without any default value (the value is random).
b.resize(10) will return a 10 dimensional zero vector.

Are there are any other differences between usage of SimTK::Vector and std::vector like this?

best regards,
jiang

Re: Difference between SimTK::Vector and std::vector

Posted: Sun Oct 11, 2015 6:47 pm
by sherm
Hi, Jiang. Yes, there are many differences between SimTK::Vector (a 1-dimensional numeric object) and std::vector (a container class). Other than the similarity in the names they are basically unrelated classes. Simbody has Vector and Matrix classes for dealing with 1- and 2-d numerical objects. Here a Vector is the type of a column of a Matrix (there is an obscure RowVector class that is the type of a row of a Matrix).

Regarding resize(), std::vector::resize() changes size while preserving the existing content. SimTK::Vector::resize() changes size without preserving existing content; the method SimTK::Vector::resizeKeep() is available to provide the more expensive option in which the existing content is preserved. Also, Vector and Matrix objects are intentionally uninitialized in Release builds to avoid unnecessary touching of large amounts of memory. In Debug builds their elements are initialized to NaN.

You might want to scan through the Doxygen documentation for Vector here (be sure to look at the inheritance diagram since most methods are inherited from VectorBase or MatrixBase). There is also a design document about Simbody's arithmetic classes here.

Regards,
Sherm

Re: Difference between SimTK::Vector and std::vector

Posted: Mon Oct 12, 2015 4:18 am
by jp123909
hi Sherm,

Thank you very much for your reply.
That is very helpful for me to understand SimTK::vector and Array.

best regards,
jiang

Re: Difference between SimTK::Vector and std::vector

Posted: Mon Oct 12, 2015 9:20 am
by sherm
I should also mention that Simbody's SimTK::Array_<T> class is intended to be a container class like std::vector<T>; in that case the APIs are intended to be the same -- the classes are almost interchangeable.