public void NominalMunchAugmentor_Augment() { var random = new Random(2342); var data = new F64Matrix(10, 2); data.Map(() => random.Next(2)); var sut = new NominalMungeAugmentator(0.5); var actual = sut.Agument(data); var expected = new F64Matrix(new double[] { 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0 }, 10, 2); Assert.AreNotEqual(data, actual); Assert.AreEqual(expected.RowCount, actual.RowCount); Assert.AreEqual(expected.ColumnCount, actual.ColumnCount); var expectedData = expected.Data(); var actualData = expected.Data(); for (int i = 0; i < expectedData.Length; i++) { Assert.AreEqual(expectedData[i], actualData[i], 0.00001); } }
public void ContinuousMunchAugmentor_Augment() { var random = new Random(2342); var data = new F64Matrix(10, 2); data.Map(() => random.NextDouble()); var sut = new ContinuousMungeAugmentator(0.5, 1.0); var actual = sut.Agument(data); var expected = new F64Matrix(new double[] { 0.246329100917247, 0.00372775551105279, 0.797896387892727, 0.441504622549519, 0.585635684703307, 0.227548045212192, 0.254812818139239, 0.387049265851755, 0.722062122878647, 0.888775135804329, 0.714802723710799, 0.900545311114073, 0.643038906922116, 0.907352483788204, 0.316260776164132, 0.658306110025526, 0.343312428492733, 0.0337710422620974, 0.0760259759035082, 0.148426381940221 }, 10, 2); Assert.AreNotEqual(data, actual); Assert.AreEqual(expected.RowCount, actual.RowCount); Assert.AreEqual(expected.ColumnCount, actual.ColumnCount); var expectedData = expected.Data(); var actualData = expected.Data(); for (int i = 0; i < expectedData.Length; i++) { Assert.AreEqual(expectedData[i], actualData[i], 0.00001); } }
public void F64MatrixExtensions_Map() { var matrix = new F64Matrix(InputData.ToArray(), 2, 3); matrix.Map(() => 10); var expected = Enumerable.Range(0, matrix.Data().Length).Select(v => 10.0).ToArray(); CollectionAssert.AreEqual(expected, matrix.Data()); }
/// <summary> /// Multiply matrix a with matrix b /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static F64Matrix MultiplyF64(F64Matrix a, F64Matrix b) { var aData = a.Data(); var aRows = a.RowCount; var aCols = a.ColumnCount; var bData = b.Data(); var bRows = b.RowCount; var bCols = b.ColumnCount; var cRows = aRows; var cCols = bCols; var cData = new double[cRows * cCols]; if (aCols != bRows) { throw new ArgumentException("matrix a cols: " + aCols + " differs from matrix b rows: " + bRows); } Parallel.For(0, cRows, i => { for (int k = 0; k < bRows; k++) { for (int j = 0; j < cCols; j++) { cData[i * cCols + j] += aData[i * aCols + k] * bData[k * bCols + j]; } } }); return(new F64Matrix(cData, cRows, cCols)); }
public void F64MatrixExtensions_Clear() { var matrix = new F64Matrix(InputData.ToArray(), 2, 3); matrix.Clear(); CollectionAssert.AreEqual(new double[2 * 3], matrix.Data()); }
/// <summary> /// Pins the provided F64Matrix /// </summary> /// <param name="matrix"></param> public F64MatrixPinnedPtr(F64Matrix matrix) { if (matrix == null) { throw new ArgumentNullException("matrix"); } var data = matrix.Data(); m_handle = GCHandle.Alloc(data, GCHandleType.Pinned); m_ptr = (double *)m_handle.AddrOfPinnedObject().ToPointer(); m_rows = matrix.RowCount; m_cols = matrix.ColumnCount; }
/// <summary> /// Multiply matrix a with matrix b /// Copies output to provided matrix. /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="output"></param> public static void MultiplyF64(F64Matrix a, F64Matrix b, F64Matrix output) { var aData = a.Data(); var aRows = a.RowCount; var aCols = a.ColumnCount; var bData = b.Data(); var bRows = b.RowCount; var bCols = b.ColumnCount; var outputArray = output.Data(); if (aCols != bRows) { throw new ArgumentException("matrix a cols: " + aCols + " differs from matrix b rows: " + bRows); } if (output.RowCount != aRows) { throw new ArgumentException("output matrix rows: " + output.RowCount + " differs from matrix a rows: " + aRows); } if (output.ColumnCount != bCols) { throw new ArgumentException("output matrix rows: " + output.ColumnCount + " differs from matrix b cols: " + bCols); } Parallel.For(0, aRows, i => { for (int k = 0; k < bRows; k++) { for (int j = 0; j < bCols; j++) { outputArray[i * bCols + j] += aData[i * aCols + k] * bData[k * bCols + j]; } } }); }
/// <summary> /// Multiply vector v with matrix a /// </summary> /// <param name="a"></param> /// <param name="v"></param> /// <returns></returns> public static double[] MultiplyVectorF64(F64Matrix a, double[] v) { var rows = a.RowCount; var cols = a.ColumnCount; var data = a.Data(); var output = new double[rows]; if (cols != v.Length) { throw new ArgumentException("matrix cols: " + cols + " differs from vector length: " + v.Length); } for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { output[i] += v[j] * data[i * cols + j]; } } return(output); }
/// <summary> /// Multiply vector v with matrix a. /// Copies output to provided array. /// </summary> /// <param name="a"></param> /// <param name="v"></param> /// <param name="output"></param> public static void MultiplyVectorF64(F64Matrix a, double[] v, double[] output) { var rows = a.RowCount; var cols = a.ColumnCount; var data = a.Data(); if (cols != v.Length) { throw new ArgumentException("matrix cols: " + cols + " differs from vector length: " + v.Length); } for (int i = 0; i < rows; ++i) { var sum = 0.0; for (int j = 0; j < cols; ++j) { sum += v[j] * data[i * cols + j]; } output[i] = sum; } }