Recently I needed to do simple mathematical calculations in my REST API. Unfortunately, I could not find any (maintained) library that would allow me to compute things like standard deviation, except Swift 4 TensorFlow, which sounds like overkill dependency.
What about using an existing C library (gnu gsl) and provide friendly Swifty APIs on top of it?
...
for _ in 0 ..< 50_000 {
populations.append(Array.random(count: 1000))
}
...
var results = populations.map { $0.standardDeviation() }
...
vs. Python alternative:
...
for i in range(0, 50_000):
populations.append(numpy.random.rand(1000))
...
results = [x.std() for x in populations]
...
The pleasant result is that Swift code that calls underlying C is faster than Pythons numpy.
Swift:
Computation time: 0.691 s (only map computing standard deviation)
Total time: 1.294 s (whole script)
Python:
Computation time: 1.319 s
Total time: 2.088 s
I am curious, what do you think about a library like this?
Would it be useful for you, or do you think it would be useful for the community?
Is there anybody who would want to help with this project?
I think this is a great idea. Swift has a lot of potential as a general programming language, but it's still a bit restricted by the fact that most libraries and support out there focus on iOS development. I think adding scientific features like these to the language will be a big step forward. I would personally prefer using Swift over Python for this stuff, as it's a nicer language, and like you noticed, substantially faster.
If you want help with the project, I would be willing to contribute. Let me know if you're interested. :)
Yes, that would be awesome! Feel free to check the repository. I am quite new in the Swift territory, so I will be glad for any ideas about how to do it better. So far, there are only a few functions from GSL, if you would like to add more, perhaps just open an issue and assign it to yourself, so we will not work on the same thing.
Today I implemented automatic code documentation generation.
Oops, sorry for the delay. For some reason I expected a notification if you responded. I'll open up the repository and see what I can do with it over the next few days. :)
Many standard library functions can be replaced by some one-liners (albeit long ones), but it doesn't mean they shouldn't exist. In addition to the convenience they bring, things like scientific libraries help bring Swift out of the "Swift is the iOS language" stigma.
sorry for coming this late I would be interested into her to built this, I recently switched to python and I am entering into swift from C/C++ background and after doing stuff on science will be more than happy to help on anything I could :).