vectors in julia - linear dynamical systemee263.stanford.edu/notes/julia_vectors_slides.pdf · i...
TRANSCRIPT
![Page 1: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/1.jpg)
Vectors in Julia
Keegan Go David Zeng Stephen Boyd
EE263Stanford University
October 1, 2015
![Page 2: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/2.jpg)
Vectors in Julia
main topics:
I how to create and manipulate vectors in Julia
I how Julia notation differs from math notation
2
![Page 3: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/3.jpg)
Scalars
I represented by two types, Int64 and Float64
a = 1
b = 0.5
I usually the types work together correctly, for example
1 + 0.5
produces a float
3
![Page 4: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/4.jpg)
Outline
Vectors
Vector operations
Norm and distance
Vectors 4
![Page 5: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/5.jpg)
Vectors
I vectors are represented by arrays in Julia
I to create the 3-vector
x = (8,−4, 3.5) =
8−43.5
use
x = [8, -4, 3.5]
(x = [8;-4;3.5] also works)
I watch out for similar looking expressions
– (8,-4,3.5) and {8,-4,3.5} mean something else– [8 -4 3.5] is a row vector (later)
I length of an array: length(x)
Vectors 5
![Page 6: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/6.jpg)
Indexing and slicing
I indexes run from 1 to n: x2 is x[2]
I can also set an element, e.g., x[3] = 10.5
I use a range to select more than one element
I x[2:3] selects the second and third elements
I to select every other element use x[1:2:end]
Vectors 6
![Page 7: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/7.jpg)
Block vectors
I to form a stacked vector like
a = (b, c) =
[bc
](with b and c vectors)
a = [b; c]
I can mix vectors and scalars:
a = [b, 2, c, -6]
Vectors 7
![Page 8: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/8.jpg)
Basic functions for arrays
I sum of (the entries of) a vector: sum(x)
I mean of the entries (avg(x)): mean(x)
I 0n is zeros(n)
I 1n is ones(n)
Vectors 8
![Page 9: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/9.jpg)
Creating unit vectors
I form e3 with length 10
I create a zero vector of size 10 then set the third element to 1
e_3 = zeros(10); e_3[3] = 1;
Vectors 9
![Page 10: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/10.jpg)
Julia array types
I an array’s type is the most specific given its elements
I consider arr1 = [100, 7, -83] and arr2 = [4.5, -10, 13]
I arr1 is an Int array while arr2 is a Float array
I arr1[2] = 0.1 will error because arr1 can only store Ints
I to make arr1 a Float array, give one entry a decimal point
arr1 = [100., 7, -83]
Vectors 10
![Page 11: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/11.jpg)
List of vectors
I to form a list with vectors a, b, and c:
vector_list = Any[a,b,c]
I the second vector in this list is vector_list[2]
I to access an element in a vector: vector_list[2][3]
Vectors 11
![Page 12: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/12.jpg)
Notation
I do not mix mathematical notation with Julia notation
I notations are not compatible, for example
v = (0,1,1)
produces a tuple, not an array (vector)
I similarly,
v = [1, 10, 7]
defines an array (vector) in Julia, but isn’t mathematically correct
Vectors 12
![Page 13: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/13.jpg)
Outline
Vectors
Vector operations
Norm and distance
Vector operations 13
![Page 14: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/14.jpg)
Vector addition and subtraction
I vector addition uses +, for example 123
+
456
is written
[1, 2, 3] + [4, 5, 6]
I subtraction uses -
I the arrays must have the same length (unless one is scalar)
Vector operations 14
![Page 15: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/15.jpg)
Scalar-vector addition
I in Julia, a scalar and a vector can be added
I the scalar is added to each entry of the vector
[2, 4, 8] + 3
gives (in mathematical notation) 248
+ 31 =
5711
Vector operations 15
![Page 16: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/16.jpg)
Scalar-vector multiplication
I scalar-vector multiplication uses *
I for example,
(−2)
196
is written
-2 * [1, 9, 6]
I the other order gives the same result:
[1, 9, 6] * -2
Vector operations 16
![Page 17: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/17.jpg)
Inner product
I inner product aT b is written as
dot(a,b)
which returns a scalar (Int or Float)
I a and b must have the same length
Vector operations 17
![Page 18: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/18.jpg)
Outline
Vectors
Vector operations
Norm and distance
Norm and distance 18
![Page 19: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/19.jpg)
Norm and distance
I the norm ‖x‖ =√x21 + x2
2 + · · ·+ x2n is written
norm(x)
I dist(x, y) = ‖x− y‖ is written
norm(x-y)
Norm and distance 19
![Page 20: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/20.jpg)
RMS value
I rms(x) is defined as
rms(x) =
√1
n(x2
1 + · · ·+ x2n) =
‖x‖√n.
I can be expressed as
rms_x = norm(x)/sqrt(length(x))
Norm and distance 20
![Page 21: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/21.jpg)
Standard deviation
I standard deviation is defined as
std(x) =‖x− avg(x)1‖√
n
I which can be expressed as
std_of_x = norm(x - mean(x))/sqrt(length(x))
I warning: the Julia function std uses the slightly different definition
std(x) =‖x− avg(x)1‖√
n− 1
Norm and distance 21
![Page 22: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/22.jpg)
Angle
I the angle between two vectors a and b is
∠(a, b) = arccos
(aT b
‖a‖ ‖b‖
)I can be expressed as
angle_a_b = acos(dot(a,b)/(norm(a)*norm(b)))
Norm and distance 22
![Page 23: Vectors in Julia - Linear Dynamical Systemee263.stanford.edu/notes/julia_vectors_slides.pdf · I can mix vectors and scalars: a = [b, 2, c, -6] Vectors 7. Basic functions for arrays](https://reader036.vdocument.in/reader036/viewer/2022062402/5fc831aa09c70c21587e55d2/html5/thumbnails/23.jpg)
Nearest neighbor example
# Compares vectors in vector_list against a_vector
# and returns the index of the one which is closest
function nearest_neighbor(vector_list, a_vector)
closest_distance = Inf
closest_index = 0
for i in 1:length(vector_list)
ith_distance = norm(vector_list[i] - a_vector)
if (ith_distance < closest_distance)
closest_distance = ith_distance
closest_index = i
end
end
return closest_index
end
Norm and distance 23