딥스탯 2018. 9. 11. 21:02
1_Getting_started

Getting started

Reference

https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/intro-to-julia (github : JuliaComputing/JuliaBoxTutorials/intro-to-julia)

Topics:

  1. How to print
  2. How to assign variables
  3. How to comment
  4. Syntax for basic math
  5. Exercises

Series

http://deepstat.tistory.com/46 (01. Getting started(한글))

How to print

Using println() to print

In [1]:
println("Hello, Julia!")
Hello, Julia!

How to assign variables

In [2]:
a = 1
println(a)
1
In [3]:
typeof(a)
Out[3]:
Int64
In [4]:
b = 1.1
println(b)
typeof(b)
1.1
Out[4]:
Float64
In [5]:
😺 = "smiley cat!"
println(😺)
typeof(😺)
smiley cat!
Out[5]:
String

To type a smiley cat, use tab completion to select the emoji name and then tab again

In [6]:
# \:smi + <tab> --> select with down arrow + <enter> ---> <tab> + <enter> to complete

After assigning a value to a variable, we can reassign a value of a different type to that variable without any issue.

In [7]:
😺 = 1
println(😺)
typeof(😺)
1
Out[7]:
Int64

This allows us to write code like

In [8]:
😄 = 0
😠 = -1
😄 - 😺 == 😠
Out[8]:
true

How to comment

In [9]:
# We can leave comments on a single line using the pound/hash key
In [10]:
#=

For multi-line comments,
use the '#= =#' sequence.

=#

Syntax for basic math

In [11]:
my_sum = 3 + 7
Out[11]:
10
In [12]:
my_difference = 10 - 3
Out[12]:
7
In [13]:
my_product = 20 * 5
Out[13]:
100
In [14]:
my_quotient = 100 / 10
Out[14]:
10.0
In [15]:
my_power = 10 ^ 2
Out[15]:
100
In [16]:
my_modulus = 101 % 2
Out[16]:
1

Exercises

1.1

Look up docs for the convert function.

In [17]:
?convert
search: convert code_native @code_native

Out[17]:
convert(T, x)

Convert x to a value of type T.

If T is an Integer type, an InexactError will be raised if x is not representable by T, for example if x is not integer-valued, or is outside the range supported by T.

Examples

jldoctest
julia> convert(Int, 3.0)
3

julia> convert(Int, 3.5)
ERROR: InexactError: Int64(Int64, 3.5)
Stacktrace:
[...]

If T is a AbstractFloat or Rational type, then it will return the closest value to x representable by T.

jldoctest
julia> x = 1/3
0.3333333333333333

julia> convert(Float32, x)
0.33333334f0

julia> convert(Rational{Int32}, x)
1//3

julia> convert(Rational{Int64}, x)
6004799503160661//18014398509481984

If T is a collection type and x a collection, the result of convert(T, x) may alias all or part of x.

jldoctest
julia> x = Int[1, 2, 3];

julia> y = convert(Vector{Int}, x);

julia> y === x
true

1.2

Assign 365 to a variable named days. Convert days to a float.

In [18]:
days = 356
Out[18]:
356
In [19]:
days_copy = convert(Float64,days)
Out[19]:
356.0
In [20]:
println(days)
typeof(days)
356
Out[20]:
Int64
In [21]:
println(days_copy)
typeof(days_copy)
356.0
Out[21]:
Float64

1.3

See what happens when you execute

convert(Int64, "1")

and

parse(Int64, "1")
In [22]:
convert(Int64, "1")
MethodError: Cannot `convert` an object of type String to an object of type Int64
Closest candidates are:
  convert(::Type{T<:Number}, !Matched::T<:Number) where T<:Number at number.jl:6
  convert(::Type{T<:Number}, !Matched::Number) where T<:Number at number.jl:7
  convert(::Type{T<:Integer}, !Matched::Ptr) where T<:Integer at pointer.jl:23
  ...

Stacktrace:
 [1] top-level scope at In[22]:1
In [23]:
parse(Int64, "1")
Out[23]:
1
In [24]:
typeof(parse(Int64, "1"))
Out[24]:
Int64
In [25]:
?parse
search: parse tryparse partialsortperm partialsortperm! pairs skipchars

Out[25]:
parse(type, str; base)

Parse a string as a number. For Integer types, a base can be specified (the default is 10). For floating-point types, the string is parsed as a decimal floating-point number. Complex types are parsed from decimal strings of the form "R±Iim" as a Complex(R,I) of the requested type; "i" or "j" can also be used instead of "im", and "R" or "Iim" are also permitted. If the string does not contain a valid number, an error is raised.

Examples

jldoctest
julia> parse(Int, "1234")
1234

julia> parse(Int, "1234", base = 5)
194

julia> parse(Int, "afc", base = 16)
2812

julia> parse(Float64, "1.2e-3")
0.0012

julia> parse(Complex{Float64}, "3.2e-1 + 4.5im")
0.32 + 4.5im