matAddCoordinate¶
Performs element-wise addition on two real matrices stored in coordinate format, \(C\leftarrow\alpha A+\beta B\).
Synopsis¶
matAddCoordinate (n, alpha, a, beta, b, nzC)
The type double function is matAddCoordinate
.
Required Arguments¶
- int
n
(Input) - The order of the matrices A and B.
- float alpha (Input)
- Scalar multiplier for A.
- sparse_elem
a[]
(Input) - Vector of length
nz_a
containing the location and value of each nonzero entry in the matrix A. - float
beta
(Input) - Scalar multiplier for B.
- sparse_elem
b[]
(Input) - Vector of length
nz_b
containing the location and value of each nonzero entry in the matrix B. - int
nzC
(Output) - The number of nonzeros in the sum \(\alpha A+\beta B\).
Return Value¶
An array of type sparse_elem containing the computed sum. In the event of
an error or if the return matrix has no nonzero elements, None
is
returned.
Optional Arguments¶
aTranspose
- Replace A with \(A^T\) in the expression \(\alpha A+\beta B\).
bTranspose
- Replace B with \(B^T\) in the expression \(\alpha A+\beta B\).
Description¶
The function matAddCoordinate
forms the sum \(\alpha A+\beta B\),
given the scalars α and β, and the matrices A and B in coordinate format.
The transpose of A and/or B may be used during the computation if
optional arguments are specified. The method starts by storing A in a
linked list data structure, and performs the multiply by α. Next the data in
matrix B is traversed and if the coordinates of a nonzero element
correspond to those of a nonzero element in A, that entry in the linked
list is updated. Otherwise, a new node in the linked list is created. The
multiply by β occurs at this time. Lastly, the linked list representation of
C is converted to coordinate representation, omitting any elements that may
have become zero through cancellation.
Examples¶
Example 1¶
Add two real matrices of order 4 stored in coordinate format. Matrix A has five nonzero elements. Matrix B has seven nonzero elements.
from __future__ import print_function
from pyimsl.math.matAddCoordinate import matAddCoordinate
a = [[0, 0, 3],
[0, 3, -1],
[1, 2, 5],
[2, 0, 1],
[3, 1, 3]]
b = [[0, 1, -2],
[0, 3, 1],
[1, 0, 3],
[2, 2, 5],
[2, 3, 1],
[3, 0, 4],
[3, 1, 3]]
nz_a = 5
nz_b = 7
nz_c = []
n = 4
alpha = 1.0
beta = 1.0
c = matAddCoordinate(n, alpha, a, beta, b, nzC=nz_c)
print(" row column value")
for i in range(0, nz_c[0]):
print("%3d %5d %8.2f" % (c[i][0], c[i][1], c[i][2]))
Output¶
row column value
0 0 3.00
0 1 -2.00
1 0 3.00
1 2 5.00
2 0 1.00
2 2 5.00
2 3 1.00
3 0 4.00
3 1 6.00
Example 2¶
Compute \(2*A^T+2*B^T\), where
from __future__ import print_function
from pyimsl.math.matAddCoordinate import matAddCoordinate
a = [[0, 0, 3],
[0, 3, -1],
[1, 2, 5],
[2, 0, 1],
[3, 1, 3]]
b = [[0, 1, -2],
[0, 3, 1],
[1, 0, 3],
[2, 2, 5],
[2, 3, 1],
[3, 0, 4],
[3, 1, 3]]
nz_a = 5
nz_b = 7
nz_c = []
n = 4
alpha = 2.0
beta = 2.0
c = matAddCoordinate(n, alpha, a, beta, b, nzC=nz_c,
aTranspose=True, bTranspose=True)
print(" row column value")
for i in range(0, nz_c[0]):
print("%3d %5d %8.2f" % (c[i][0], c[i][1], c[i][2]))
Output¶
row column value
0 0 6.00
0 1 6.00
0 2 2.00
0 3 8.00
1 0 -4.00
1 3 12.00
2 1 10.00
2 2 10.00
3 2 2.00