Installation of Julia in Mac and Windows is very simple.
For linux, if we have a Ubuntu, we can go to https://julialang.org/downloads/ and download a version of "Generic Linux Binaries for x86". Then decompress it, you will see julia has been there (bin directory).
启动julia,然后在julia terminal中输入
using Pkg
Pkg.add("IJulia")
装完后退出,然后启动jupyter lab即可。
The result from the last computation performed by Julia will always be stored in ans, see the example below
In [22]:
a = 1
Out[22]:
In [23]:
b = ans+2.
println(b)
print(ans)
When using new package in Julia, just use
In [25]:
using PyPlot
Recall that the first thing you need is to switch to the package manager mode with ]. After doing so, and to check the status of your packages, and to add, update, and remove additional packages, you can use:
In [ ]:
st # checks status
add IJulia # add package IJulia
up IJulia # update IJulia rm IJulia # remove package
Genrally we can use a package in the julia shell mode by typing:
In [31]:
using PyPlot
or we can use a specified function of a package, say
In [32]:
import PyPlot: plot
In most occasions, however, importing the whole package will be the simplest approach and the recommended default. Some other useful packages:
We can also install some other packages that can faciliate the interaction of Julia and other languages like python
Julia has variables, values, and types. A variable is a name bound to a value. Julia is case sensitive, so "a" is a different variable than "A". A variable doesn't hve a type, but its value has the type which attributes the content. All the values in Julia are thus $objects$.
In the base implementation of Julia, there are 230 different methods for the function sum! You can list them with the command as in:
In [ ]:
method_list = methods(+) # list all the methods for sum
In [ ]:
There is a types tree, but we don't need keep it in mind because we can use the built-in funciton to check its spertype and subtypes
In [39]:
supertype(Float64)
Out[39]:
In [40]:
subtypes(Integer)
Out[40]:
In [41]:
supertype(Integer)
Out[41]:
In [42]:
supertype(Real)
Out[42]:
We can always check the type of a variable with
In [45]:
a=1
typeof(a)
Out[45]:
In [46]:
# determine type of elements in collection a
eltype(a)
Out[46]:
You can fix the type a variable with the operator :: (read as “is an instance of”):
In [58]:
a::Float64
Out[58]:
In [65]:
b::Int = 10
In [66]:
sizeof(a) # check the memory size of the variable
Out[66]:
In [73]:
# If you want to know more about the state of your memory at any given time, you can
# check the workspace
varinfo()
Out[73]:
In [89]:
a=0
Out[89]:
In [90]:
varinfo()
Out[90]:
In [ ]:
Julia’s sophisticated type structure provides you with extraordinary capabilities. For instance, you can use a greek letter as a variable by typing its LATEX’s name plus pressing tab: \alpha (+ press Tab) You will get
In [91]:
α # The code will be much easier to read and maintain.
Out[91]:
You can extend this capability of to all Unicode characters and operate on exotic variables
In [93]:
α = 1
β = 2
sum = α + β
println(sum)
In [ ]:
In [ ]:
In [5]:
# Listing1.3
using Statistics
@time begin
data = Float64[]
for i in 1:10^6
group = Float64[]
for j in 1:5*10^2
push!(group, rand())
end
push!(data, mean(group))
end
println("98% of the means lie in the estimated range:",
(quantile(data, 0.01), quantile(data,0.99)))
end;
In [ ]:
In [ ]:
In [2]:
# Listing 1.4 Fast code example compared to Listing 1.3
using Statistics
@time begin
data = [mean(rand(5*10^2)) for _ in 1:10^6]
println("98% of the means lie in the estimated range: ", (
quantile(data, 0.01), quantile(data,0.99)))
end
In [6]:
;echo '1' # ";" can be used to activate the shell mode
In [8]:
In [ ]: