mat_add_coordinate (complex)
Performs element-wise addition on two complex matrices stored in coordinate format, C ←αA + βB.
Synopsis
#include <imsl.h>
Imsl_c_sparse_elem *imsl_c_mat_add_coordinate (int n, int nz_a, f_complex alpha, Imsl_c_sparse_elem a[], int nz_b, f_complex beta, Imsl_c_sparse_elem b[], int *nz_c, ..., 0)
The type double function is imsl_z_mat_add_coordinate.
Required Arguments
int n (Input)
The order of the matrices A and B.
int nz_a (Input)
Number of nonzeros in the matrix A.
f_complex alpha (Input)
Scalar multiplier for A.
Imsl_c_sparse_elem a[] (Input)
Vector of length nz_a containing the location and value of each nonzero entry in the matrix A.
int nz_b (Input)
Number of nonzeros in the matrix B.
f_complex beta (Input)
Scalar multiplier for B.
Imsl_c_sparse_elem b[] (Input)
Vector of length nz_b containing the location and value of each nonzero entry in the matrix B.
Int *nz_c (Output)
The number of nonzeros in the sum αA + βB.
Return Value
A pointer to an array of type Imsl_c_sparse_elem containing the computed sum. In the event of an error or if the return matrix has no nonzero elements, NULL is returned.
Synopsis with Optional Arguments
#include <imsl.h>
Imsl_c_sparse_elem *imsl_c_mat_add_coordinate (int n, int nz_a, f_complex alpha, Imsl_c_sparse_elem a[], int nz_b, f_complex beta, Imsl_c_sparse_elem b[], int *nz_c,
IMSL_A_TRANSPOSE,
IMSL_B_TRANSPOSE,
IMSL_A_CONJUGATE_TRANSPOSE,
IMSL_B_CONJUGATE_TRANSPOSE,
0)
Optional Arguments
IMSL_A_TRANSPOSE,
Replace A with AT in the expression αA + βB.
IMSL_B_TRANSPOSE,
Replace B with BT in the expression αA + βB.
IMSL_A_CONJUGATE_TRANSPOSE,
Replace A with AH in the expression αA + βB.
IMSL_B_CONJUGATE_TRANSPOSE,
Replace B with BH in the expression αA + βB.
Description
The function imsl_c_mat_add_coordinate forms the sum αA + βB, given the scalars αand β and the matrices A and B in coordinate format. The transpose or conjugate 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 complex matrices of order 4 stored in coordinate format. Matrix A has five nonzero elements. Matrix B has seven nonzero elements.
#include <imsl.h>
#include <stdio.h>
int main ()
{
Imsl_c_sparse_elem a[] = {0, 0, 3, 4,
0, 3, -1, 2,
1, 2, 5, -1,
2, 0, 1, 2,
3, 1, 3, 0};
Imsl_c_sparse_elem b[] = {0, 1, -2, 1,
0, 3, 1, -2,
1, 0, 3, 0,
2, 2, 5, 2,
2, 3, 1, 4,
3, 0, 4, 0,
3, 1, 3, -2};
int nz_a = 5, nz_b = 7, nz_c, n = 4, i;
f_complex alpha = {1.0, 0.0}, beta = {1.0, 0.0};
Imsl_c_sparse_elem *c;
c = imsl_c_mat_add_coordinate(n, nz_a, alpha, a, nz_b, beta,
b, &nz_c,
0);
printf(" row column value\n");
for (i = 0; i < nz_c; i++)
printf("%3d %5d %8.2f %8.2f\n",
c[i].row, c[i].col, c[i].val.re, c[i].val.im);
}
Output
row column value
0 0 3.00 4.00
0 1 -2.00 1.00
1 0 3.00 0.00
1 2 5.00 -1.00
2 0 1.00 2.00
2 2 5.00 2.00
2 3 1.00 4.00
3 0 4.00 0.00
3 1 6.00 -2.00
Example 2
Compute 2+3i*AT + 2-i*BT, where
#include <imsl.h>
#include <stdio.h>
int main ()
{
Imsl_c_sparse_elem a[] = {0, 0, 3, 4,
0, 3, -1, 2,
1, 2, 5, -1,
2, 0, 1, 2,
3, 1, 3, 0};
Imsl_c_sparse_elem b[] = {0, 1, -2, 1,
0, 3, 1, -2,
1, 0, 3, 0,
2, 2, 5, 2,
2, 3, 1, 4,
3, 0, 4, 0,
3, 1, 3, -2};
int nz_a = 5, nz_b = 7, nz_c, n = 4, i;
f_complex alpha = {2.0, 3.0}, beta = {2.0, -1.0};
Imsl_c_sparse_elem *c;
c = imsl_c_mat_add_coordinate(n, nz_a, alpha, a,
nz_b, beta, b, &nz_c,
IMSL_A_TRANSPOSE,
IMSL_B_TRANSPOSE,
0);
printf(" row column value\n");
for (i = 0; i < nz_c; i++)
printf("%3d %5d %8.2f %8.2f\n",
c[i].row, c[i].col, c[i].val.re, c[i].val.im);
}
Output
row column value
0 0 -6.00 17.00
0 1 6.00 -3.00
0 2 -4.00 7.00
0 3 8.00 -4.00
1 0 -3.00 4.00
1 3 10.00 2.00
2 1 13.00 13.00
2 2 12.00 -1.00
3 0 -8.00 -4.00
3 2 6.00 7.00