Exemple #1
0
    public static DenseMatrix Create()
    {
        var storage = DenseColumnMajorMatrixStorage <double> .OfArray(new double[1, 1]);

        var type = typeof(DenseColumnMajorMatrixStorage <double>);

        type.GetField("RowCount").SetValue(storage, 0);
        type.GetField("ColumnCount").SetValue(storage, 0);
        type.GetField("Data").SetValue(storage, new double[0]);
        return(new DenseMatrix(storage));
    }
Exemple #2
0
        public void MapToAutoIncludeZeros(TestMatrixStorage aType, TestMatrixStorage resultType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0 }, { 0.0, 4.0 }
            });
            var result   = TestData.MatrixStorage <double>(resultType, 2, 2);
            var expected = DenseColumnMajorMatrixStorage <double> .OfArray(new[, ] {
                { 0.0, -1.0 }, { 1.0, -3.0 }
            });

            a.MapTo(result, u => - u + 1.0, Zeros.AllowSkip);
            Assert.That(result.Equals(expected));
        }
Exemple #3
0
        public void MapToSkipZeros(TestMatrixStorage aType, TestMatrixStorage resultType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0 }, { 0.0, 4.0 }
            });
            var result   = TestData.MatrixStorage <double>(resultType, 2, 2);
            var expected = DenseColumnMajorMatrixStorage <double> .OfArray(new[, ] {
                { -1.0, -2.0 }, { 0.0, -4.0 }
            });

            a.MapTo(result, u => - u, Zeros.AllowSkip, ExistingData.Clear);
            Assert.That(result.Equals(expected));
        }
Exemple #4
0
        public void Map2ToAutoIncludeZeros(TestMatrixStorage aType, TestMatrixStorage bType, TestMatrixStorage resultType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0, 0.0 }, { 4.0, 0.0, 6.0 }
            });
            var b = TestData.MatrixStorage(bType, new[, ] {
                { 11.0, 12.0, 13.0 }, { 0.0, 0.0, 16.0 }
            });
            var result   = TestData.MatrixStorage <double>(resultType, 2, 3);
            var expected = DenseColumnMajorMatrixStorage <double> .OfArray(new[, ] {
                { 13.0, 15.0, 14.0 }, { 5.0, 1.0, 23.0 }
            });

            a.Map2To(result, b, (u, v) => u + v + 1.0, Zeros.AllowSkip, ExistingData.AssumeZeros);
            Assert.That(result.Equals(expected));
        }
Exemple #5
0
        public static MatrixStorage <T> MatrixStorage <T>(TestMatrixStorage type, T[,] data)
            where T : struct, IEquatable <T>, IFormattable
        {
            switch (type)
            {
            case TestMatrixStorage.DenseMatrix:
                return(DenseColumnMajorMatrixStorage <T> .OfArray(data));

            case TestMatrixStorage.SparseMatrix:
                return(SparseCompressedRowMatrixStorage <T> .OfArray(data));

            case TestMatrixStorage.DiagonalMatrix:
                return(DiagonalMatrixStorage <T> .OfArray(data));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #6
0
        public void MapIndexedToSkipZeros(TestMatrixStorage aType, TestMatrixStorage resultType)
        {
            var a = TestData.MatrixStorage(aType, new[, ] {
                { 1.0, 2.0 }, { 0.0, 4.0 }
            });
            var result   = TestData.MatrixStorage <double>(resultType, 2, 2);
            var expected = DenseColumnMajorMatrixStorage <double> .OfArray(new[, ] {
                { -1.0, -2.0 }, { 0.0, -4.0 }
            });

            int badValueCount = 0; // one time is OK for zero-check

            a.MapIndexedTo(result, (i, j, u) => { if (a.At(i, j) != u)
                                                  {
                                                      Interlocked.Increment(ref badValueCount);
                                                  }
                                                  return(-u); }, Zeros.AllowSkip);
            Assert.That(badValueCount, Is.LessThanOrEqualTo(1));
            Assert.That(result.Equals(expected));
        }
 public DenseMatrix(Complex[,] array)
     : this(DenseColumnMajorMatrixStorage <Complex> .OfArray(array))
 {
 }
 /// <summary>
 /// Create a new dense matrix as a copy of the given two-dimensional array.
 /// This new matrix will be independent from the provided array.
 /// A new memory block will be allocated for storing the matrix.
 /// </summary>
 public static DenseMatrix OfArray(Complex[,] array)
 {
     return(new DenseMatrix(DenseColumnMajorMatrixStorage <Complex> .OfArray(array)));
 }
 /// <summary>
 /// Create a new dense matrix as a copy of the given two-dimensional array.
 /// This new matrix will be independent from the provided array.
 /// A new memory block will be allocated for storing the matrix.
 /// </summary>
 public static DenseMatrix OfArray(double[,] array)
 {
     return(new DenseMatrix(DenseColumnMajorMatrixStorage <double> .OfArray(array)));
 }
 /// <summary>
 /// Create a new dense matrix as a copy of the given two-dimensional array.
 /// This new matrix will be independent from the provided array.
 /// A new memory block will be allocated for storing the matrix.
 /// </summary>
 public static DenseMatrix OfArray(float[,] array)
 {
     return(new DenseMatrix(DenseColumnMajorMatrixStorage <float> .OfArray(array)));
 }
Exemple #11
0
 public DenseMatrix(double[,] array)
     : this(DenseColumnMajorMatrixStorage <double> .OfArray(array))
 {
 }
 public DenseMatrix(float[,] array)
     : this(DenseColumnMajorMatrixStorage <float> .OfArray(array))
 {
 }