using System;using System.Linq;namespace Mtrx{ static class Program { static void Main(string[] args) { var matrix1 = new double[][] { new [] {1.0, 2}, new [] {3.0, 4} }; var matrix2 = new double[][] { new [] {1.0, 2}, new [] {3.0, 4} }; var A = MMult(matrix1, matrix2); if (!A.Success) { Console.WriteLine(A.Error); return; } Print2DMatrix(A.Data); } private static Result<double[][]> MMult(double[][] A, double[][] B) { if (A[0].Length != B.Length) return Result<double[][]>.GetError("It's imposable to multiply this matrix because of their dimensions"); var result = A.Select( (row, rowIndex) => B[0].Select((_, columnIndex) => B.Select(__ => __[columnIndex]) .Zip(row, (rowCell, columnCell) => rowCell * columnCell).Sum() ).ToArray() ).ToArray(); return Result<double[][]>.GetResult(result); } private static void Print2DMatrix<T>(T[][] matrix) { for (var i = 0; i < matrix.Length; ++i) { for (var j = 0; j < matrix[i].Length; ++j) { Console.Write(matrix[j][i]); Console.Write(" "); } Console.WriteLine(); } } } internal class Result<T> { public T Data { get; } public string Error { get; } public bool Success => Error is null; public static Result<T> GetError(string error) { return new Result<T>(default, error); } public static Result<T> GetResult(T result) { return new Result<T>(result); } private Result(T data, string error = null) { Data = data; Error = error; } }}
Код:
using System;using System.Linq;namespace Mtrx{ static class Program { static void Main(string[] args) { var matrix1 = new double[][] { new [] {1.0, 2}, new [] {3.0, 4} }; var matrix2 = new double[][] { new [] {1.0, 2}, new [] {3.0, 4} }; var A = MMult(matrix1, matrix2); if (!A.Success) { Console.WriteLine(A.Error); return; } Print2DMatrix(A.Data); } private static Result<double[][]> MMult(double[][] A, double[][] B) { if (A[0].Length != B.Length) return Result<double[][]>.GetError("It's imposable to multiply this matrix because of their dimensions"); var result = A.Select( (row, rowIndex) => B[0].Select((_, columnIndex) => B.Select(__ => __[columnIndex]) .Zip(row, (rowCell, columnCell) => rowCell * columnCell).Sum() ).ToArray() ).ToArray(); return Result<double[][]>.GetResult(result); } private static void Print2DMatrix<T>(T[][] matrix) { for (var i = 0; i < matrix.Length; ++i) { for (var j = 0; j < matrix[i].Length; ++j) { Console.Write(matrix[j][i]); Console.Write(" "); } Console.WriteLine(); } } } internal class Result<T> { public T Data { get; } public string Error { get; } public bool Success => Error is null; public static Result<T> GetError(string error) { return new Result<T>(default, error); } public static Result<T> GetResult(T result) { return new Result<T>(result); } private Result(T data, string error = null) { Data = data; Error = error; } }}