Matrices Practice

download Matrices Practice

of 40

Transcript of Matrices Practice

  • 8/13/2019 Matrices Practice

    1/40

    Matrices

    COMM2M

    Harry R. Erwin, PhDUniversity of Sunderland

  • 8/13/2019 Matrices Practice

    2/40

    Basic Concepts

    Vector space

    Linear transformation

  • 8/13/2019 Matrices Practice

    3/40

    Resources

    Korevaar, J., 1968,Mathematical Methods,

    Academic Press.

  • 8/13/2019 Matrices Practice

    4/40

    Vector Space

    A vector space or linear space Vis a collection of

    elements with the following properties:

    Ifxandy are any two elements of V then V contains anelement that may be called the sumx + y ofx andy.

    Ifx is any element of V and an arbitrary scalar

    (element of the underlying commutative field) then V

    also contains an element that can be called the scalarmultiple x.

    In applied mathematics, the underlying commutative

    field is almost always the real or complex numbers.

  • 8/13/2019 Matrices Practice

    5/40

    Vector Space Axioms1. Existence of vector addition.

    2. Addition is commutative.

    3. Addition is associative.

    4. Existence of a unique zero vector.

    5. Existence of a unique additive inverse.

    6. Existence of scalar multiplication.

    7. Scalar multiplication is associative.8. Multiplication by 1 is the identity.

    9. Multiplication by 0 produces the zero vector.

    10. Multiplication and addition interact as expected.

  • 8/13/2019 Matrices Practice

    6/40

    Examples of Vector Spaces

    Ordinary n-dimensional space,Rn.

    Complex n-dimensional space, Cn.

    Infinite sequences of real or complex numbers.

    The continuous functions, C(a,b),defined on a finiteinterval (a,b).

    Cn(a,b),the continuous functions on the same

    interval with ncontinuous derivatives. The integrable functions on the same interval with a

    linear norm, 1[a,b] or squared norm, 2[a,b].

    The linear maps betweenRnandRm(or Cnand Cm).

  • 8/13/2019 Matrices Practice

    7/40

    Linear Transformation

    A transformation from one vector space intoanother (or into itself) that commutes withaddition and scalar multiplication. If such atransformation is denotedL:

    L(x+y) = Lx + Ly

    Lx = Lx

    Ordinary differentiation is an example. Everylinear ordinary or partial differential operator is alinear transformation.

    Laplace, Fourier, and other integral

    transformations are linear.

  • 8/13/2019 Matrices Practice

    8/40

    Matrices and Linear

    Transformations Matrix productsAxT(whereAis a matrix,

    andxTa column vector) can define linear

    transformations with respect to given finitedimensional bases over a commutative

    field.

    Many linear problems in engineering andmathematics can be written in the form:

    Lx = z.

  • 8/13/2019 Matrices Practice

    9/40

    Vector Spaces and Coordinate

    Bases A basis of a vector space is a collection of

    vectors so that any vector in the space can

    be uniquely described as a linearcombination of those basis vectors.

    Every vector space has such a basis.

    The number of such basis vectors is thedimension of the vector space. This can beshown unique.

  • 8/13/2019 Matrices Practice

    10/40

    Scalar Norms

    For a real number, r,the absolute value of

    the number, |r|,is defined to be r if r > 0,

    and otherwise -r.

    For a complex number, a + bi,the absolute

    value of the number is defined to be the

    positive square root of (a2+b2).

  • 8/13/2019 Matrices Practice

    11/40

    Normed Vector Spaces

    A norm for a vector space is a map, m,fromthe vector space into the non-negative real

    numbers that has the following properties:m(v) = ||m(v)

    m(a+b) m(a) + m(b)

    m(x) = 0 iff x is the zero vector

    mcan be referred to as a distance functionand written ||x||.

  • 8/13/2019 Matrices Practice

    12/40

    Properties of Normed Vector

    Spaces In a normed vector space, every basis vector has a

    length > 0. If the underlying scalar field is the

    complex or real numbers, each basis vector can bereplaced by a basis vector of length one.

    Three useful norms are:

    ||x||1 = |x1| + . . . + |xn|

    ||x||2 = (|x1|2+ . . . + |xn|2)

    ||x||= max(|x1|, . . . , |xn|)

  • 8/13/2019 Matrices Practice

    13/40

    Inner Product

    The inner product of two vectors,xandy, in anormed vector space relative to a specific basis ofunit vectors is defined as:

    x.y= xiyi.Wherexiandyi are the coefficients of each vector.

    Ifx.y= 0, the vectors are normal to each other.

    If bi.bj= 0 for any two different vectors in thebasis, the vector space has an orthonormal basis.This can be very convenient.

    A finite dimensional vector space overRor Chas

    an infinite number of such bases.

  • 8/13/2019 Matrices Practice

    14/40

    Matrices and Linear

    Transformations Suppose we have a linear transformation,L,

    between two finite dimensional vector spaces,FnandFm, each with an identified basis.Lthen can

    be written as a nxmmatrixMwith coefficients inF.

    If Tis a transformation between two bases ofFn,(T:B

    1

    -> B2

    ), then the matrix transformation forLwritten in terms ofB2is T

    -1.M. If TtransformsFmtoFm, the matrix transformation isM

    .T, (usingmatrix multiplication and inversion).

  • 8/13/2019 Matrices Practice

    15/40

    Writing Matrices

    A matrixAcan this be written as a collection ofelements aij, where i is the row number and j is thecolumn number.

    The transpose,AT, is a matrix consisting of aji.

    Matrices of the same number of rows and columnscan be summed.

    If matrix A has i rows and j columns, and matrix Bhas j rows and k columns, the productA.B isdefined as a matrix C with i rows and k columnssuch that cik= aij

    .bjk.

  • 8/13/2019 Matrices Practice

    16/40

    Other Key Concepts

    The rank of a matrix is the rank of thecorresponding linear transformation (thedimension of the space that the lineartransformation maps into). This may besmallerthan the number of columns of the matrix.

    Two matricesA, Bare equivalent if they representthe same linear transformation for different bases.

    SupposeRand Sare invertibletransformationsbetween bases in the source and destination vectorspaces such thatR.A = B.S. ThenAandBareequivalent.

  • 8/13/2019 Matrices Practice

    17/40

    Square Matrices

    A square matrix represents a linear transformation

    from a vector space to itself.

    The square matrix,I(defined as aij= 1.0 if i=j and0.0 otherwise)represents the identity

    transformation.

    An invertible matrixAhas a second unique

    matrix,A-1such thatA.A-1= I. HenceArepresents

    a one-to-one linear transformation.

  • 8/13/2019 Matrices Practice

    18/40

    Determinant

    The determinant of a linear transformationLfrom avector space to itself is a non-zero function to the scalarfield that computes the (signed) volume of the image of

    a n-cube whenL is applied to it. The sign describeswhether the resulting image has the same orientation.

    The determinant is independent of the basis used torepresent the transformation.

    Algorithms for matrix inversion typically use thedeterminant. Computing eachcomponent of thedeterminant involves nmultiplications, where nis thedimension of the vector space.

  • 8/13/2019 Matrices Practice

    19/40

    Computational Issues in Matrix

    Arithmetic Computer arithmetic is almost always inexact:

    When you add two variables, much of the significance

    of the smaller variable can be lost. If it is small enoughrelative to the other, it is treated as zero.

    When you multiply two variables, the lowest order bit

    of the product is noisy.

    When you take the difference of two nearly equalnumbers, most of the resulting bits are noise.

    Determinants are particularly vulnerable to this.

  • 8/13/2019 Matrices Practice

    20/40

    Problems in Matrix Arithmetic

    Watch out for nearly singular systems of linearequations. These are systems where the determinantis close to zero. Round-off errors are likely to make

    these systems linearly dependent. Watch for accumulated round-off errors in systems

    with high dimensionality. Your solutions need to betested rather than trusted.

    Watch out for systems where the row or columnnorms vary massively. The smaller rows/columnswill lose much of their significance.

    MATLAB is designed to handle these problems.

  • 8/13/2019 Matrices Practice

    21/40

    MATLAB and Matrices

    The basic data type in MATLAB is adouble, an array of complex numbers.

    At this point, we will consider mby nmatrices. A column vectorhas n = 1and arow vectorhas m = 1.

    To access the ijth component of the matrixA, use A(i,j).

  • 8/13/2019 Matrices Practice

    22/40

    Creating Matrices

    zeros(m,n) (or zeros([m,n])) creates an m-by-nmatrix of 0.0.

    ones(m,n) creates an m-by-n matrix of 1.0. eye(m,n) creates an m-by-n matrix of 1.0 for i = jand otherwise 0.0.

    eye(m) (etc.) creates an m-by-m matrix.

    rand(m,n) contains uniformly distributed randomnumbers selected from [0,1].

    randn(m,n) contains normally distributed randomnumbers (from the standard normal distribution).

  • 8/13/2019 Matrices Practice

    23/40

    Literal Matrices

    Matrices can be built explicitly using the squarebracket notation:

    A = [2 3 57 11 13

    17 19 23]

    This creates a 3x3 matrix with those values.

    Row ends can be specified by ; instead of carriagereturns. Separators are spaces or ,

    Dont separate a + or - sign by a space!

  • 8/13/2019 Matrices Practice

    24/40

    Assembling Matrices

    B = [1 2; 3 4]

    C = [B zeros(2)

    ones(2) eye(2)]

    C =

    1 2 0 0

    3 4 0 01 1 1 0

    1 1 0 1

  • 8/13/2019 Matrices Practice

    25/40

    Other Approaches

    Block diagonal matrices can be created by

    the blkdiag function.

    Tiled matrices can be created by usingrepmat.

    There is a large list of special matrices.

  • 8/13/2019 Matrices Practice

    26/40

    Subscripting and the Colon

    Notation The colon is used to define vectors that can act as

    subscripts. For integers i and j, i:j is used to denotea row vector from i to j in steps of 1.

    A nonunit stride or step is denoted i:s:j.

    Matrix subscripts (1 or greater!) are accessed asA(i,j).

    A(p:q,r:s) is a submatrix of A. A(:,j) is the jth column and A(i,:) is the ith row.

    endrepresents the last column or row.

  • 8/13/2019 Matrices Practice

    27/40

    Arbitrary Submatrices

    A([i j k],[p q]) is the 3x2 submatrix built from the

    intersection of the ith, jth, and kth rows with the

    pth and qth columns. A(:) is a vector consisting of all the elements of A

    taken from the columns in order from first to last.

    A(:) = valueswill fill A, preserving its shape.

    linspace(a,b,n) will create a vector of n values

    equally spaced from a to b. n defaults to 100.

  • 8/13/2019 Matrices Practice

    28/40

    The Empty Matrix

    [] is an empty 0-by-0 matrix.

    Assigning [] to a row or column deletes that

    row or column from the matrix.

    Also is used as a placeholder in argument

    lists.

  • 8/13/2019 Matrices Practice

    29/40

    Matrix and Array Operations

    Operation Matrix Array

    Addition + +

    Subtraction - -

    Multiplication * .*

    Left Division \ .\

    Right Division / ./

    Exponentiation ^ .^

  • 8/13/2019 Matrices Practice

    30/40

    Left and Right Division

    a/b means a/b in the usual sense.

    a\b means b/a!

    For matrices, these are carried out using

    matrix operations

    A/B means A*B-1 (solving X*B = A)

    A\B means A-1*B (solving A*X = B)

    For elementwise operations, precede with .

  • 8/13/2019 Matrices Practice

    31/40

    Matrix Powers

    A^n is defined for all powers, including negative

    and non-integer.

    .^ is elementwise. Conjugate transpose operation is A

    Transpose without conjugation is A.

    There are functional alternatives. x*y is the dot product of two column vectors.

    cross(x,y) is the cross product when defined.

  • 8/13/2019 Matrices Practice

    32/40

    Scalars and Matrices

    A + x will add x to every entry in A

    A*x will multiply every entry in A by x

    A/x will divide in the same way.

  • 8/13/2019 Matrices Practice

    33/40

    Data Analysis

    max

    min

    mean median

    std

    var

    sort

    sum

    prod cumsum

    cumprod

    diff

  • 8/13/2019 Matrices Practice

    34/40

    Linear Algebra and MATLAB

    norm(x,y)will give the y-norm of the

    vector x. y = inf will produce the max

    absolute value and y = -inf will produce themin absolute value.

    The p-norm of a matrix is defined as

    ||Ax||p/||x||p, forxnon-zero in length.

  • 8/13/2019 Matrices Practice

    35/40

    Linear Equations

    The fundamental tool is the backslash

    operator, \.

    SolvesAx = b

    AX = B

  • 8/13/2019 Matrices Practice

    36/40

    Results

    If A is n-by-n non-singular, A\b is the

    solution to Ax = b. Solution methods

    include:LU factorization with partial pivoting

    Triangular (by substitution)

    Hermitian positive definite (Choleskyfactorization)

    Checks conditioning.

  • 8/13/2019 Matrices Practice

    37/40

    Overdetermined

    If A is m-by-n, with m>n, it has more

    equations than unknowns.

    A\b is a least squares solution.

  • 8/13/2019 Matrices Practice

    38/40

    Underdetermined System

    Fewer equations than unknowns.

    If it is solvable at all, A\b produces a basic

    solution. Otherwise A\b is a least squaressolution.

  • 8/13/2019 Matrices Practice

    39/40

    Inverse, Pseudo-Inverse and

    Determinant The matrix inverse is computed by inv(A).

    Not usually needed, A\bis faster and more

    accurate. det(A)is the determinant of a square

    matrix. Sensitive to rounding errors, but

    accurate for integer matrices. pinv(A)computes the pseudo-inverse of A.

  • 8/13/2019 Matrices Practice

    40/40

    Tutorial Assignment

    A perception of depth in a 2-D display can begenerated by the use of a stereogram.

    Suppose you have some three-dimensional data{xi,yi,zi}.

    Create a pair of side-by-side plots consisting of thevectors (x-d,y)and (x+d,y),where

    di= c(zmax-zi)*(xmax-xmin)/(zmax-zmin). c depends on the separation of the displays and the

    units of measurement. Explore various options.