Matrices-Unit26.mws

MATRICES AND MATRIX OPERATIONS: Unit 26

Dr. Wlodzislaw Kostecki

The Papua New Guinea University of Technology (PNGUT)

Department of Electrical and Communication Engineering

Lae, Morobe Province

Papua New Guinea

Copyright © 2000 by Wlodzislaw Kostecki

All rights reserved

-------------------------------------------------------------------

(26) Applying a function to matrix elements

OBJECTIVES :

To provide alternative methods of application of a function to each element of a matrix.

To provide alternative methods of application of a function to selected elements of a matrix.

To provide examples of application of some common functions to constant-matrix elements.

To show how to suppress Maple s internal evaluation of matrix elements upon application of a function if the textbook form of display of such a matrix is desired.

To show how to perform exact and floating-point evaluation of a matrix that has been entered with Maple s internal evaluation suppressed.

> restart : with(linalg, coldim, diag, rowdim) :

There are three alternative methods of applying a function to each element of a matrix. One method uses the evalm function and two methods employ the map function.

For example, consider a ( 2 × 3 ) matrix [ A ]

> A := matrix(2, 3, [a[11], a[12], a[13], a[21], a[22], a[23]]) : A = matrix(A) ;

A = matrix([[a[11], a[12], a[13]], [a[21], a[22], a...

and obtain a matrix whose elements are equal to the cosine of the elements of [ A ].

This may be done using any of the following alternative methods.

Method 1 . Using the evalm function:

> A_cos := evalm(cos(A)) : A_cos = matrix(A_cos) ;

A_cos = matrix([[cos(a[11]), cos(a[12]), cos(a[13])...

Method 2 . Using the map function:

> A_cos := map(cos, A) : A_cos = matrix(A_cos) ;

A_cos = matrix([[cos(a[11]), cos(a[12]), cos(a[13])...

Method 3 . Using the function map together with the arrow-type procedure:

> A_cos := map(x->cos(x), A) : A_cos = matrix(A_cos) ;

A_cos = matrix([[cos(a[11]), cos(a[12]), cos(a[13])...

* * *

N.B. If a function is to be applied only to selected elements of a matrix, inputting of such a matrix requires application of the function individually to each of the elements chosen. This can be done using either of the following alternative methods.

Method 1 . Use the subs function to substitute the selected matrix elements with cosine of these elements.

For example, apply the function cosine to the elements a[11] , a[13] , and a[22] of the same matrix [ A ].

> A_cos_select := subs(A[1,1]=cos(A[1,1]), A[1,3]=cos(A[1,3]), A[2,2]=cos(A[2,2]), matrix(A)) :

> A_cos_select = matrix(A_cos_select) ;

A_cos_select = matrix([[cos(a[11]), a[12], cos(a[13...

Method 2 . Specify the selected matrix elements to which the function cosine is to be applied and evaluate the matrix.

For example, apply the function cosine to the elements a[12] , a[21] , and a[23] of the same matrix [ A ].

> A[1,2] := cos(A[1,2]) : A[2,1] := cos(A[2,1]) : A[2,3] := cos(A[2,3]) :

> A_cos_select := eval(A) : A_cos_select = matrix(A_cos_select) ;

A_cos_select = matrix([[a[11], cos(a[12]), a[13]], ...

* * *

N.B. If a function is to be applied to diagonal elements of a diagonal matrix, either of the following methods may be used.

For example, apply a function f to the diagonal elements of a ( 4 × 4 ) diagonal matrix [ A ] given as

> A := diag(a[11], a[22], a[33], a[44]) : A = matrix(A) ;

A = matrix([[a[11], 0, 0, 0], [0, a[22], 0, 0], [0,...

Method 1 . Using the array function together with the diagonal indexing function:

> A_f := array(diagonal, 1..4, 1..4, [(1,1)=f(A[1,1]), (2,2)=f(A[2,2]), (3,3)=f(A[3,3]), (4,4)=f(A[4,4])]) :

> A_f = matrix(A_f) ;

A_f = matrix([[f(a[11]), 0, 0, 0], [0, f(a[22]), 0,...

Method 2 . Using the double for -loop construct:

> a_f := array(diagonal, 1..rowdim(A), 1..coldim(A)) :

> for i to rowdim(A) do for j to coldim(A) do if j = i then a_f[i,j] := f(A[i,j]) fi : od : od :

> A_f := matrix(a_f) : A_f = matrix(A_f) ;

A_f = matrix([[f(a[11]), 0, 0, 0], [0, f(a[22]), 0,...

N.B. The construction of this method is such that it has no effect on [ A ] in view of a possible need for using the matrix in further computations.

* * *

For the purposes of numerical examples of application of functions to all elements of a matrix, consider a ( 2 × 3 ) matrix [ A ] given as

> A := matrix(2, 3, [-2, 3, 1, 5, 2, 4]) : A = matrix(A) ;

A = matrix([[-2, 3, 1], [5, 2, 4]])

Apply the following functions to the matrix elements and compute their values.

(a) Apply the exponential function to each element of matrix [ A ]

> A_exp := map(exp, A) : A_exp = matrix(A_exp) ;

A_exp = matrix([[exp(-2), exp(3), exp(1)], [exp(5),...

or, using the evalm function,

> A_exp := evalm(exp(A)) : A_exp = matrix(A_exp) ;

A_exp = matrix([[exp(-2), exp(3), exp(1)], [exp(5),...

Floating-point evaluation of the above exact result gives the following matrix:

> A_exp := evalf(matrix(A_exp)) : A_exp = matrix(A_exp) ;

A_exp = matrix([[.1353352832, 20.08553692, 2.718281...

(b) Apply the function natural logarithm to each element of matrix [ A ]

N.B. Upon application of a function to matrix elements, Maple computes internally the exact values of new matrix elements and displays them in this form. If it is required to display the new elements in the unevaluated form f (original element), a proper use of single quotes will prevent internal evaluation. This is illustrated below for the function natural logarithm applied to the elements of matrix [ A ].

> A_ln := map('('ln', A)') : A_ln = matrix(A_ln) ;

A_ln = matrix([[ln(-2), ln(3), ln(1)], [ln(5), ln(2...

N.B. It may be easily noticed that evaluation of the element at location (1,1) will give a complex number. Therefore, evaluation of the above matrix will yield a complex matrix. The exact evaluation may be done by using either the function evalc , which is specifically designed to evaluate complex-valued expressions or the function eval .

Method 1 . Using the function map together with the arrow-type procedure including function evalc :

> A_ln = map(x->evalc(x), A_ln) ;

A_ln = matrix([[ln(2)+I*Pi, ln(3), 0], [ln(5), ln(2...

Method 2 . Using the function map together with the arrow-type procedure including function eval :

> A_ln := map(x->eval(x), A_ln) : A_ln = matrix(A_ln) ;

A_ln = matrix([[ln(2)+I*Pi, ln(3), 0], [ln(5), ln(2...

Floating-point evaluation yields

> A_ln := evalf(matrix(A_ln)) : A_ln = matrix(A_ln) ;

A_ln = matrix([[.6931471806+3.141592654*I, 1.098612...

(c) Apply the trigonometric function sine to each element of matrix [ A ]

> A_sin := map(sin, A) : A_sin = matrix(A_sin) ;

A_sin = matrix([[-sin(2), sin(3), sin(1)], [sin(5),...

or, as a floating-point approximation,

> A_sin := evalf(matrix(A_sin)) : A_sin = matrix(A_sin) ;

A_sin = matrix([[-.9092974268, .1411200081, .841470...

(d) Apply the inverse trigonometric function arc tangent to each element of matrix [ A ]

> A_arctan := map('('arctan', A)') : A_arctan = matrix(A_arctan) ;

A_arctan = matrix([[arctan(-2), arctan(3), arctan(1...

Exact evaluation of the above matrix yields

> A_arctan := map(x->eval(x), A_arctan) : A_arctan = matrix(A_arctan) ;

A_arctan = matrix([[-arctan(2), arctan(3), 1/4*Pi],...

Floating-point evaluation yields

> A_arctan := evalf(matrix(A_arctan)) : A_arctan = matrix(A_arctan) ;

A_arctan = matrix([[-1.107148718, 1.249045772, .785...

(e) Apply the hyperbolic function hyperbolic sine to each element of matrix [ A ]

> A_sinh := map(sinh, A) : A_sinh = matrix(A_sinh) ;

A_sinh = matrix([[-sinh(2), sinh(3), sinh(1)], [sin...

or, as a floating-point approximation,

> A_sinh := evalf(matrix(A_sinh)) : A_sinh = matrix(A_sinh) ;

A_sinh = matrix([[-3.626860408, 10.01787493, 1.1752...

(f) Apply the inverse hyperbolic function inverse hyperbolic sine to each element of matrix [ A ]

> A_arcsinh := map('('arcsinh', A)') : A_arcsinh = matrix(A_arcsinh) ;

A_arcsinh = matrix([[arcsinh(-2), arcsinh(3), arcsi...

Exact evaluation of the above matrix yields

> A_arcsinh := map(x->eval(x), A_arcsinh) : A_arcsinh = matrix(A_arcsinh) ;

A_arcsinh = matrix([[-arcsinh(2), arcsinh(3), ln(1+...

Floating-point evaluation yields

> A_arcsinh := evalf(matrix(A_arcsinh)) : A_arcsinh = matrix(A_arcsinh) ;

A_arcsinh = matrix([[-1.443635475, 1.818446459, .88...

(g) Apply the square root function to each element of matrix [ A ]

> A_sqrt := map(sqrt, A) : A_sqrt = matrix(A_sqrt) ;

A_sqrt = matrix([[I*sqrt(2), sqrt(3), 1], [sqrt(5),...

or, as a floating-point approximation,

> A_sqrt := evalf(matrix(A_sqrt)) : A_sqrt = matrix(A_sqrt) ;

A_sqrt = matrix([[1.414213562*I, 1.732050808, 1.], ...

(h) Apply a positive fractional exponent of 2 / 3 to each element of matrix [ A ]

> `A_pwr(2/3)` := map(x->x^(2/3), A) : A_pwr(`2/3`) = matrix(`A_pwr(2/3)`) ;

A_pwr(`2/3`) = matrix([[(-2)^(2/3), 3^(2/3), 1], [5...

Since evaluation of the element at location (1,1) will give a complex number, a further evaluation of the above matrix may be tried by using the function map together with the arrow-type procedure including function evalc . This yields the following complex matrix:

> `A_pwr(2/3)` := map(x->evalc(x), `A_pwr(2/3)`) : A_pwr(`2/3`) = matrix(`A_pwr(2/3)`) ;

A_pwr(`2/3`) = matrix([[-1/2*2^(2/3)+1/2*I*2^(2/3)*...

Floating-point evaluation yields

> `A_pwr(2/3)` := evalf(matrix(`A_pwr(2/3)`)) : A_pwr(`2/3`) = matrix(`A_pwr(2/3)`) ;

A_pwr(`2/3`) = matrix([[-.7937005260+1.374729638*I,...

(h) Apply a negative fractional exponent of – 2 / 5 to each element of matrix [ A ]

To display this operation in "like-in-a-book" form, suppress Maple s internal evaluation as follows:

> `A_pwr(-2/5)` := map(x->x^(`-2/5`), A) : A_pwr(`-2/5`) = matrix(`A_pwr(-2/5)`) ;

A_pwr(`-2/5`) = matrix([[(-2)^`-2/5`, 3^`-2/5`, 1],...

Normally, Maple makes internal evaluation and displays the result in the following form:

> `A_pwr(-2/5)` := map(x->x^(-2/5), A) : A_pwr(`-2/5`) = matrix(`A_pwr(-2/5)`) ;

A_pwr(`-2/5`) = matrix([[-1/2*(-2)^(3/5), 1/3*3^(3/...

Since evaluation of the element at location (1,1) will give a complex number, a further evaluation of the above matrix may be tried by using the function map together with the arrow-type procedure including function evalc . This yields the following complex matrix:

> `A_pwr(-2/5)` := map(x->evalc(x), `A_pwr(-2/5)`) : A_pwr(`-2/5`) = matrix(`A_pwr(-2/5)`) ;

A_pwr(`-2/5`) = matrix([[1/2*2^(3/5)*cos(2/5*Pi)-1/...

Floating-point evaluation yields

> `A_pwr(-2/5)` := evalf(matrix(`A_pwr(-2/5)`)) : A_pwr(`-2/5`) = matrix(`A_pwr(-2/5)`) ;

A_pwr(`-2/5`) = matrix([[.2341910885-.7207660590*I,...

* * *

Proceed to Unit (27) for " Differentiation of matrices comprising functions ".

-------------------------------------------------------------------