In [1]:
println("Greetings! স্বাগতম!")


Greetings! স্বাগতম!

In [2]:
julia -v


julia not defined
while loading In[2], in expression starting on line 1

In [3]:
print(julia -v)


julia not defined
while loading In[3], in expression starting on line 1

In [4]:
pi


Out[4]:
π = 3.1415926535897...

In [5]:
typeof(1)


Out[5]:
Int64

In [6]:
WORD_SIZE


Out[6]:
64

In [7]:
typeof(ans)


Out[7]:
Int64

In [8]:
1/Inf


Out[8]:
0.0

In [9]:
1/0


Out[9]:
Inf

In [10]:
x = 3
(x-1)x


Out[10]:
6

In [11]:
x = 1
x+= 3


Out[11]:
4

In [12]:
z = hypot(1, 3)


Out[12]:
3.1622776601683795

In [13]:
! + 2 im


syntax: extra token "im" after end of expression
while loading In[13], in expression starting on line 1

In [14]:
1 + 2im


Out[14]:
1 + 2im

In [15]:
sqrt(4)


Out[15]:
2.0

In [16]:
sqrt(-1)


DomainError
while loading In[16], in expression starting on line 1

 in sqrt at math.jl:133

In [17]:
2//3


Out[17]:
2//3

In [18]:
Int("x")


type cannot be constructed
while loading In[18], in expression starting on line 1

In [19]:
Int('x')


type cannot be constructed
while loading In[19], in expression starting on line 1

In [20]:
Char(120)


type cannot be constructed
while loading In[20], in expression starting on line 1

In [21]:
'A' < 'a'


Out[21]:
true

In [22]:
str = "Hello, World"


Out[22]:
"Hello, World"

In [23]:
str[1]


Out[23]:
'H'

In [24]:
str[10]


Out[24]:
'r'

In [25]:
str[end]


Out[25]:
'd'

In [26]:
endof(str)


Out[26]:
12

In [1]:
mystring = "Hello World"


Out[1]:
"Hello World"

In [2]:
mystring[6:6]


Out[2]:
" "

In [3]:
mystring[6:7]


Out[3]:
" W"

In [4]:
this67 = mystring[6:7]


Out[4]:
" W"

In [5]:
typeof(this67)


Out[5]:
ASCIIString (constructor with 2 methods)

In [6]:
length(mystring)


Out[6]:
11

In [7]:
for i = 1:endof(mystring)
    try
        println(mystring[i])
    catch
        #ignore index error
    end
end


H
e
l
l
o
 
W
o
r
l
d

In [9]:
greet = "Hello"
whom = "World"
bothtogether = string(greet, ", ", whom)


Out[9]:
"Hello, World"

In [10]:
$greet $whom


unsupported or misplaced expression $
while loading In[10], in expression starting on line 1

In [11]:
"$greet, $whom"


Out[11]:
"Hello, World"

In [12]:
v = [1,2,3]


Out[12]:
3-element Array{Int64,1}:
 1
 2
 3

In [13]:
world = "world"
"Hello, $world"


Out[13]:
"Hello, world"

In [14]:
contains("Hello World", "world")


Out[14]:
false

In [15]:
contains("Hello World", "World")


Out[15]:
true

In [16]:
contains("Hello World", 'o')


`contains` has no method matching contains(::ASCIIString, ::Char)
while loading In[16], in expression starting on line 1

In [17]:
in("Hello World", 'o')


Out[17]:
false

In [18]:
# create repeats
myreps = repeat("x", 10)


Out[18]:
"xxxxxxxxxx"

In [19]:
# you can join words and letters
thisJoined = join(["a", "b"],"," )


Out[19]:
"a,b"

In [23]:
myreg = r"^\x*(?:#|$)"


Out[23]:
r"^\x*(?:#|$)"

In [21]:
typeof(ans)


Out[21]:
Regex (constructor with 3 methods)

In [24]:
ismatch(myreg, "not a comment")


Out[24]:
false

In [25]:
match(myreg, "not a comment")
# will not result in anything

In [26]:
match(myreg, "# a comment")
# will result in hash


Out[26]:
RegexMatch("#")

In [27]:
function myf(x,y)
    z = x * y
end
myf(2,3)


Out[27]:
6

In [28]:
myf


Out[28]:
myf (generic function with 1 method)

In [31]:
function g(x,y)
    #return x * y
    return x + y
end
g(2,3)


Out[31]:
5

In [32]:
+(1,2,3)


Out[32]:
6

In [46]:
function hypot(x,y)
    x = abs(x)
    y = abs(y)
    if x > y
        r = y/x
        return x * sqrt(1+r*r)
    end
    if y == 0
        return zero(x)
    end
    r = x/y
    return y*sqrt(1+r*r)
end
hypot(5,2)


Out[46]:
5.385164807134505

In [48]:
hypot(6,5)


Out[48]:
7.810249675906656

In [49]:
hypot(6,0)


Out[49]:
6.0

In [50]:
*(1,2,4)


Out[50]:
8

In [51]:
function foo(a,b)
    a+b, a*b
end
foo(2,3)


Out[51]:
(5,6)

In [52]:
x,y = foo(2,3); x


Out[52]:
5

In [53]:
y


Out[53]:
6

In [54]:
## compound expressions
# single expression evaluates several subexpressions
# returns the value of last expression

x = begin
    a = 1
    b = 2
    a+b
end


Out[54]:
3

In [55]:
x


Out[55]:
3

In [56]:
# Another way to write the same thing
p = (d = 1; e = 2; d + e)
p


Out[56]:
3

In [58]:
# Yet another way
q = begin g1 = 1; h = 2; g1 + h end
q


Out[58]:
3

In [59]:
## Conditional evaluation

a = 10
b = 20

function test(a,b)
if a < b
    println("a is less than b")
elseif a == b
    println("a is equal to b")
else 
    println("a is not less than b")
end
end


Out[59]:
test (generic function with 1 method)

In [60]:
test(10,20)


a is less than b

In [61]:
test(20,10)


a is not less than b

In [62]:
test (10, 10)


a is equal to b

In [65]:
## if blocks are leaky and you can use new variables within if

function test2(x,y)
    if x < y
        relation = "less"
    elseif x == y
        relation = "same"
    else 
        relation = "more"
    end
    println("x is ", relation, "  y")
end

test2(10, 20)


x is less  y

In [66]:
# note the variable "relation" is defined within if block
# it can still be used outside of the if block

In [67]:
# Example of chaining
x = 1; y = 2
println(x < y ? "less than" : "more than")


less than

In [68]:
x = 7; y = 2
println(x < y ? "less than" : "more than")


more than

In [69]:
i = 1; 
while i <= 5 
    println(i) 
    i += 1
end


1
2
3
4
5

In [71]:
## use of for
for i = 1:5
    println(i)
end


1
2
3
4
5

In [72]:
function producer()
    produce("start")
    for n = 1:4 # that is n has a value from 1 to 4
        produce(2n) # multiple n with 2
    end
    produce("stop")
end;

## now, we are going to consume the values produced
p = Task(producer)
consume(p)


Out[72]:
"start"

In [73]:
consume(p)


Out[73]:
2

In [74]:
# what happens when we do a for loop on this?
for x in Task(producer)
    println(x)
end


start
2
4
6
8
stop

Scope of variables

  • Region of code where a variable is visible
  • Avoids name conflicts
  • scope blocks (blocks of code used like that)

In [75]:
function foo(n)
    x = 0
    for i = 1:n
        x = x + 1
    end
    x
end


Out[75]:
foo (generic function with 2 methods)

In [76]:
foo(10)


Out[76]:
10

In [77]:
function foo(n)
    x = 0
    for i = 1:n
        local x
        x = 1
    end
    x
end
foo(10)


Out[77]:
0

Why 0?

  • Because x exists only inside the loop

In [78]:
# x need not come before
function foo(n)
    f = y -> n + x + y
    x = 1
    f(2)
end
foo(10)


Out[78]:
13

In [79]:
const e


syntax: expected assignment after "const"
while loading In[79], in expression starting on line 1

In [80]:
const e = 2.71
const pi = 3.141


cannot declare e constant; it already has a value
while loading In[80], in expression starting on line 1

In [81]:
methods(+)


Out[81]:
119 methods for generic function +:

In [82]:
methods(for)


syntax: unexpected )
while loading In[82], in expression starting on line 1

In [83]:
methods("for")


`methods` has no method matching methods(::ASCIIString)
while loading In[83], in expression starting on line 1

In [84]:
mean(Squares(100))


Squares not defined
while loading In[84], in expression starting on line 1

In [85]:
25 in Squares(10)


Squares not defined
while loading In[85], in expression starting on line 1

In [90]:
immutable Squares
    count::Int
end
Base.start(::Squares) = 1
Base.next(S::Squares, state) = (state*state, state+1)
Base.done(S::Squares, s) = s > S.count
Base.eltype(::Type{Squares}) = Int
Base.length(S::Squares) = S.count

for i in Squares(7)
    println(1)
end


1
1
1
1
1
1
1

In [91]:
25 in Squares(10)


Out[91]:
true

In [92]:
mean(Squares(100))


Out[92]:
3383.5

In [93]:
for i in Squares(7)
    println(i)
end


1
4
9
16
25
36
49

In [94]:
prog = "1 + 1" # julia programme starts as a string


Out[94]:
"1 + 1"

In [95]:
## parse each string such as 1+1 into an object.
# this object is called expression
expression1 = parse(prog)
expression1


Out[95]:
:(1 + 1)

In [96]:
## This has three parts:
# A symbol
expression1.head


Out[96]:
:call

In [97]:
# next, expression arguments,
expression1.args


Out[97]:
3-element Array{Any,1}:
  :+
 1  
 1  

In [98]:
# third, the result type
expression1.typ


Out[98]:
Any

In [99]:
# Another way to write the programme above,
Exp2 = Expr(:call, :+, 1, 1)


Out[99]:
:(1 + 1)

In [101]:
## Expression1 and Exp2 are same, see
expression1 == Exp2 # will return true


Out[101]:
true

In [102]:
## Here is the data structure
datastruct = dump(Exp2)


Expr 
  head: Symbol call
  args: Array(Any,(3,))
    1: Symbol +
    2: Int64 1
    3: Int64 1
  typ: Any

In [103]:
# ":" is a symbol
:foo


Out[103]:
:foo

In [104]:
typeof(ans)


Out[104]:
Symbol

In [105]:
# Use : to create expression objectis
ex1 = :(a+b*c+1)


Out[105]:
:(a + b * c + 1)

In [106]:
typeof(ex1)


Out[106]:
Expr

In [107]:
## ehre is the structure
parse(ex1)


`parse` has no method matching parse(::Expr)
while loading In[107], in expression starting on line 2

In [108]:
ex1.head


Out[108]:
:call

In [109]:
dump(ex1)


Expr 
  head: Symbol call
  args: Array(Any,(4,))
    1: Symbol +
    2: Symbol a
    3: Expr 
      head: Symbol call
      args: Array(Any,(3,))
        1: Symbol *
        2: Symbol b
        3: Symbol c
      typ: Any
    4: Int64 1
  typ: Any

In [110]:
eval(ex1)


c not defined
while loading In[110], in expression starting on line 1

In [111]:
:(1+2)


Out[111]:
:(1 + 2)

In [112]:
eval(ans)


Out[112]:
3

In [113]:
ex = :(a+b)


Out[113]:
:(a + b)

In [114]:
eval(ex)


Out[114]:
30

In [115]:
a = 5; b = 10


Out[115]:
10

In [116]:
eval(ex)


Out[116]:
15

In [117]:
macro sayHello()
    return :(println("Hello World"))
end

In [118]:
@sayHello()


Hello World

In [119]:
macro sayHello(name)
    return :(println("Hello ", $name))
end
@sayHello(John)


John not defined
while loading In[119], in expression starting on line 4

In [120]:
@sayHello("John")


Hello John

In [121]:
const x = rand(8)


cannot declare x constant; it already has a value
while loading In[121], in expression starting on line 1

In [122]:
x = rand(8)


Out[122]:
8-element Array{Float64,1}:
 0.151211 
 0.0135414
 0.227328 
 0.183847 
 0.351597 
 0.477987 
 0.44913  
 0.44848  

In [131]:
Float64[ 0.25 * x[i-1]  for i = 2:length(x)-1]


Out[131]:
6-element Array{Float64,1}:
 0.0378028 
 0.00338535
 0.0568321 
 0.0459619 
 0.0878993 
 0.119497  

In [132]:
Pkg.status()


25 required packages:
 - DataArrays                    0.2.20
 - DataFrames                    0.6.10
 - Distances                     0.2.1
 - Distributions                 0.8.7
 - GLM                           0.4.8
 - Gadfly                        0.3.18
 - GitHub                        1.0.3
 - Graphs                        0.6.0
 - HypothesisTests               0.2.10
 - IJulia                        1.1.8
 - Jewel                         1.0.6
 - LaTeX                         0.0.2
 - LaTeXStrings                  0.1.6
 - MLBase                        0.5.2
 - Markdown                      0.3.0
 - MultivariateStats             0.2.1
 - NHST                          0.0.2
 - PyPlot                        2.1.1
 - RCall                         0.3.1
 - RDatasets                     0.1.2
 - Rif                           0.0.12
 - Stats                         0.1.0
 - StatsBase                     0.7.4
 - TimeSeries                    0.5.11
 - Weave                         0.0.4
58 additional packages:
 - ArgParse                      0.3.0
 - ArrayViews                    0.6.4
 - BinDeps                       0.3.19
 - Calculus                      0.1.14
 - Codecs                        0.1.5
 - ColorTypes                    0.1.7
 - ColorVectorSpace              0.0.5
 - Colors                        0.5.4
 - Compat                        0.7.7
 - Compose                       0.3.18
 - Conda                         0.1.8
 - Contour                       0.0.8
 - DataStructures                0.3.13
 - Dates                         0.3.2
 - Docile                        0.5.19
 - DualNumbers                   0.1.5
 - FactCheck                     0.4.1
 - FixedPointNumbers             0.0.12
 - ForwardDiff                   0.0.3
 - GZip                          0.2.18
 - GnuTLS                        0.0.5
 - Graphics                      0.1.0
 - Grid                          0.3.11
 - Hexagons                      0.0.4
 - Homebrew                      0.2.0
 - HttpCommon                    0.1.2
 - HttpParser                    0.0.13
 - Images                        0.4.50
 - ImmutableArrays               0.0.11
 - Iterators                     0.1.9
 - JSON                          0.5.0
 - JuliaParser                   0.6.2
 - KernelDensity                 0.1.2
 - LNR                           0.0.1
 - Lazy                          0.10.1
 - Loess                         0.0.5
 - MacroTools                    0.2.0
 - NaNMath                       0.1.1
 - Nettle                        0.2.0
 - Optim                         0.4.4
 - PDMats                        0.3.6
 - Polynomials                   0.0.4
 - PyCall                        1.2.0
 - Reexport                      0.0.3
 - Requests                      0.2.4
 - Requires                      0.2.0
 - Roots                         0.1.20
 - SHA                           0.1.2
 - SIUnits                       0.0.6
 - Showoff                       0.0.6
 - SortingAlgorithms             0.0.6
 - StatsFuns                     0.2.0
 - TexExtensions                 0.0.3
 - TextWrap                      0.1.5
 - URIParser                     0.0.7
 - WoodburyMatrices              0.1.2
 - ZMQ                           0.3.1
 - Zlib                          0.1.12

In [133]:
julia -v


julia not defined
while loading In[133], in expression starting on line 1

In [134]:
versioninfo()


Julia Version 0.3.11
Commit 483dbf5* (2015-07-27 06:18 UTC)
Platform Info:
  System: Darwin (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas
  LIBM: libopenlibm
  LLVM: libLLVM-3.3

Beyond this, I am going to update Julia to version. 0.4 and update Ijulia as well. So download Julia, install, then do Pkg.add(IJulia)


In [ ]: