My very first Julia package: Simpson.jl

Hi,
Simpson.jl is a Julia package to integrate y(x) using samples and the composite Simpson’s rule. If x is None, spacing of dx is assumed. If there are an even number of samples, N, then there are an odd number of intervals (N-1), but Simpson’s rule requires an even number of intervals. The parameter ‘even’ controls how this is handled.

The code is based on SciPy v1.7.1

Comments and suggestions are welcome :slight_smile:

ATB, Adam

6 Likes

One thing I would recomend is using Symbols rather than Strings for the options. There’s no reason to do string comparison for code like this.

4 Likes

Thanks, I’ll learn about using Symbols and modify the code.

1 Like

Looks great for a first package. I especially love the documentation, if only all smaller/obscure packages were this well documented :wink:

One suggestion is that instead of using error you should be more specific with which type of error is being thrown. For example, the first error says: error("If given, length of x must be the same as y.") which is a problem with the user’s input, so it’s an ArgumentError.

Some previous discussion on this can be found here.

3 Likes

Thanks!
I’ve corrected error handling as you suggested.

Your code with Symbol instead of String looks like this:

function simpson(y, x=nothing, dx=1.0; even=:avg)
    #code
    if N % 2 == 0
        # code
        if even in (:avg, :first)
            # code
        end
        # Compute using Simpson's rule on last set of intervals
        if even in (:avg, :last)
            # code
        end
        if even === :avg
            # code
        end
        # code
    else
        # code
    end
    return result
end

I could do a PR, but I don’t have a NotABug account.

Done, thanks for the hint!

1 Like