We’re gonna learn how to make a test for your Julia code that runs whenever you publish it online. We’re gonna use
GitHub to store the code; Travis CI to run your tests; Coveralls.io to verify which lines of code your test are missing. Alternatively, for a open source alternative, see GitLab, which I don’t know enough yet.
Let’s make a litte code to solve a linear system, paying attention to the problems it may arise, like incorrect dimensions, underdetermined and overdetermined systems, singular matrices, etc.
Coveralls.io to verify which lines of code your test
are missing.
Alternatively, for a open source alternative, see GitLab,
which I don’t know enough yet.
Let’s make a litte code to solve a linear system, paying attention to the
problems it may arise, like incorrect dimensions, underdetermined and
overdetermined systems, singular matrices, etc.
And we’ll be using the factorizations, but not \.
The math
A simple (not cheap) way to do it is using Singular Value Decomposition (SVD).
We have
where $r$ is the rank of $A$.
Since the columns of $V$ form a basis for $\mathbb{R}^n$ (where $x$ resides),
then
Now, we have
If the system has a solution, that is $Ax = b$, then we multiply by $u_j^T$,
obtaining
Thus, .
If the system doesn’t have a solution, this still holds. I’ll leave the steps to
you.
If , then $\alpha$ has undetermined values. However, when that’s the
case, the solution we’ll use is the one with the smallest norm, that is, the one
that minimizes $\Vert x\Vert$.
Since are orthonormal, then
.
So, in order to minimize the norm of x, we use for $i > r$.
The code
We’ll store the code on this GitHub
repository.
Note, however, that it will point to the completed version.
A possible implementation of our code is as follows:
# File src/solve_linear.jlfunction linear_system(A,b)(m,n)=size(A)ifm!=length(b)error("A and b are not compatible")endU,σ,V=svd(A)returnV*(U'*b./σ)end
If the result is around $1e-16$, then everything went well.
Very rarely, the generated matrix could be ill-conditioned.
Run again, to verify if that’s the case.
If everything went well, we’ll write a test now.
For now, let’s write a simple test running a lot of square linear systems.
For each system, to avoid using A\b, we’ll create a vector b from a known
solution. Then we’ll assure that
and .
To do that, we’ll use Base.Test.
Note however, that the condition of the matrix influences the error, and there
are numerical errors involved. So we’ll use the condition
.
The code is
The first line is a kludge to read the correct file from wherever the run the
code. If you’re not building a module, this is ok. But normally you’ll want to
build a module. Ignore that for now.
The first for varies the dimension, and the second for runs the code a specific
number of times.
This totals a hundred square linear systems being run.
The @test macro verifies that the given expression is true.
If any solution is wrong, the code will be wrong. Also, if you use a smaller
tolerance, the numerical rounding may give a error here.
Ok, first thing you wanna do now is commit this code.
Then, go to GitHub, create an account, then a repository for this code (e.g.
linear_system.jl), then push the code. Use the name with .jl in the end for the repository.
Enter your password and verify the code is online.
Online testing
Now go to Travis and create an account. Go to your profile and
click on the Sync account button if necessary.
Find your repository and set the button to on.
Now, with the next commit, a test will start. Let’s make it happen.
Create a file .travis.yml (yes, with a leading dot) with information for the
build. Here’s a simple file:
Now, go to your travis page, and after a while you’ll see your repository with a
test running (or already finished, because it is short).
You should have a passing test. If not, verify your files again, then the error
on travis.
Notice that you can see the complete log of what is run.
Using an online automated testing is useful for many reasons:
Everyone can see if the code is working;
Pull requests generate a travis build, so you can see if it’s working;
You don’t forget to test;
You test on a clean environment;
You can test with multiple versions of Julia (or other linguage).
Coverage
Now, let’s see the code coverage.
First, for coverage you’ll need a package to see the coverage, and the service
to publish the coverage.
Use Coverage.jl to see your coverage
(including locally).
Install with
julia>Pkg.add("Coverage")
Then run
$ julia --code-coverage=user --inline=no test/test1.jl
This will generate a file src/solve_linear.jl.xxx.cov with the information.
The option --inline=no gives more accurate results, but slow down the code.
You can see which function are not being run by reading it, but it’s better to
see it online.
To see a summary, use
julia>usingCoveragejulia>cov=process_folder()julia>c,t=get_summary(cov)julia>println("$(100c/t)% of lines covered")
But we want to see it online. So go to Coveralls.io and create an account.
Click on Add repos and find you repository.
Enable it, and change the .travis.yml file to
After a success, we install Coverage and run the relevant code.
Check your repository on Coveralls to see the results.
Notice how the error line on our code never gets called.
Improving to a module
If you want people to use your code, you should use a module in Julia.
This allows easy installation of your code, and not much more work.
Changing to a module is very simple, so I’ll run through it.
The folders src and test are required. But we also need
A file in src with the same name as the repository;
The keyword module on that file;
export the relevant functions;
A file test/runtests.jl that run the tests;
A README.md for people to know about your thing;
A LICENSE.md for people to know what they can do with your file;
Different .travis.yml.
I’m using the name BlogAutomatedTesting.jl, so a create the file
# file src/solve_linear.jlexportlinear_systemfunction linear_system(A,b)(m,n)=size(A)ifm!=length(b)error("A and b are not compatible")endU,σ,V=svd(A)returnV*(U'*b./σ)end
The class is conducted in Octave, which I find a little annoying to
deal with as a language, so to play around with some data, I wanted to
convert the procedure to Julia. Not having
found anything prebuilt from quick Googling, I translated it myself,
and leave it here for anyone who'd like to refer to it later. The
mathematical formulas I'm working from are in the above-referenced
Wikipedia article, at "Non-degenerate
case".
The set of data I'll represent as X.
First we calculate our μ, just the mean of each column in X:
mu = mean(X, 1)
Having found our mean, we can now calculate the covariance matrix:
function covariance(x, mu)
n = length(x[:,1])
(x .- mu)' * (x .- mu) ./ n
end
sigma = covariance(X, mu)
And having both μ and Σ, we can calculate the probability for any
vector x against our distribution: