public void expand_and_squeeze() { double[,] a = { { 1, 2, 3 }, { 4, 5, 6 }, }; double[,,] actual0 = a.ExpandDimensions(0).To <double[, , ]>(); double[,,] actual1 = a.ExpandDimensions(1).To <double[, , ]>(); double[,,] actual2 = a.ExpandDimensions(2).To <double[, , ]>(); double[,,] expected0 = { { { 1, 2, 3 }, { 4, 5, 6 } }, }; double[,,] expected1 = { { { 1, 2, 3 } }, { { 4, 5, 6 } }, }; double[,,] expected2 = { { { 1 }, { 2 }, { 3 } }, { { 4 }, { 5 }, { 6 } }, }; Assert.AreEqual(actual0, expected0); Assert.AreEqual(actual1, expected1); Assert.AreEqual(actual2, expected2); // test squeeze Assert.AreEqual(a, expected0.Squeeze()); Assert.AreEqual(a, expected1.Squeeze()); Assert.AreEqual(a, expected2.Squeeze()); }
public void example_jagged() { #region doc_convert_jagged // Let's say we would like to convert the following // matrix of strings to a matrix of double values: string[][] from = { new[] { "0", "1", "2" }, new[] { "3", "4", "5" }, }; // Using a convertor: double[][] a = Jagged.Convert(from, x => Double.Parse(x)); // Using a default converter for the type: double[][] b = Jagged.Convert <string, double>(from); // Without using generics for the input type, will // also work for tensors (matrices with rank > 2) Array tensor = Jagged.Convert <double>(from); // Using universal converter: double[,] d = Matrix.To <double[, ]>(from); double[][] e = Matrix.To <double[][]>(from); // When using an universal converter, we can also use // it to squeeze / reshape the matrix to a new shape: double[,,] f = Matrix.To <double[, , ]>(from); double[][][] g = Matrix.To <double[][][]>(from); #endregion Assert.IsTrue(a.IsEqual(b)); Assert.IsTrue(a.IsEqual(tensor)); Assert.IsTrue(a.IsEqual(d)); Assert.IsTrue(a.IsEqual(e)); Assert.IsTrue(a.IsEqual(e.Squeeze())); Assert.IsTrue(a.IsEqual(f.Squeeze())); }