Example 1: SparseMatrix
The matrix product of two matrices is computed using a method from the SparseMatrix
class. The one norm of the result is also computed. The matrix and its norm are printed.
using System;
using Imsl.Math;
public class SparseMatrixEx1
{
public static void Main(String[] args)
{
SparseMatrix b = new SparseMatrix(6, 6);
b.Set(0, 0, 10.0);
b.Set(1, 1, 10.0);
b.Set(1, 2, -3.0);
b.Set(1, 3, -1.0);
b.Set(2, 2, 15.0);
b.Set(3, 0, -2.0);
b.Set(3, 3, 10.0);
b.Set(3, 4, -1.0);
b.Set(4, 0, -1.0);
b.Set(4, 3, -5.0);
b.Set(4, 4, 1.0);
b.Set(4, 5, -3.0);
b.Set(5, 0, -1.0);
b.Set(5, 1, -2.0);
b.Set(5, 5, 6.0);
SparseMatrix c = new SparseMatrix(6, 3);
c.Set(0, 0, 5.0);
c.Set(1, 2, -3.0);
c.Set(2, 0, 1.0);
c.Set(2, 2, 7.0);
c.Set(3, 0, 2.0);
c.Set(4, 1, -5.0);
c.Set(4, 2, 2.0);
c.Set(5, 2, 4.0);
SparseMatrix A = SparseMatrix.Multiply(b, c);
// Get the one norm of matrix A
Console.Out.WriteLine("The 1-norm of the matrix is "
+ A.OneNorm());
SparseMatrix.SparseArray sa = A.ToSparseArray();
// Print the matrix and its one norm
Console.Out.WriteLine("row column value");
for (int i = 0; i < sa.NumberOfRows; i++)
{
for (int j = 0; j < sa.Index[i].Length; j++)
{
int jj = sa.Index[i][j];
Console.Out.WriteLine(" " + i + " " + jj + " "
+ sa.Values[i][j]);
}
}
}
}
Output
The 1-norm of the matrix is 198
row column value
0 0 50
1 0 -5
1 2 -51
2 0 15
2 2 105
3 0 10
3 1 5
3 2 -2
4 0 -15
4 1 -5
4 2 -10
5 0 -5
5 2 30
Link to C# source.