Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Complex Numbers in C#
A complex number in C# consists of two components: a real part and an imaginary part. For example, in the complex number 7+5i, the real part is 7 and the imaginary part is 5 (the coefficient of i).
C# allows you to create custom data structures to represent complex numbers using struct and implement mathematical operations using operator overloading.
Syntax
Following is the syntax for creating a complex number structure −
public struct Complex {
public double real;
public double imaginary;
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
}
Following is the syntax for overloading the addition operator −
public static Complex operator +(Complex one, Complex two) {
return new Complex(one.real + two.real, one.imaginary + two.imaginary);
}
Understanding Complex Number Structure
Using Basic Complex Number Operations
Example
using System;
public struct Complex {
public double real;
public double imaginary;
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex one, Complex two) {
return new Complex(one.real + two.real, one.imaginary + two.imaginary);
}
public override string ToString() {
if (imaginary >= 0)
return String.Format("{0} + {1}i", real, imaginary);
else
return String.Format("{0} - {1}i", real, Math.Abs(imaginary));
}
}
class Program {
static void Main() {
Complex val1 = new Complex(7, 1);
Complex val2 = new Complex(2, 6);
Complex result = val1 + val2;
Console.WriteLine("First: {0}", val1);
Console.WriteLine("Second: {0}", val2);
Console.WriteLine("Result (Sum): {0}", result);
}
}
The output of the above code is −
First: 7 + 1i Second: 2 + 6i Result (Sum): 9 + 7i
Using Complex Numbers with Multiple Operations
Example
using System;
public struct Complex {
public double real;
public double imaginary;
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex a, Complex b) {
return new Complex(a.real + b.real, a.imaginary + b.imaginary);
}
public static Complex operator -(Complex a, Complex b) {
return new Complex(a.real - b.real, a.imaginary - b.imaginary);
}
public static Complex operator *(Complex a, Complex b) {
return new Complex(
a.real * b.real - a.imaginary * b.imaginary,
a.real * b.imaginary + a.imaginary * b.real
);
}
public double Magnitude() {
return Math.Sqrt(real * real + imaginary * imaginary);
}
public override string ToString() {
if (imaginary >= 0)
return String.Format("{0} + {1}i", real, imaginary);
else
return String.Format("{0} - {1}i", real, Math.Abs(imaginary));
}
}
class Program {
static void Main() {
Complex c1 = new Complex(3, 4);
Complex c2 = new Complex(1, -2);
Console.WriteLine("Complex 1: {0}", c1);
Console.WriteLine("Complex 2: {0}", c2);
Console.WriteLine("Addition: {0}", c1 + c2);
Console.WriteLine("Subtraction: {0}", c1 - c2);
Console.WriteLine("Multiplication: {0}", c1 * c2);
Console.WriteLine("Magnitude of c1: {0:F2}", c1.Magnitude());
}
}
The output of the above code is −
Complex 1: 3 + 4i Complex 2: 1 - 2i Addition: 4 + 2i Subtraction: 2 + 6i Multiplication: 11 - 2i Magnitude of c1: 5.00
Using Built-in System.Numerics.Complex
C# also provides a built-in Complex class in the System.Numerics namespace −
Example
using System;
using System.Numerics;
class Program {
static void Main() {
Complex c1 = new Complex(3, 4);
Complex c2 = new Complex(1, -2);
Console.WriteLine("Complex 1: {0}", c1);
Console.WriteLine("Complex 2: {0}", c2);
Console.WriteLine("Addition: {0}", c1 + c2);
Console.WriteLine("Magnitude: {0:F2}", c1.Magnitude);
Console.WriteLine("Phase: {0:F2} radians", c1.Phase);
}
}
The output of the above code is −
Complex 1: (3, 4) Complex 2: (1, -2) Addition: (4, 2) Magnitude: 5.00 Phase: 0.93 radians
Conclusion
Complex numbers in C# can be implemented using custom structures with operator overloading or by using the built-in System.Numerics.Complex class. Both approaches allow you to perform mathematical operations like addition, subtraction, multiplication, and calculate properties like magnitude and phase.
