Vectors in Julia Reese Pathak Keegan Go David Zeng EE103 Stanford University September 28, 2016 Stephen Boyd Vectors in Julia main topics: I how to create and manipulate vectors in Julia I how Julia notation differs from math notation 2 Outline Vectors Vector operations Norm and distance Vectors 3 Vectors I vectors are represented by arrays in Julia I to create the 3-vector 8 x = (8, −4, 3.5) = −4 3.5 use x = [8, -4, 3.5] I (x = [8;-4;3.5] also works) watch out for similar looking expressions I length of a vector: length(x) – (8,-4,3.5) and [8 -4 3.5] are not equivalent in Julia Vectors 4 Ranges I to get a range from i to j (for i ≤ j), use a colon (:) – the range from 1 to 10 is 1:10 – collect(1:10) returns the array I I the default increment between values is 1. (1:3 is 1, 2, 3) to specify an increment size add an additional argument: – the range from 1 to 10 with a step size of 0.1 is 1:0.1:10 Vectors 5 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 use a range to select more than one element I – x[2:3] selects the second and third elements I x[end] selects the last element I to select every other element use x[1:2:end] Vectors 6 Block vectors I to form a stacked vector like a = (b, c) = b c (with b and c vectors) a = [b; c] (a = [b, c] does NOT work) I can mix vectors and scalars: a = [b; 2; c; -6] Vectors 7 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 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 List of vectors I to form a list with vectors a, b, and c: vector_list = [a,b,c] I the second vector in this list is vector_list[2] I to access an element in a vector: vertor_list[2][3] Vectors 10 Outline Vectors Vector operations Norm and distance Vector operations 11 Vector addition and subtraction I vector addition uses +, for example 1 4 2 + 5 3 6 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 12 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) 2 5 4 + 31 = 7 8 11 Vector operations 13 Scalar-vector multiplication I scalar-vector multiplication uses * I for example, 1 (−2) 9 6 is written -2 * [1, 9, 6] I the other order gives the same result: [1, 9, 6] * -2 Vector operations 14 Inner product I inner product aT b is written as dot(a,b) I a and b must have the same length Vector operations 15 Outline Vectors Vector operations Norm and distance Norm and distance 16 Norm and distance I the norm kxk = p x21 + x22 + · · · + x2n is written norm(x) I dist(x, y) = kx − yk is written norm(x-y) Norm and distance 17 RMS value I rms(x) is defined as r rms(x) = I 1 2 kxk (x + · · · + x2n ) = √ . n 1 n can be expressed as rms_x = norm(x)/sqrt(length(x)) Norm and distance 18 Standard deviation I standard deviation is defined as std(x) = I kx − avg(x)1k √ n which can be expressed as std_of_x = norm(x - mean(x))/sqrt(length(x)) I warning: the Julia function std does not use this definition Norm and distance 19 Angle I the angle between two vectors a and b is T a b ∠(a, b) = arccos kak kbk I can be expressed as angle_a_b = acos(dot(a,b)/(norm(a)*norm(b))) Norm and distance 20