matMulRect

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

Synopsis

matMulRect (string)

Required Arguments

char string (Input)
String indicating matrix multiplication to be performed.

Return Value

The result of the multiplication. This is always an array, even if the result is a single number. If no answer was computed, then 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 x (Input)
The vector x of size nx.
yVector, float y (Input)
The 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

\[\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^TAy\) are computed and printed.

from pyimsl.math.matMulRect import matMulRect
from pyimsl.math.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)

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

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

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

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

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

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

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

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