matMulRect

Computes the transpose of a matrix, a matrix-vector product, a matrix-matrix product, a bilinear form, or any triple product.

Synopsis

matMulRect (string)

Required Arguments

char string (Input)
String indicating operation to be performed. See the Description section below for more details.

Return Value

The result of the operation. This is always a float, even if the result is a single number. If no answer was computed, None is returned.

Optional Arguments

aMatrix, float a[] (Input)
The nrowa × ncola matrix A.
bMatrix, float b[] (Input)
The nrowb × ncolb matrix A.
xVector, float (Input)
Vector x of size nx.
yVector, float (Input)
Vector y of size ny.

Description

This function computes a matrix-vector product, a matrix-matrix product, a bilinear form of a matrix, or a triple product according to the specification given by string. For example, if “A\*x” is given, Ax is computed. In string, the matrices A and B and the vectors x and y can be used. Any of these four names can be used with trans, indicating transpose. The vectors x and y are treated as n × 1 matrices.

If string contains only one item, such as “x” or “trans(A)”, then a copy of the array, or its transpose, is returned. If string contains one multiplication, such as “A\*x” or “B\*A”, then the indicated product is returned. Some other legal values for string are “trans(y)\*A”, “A\*trans(B)”, “x\*trans(y)”, or “trans(x)\*y”.

The matrices and/or vectors referred to in string must be given as optional arguments. If string is “B\*x”, then bMatrix and xVector must be given.

Example

Let A, B, x, and y equal the following matrices:

\[\begin{split}A = \begin{bmatrix} 1 & 2 & 9 \\ 5 & 4 & 7 \\ \end{bmatrix} \phantom{...} B = \begin{bmatrix} 3 & 2 \\ 7 & 4 \\ 9 & 1 \\ \end{bmatrix} \phantom{...} x = \begin{bmatrix} 7 \\ 2 \\ 1 \\ \end{bmatrix} \phantom{...} y = \begin{bmatrix} 3 \\ 4 \\ 2 \\ \end{bmatrix}\end{split}\]

The arrays \(A^T\), Ax, \(x^T\)\(A^T\), AB, \(B^T\)\(A^T\), \(x^Ty\), \(xy^T\) and \(x^T Ay\) are computed and printed.

from pyimsl.stat.matMulRect import matMulRect
from pyimsl.stat.writeMatrix import writeMatrix

a = [[1, 2, 9], [5, 4, 7]]
b = [[3, 2], [7, 4], [9, 1]]
x = [7, 2, 1]
y = [3, 4, 2]
ans1 = matMulRect("trans(A)", aMatrix=a)
writeMatrix("trans(A)", ans1)

ans2 = matMulRect("A*x", aMatrix=a, xVector=x)
writeMatrix("A*x", ans2)

ans3 = matMulRect("trans(x)*trans(A)", aMatrix=a, xVector=x)
writeMatrix("trans(x)*trans(A)", ans3)

ans4 = matMulRect("A*B", aMatrix=a, bMatrix=b)
writeMatrix("A*B", ans4)

ans5 = matMulRect("trans(B)*trans(A)", aMatrix=a, bMatrix=b)
writeMatrix("trans(B)*trans(A)", ans5)

ans6 = matMulRect("trans(x)*y", xVector=x, yVector=y)
writeMatrix("trans(x)*y", ans6)

ans7 = matMulRect("x*trans(y)", xVector=x, yVector=y)
writeMatrix("x*trans(y)", ans7)

shortx = [7, 2]   # Use only the first two components of x
ans8 = matMulRect("trans(x)*A*y", aMatrix=a, xVector=shortx, yVector=y)
writeMatrix("trans(x)*A*y", ans8)

Output

 
         trans(A)
             1            2
1            1            5
2            2            4
3            9            7
 
           A*x
          1            2
         20           50
 
    trans(x)*trans(A)
          1            2
         20           50
 
            A*B
             1            2
1           98           19
2          106           33
 
     trans(B)*trans(A)
             1            2
1           98          106
2           19           33
 
trans(x)*y
         31
 
               x*trans(y)
             1            2            3
1           21           28           14
2            6            8            4
3            3            4            2
 
trans(x)*A*y
         293