# The counter-intuitive rise of Python in scientific computing

**URL:** https://fortran-lang.discourse.group/t/the-counter-intuitive-rise-of-python-in-scientific-computing/469
**Category:** Uncategorized
**Created:** [December 8, 2020, 1:38pm UTC](https://fortran-lang.discourse.group/t/the-counter-intuitive-rise-of-python-in-scientific-computing/469 "2020-12-08T13:38:31Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)
#### Post date: [December 8, 2020, 1:38pm UTC](https://fortran-lang.discourse.group/t/the-counter-intuitive-rise-of-python-in-scientific-computing/469/1 "2020-12-08T13:38:32Z")

</div>

> **[The counter-intuitive rise of Python in scientific computing](https://cerfacs.fr/coop/fortran-vs-python)**
>
> Why are some people in scientific computic moving from Fortran to a slower language, Python?

I found this blog post interesting, even though I do not fully agree with the authors’ conclusion. I don’t think it is the agility of Python as a programming language itself, but the developed infrastructure of libraries, online documentation and learning resources, which enabled Bob to quickly find the Python kdtree in the first place. There _is_ in fact a [kdtree](https://github.com/jmhodges/kdtree2) library available for Fortran. According to [some resources](https://stackoverflow.com/a/15230041/4283055) it even delivers performance similar to mature C and C++ libraries. However, it lacks the nice webpage, proper unit testing, documentation, and user examples, which are needed so it could stand out.

Flipping the coin of the debate, we could also say it is the lack of Fortran developer resources (and developers themselves), particularly for “classic” computer science algorithms like searching, sorting, heaps, stacks and queues, tree construction, etc., which obstructs adoption of Fortran in some domains of computational science.

The previous weekend I followed a Python [quadtree tutorial](https://scipython.com/blog/quadtrees-2-implementation-in-python/) and was quickly able to adapt it to Fortran (code is available [here](https://gist.github.com/ivan-pi/47d257f00bba4ae064580413f8bc9da9)). Usage of the tree is not any more complicated than in Python:

```nohighlight
integer, parameter :: n = 140
real(wp) :: points(n,2)
type(qtree) :: tree
integer, allocatable :: idxs(:)

! Square quadtree box with center at (0.5,0.5) and width 1
call tree%init(0.5_wp,0.5_wp,1.0_wp,1.0_wp)

! Populate with random points
call random_number(points)
call tree%populate(points)

! Query indexes in rectangle centered at (0.75,0.75) and width 0.6
idxs = tree%query(boundaries(0.75_wp,0.75_wp,0.6_wp,0.6_wp))

! Query indexes in circle with radius 0.3 centered at (0.75,0.75)
idxs = tree%query_radius([0.75_wp,0.75_wp],0.3_wp)

```

Sample output:

 ![qtree](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/3b29a95d726c1e37d4dde6671227b8811d3efbe2.png)

A few months ago I also followed a [gist](https://gist.github.com/jakevdp/5216193) by [Jake Vanderplas](https://jakevdp.github.io/pages/about.html) and created a [Fortran Balltree](https://gist.github.com/ivan-pi/661c0884069baced35c71e6d5b6fe3ce). Some errors remains in the query function ☹ ), but at the time the build process worked nicely:

 ![ball_tree_cropped](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/8755e4efbd9bd3c136e2ee6daf0a173487fca2ed.png)

I was planning to polish these some more and make them available in the future as an `fpm` package. It would be nice to find some collaborators!

---

_[View the full topic](https://fortran-lang.discourse.group/t/the-counter-intuitive-rise-of-python-in-scientific-computing/469)._
