0
0
R Programmingprogramming~5 mins

Complex type in R Programming

Choose your learning style9 modes available
Introduction
A complex type lets you work with numbers that have two parts: a real part and an imaginary part. This helps solve problems involving waves, signals, or rotations.
When you need to represent numbers with both real and imaginary parts, like in electrical engineering.
When working with signals or sound waves that have phase and amplitude.
When solving math problems involving square roots of negative numbers.
When modeling rotations or oscillations in physics or computer graphics.
Syntax
R Programming
z <- complex(real = 3, imaginary = 4)
Use the complex() function with real and imaginary parts as numbers.
You can also create complex numbers using the syntax: z <- 3 + 4i
Examples
Create complex numbers using both the complex() function and the shorthand with 'i'.
R Programming
z1 <- complex(real = 2, imaginary = 5)
z2 <- 1 + 3i
Use Re() and Im() to get the real and imaginary parts of a complex number.
R Programming
Re(z1)  # Gets the real part
Im(z1)  # Gets the imaginary part
Use Mod() and Arg() to find the size and direction of a complex number.
R Programming
Mod(z2)  # Gets the magnitude (length) of the complex number
Arg(z2)  # Gets the angle (in radians)
Sample Program
This program creates a complex number 3 + 4i, then prints it and its parts: real, imaginary, magnitude, and angle.
R Programming
z <- complex(real = 3, imaginary = 4)
print(z)
print(Re(z))
print(Im(z))
print(Mod(z))
print(Arg(z))
OutputSuccess
Important Notes
The imaginary unit is represented by 'i' in R.
The magnitude (Mod) is the distance from zero on the complex plane.
The angle (Arg) is measured in radians from the positive real axis.
Summary
Complex type stores numbers with real and imaginary parts.
Use complex(), or write numbers like 3 + 4i.
Functions Re(), Im(), Mod(), and Arg() help explore complex numbers.