複數乘法
複數乘法¶
介紹¶
Complex Multiplication; 复数乘法;
複數乘法有兩種做法,第一種做法使用了四個乘法指令,第二種做法使用三個乘法指令。在效能上第二種做法比較好,因此我們會實作第二種做法。
假設兩個複數分別是 a 和 b:
a = a.r + ja.i
b = b.r + jb.i
c 是 a 和 b 的複數乘積:
c = c.r + jc.i
第一種做法(四個乘法):
c = a × b
= ( a.r + ja.i )( b.r + jb.i )
= a.r × b.r + ja.i × b.r + ja.r × b.i - a.i × b.i
= ( a.r × b.r - a.i × b.i ) + j( a.i × b.r + a.r × b.i )
c.r = a.r × b.r - a.i × b.i
c.i = a.i × b.r + a.r × b.i
c = c.r + jc.i
第二種做法(三個乘法):
p = a.r × b.r
q = a.i × b.i
s = ( a.r + a.i )( b.r + b.i )
c.r = p - q
c.i = s - p - q
c = c.r + jc.i
環境¶
作業系統:Microsoft Windows 7 (64 bit)
開發工具:Microsoft Visual C# 2015
實作¶
namespace Complex_Multiplication { public struct cm { public double r; public double i; } class Program { static void Main(string[] args) { cm a = new cm(); cm b = new cm(); cm c = new cm(); double p = 0.0; double q = 0.0; double s = 0.0; // Example: (3+5i)(2-4i)= a.i = 3; // Test a.r = 5; // Test b.i = 2; // Test b.r = -4; // Test p = a.r * b.r; q = a.i * b.i; s = (a.r + a.i) * (a.r + a.i); c.r = p - q; c.i = s - p - q; Console.WriteLine("c.i = " + c.i.ToString() + " , c.r = " + c.r.ToString()); Console.ReadKey(); } } }