private static void CheckSize <T>(BaseMatrix <T> a, BaseMatrix <T> b) { if (a.Size != b.Size) { throw new InvalidOperationException($"Matrixes have different sizes"); } }
/// <summary> /// Add two matrixes. /// </summary> /// <typeparam name="T">Type of values in matrixes.</typeparam> /// <param name="firstMatrix"></param> /// <param name="secondMatrix"></param> /// <exception cref="InvalidOperationException">if cannot add two matrixes.</exception> public static void Add <T>(this BaseMatrix <T> firstMatrix, BaseMatrix <T> secondMatrix) { dynamic resolveMatrix = firstMatrix; dynamic resolveOtherMatrix = secondMatrix; try { CheckSize(firstMatrix, secondMatrix); Add(resolveMatrix, resolveOtherMatrix); } catch (RuntimeBinderException) { throw new InvalidOperationException(nameof(firstMatrix)); } }
private static void AddLogic <T>(BaseMatrix <T> firstMatrix, BaseMatrix <T> secondMatrix) { for (int i = 0; i < firstMatrix.Size; i++) { for (int j = 0; j < firstMatrix.Size; j++) { try { dynamic value = secondMatrix[i, j]; firstMatrix[i, j] += value; } catch (RuntimeBinderException) { throw new InvalidOperationException(nameof(T)); } } } }