Ejemplo n.º 1
0
        /// <summary>
        /// Computes the norm of a sparse matrix.
        /// </summary>
        /// <returns>The requested norm of the matrix.</returns>
        public override double Norm(MatrixNorm which)
        {
            int nz = this.NonZerosCount;

            double sum, norm = 0.0;

            if (which == MatrixNorm.OneNorm)
            {
                for (int j = 0; j < ncols; j++)
                {
                    sum = 0.0;
                    for (int i = ColumnPointers[j]; i < ColumnPointers[j + 1]; i++)
                    {
                        sum += Math.Abs(Values[i]);
                    }
                    norm = Math.Max(norm, sum);
                }
            }
            else if (which == MatrixNorm.FrobeniusNorm)
            {
                sum = 0.0;
                for (int i = 0; i < nz; i++)
                {
                    sum   = Math.Abs(Values[i]);
                    norm += sum * sum;
                }
                norm = Math.Sqrt(norm);
            }
            else if (which == MatrixNorm.InfinityNorm)
            {
                var work = new double[nrows];
                for (int j = 0; j < ncols; j++)
                {
                    for (int i = ColumnPointers[j]; i < ColumnPointers[j + 1]; i++)
                    {
                        work[RowIndices[i]] += Math.Abs(Values[i]);
                    }
                }
                for (int j = 0; j < nrows; j++)
                {
                    norm = Math.Max(norm, work[j]);
                }
            }
            else
            {
                for (int i = 0; i < nz; i++)
                {
                    norm = Math.Max(norm, Math.Abs(Values[i]));
                }
            }

            return(norm);
        }
Ejemplo n.º 2
0
 /// <inheritdoc />
 public abstract double Norm(MatrixNorm which);