Skip to content
Snippets Groups Projects
Commit bed3fcc8 authored by Michele Nottoli's avatar Michele Nottoli
Browse files

Added a partial version of notebook 1.

parent 989881ba
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:7aad3586-d57e-4000-8688-aa5b53d48416 tags:
# Basics
%% Cell type:markdown id:bf904dbe-3197-4a91-bbb8-05e1c7f7dfee tags:
## Variables and elementary types
Defining variables in Julia works like in most languages:
%% Cell type:code id:3c44023b-06c9-4420-9af3-da2ec37b02da tags:
``` julia
int = 4 # An integer
```
%% Cell type:code id:b2f9ba27-bf65-40d8-b0ac-e3d86c99dd40 tags:
``` julia
str = "Hi" # A string
```
%% Cell type:code id:87a83355-5088-47d3-b698-2c6902aaa6a0 tags:
``` julia
float = 1.2 # A floating-point number
```
%% Cell type:code id:597523f1-4041-4fc6-9be2-2c94a93fd633 tags:
``` julia
bool = true # A boolean (also false)
```
%% Cell type:markdown id:0c538bdb-f995-4589-bee7-0989c922090c tags:
The type is automatically inferred:
%% Cell type:code id:b80c6777-3dea-4f45-98b3-0a3eee196183 tags:
``` julia
typeof(int)
```
%% Cell type:code id:1fb6cbd2-871f-40a4-ae84-e7ee46e75a61 tags:
``` julia
typeof(str)
```
%% Cell type:code id:a16a6f08-f19a-42e0-baac-aaa88ebba1af tags:
``` julia
typeof(float)
```
%% Cell type:markdown id:09d035eb-22aa-49f5-9fd1-a5b16973f386 tags:
Julia supports a large range of integer and floating-point types out of the box, for example:
%% Cell type:code id:88f24b01-f4a5-4030-af2c-a5d30223d047 tags:
``` julia
x = UInt8(1) # 8-bit wide unsigned integer
y = Int32(-1) # 32-bit wide signed integer
z = Float32(0.2) # single precision
α = Float16(-1.0) # half precision
β = ComplexF64(2. + 8im) # Complex number (composed of two Float64)
```
%% Cell type:markdown id:a615bbc0-732e-461f-8c4c-a972fa2be731 tags:
If you give a variable an illegal name, you get a syntax error:
%% Cell type:code id:1ca20515-6d66-40c3-84f1-2e24ece096cd tags:
``` julia
76trombones = "big parade"
```
%% Cell type:code id:2c61f59e-2c2f-4049-8624-35e0712b2cf1 tags:
``` julia
more@ = 1000000
```
%% Cell type:code id:85040b19-b0a9-4baa-8748-5ea62f8b5633 tags:
``` julia
struct = "Advanced Theoretical Zymurgy"
```
%% Cell type:markdown id:cc36688b-c983-43f8-8bdf-9b82aae59909 tags:
It turns out that struct is one of Julia’s keywords. The REPL uses keywords to recognize the structure of the program, and they cannot be used as variable names.
%% Cell type:markdown id:de111dbe-f7ed-4e1a-8ec0-c5e65bd63bbf tags:
### Strings
%% Cell type:markdown id:1b3531e9-33db-4c51-b394-c1def6621519 tags:
In general, you can’t perform mathematical operations on strings, even if the strings look like numbers, so the following are illegal:
%% Cell type:code id:246f1db8-d734-413d-9237-b2434e071914 tags:
``` julia
"2" - "1"
"eggs" / "easy"
"third" + "a charm"
```
%% Cell type:markdown id:c94f1187-67f8-473e-910c-2eb145ed59dd tags:
But there are two exceptions, * and ^.
%% Cell type:code id:b3a0f6e1-e28b-4fb7-baa3-bb003de3dc24 tags:
``` julia
"Hello " * "world"
```
%% Cell type:code id:0c963c3c-47c7-4805-8be6-621b72fdb62e tags:
``` julia
"hello! "^5
```
%% Cell type:markdown id:61ff8194-669c-4ece-ba31-44e88ed808ac tags:
## Exercises
%% Cell type:markdown id:e0aecc9c-e77f-449e-8aff-e1ffcf5d7833 tags:
Practice using Julia as a calculator:
- The volume of a sphere with radius r is 4/3πr^3. What is the volume of a sphere with radius 5?
%% Cell type:code id:fc538246-418c-4ebe-b487-81feba4383c8 tags:
``` julia
```
%% Cell type:markdown id:10f1604d-8e81-4f96-8b6f-95753f0b5c4f tags:
- Suppose the cover price of a book is 24.95, but bookstores get a 40 % discount. Shipping costs 3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies?
%% Cell type:code id:5d99fb94-0d78-4812-abbb-b528bc8cbf73 tags:
``` julia
```
%% Cell type:markdown id:4f6858ae-316a-4102-b9d8-9a5e087af1ca tags:
- If I leave my house at 6:52 am and run 1 km at an easy pace (6:00 per km), then 3 km at tempo (5:00 per km) and 1 km at easy pace again, what time o I get home for breakfast?
%% Cell type:code id:bd4150cf-005a-4087-aa92-5f4b18e4d197 tags:
``` julia
```
%% Cell type:markdown id:dc9cddb9-717d-4ba0-b478-beb00afc46c8 tags:
## Conditionals and Boolean Expressions
A boolean expression is an expression that is either true or false. The following examples use the operator ==, which compares two operands and produces true if they are equal and false otherwise:
%% Cell type:code id:7de4ffe9-4730-47dd-b074-1525bf9937b1 tags:
``` julia
5 == 5
```
%% Cell type:code id:c02939dd-6f25-4916-8394-a5f95d0200df tags:
``` julia
5 == 6
```
%% Cell type:markdown id:9a108e63-7d58-489d-b7ef-49629c879d29 tags:
The == operator is one of the relational operators; the others are:
%% Cell type:code id:b59cd314-fd03-4609-ac1b-7329a2c3cc38 tags:
``` julia
x = 3; y = 4
x != y # x is not equal to y
x ≠ y # (to get the ≠ symbol, type \ne and then press TAB)
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x ≥ y # (to get the ≥ symbol, type \ge and then press TAB)
x <= y # x is less than or equal to y
x ≤ y # (to get the ≤ symbol, type \le and then press TAB)
```
%% Cell type:markdown id:15e20f55-1a43-4d0d-94bc-d2de0ee2fe8c tags:
There are three logical operators: && (and), || (or), and ! (not). The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 && x < 10 is true only if x is greater than 0 and less than 10.
%% Cell type:code id:d1e6e2ce-bb0c-4776-80b6-22ceb9c68397 tags:
``` julia
x = 4
x > 0 && x < 10
```
%% Cell type:markdown id:da591654-6360-4f56-956b-07493d1d2247 tags:
It is also worth mentioning that in combined logical expression not all conditions are necessarily executed.
%% Cell type:code id:e92abf7b-5414-4aee-abaf-a4e52b1c06d3 tags:
``` julia
true || println("The RHS of || is only run")
false || println("if the LHS is false")
```
%% Cell type:code id:c8a9d8ac-5378-4ebb-810b-9ae3194d670c tags:
``` julia
iseven(3) && println("The RHS of || is only run")
isodd(3) && println("if the LHS is true")
```
%% Cell type:markdown id:be0c63e0-35a9-43c7-9b30-d04072ee6904 tags:
Finally, the ! operator negates a boolean expression, so !(x > y) is true if x > y is false, that is, if x is less than or equal to y.
%% Cell type:code id:e43ab736-03e2-4040-a15e-48305037ba23 tags:
``` julia
x = 4
!(x > 0 && x < 10)
```
%% Cell type:markdown id:7064ae20-6d63-40fc-85c5-080ace54e7b6 tags:
Unsurprisingly Julia has the standard conditionals.
%% Cell type:code id:4f140e2c-a240-496d-904c-c60778397bcf tags:
``` julia
x = 3; y = 5
if x < y
println("x is less than y")
elseif x > y
println("x is greater than y")
else
println("x is equal to y")
end
```
%% Cell type:markdown id:4c57139e-da0c-48ef-a86f-4de80e1d7ca4 tags:
There is also an inline conditional.
%% Cell type:code id:9d0801fd-b0d3-41e0-8b54-19a6b4539231 tags:
``` julia
x = 3
x < 5 ? "smaller than 5" : "larger or equal 5"
```
%% Cell type:markdown id:e4510840-0890-4217-8a8c-c24aa5d3405b tags:
## Iterations
Julia implements the standard for and while constructs.
%% Cell type:code id:9632f210-c713-4829-8187-cf5554adad87 tags:
``` julia
for i in 1:5
println("Hello from number $i")
end
```
%% Cell type:markdown id:f00caf15-4992-4c0a-8324-9d2be966346d tags:
Where the `1:5` is a `Range`-object, but it could be any iterable. There are a few syntax variations, including:
%% Cell type:code id:70a9f620-e4ce-4367-b0f5-de37db3f48b5 tags:
``` julia
sum = 0
# To get ∈ write \in and then press TAB
for j ∈ 1:0.5:3 # Note the step parameter
sum += j
end
println(sum)
```
%% Cell type:code id:e83b2d88-b0f2-43f1-9187-65fbc75f0d3b tags:
``` julia
n = 10
while n > 0
print(n, " ")
n = n - 1
end
```
%% Cell type:markdown id:f19d5051-ce82-49cc-80aa-2434a6adc9d0 tags:
The loops can be further controlled with the break and continue keywords.
%% Cell type:markdown id:d0ebce97-3054-4eba-821d-92f27793d752 tags:
Sometimes you don’t know it’s time to end a loop until you get half way through the body. In that case you can use the break statement to jump out of the loop.
%% Cell type:code id:9bb642e3-e3b1-488d-939f-6932feb62479 tags:
``` julia
while true
print("> ")
line = readline()
if line == "done"
break
end
println(line)
end
println("Done!")
```
%% Cell type:markdown id:98eee547-21fe-4022-bde5-b02ababa26dd tags:
The break statement exits the loop. When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for the next iteration, skipping the execution of statements inside the body of the loop for the current iteration. For example:
%% Cell type:code id:7f1c2595-8836-4308-9769-f4140d402582 tags:
``` julia
for i in 1:10
if i % 3 == 0
continue
end
print(i, " ")
end
```
%% Cell type:markdown id:990cefae-abfe-4649-8d68-3fac1b1e268b tags:
### Exercises
%% Cell type:markdown id:bdb2eef9-bdee-4cf3-8bab-d6c1d46ecc2f tags:
The mathematician Srinivasa Ramanujan found an infinite series that can be used to generate a numerical approximation of 1/π
%% Cell type:markdown id:feeb613b-544b-4a01-8817-9db86b8775e3 tags:
1 2 √2 ∞ (4k)! (1103 + 26390 k)
- = ------ ∑ ------------------------
π 9801 k=0 (k!)^4 396^(4 k)
%% Cell type:markdown id:8a8ae5ef-f9ec-480e-9927-8d21ecdf1915 tags:
Use this formula to compute an estimate of π. It should use a while loop ot compute terms of the summation untile the last term is smaller than 1e-15. Afterwards, you can check the result by comparing it to π (to get π in Julia, write \pi and then press TAB).
%% Cell type:code id:cd1a716d-ddaf-4167-a83f-129dc3163ea1 tags:
``` julia
```
%% Cell type:code id:4ff56e28-969e-4a2d-b33c-21b4a4ecc4de tags:
``` julia
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment