Last active
January 7, 2016 07:04
-
-
Save lasagnaphil/be5e385ef5531eb51340 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace CSharpStudy1 | |
{ | |
static class LinearAlgebra | |
{ | |
public static int[,] Multiply(int[,] mat1, int[,] mat2) | |
{ | |
if (mat1.GetLength (1) != mat2.GetLength (0)) { | |
Console.WriteLine ("Error: trying to calculate two matrices with incompatible size"); | |
return null; | |
} | |
int[,] answer = new int[mat1.GetLength (0), mat2.GetLength (1)]; | |
for (int i = 0; i < mat1.GetLength (0); i++) { | |
for (int k = 0; k < mat1.GetLength (1); k++) { | |
for (int j = 0; j < mat2.GetLength (0); j++) { | |
answer [i, k] += mat1 [i, j] * mat2 [j, k]; | |
} | |
} | |
} | |
return answer; | |
} | |
public static void printMatrix(int[,] mat) | |
{ | |
for (int i = 0; i < mat.GetLength (0); i++) { | |
for (int j = 0; j < mat.GetLength (1); j++) { | |
Console.Write ("[ {0}, {1} ] : {2} ", i, j, mat [i, j]); | |
} | |
Console.WriteLine (); | |
} | |
Console.WriteLine (); | |
} | |
} | |
static class MainClass | |
{ | |
static void Main (string[] args) | |
{ | |
int[,] mat1 = new int[2, 2] { { 10, 5 }, { 5, 10 } }; | |
int[,] mat2 = new int[2, 2] { { 3, 4 }, { 3, 4 } }; | |
int[,] result = LinearAlgebra.Multiply (mat1, mat2); | |
LinearAlgebra.printMatrix (result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment