Most scientific disciplines introduce an idea of the amount of some entity or property of interest. Furthermore, the amount is usually combined with the concept of a number, an abstraction of the observation that the two sets {Mary, Jane, Tom} and {apple, plum, cherry} seem quite different, but we can match one distinct person to one distinct fruit as in {Maryplum, Janeapple, Tomcherry}. In contrast we cannot do the same matching of distinct persons to a distinct color from the set {red, green}, and one of the colors must be shared between two persons. Formal definition of the concept of a number from the above observations is surprisingly difficult since it would be self-referential due to the apperance of the numbers “one” and “two”. Leaving this aside, the key concept is that of quantity of some property of interest that is expressed through a number. Several types of numbers have been introduced in mathematics to express different types of quantities, and the following will be used throughout this text:
The set of natural numbers, , infinite and countable, ;
The set of integers, , infinite and countable;
The set of rational numbers , infinite and countable;
The set of real numbers, infinite, not countable, can be ordered;
The set of complex numbers, , infinite, not countable, cannot be ordered.
A computer has a finite amount of memory, hence cannot represent all numbers, but rather subsets of the above sets. Furthermore, computers internally use binary numbers composed of binary digits, or bits. Many computer number types are defined for specific purposes, and are often encountered in applications such as image representation or digital data acquisition. Here are the main types.
The number types uint8, uint16, uint32, uint64 represent subsets of the natural numbers (unsigned integers) using 8, 16, 32, 64 bits respectively. An unsigned integer with bits can store a natural number in the range from to . Two arbitrary natural numbers, written as can be added and will give another natural number, . In contrast, addition of computer unsigned integers is only defined within the specific range to .
octave] |
i=uint8(15); j=uint8(10); k=i+j |
k = 25
octave] |
i=uint8(150); j=uint8(200); k=i+j |
k = 255
octave] |
k=i-j |
k = 0
octave] |
The number types int8, int16, int32, int64 represent subsets of the integers. One bit is used to store the sign of the number, so the subset of that can be represented is from to
octave] |
i=int8(100); j=int8(101); k=i+j |
k = 127
octave] |
k=i-j |
k = -1
octave] |
Computers approximate the real numbers through the set of floating point numbers. Floating point numbers that use bits are known as single precision, while those that use are double precision. A floating point number is stored internally as where , are bits within the mantissa of length , and , are bits within the exponent, along with signs for each. The default number type is usually double precision, more concisely referred to double. Common constants such as , are predefined as double, can be truncated to single, and the number of displayed decimal digits is controlled by format. The function disp(x) displays its argument x.
octave] |
format long; disp([e pi]) |
2.718281828459045 3.141592653589793
octave] |
disp([single(e) single(pi)]) |
2.7182817 3.1415927
octave] |
The approximation of the reals by the floats is characterized by: realmax, the largest float, realmin the smallest positive float, and eps known as machine epsilon. Machine epsilon highlights the differences between floating point and real numbers since it is defined as the largest number that satisfies . If of course implies , but floating points exhibit “granularity”, in the sense that over a unit interval there are small steps that are indistinguishable from zero due to the finite number of bits available for a float. Machine epsilon is small, and floating point errors can usually be kept under control. Keep in mind that perfect accuracy is a mathematical abstraction, not encountered in nature. In fields as sociology or psychology 3 digits of accuracy are excellent, in mechanical engineering this might increase to 6 digits, or in electronic engineering to 8 digits. The most precisely known physical constant is the Rydberg constant known to 12 digits. The granularity of double precision expressed by machine epsilon is sufficient to represent natural phenomena.
octave] |
format short; disp([realmin realmax eps 1+eps]) |
2.2251e-308 1.7977e+308 2.2204e-16
1.0000e+00
octave] |
Within the reals certain operations are undefined such as . Special float constants are defined to handle such situations: Inf is a float meant to represent infinity, and NaN (“not a number”) is meant to represent an undefinable result of an arithmetic operation.
octave] |
warning("off"); disp([Inf 1/0 2*realmax NaN Inf-Inf Inf/Inf]) |
Inf Inf Inf NaN NaN NaN
octave] |
Complex numbers are specified by two reals, in Cartesian form as , or in polar form as , , . The computer type complex is similarly defined from two floats and the additional constant I is defined to represent . Functions are available to obtain the real and imaginary parts within the Cartesian form, or the absolute value and argument of the polar form.
octave] |
z1=complex(1,1); z2=complex(1,-1); disp([z1+z2 z1/z2]) |
2 + 0i 0 + 1i
octave] |
disp([real(z1) real(z2) real(z1+z2) real(z1/z2)]) |
1 1 2 0
octave] |
disp([imag(z1) imag(z2) imag(z1+z2) imag(z1/z2)]) |
1 -1 0 1
octave] |
disp([abs(z1) abs(z2) abs(z1+z2) abs(z1/z2)]) |
1.4142 1.4142 2.0000 1.0000
octave] |
disp([arg(z1) arg(z2) arg(z1+z2) arg(z1/z2)]) |
0.78540 -0.78540 0.00000 1.57080
octave] |
I-sqrt(-1) |
ans = 0
octave] |
Care should be exercised about the cummulative effect of many floating point errors. For instance, in an “irrational” numerical investigation of Zeno's paradox, one might want to compare the distance traversed by step sizes that are scaled by starting from one to , traversed by step sizes scaled by starting from
In the reals the above two expressions are equal, , but this is not verfied for all when using floating point numbers. Lists of the values , for the two orderings , and , can be generated and summed.
octave] |
N=10; S=pi.^(0:-1:-N); T=pi.^(-N:1:0); sum(S)==sum(T) |
ans = 1
octave] |
N=15; S=pi.^(0:-1:-N); T=pi.^(-N:1:0); sum(S)==sum(T) |
ans = 0
octave] |
In the above numerical experiment a==b expresses an equality relationship which might evaluate as true denoted by 1, or false denoted by 0.
octave] |
disp([1==1 1==2]) |
1 0
octave] |
The above was called an “irrational” investigation since in Zeno's original paradox the scaling factor was 2 rather than , and due to the binary representation used by floats equality always holds.
octave] |
N=30; S=2.^(0:-1:-N); T=2.^(-N:1:0); sum(S)==sum(T) |
ans = 1
octave] |
The above numbers and their computer approximations are sufficient to describe many quantities encountered in applications. Typical examples include:
the position of a point on the unit line segment , approximated by the floating point number , to within machine epsilon precision, ;
the measure of resistance to change of the rate of motion known as mass, , ;
the population of a large community expressed as a float , even though for a community of individuals the population is a natural number, as in “the population of the United States is , i.e., 328.2 million”.
In most disciplines, there is a particular interest in comparison of two quantities, and to facilitate such comparison a common reference is used known as a standard unit. For measurement of a length , the meter is a standard unit, as in the statement , that states that is obtained by taking the standard unit ten times, . The rules for carrying out such comparisons are part of the definition of real and rational numbers. These rules are formalized in the mathematical definition of a field presented in the next chapter. Quantities that obey such rules, i.e., belong to a field, can be used in changes of scale and are called scalars. Not all numbers are scalars in this sense. For instance, the integers would not allow a scaling of 1:2 (halving the scale) even though 1,2 are integers.
Other quantities require more than a single number. The distribution of population in the year 2000 among the alphabetically-ordered South American countries (Argentina, Bolivia,..,Venezuela) requires 12 numbers. These are placed together in a list known in mathematics as a tuple, in this case a 12-tuple , with the population of Argentina, that of Bolivia, and so on. An analogous 12-tuple can be formed from the South American populations in the year 2020, say . Note that it is difficult to ascribe meaning to apparently plausible expressions such as since, for instance, some people in the 2000 population are also in the 2020 population, and would be counted twice.
In contrast to the population 12-tuple example above, combining multiple numbers is well defined in operations such as specifying a position within a three-dimensional Cartesian grid, or determining the resultant of two forces in space. Both of these lead to the consideration of 3-tuples or triples such as the force . When combined with another force the resultant is . If the force is amplified by the scalar and the force is similarly scaled by , the resultant becomes
It is useful to distinguish tuples for which scaling and addition is well defined from simple lists of numbers. In fact, since the essential difference is the behavior with respect to scaling and addition, the focus should be on these operations rather than the elements of the tuple.
The above observations underlie the definition of a vector
space by a set
whose elements satisfy certain scaling and addition properties,
denoted all together by the 4-tuple . The first element of the 4-tuple
is a set whose elements are called vectors. The second
element is a set of scalars, and the third is the vector addition
operation. The last is the scaling operation, seen as multiplication
of a vector by a scalar. The vector addition and scaling operations
must satisfy rules suggested by positions or forces in
three-dimensional space, which are listed in Table 1.
In particular, a vector space requires definition of two
distinguished elements: the zero vector ,
and the identity scalar element .
Addition rules for
Closure
Associativity
Commutativity
Zero vector
Additive inverse
Scaling rules for
,
Closure
Distributivity
Distributivity
Composition
Scalar identity
The definition of a vector space reflects everyday experience with vectors in Euclidean geometry, and it is common to refer to such vectors by descriptions in a Cartesian coordinate system. For example, a position vector within the plane can be referred through the pair of coordinates . This intuitive understanding can be made precise through the definition of a vector space , called the real 2-space. Vectors within are elements of , meaning that a vector is specified through two real numbers, . Addition of two vectors, , is defined by addition of coordinates . Scaling by scalar is defined by . Similarly, consideration of position vectors in three-dimensional space leads to the definition of the , or more generally a real -space , , .
Note however that there is no mention of coordinates in the definition of a vector space as can be seen from the list of properties in Table 1. The intent of such a definition is to highlight that besides position vectors, many other mathematical objects follow the same rules. As an example, consider the set of all continuous functions , with function addition defined by the sum at each argument , and scaling by defined as . Read this as: “given two continuous functions and , the function is defined by stating that its value for argument is the sum of the two real numbers and ”. Similarly: “given a continuous function , the function is defined by stating that its value for argument is the product of the real numbers and ”. Under such definitions is a vector space, but quite different from . Nonetheless, the fact that both and are vector spaces can be used to obtain insight into the behavior of continuous functions from Euclidean vectors, and vice versa.
(1) |
with the components arranged vertically and enclosed in square brackets. Given two vectors , and a scalar , vector addition and scaling are defined in by real number addition and multiplication of components
(2) |
The vector space is defined using the real numbers as the set of scalars, and constructing vectors by grouping together scalars, but this approach can be extended to any set of scalars , leading to the definition of the vector spaces . These will often be referred to as -vector space of scalars, signifying that the set of vectors is .
To aid in visual recognition of vectors, the following notation conventions are introduced:
vectors are denoted by lower-case bold Latin letters: ;
scalars are denoted by normal face Latin or Greek letters: ;
the components of a vector are denoted by the corresponding normal face with subscripts as in equation (1);
related sets of vectors are denoted by indexed bold Latin letters: .
In Octave, successive components placed vertically are separated by a semicolon.
octave] |
[1; 2; -1; 2] |
ans =
1
2
-1
2
octave] |
The equal sign in mathematics signifies a particular equivalence relationship. In computer systems such as Octave the equal sign has the different meaning of assignment, that is defining the label on the left side of the equal sign to be the expression on the right side. Subsequent invocation of the label returns the assigned object. Components of a vector are obtained by enclosing the index in parantheses.
octave] |
u=[1; 2; -1; 2]; u |
u =
1
2
-1
2
octave] |
u(3) |
ans = -1
octave] |
and is the notation used to denote a row vector. In Octave, horizontal placement of successive components in a row is denoted by a space.
octave] |
uT=transpose(u) |
uT =
1 2 -1 2
octave] |
[1 2 -1 2] |
ans =
1 2 -1 2
octave] |
uT(4) |
ans = 2
octave] |
octave] |
uT=[1 0 1 2]; vT=[2 1 3 -1]; wT=uT+vT; disp(wT) |
3 1 4 1
octave] |
rT=[1 2]; uT+rT |
operator +: nonconformant arguments (op1
is 1x4, op2 is 1x2)
octave] |
a=3; zT=a*uT; disp(zT) |
3 6 -3 6
octave] |
octave] |
m=4; disp(1:m) |
1 2 3 4
octave] |
disp(m:-1:2) |
4 3 2
octave] |
r=0; s=0.2; t=1; disp(r:s:t) |
0.00000 0.20000 0.40000 0.60000 0.80000
1.00000
octave] |
r=0; s=0.3; t=1; disp(r:s:t) |
0.00000 0.30000 0.60000 0.90000
octave] |
r=0; s= -0.2; t=1; disp(r:s:t) |
[](1x0)
octave] |
An efficient, expressive feature of many software systems including Octave is to use ranges as indices to a vector, as shown below for the definition of . Note that the index range i is organized as a row, and a transpose operation must be applied to obtain z as a column vector.
octave] |
m=4; i=1:m; z(i)=i.^i; z=transpose(z); disp(z) |
1
4
27
256
octave] |
i |
i =
1 2 3 4
octave] |
disp(transpose(z)) |
0 0 0 0
octave] |
octave] |
t=0:0.1:1.5; disp(sin(t)) |
Columns 1 through 8:
0.00000 0.09983 0.19867 0.29552 0.38942
0.47943 0.56464 0.64422
Columns 9 through 16:
0.71736 0.78333 0.84147 0.89121 0.93204
0.96356 0.98545 0.99749
octave] |
Indeed, such a piece-by-piece approach is not the way humans organize large amounts of information, preferring to conceptualize the data as some other entity: an image, a sound excerpt, a smell, a taste, a touch, a sense of balance, or relative position. All seven of these human senses will be shown to allow representation by linear algebra concepts, including representation by vectors.
As a first example consider visualization, the process of transforming data into a sight perception. A familiar example is constructing a plot of the graph of a function. Recall that in mathematics the graph of a function relating elements of the domain to those in codomain is a set of ordered pairs . For a commonly encountered function such as , the graph contains an uncountably infinite number of elements, and obviously cannot be explicitly listed. The sine function is continuous, meaning that no matter how small an open interval within the function codomain one considers, there exists an interval in the function domain whose image by the sine function is contained in . In mathematical “” notation this is stated as: . This mathematical notation is concise and precise, but perceptive mainly to the professional mathematician. A more intuitive visualization of continuity is obtained by approximating the graph of a function by a finite set of samples, . Strictly speaking, the sampled graph would indicate jumps interpretable as discontinuities, but when plotting the points human sight perception conveys a sense of continuity for large sample sizes, . For the sine function example, consider sampling the domain with a step size , . To obtain a visual representation of the sampled sine function the Octave plot function can be used to produce a figure that will appear in another window, interactively investigated, and subsequently closed. For large one cannot visually distinguish the points in the graph sample, though this is apparent for smaller sample sizes. This is shown below by displaying a subrange of the sampled points with stride . This example also shows the procedure to save a permanent copy of the displayed figure through the Octave print -deps command that places the currently displayed plot into an Encapsulated Postscript file. The generated figure file can be linked to a document as shown here in Figure 1, in which both plots render samples of the graph of the sine function, but the one with large is perceived as being continuous.
octave]
m=1000; h=2*pi/m; x=(0:m-1)*h;
octave]
y=sin(x); plot(x,y);
octave]
close;
octave]
s=50; i=1:s:m; xs=x(i); ys=y(i);
octave]
plot(x,y,'b',xs,ys,'bo');
octave]
print -depsc L01Fig01.eps;
octave]
close;
octave]
![]() |
Figure 1. Visualization of vectors of
sampled function graphs.
|
The real numbers themselves form the vector space , as does any field of scalars, . Juxtaposition of real numbers has been seen to define the new vector space . This process of juxtaposition can be continued to form additional mathematical objects. A matrix is defined as a juxtaposition of compatible vectors. As an example, consider vectors within some vector space . Form a matrix by placing the vectors into a row,
(3) |
To aid in visual recognition of a matrix, upper-case bold Latin letters will be used to denote matrices. The columns of a matrix will be denoted by the corresponding lower-case bold letter with a subscripted index as in equation (3). Note that the number of columns in a matrix can be different from the number of components in each column, as would be the case for matrix from equation (3) when choosing vectors from, say, the real space , .
Vectors were seen to be useful juxtapositions of scalars that could describe quantities a single scalar could not: a position in space, a force in physics, or a sampled function graph. The crucial utility of matrices is their central role in providing a description of new vectors other then their column vectors, and is suggested by experience with Euclidean spaces.
Consider first , the vector space of real numbers. A position vector on the real axis is specified by a single scalar component, , . Read this to mean that the position is obtained by traveling units from the origin at position vector . Look closely at what is meant by “unit” in this context. Since is a scalar, the mathematical expression has no meaning, as addition of a vector to a scalar has not been defined. Recall that scalars were introduced to capture the concept of scaling of a vector, so in the context of vector spaces they always appear as multiplying some vector. The correct mathematical description is , where is the unit vector . Taking the components leads to , where are the first (and in this case only) components of the vectors. Since , , , one obtains the identity .
Now consider , the vector space of positions in the plane. Repeating the above train of thought leads to the identification of two direction vectors and
octave] |
x=2; y=4; e1=[1; 0]; e2=[0; 1]; r=x*e1+y*e2 |
r =
2
4
octave] |
Continuing the procees to gives
For arbitrary , the components are now rather than the alphabetically ordered letters common for or . It is then consistent with the adopted notation convention to use to denote the position vector whose components are . The basic idea is the same as in the previous cases: to obtain a position vector scale direction by , by ,..., by , and add the resulting vectors.
Juxtaposition of the vectors leads to the formation of a matrix of special utility known as the identity matrix
The identity matrix is an example of a matrix in which the number of column vectors is equal to the number of components in each column vector . Such matrices with equal number of columns and rows are said to be square. Due to entrenched practice an exception to the notation convention is made and the identity matrix is denoted by , but its columns are denoted the indexed bold-face of a different lower-case letter, . If it becomes necessary to explicitly state the number of columns in , the notation is used to denote the identity matrix with columns, each with components.
The expression expresses the idea of scaling vectors within a set and subsequent addition to form a new vector . The matrix groups these vectors together in a single entity, and the scaling factors are the components of the vector . To bring all these concepts together it is natural to consider the notation
as a generalization of the scalar expression . It is clear what the operation should signify: it should capture the vector scaling and subsequent vector addition . A specific meaning is now ascribed to by identifying two definitions to one another.
and can be combined into a matrix . The value of the components are the scaling factors and can be combined into a vector . The same force must result irrespective of whether its components are given along the Cartesian axes or the inclined plane directions leading to the equality
(4) |
Interpret equation (4) to state that the vector could be obtained either as a linear combination of , , or as a linear combination of the columns of , . Of course the simpler description seems to be for which the components are already known. But this is only due to an arbitrary choice made by a human observer to define the force in terms of horizontal and vertical components. The problem itself suggests that the tangential and normal components are more relevant; for instance a friction force would be evaluated as a scaling of the normal force. The components in this more natural reference system are not known, but can be determined by solving the vector equality , known as a linear system of equations. Procedures to carry this out will be studied in more detail later, but Octave provides an instruction for this common problem, the backslash operator, as in x=A\b.
ans =
0.20000
0.40000
x =
0.37321
0.24641
ans =
0.32321 -0.12321
0.18660 0.21340
octave]
ex=[1; 0]; ey=[0; 1];
octave]
b=[0.2; 0.4]; I=[ex ey]; I*b
octave]
th=pi/6; c=cos(th); s=sin(th);
octave]
tvec=[c; s]; nvec=[-s; c];
octave]
A=[tvec nvec];
octave]
x=A\b
octave]
[x(1)*tvec x(2)*nvec]
octave]
|
Figure 2. Alternative decompositions of force
on inclined plane.
|
Define now another periodic function by repeating the values from the interval on all intervals , for . The function is not given in terms of the “naturally” periodic functions , , but could it thus be expressed? This can be stated as seeking a linear combination as studied in Fourier analysis. The coefficients could be determined from an analytical formula involving calculus operations but we'll seek an approximation using a linear combination of terms
Organize this as a matrix vector product , with
The idea is to sample the column vectors of at the components of the vector , , , . Let , and , denote the so-sampled functions leading to the definition of a vector and a matrix . There are coefficients available to scale the column vectors of , and has components. For it is generally not possible to find such that would exactly equal , but as seen later the condition to be as close as possible to leads to a well defined solution procedure. This is known as a least squares problem and is automatically applied in the Octave x=A\b instruction when the matrix A is not square. As seen in the following numerical experiment and Figure 3, the approximation is excellent even though the information conveyed by samples of is now much more efficiently stored in the form chosen for the columns of and the scaling coefficients that are the components of .
octave]
m=1000; h=2*pi/m; j=1:m;
octave]
t(j)=(j-1)*h; t=transpose(t);
octave]
n=5; A=[];
octave]
for k=1:n
A = [A sin(k*t)];
end
octave]
bt=t.*(pi-t).*(2*pi-t);
octave]
x=A\bt;
octave]
b=A*x;
octave]
s=50; i=1:s:m;
ts=t(i); bs=bt(i);
plot(ts,bs,'ok',t,b,'r');
octave]
print -depsc L01Fig02.eps
octave]
close;
octave]
The above examples highlight some essential aspects of linear algebra in the context of data science applications.
Vectors organize information that cannot be expressed as a single number and for which there exists a concept of scaling and addition.
Matrices group together multiple vectors.
The matrix-vector product expresses a linear combination of the column vectors of the matrix.
Solving a linear system , to find for given , re-expresses the linear combination
as another linear combination
For certain problems the linear combination might be more insightful, but the above transformation is information-preserving, with both having the same number of components.
Finding the best approximation of some given by a linear combination of the column vectors of is known as a least squares problem and transforms the information from the components of into components of , and knowledge of the form of the column vectors. If and the form of the columns of can be succintly stated, the transformation compresses information.
Data science seeks to extract regularity directly from available data, not necessarily invoking any additional hypotheses. The typical scenario is that immense amounts of data are available, with limited capability of human analysis. In this context it is apparent that the least squares problem is of greater interest than solving a linear system with a square matrix. It should also be clear that while computation by hand of small examples is useful to solidify theroretical concepts, it is essential to become proficient in the use of software that can deal with large data sets, such as Octave.