private static void test <T>(T[][] from, double[][] expected) { double[][] z = from.Convert <T, double>(); double[,] a = Matrix.Convert <T, double>(from); double[][] b = Jagged.Convert <T, double>(from); double[,] c = (double[, ])Matrix.Convert <double>(from); double[][] d = (double[][])Jagged.Convert <double>(from); double[,] e = Matrix.To <double[, ]>(from); double[][] f = Matrix.To <double[][]>(from); Assert.IsTrue(expected.IsEqual(z)); Assert.IsTrue(expected.IsEqual(a)); Assert.IsTrue(expected.IsEqual(b)); Assert.IsTrue(expected.IsEqual(c)); Assert.IsTrue(expected.IsEqual(d)); Assert.IsTrue(expected.IsEqual(e)); Assert.IsTrue(expected.IsEqual(f)); }
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())); }