# Pulsars and neutron stars/Introduction to Julia for pulsar astronomers

## IntroductionEdit

Julia is a relatively new language that is extremely powerful, but the current documentation is difficult to find and understand. Here we provide some notes of interest to pulsar astronomers. Many of the figures in this wiki-book were produced using Julia. Julia can easily be [downloaded and installed on most computers. It has its own powerful routines, but it is also possible to use Python or C code directly. It has been designed for parallel or distributed computing and the programmers take pride in the speed that the code runs - it is much faster than Python.

## Getting startedEdit

Julia can be run from a personal computer, using Julia notebooks or using a webhosting service. Julia can be used as a simple calculator

```
julia> 3+3
6
julia> x=10
10
julia> x+2
12
```

It is also possible to write algebraic expressions (in a simpler manner than in most languages)

```
julia> x=10
10
julia> 2x
20
julia> 2*x
20
```

Strings can easily be created:

```
julia> x="Hello";
julia> x
"hello"
```

Note that the use of the ";" on the end of the line. If used then no output is given for that line.

## Making arrays of valuesEdit

```
julia> x=linspace(0,99,100)
julia> x+10
```

will allocate 100 values from 0 to 99. The second command will add 10 to each element in the array.

To set an array that is initialised to zero, we can use:

```
julia> x=zeros(100)
```

will allocate an array of 100 zeros.

To allocate random Gaussian noise with zero mean and variance of 1, we can write:

```
julia> x = randn(100) # Gaussian, random noise
julia> y = rand(100) # Linear, random deviates between 0 and 1
```

Note the use of the # symbol as a comment.

## Making plotsEdit

There are multiple ways to make plots in Julia, but they require the addition of new packages. The packages are:

- PyPlot
- Winston
- Gaston

To install the package, e.g.,:

```
julia> Pkg.add("Gaston")
```

### GastonEdit

Gaston is based on gnuplot, which needs to be independently installed on your system.

```
julia> Pkg.add("Gaston") # This only needs to be done once
julia> using Gaston;
julia> set_terminal("x11");
julia> x=randn(1000); # Set some random numbers
julia> plot(x) # Note not using a semi-colon here as we want to see the result
```

We can also use two arrays:

```
julia> using Gaston;
julia> set_terminal("x11");
julia> x=linspace(0,100,1000);
julia> y=randn(1000); # Set some random numbers
julia> plot(x,y)
```

### WinstonEdit

This produces MATLAB style plots

```
julia> using Winston;
julia> x=linspace(0,100,1000);
julia> y=randn(1000); # Set some random numbers
julia> plot(x,y,"b") # Plot as a blue line
julia> plot(x,y,"r") # Plot as a red line
julia> plot(x,y,"ro") # Plot as red open circles
julia> plot(x,y,"x") # Plot as red crosses
julia> oplot(x,y,"b") # Overplot the current figure using a blue line
julia> xlabel("My x-label") # Add an x-label to the current figure
julia> ylabel("My y-label (\\mus s)") # Add a y-lable to the current figure. Note the use of LaTeX symbols
julia> title("This is my title") # Add a title to the plot
julia> savefig("try.png") # Save a png file of the figure in the local directory
```

Figures can also be built up from multiple components as follows:

```
julia> using Winston;
julia> p = FramedPlot(xlabel="x-label",ylabel="y-label",xlog=true,ylog=true,xrange=(9e-4,20),yrange=(1e-14,1e-9)) # Note the setting of labels and logarithmic scaling and ranges.
julia> x = abs(randn(1000))
julia> y = abs(randn(1000))*1e-11
julia> pts = Points(x,y)
julia> add(p,pts) # This will draw the figure as requested
julia> closefig() # This closes the current figure
julia> x2 = abs(randn(1000))
julia> y2 = abs(randn(1000))*1e-11
julia> l = Curve(x2,y2,"type","dotted") # this will now add a dotted line
julia> setattr(pts,label="My data points"); # Set a label
julia> setattr(l,label="a dotted line");
julia> leg = Legend(.1,.9, {pts,l})
julia> add(p,pts,l,leg)
```

### pyplotEdit

Pyplot using the python plotting routines

```
julia> using PyPlot
julia> x=linspace(0,100,1000);
julia> y=randn(1000); # Set some random numbers
julia> p = plot(x,y)
julia> xlabel("My x label")
julia> ylabel("My y label")
julia> title("My title")
julia> grid("on")
julia> cla() # Clear the figure
julia> p = plot(x,y,linestyle="-.",marker="None",label="Base Plot",color="red") # Plot using dotted lines in red
```

Aitoff projections can be plotted using:

```
julia> using PyPlot
julia> subplot(projection="aitoff")
julia> grid("on")
```