SGSL: A simple scientific library idea

Hello.

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?

The draft is available at github.

Sample usage (full source code at main.swift):

...
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?

Thanks and have a nice day.

9 Likes

Hi Peter,

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. :)

Have a great weekend!

Hi Renan.

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.

Have a great weekend too :)

1 Like

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. :)

1 Like

standard deviation is a two-liner using reduce, surely you don’t need a library for this

// let values:[Double] 
let mean:Double      =  values.reduce(0, +) / .init(values.count) 
let deviation:Double = (values.reduce(0){ $0 + ($1 - mean) * ($1 - mean) } / .init(values.count)).squareRoot()
1 Like

Sure, if you don’t mind making 2 passes over the sequence.

It’s also possible to find the standard deviation as well as the mean in a single pass, but it’s a bit trickier.

3 Likes

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.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy