Skip to content

Commit f2058d8

Browse files
colinleachColin LeachColin Leach
authored
Vectors concept (#802)
* WIP draft of arrays concept * changed `arrays` to `vectors`, moved from wip to concepts * added introduction + config entry * fixed config error, removed `concepts.wip/vectors`. * Update about.md * Update introduction.md --------- Co-authored-by: Colin Leach <[email protected]> Co-authored-by: Colin Leach <[email protected]>
1 parent ba9e24b commit f2058d8

File tree

9 files changed

+353
-20
lines changed

9 files changed

+353
-20
lines changed

concepts.wip/vectors/.meta/config.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

concepts.wip/vectors/about.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

concepts.wip/vectors/introduction.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

concepts.wip/vectors/links.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

concepts/vectors/.meta/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"authors": [
3+
"colinleach"
4+
],
5+
"contributors": [],
6+
"blurb": "Arrays are a huge and very important topic in Julia. This concept covers the basics of 1-D arrays and ranges."
7+
}

concepts/vectors/about.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# About
2+
3+
A central aim of Julia is to be able to perform calculations on numerical data, quickly and efficiently.
4+
_Lots_ of numerical data.
5+
6+
Typically, the data will be stored in arrays (or some related type), so it is reasonable to say that arrays are at the heart of the Julia language.
7+
8+
Arrays can be of arbitrary size (subject only to memory constraints in your hardware), and can have arbitrarily many dimensions.
9+
10+
Some types of arrays have special names (copied from Linear Algebra).
11+
A 1-Dimensional array is called a `Vector` and a 2-dimensional array is called a `Matrix`.
12+
Both are subtypes of `Array` in Julia.
13+
14+
This introductory Concept concentrates on the basics of Vectors.
15+
Higher-dimensional arrays will be covered later in the syllabus.
16+
17+
## Creating Vectors
18+
19+
In Julia, a [Vector][arrays] is an ordered sequence of values that can be accessed by index number.
20+
21+
The simplest way to create a Vector is to list the values in square brackets:
22+
23+
```julia-repl
24+
julia> num_vec = [1, 4, 9]
25+
3-element Vector{Int64}:
26+
1
27+
4
28+
9
29+
30+
julia> str_vec = ["arrays", "are", "important"]
31+
3-element Vector{String}:
32+
"arrays"
33+
"are"
34+
"important"
35+
```
36+
37+
The Vector type matches the type of each element.
38+
39+
Technically, it is a _column_ vector, but please ignore that for now if you are unfamiliar with Linear Algebra.
40+
It should make more sense after the Multidimensional Arrays concept.
41+
42+
_So Vectors need to be homogeneous (all elements the same type)?_
43+
44+
At some level, yes — and it is recommended to aim for this if you want the best performance.
45+
However, Julia can work round this limitation (when necessary) by using the `Any` type:
46+
47+
```julia-repl
48+
julia> mixed_vector = [1, "str", 'c']
49+
3-element Vector{Any}:
50+
1
51+
"str"
52+
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
53+
```
54+
55+
### Pre-filled Vectors
56+
57+
It is very common to start from vectors of all-0 or all-1 values. For these, there are functions called (unsurprisingly) `zeros()` and `ones()`, which take the vector size as a parameter.
58+
The default type is Float64, but other numeric types can optionally be specified.
59+
60+
By extension, the `fill()` function takes both a value to repeat, and the desired vector size.
61+
62+
```julia-repl
63+
julia> zeros(3)
64+
3-element Vector{Float64}:
65+
0.0
66+
0.0
67+
0.0
68+
69+
julia> ones(3)
70+
3-element Vector{Float64}:
71+
1.0
72+
1.0
73+
1.0
74+
75+
julia> ones(Int64, 3)
76+
3-element Vector{Int64}:
77+
1
78+
1
79+
1
80+
81+
julia> fill(42, 3)
82+
3-element Vector{Int64}:
83+
42
84+
42
85+
42
86+
```
87+
88+
There are many other options: see [the manual][prefilled].
89+
90+
# Indexing
91+
92+
Please read and remember this rule:
93+
94+
- ***By default, indexing in Julia starts at 1, not 0***.
95+
96+
This is familiar to anyone who has used other data science languages (R, Matlab, Fortran...), but may seem shocking to computer scientists with a background in C-like languages.
97+
98+
Otherwise, indexes work as you might guess: just put them in square brackets:
99+
100+
```julia
101+
squares = [0, 1, 4, 9, 16]
102+
squares[1] # 0
103+
squares[3] # 4
104+
squares[begin] # 0 ("begin" is synonym for 1)
105+
squares[end] # 16 ("end" is synonym for length(squares))
106+
```
107+
108+
Note the convenience of indexing with `end` (which is very useful) and `begin` (which is probably not).
109+
110+
Python programmers may be wondering about negative indices.
111+
Don't: these are not part of Julia, and will raise a `BoundsError`, as will any index smaller than 1 or bigger than `length(squares)`.
112+
113+
## Vector operations
114+
115+
Vectors in Julia are _mutable_: we can change the contents of individual cells.
116+
117+
```julia-repl
118+
julia> vals = [1, 3, 5, 7]
119+
4-element Vector{Int64}:
120+
1
121+
3
122+
5
123+
7
124+
125+
julia> vals[2] = 4 # reassign the value of this element
126+
4
127+
128+
# Only the value at position 2 changes:
129+
julia> vals
130+
4-element Vector{Int64}:
131+
1
132+
4
133+
5
134+
7
135+
```
136+
137+
There are many [functions available][dequeue] for manipulating vectors, though the Julia documentation generalizes them to "collections" rather than vectors.
138+
139+
Note that, by convention, functions that mutate their input have `!` in the name.
140+
The compiler will not enforce this, but it is a very _strong_ convention in the Julia world.
141+
142+
These are a few of the useful functions:
143+
144+
- To add values to the end of the vector, use [`push!()`][push].
145+
- To remove the last value, use [`pop!()`][pop].
146+
- To operate on the start of the vector, the corresponding functions are [`pushfirst!()`][pushfirst] and [`popfirst!()`][popfirst].
147+
- To insert or remove an element at any position, there is [`insert!()`][insert] and [`deleteat!()`][deleteat].
148+
149+
```julia-repl
150+
julia> vals = [1, 3]
151+
2-element Vector{Int64}:
152+
1
153+
3
154+
155+
julia> push!(vals, 5, 6) # can add multiple values
156+
4-element Vector{Int64}:
157+
1
158+
3
159+
5
160+
6
161+
162+
julia> pop!(vals) # mutates vals, return popped value
163+
6
164+
165+
julia> vals
166+
3-element Vector{Int64}:
167+
1
168+
3
169+
5
170+
```
171+
172+
## Concatenation
173+
174+
Concatenation is fairly simple for vectors, though a glance at the manual gives warning of complexities in store when we consider multidimensional arrays.
175+
176+
The [`append!()][append] function can take a mixture of several vectors and single elements as input.
177+
178+
```julia-repl
179+
julia> append!([1, 2], [3, 4], [-1, -2], 15)
180+
7-element Vector{Int64}:
181+
1
182+
2
183+
3
184+
4
185+
-1
186+
-2
187+
15
188+
```
189+
190+
191+
[arrays]: https://docs.julialang.org/en/v1/manual/arrays/
192+
[prefilled]: https://docs.julialang.org/en/v1/manual/arrays/#Construction-and-Initialization
193+
[dequeue]: https://docs.julialang.org/en/v1/base/collections/#Dequeues
194+
[push]: https://docs.julialang.org/en/v1/base/collections/#Base.push!
195+
[pop]: https://docs.julialang.org/en/v1/base/collections/#Base.pop!
196+
[pushfirst]: https://docs.julialang.org/en/v1/base/collections/#Base.pushfirst!
197+
[popfirst]: https://docs.julialang.org/en/v1/base/collections/#Base.popfirst!
198+
[insert]: https://docs.julialang.org/en/v1/base/collections/#Base.insert!
199+
[deleteat]: https://docs.julialang.org/en/v1/base/collections/#Base.deleteat!
200+
[append]: https://docs.julialang.org/en/v1/base/collections/#Base.append!

concepts/vectors/introduction.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Introduction
2+
3+
A central aim of Julia is to be able to perform calculations on numerical data, quickly and efficiently.
4+
_Lots_ of numerical data.
5+
6+
Typically, the data will be stored in arrays (or some related type), so it is reasonable to say that arrays are at the heart of the Julia language.
7+
8+
Arrays can be of arbitrary size (subject only to memory constraints in your hardware), and can have arbitrarily many dimensions.
9+
10+
This introductory Concept concentrates on the basics of 1-dimensional arrays, which are called Vectors.
11+
12+
## Creating Vectors
13+
14+
In Julia, a Vector is an ordered sequence of values that can be accessed by index number.
15+
16+
The simplest way to create a Vector is to list the values in square brackets:
17+
18+
```julia-repl
19+
julia> num_vec = [1, 4, 9]
20+
3-element Vector{Int64}:
21+
1
22+
4
23+
9
24+
25+
julia> str_vec = ["arrays", "are", "important"]
26+
3-element Vector{String}:
27+
"arrays"
28+
"are"
29+
"important"
30+
```
31+
32+
The Vector type matches the type of each element.
33+
34+
_So Vectors need to be homogeneous (all elements the same type)?_
35+
36+
At some level, yes — and it is recommended to aim for this if you want the best performance.
37+
However, Julia can work round this limitation (when necessary) by using the `Any` type:
38+
39+
```julia-repl
40+
julia> mixed_vector = [1, "str", 'c']
41+
3-element Vector{Any}:
42+
1
43+
"str"
44+
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
45+
```
46+
47+
# Indexing
48+
49+
Please read and remember this rule:
50+
51+
- ***By default, indexing in Julia starts at 1, not 0***.
52+
53+
This is familiar to anyone who has used other data science languages (R, Matlab, Fortran...), but may seem shocking to computer scientists with a background in C-like languages.
54+
55+
Otherwise, indexes work as you might guess: just put them in square brackets:
56+
57+
```julia
58+
squares = [0, 1, 4, 9, 16]
59+
squares[1] # 0
60+
squares[3] # 4
61+
squares[begin] # 0 ("begin" is synonym for 1)
62+
squares[end] # 16 ("end" is synonym for length(squares))
63+
```
64+
65+
Note the convenience of indexing with `end` (which is very useful) and `begin` (which is probably not).
66+
67+
Python programmers may be wondering about negative indices.
68+
Don't: these are not part of Julia, and will raise a `BoundsError`, as will any index smaller than 1 or bigger than `length(squares)`.
69+
70+
## Vector operations
71+
72+
Vectors in Julia are _mutable_: we can change the contents of individual cells.
73+
74+
```julia-repl
75+
julia> vals = [1, 3, 5, 7]
76+
4-element Vector{Int64}:
77+
1
78+
3
79+
5
80+
7
81+
82+
julia> vals[2] = 4 # reassign the value of this element
83+
4
84+
85+
# Only the value at position 2 changes:
86+
julia> vals
87+
4-element Vector{Int64}:
88+
1
89+
4
90+
5
91+
7
92+
```
93+
94+
There are many functions available for manipulating vectors, though the Julia documentation generalizes them to "collections" rather than vectors.
95+
96+
Note that, by convention, functions that mutate their input have `!` in the name.
97+
The compiler will not enforce this, but it is a very _strong_ convention in the Julia world.
98+
99+
These are a few of the useful functions:
100+
101+
- To add values to the end of the vector, use `push!()`.
102+
- To remove the last value, use `pop!()`.
103+
- To operate on the start of the vector, the corresponding functions are `pushfirst!()` and `popfirst!()`.
104+
- To insert or remove an element at any position, there is `insert!()` and `deleteat!()`.
105+
106+
```julia-repl
107+
julia> vals = [1, 3]
108+
2-element Vector{Int64}:
109+
1
110+
3
111+
112+
julia> push!(vals, 5, 6) # can add multiple values
113+
4-element Vector{Int64}:
114+
1
115+
3
116+
5
117+
6
118+
119+
julia> pop!(vals) # mutates vals, return popped value
120+
6
121+
122+
julia> vals
123+
3-element Vector{Int64}:
124+
1
125+
3
126+
5
127+
```

concepts/vectors/links.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"url": "https://docs.julialang.org/en/v1/manual/arrays/",
4+
"description": "Arrays section in the Julia manual."
5+
},
6+
{
7+
"url": "https://docs.julialang.org/en/v1/manual/arrays/#Construction-and-Initialization",
8+
"description": "Constructing and initializing arrays."
9+
},
10+
{
11+
"url": "https://docs.julialang.org/en/v1/base/collections/#Dequeues",
12+
"description": "Functions for manipulating arrays."
13+
}
14+
]

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,11 @@
11051105
"slug": "booleans",
11061106
"name": "Booleans"
11071107
},
1108+
{
1109+
"uuid": "9f3b90c3-87b4-48cb-81d0-fb6867a20cac",
1110+
"slug": "vectors",
1111+
"name": "Vectors"
1112+
},
11081113
{
11091114
"uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239",
11101115
"slug": "integer-introduction",

0 commit comments

Comments
 (0)