My 2 cents.
I think it is easier to write generally about this, rather than just
about C/C++. First, libraries in languages like Python are not
necessarily used to get a speed benefit, even if that is a
consequence. I think
@David covered the
reasons pretty well.
Taking it from the top, the language implementation to some extent
dictates what libraries you have access to. Commonly used languages in
computational science include C, C++, Python, Perl, Java, Fortran, and
R. Less common examples might be Ocaml and Common Lisp. Now, since
most of these languages are written in C, they have a natural Foreign
function
interface to
C. However, it is not so easy to call, say, a Perl library from Python
or vice versa. So in practice people tend to either
Use a library written in their implementation language, usally
something that is part of the standard libraries, or otherwise widely
available, or
Call a C/C++ library through the languages FFI. This assumes that
a wrapper does not already exist, since if it does, it is not easily
distinguishable from (1).
(2) is usually harder, because you have to wrap the C/C++ function
yourself. Also, you have to either bundle the library, or add an extra
dependency. For that reason, people are more likely to use the builtin
language libraries rather than use GSL for example, which is in C.
For very generic routines, say generating random samples from
distributions, or basic numerical routines like quadrature of
integrals, it is easy and common to reuse some library. As the
functionality one is trying to implement becomes more complex, it
becomes exponentially more unlikely that one is going to find the
exact function one wants in another library, and even one does, one
could spend lots of time searching and finally adapting the function
as necessary (the code style/design could be a problem for
example). And as discussed above, one has access to only a subset of
the libraries out there. On the other hand, implementing an algorithm
oneself if it is complex and not the main focus can be daunting, and
of course one has to deal with those pesky speed issues.
So, this becomes an optimization problem in cost/benefit analysis. My
experience is that even for comparatively standard techniques like
MCMC, I usually wind up writing my own code, because it fits better
with how I am designing the overall software.
Of course, even if you end up not using the code, it is possible to
learn from other people's code. I don't know how often scientists
actually bother to do this, though. My impression is that reading
other people's code to learn is more a software engineer thing.