C# The multiplication of matrices of complex numbers
Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
class complex
{
int x, y;
public complex(int _x, int _y)
{
x = _x;
y = _y;
}
public static complex operator*(complex a, complex b)
{
return new complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
public static complex operator+(complex a, complex b)
{
return new complex(a.x + b.x, a.y + b.y);
}
public override string ToString()
{
return String.Format("{0} : {1}", x, y);
}
}
class comp_matrix
{
int h, w;
complex[,] x;
public comp_matrix(int _h, int _w)
{
x = new complex[_h, _w];
h = _h;
w = _w;
}
public complex this[int i, int j]
{
set { x[i, j] = value; }
get { return x[i, j]; }
}
public int getH
{
set { h = value; }
get { return h; }
}
public int getW
{
set { w = value; }
get { return w; }
}
public static comp_matrix operator*(comp_matrix a, comp_matrix b)
{
comp_matrix d = new comp_matrix(a.getH, b.getW);
for (int i = 0; i < a.getH; ++i)
for (int j = 0; j < b.getW; ++j)
{
d[i, j] = new complex(0, 0);
for (int k = 0; k < a.getW; k++)
{
d[i, j] = d[i, j] + a.x[i, k] * b.x[k, j];
}
}
return d;
}
}
class Program
{
static void Main(string[] args)
{
int a, b, c;
string s = Console.ReadLine();
string[] f = s.Split(' ');
a = int.Parse(f[0]);
b = int.Parse(f[1]);
c = int.Parse(f[2]);
comp_matrix m1 = new comp_matrix(a, b);
comp_matrix m2 = new comp_matrix(b, c);
string[] d = new string[2 * b];
for (int i = 0; i < a; i++)
{
s = Console.ReadLine();
d = s.Split(' ');
for (int j = 0; j < b; j++)
{
m1[i, j] = new complex(int.Parse(d[j]), int.Parse(d[j * 2 + 1]));
}
}
d = new string[2 * c];
for (int i = 0; i < b; i++)
{
s = Console.ReadLine();
d = s.Split(' ');
for (int j = 0; j < c; j++)
{
m2[i, j] = new complex(int.Parse(d[j]), int.Parse(d[j * 2 + 1]));
}
}
comp_matrix ans = m1 * m2;
Console.WriteLine("***********************************************");
for (int i = 0; i < ans.getH; i++)
{
for (int j = 0; j < ans.getW; j++)
{
Console.Write("{0, 10} ", ans[i, j]);
}
Console.WriteLine();
}
}
}
}
Tags:
- c# complex numbers
- c# system.numerics missing
- complex number program in c#
- c# matrix
- math.net numerics
- c# array