//TODO: unit-test /// <summary> /// Creates a new instance of <see cref="Matrix{T}"/> from <see cref="JuggedMatrix{T}"/> /// </summary> /// <typeparam name="T">ANY</typeparam> /// <param name="juggedMatrix">Initial matrix</param> /// <returns>A new instance of <see cref="IMatrix{T}"/></returns> public static Matrix <T> ToMatrix <T>(this JuggedMatrix <T> juggedMatrix) { var matrix = new Matrix <T>(juggedMatrix.RowsCount, juggedMatrix.CountOnEachRow() .Max()); for (var i = 0; i < juggedMatrix.RowsCount; i++) { for (var j = 0; j < matrix.ColumnsCount; j++) { if (juggedMatrix.ElementsInRow(i) < matrix.ColumnsCount) { matrix[i, j] = juggedMatrix[i, j]; } else { matrix[i, j] = default; } } } return(matrix); }
//TODO: unit-test /// <summary> /// Converts a vanilla .NET matrix <see langword="T"/>[][] to <see cref="JuggedMatrix{T}"/> /// </summary> /// <typeparam name="T">ANY</typeparam> /// <param name="matrix">Initial matrix to convert</param> /// <returns><see cref="JuggedMatrix{T}"/>, made on base <paramref name="matrix"/></returns> public static JuggedMatrix <T> ToJuggedMatrix <T>(this T[][] matrix) { if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } if (matrix.Any(x => x == null)) { throw new NullReferenceException(nameof(matrix) + " one or more rows are null"); } var newJuggedMatrix = new JuggedMatrix <T>(matrix.Length, matrix.Select(row => row.Length)); for (var i = 0; i < matrix.Length; i++) { for (var j = 0; j < newJuggedMatrix.ElementsInRow(i); j++) { newJuggedMatrix[i, j] = matrix[i][j]; } } return(newJuggedMatrix); }