Issue2649

I rewrite it and add docstring as well as a test.