1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-29 18:24:23 +00:00
Funkin/source/funkin/audiovis/dsp/Complex.hx

81 lines
1.9 KiB
Haxe
Raw Normal View History

package funkin.audiovis.dsp;
2021-09-28 02:30:38 +00:00
/**
Complex number representation.
2021-09-28 02:30:38 +00:00
**/
@:forward(real, imag) @:notNull @:pure
abstract Complex({
final real:Float;
final imag:Float;
2021-09-28 02:30:38 +00:00
})
{
public inline function new(real:Float, imag:Float)
this = {real: real, imag: imag};
2021-09-28 02:30:38 +00:00
/**
Makes a Complex number with the given Float as its real part and a zero imag part.
**/
@:from
public static inline function fromReal(r:Float)
return new Complex(r, 0);
2021-09-28 02:30:38 +00:00
/**
Complex argument, in radians.
**/
public var angle(get, never):Float;
2021-09-28 02:30:38 +00:00
inline function get_angle()
return Math.atan2(this.imag, this.real);
2021-09-28 02:30:38 +00:00
/**
Complex module.
**/
public var magnitude(get, never):Float;
2021-09-28 02:30:38 +00:00
inline function get_magnitude()
return Math.sqrt(this.real * this.real + this.imag * this.imag);
2021-09-28 02:30:38 +00:00
@:op(A + B)
public inline function add(rhs:Complex):Complex
return new Complex(this.real + rhs.real, this.imag + rhs.imag);
2021-09-28 02:30:38 +00:00
@:op(A - B)
public inline function sub(rhs:Complex):Complex
return new Complex(this.real - rhs.real, this.imag - rhs.imag);
2021-09-28 02:30:38 +00:00
@:op(A * B)
public inline function mult(rhs:Complex):Complex
return new Complex(this.real * rhs.real - this.imag * rhs.imag, this.real * rhs.imag + this.imag * rhs.real);
2021-09-28 02:30:38 +00:00
/**
Returns the complex conjugate, does not modify this object.
**/
public inline function conj():Complex
return new Complex(this.real, -this.imag);
2021-09-28 02:30:38 +00:00
/**
Multiplication by a real factor, does not modify this object.
**/
public inline function scale(k:Float):Complex
return new Complex(this.real * k, this.imag * k);
2021-09-28 02:30:38 +00:00
public inline function copy():Complex
return new Complex(this.real, this.imag);
2021-09-28 02:30:38 +00:00
/**
The imaginary unit.
**/
public static final im = new Complex(0, 1);
2021-09-28 02:30:38 +00:00
/**
The complex zero.
**/
public static final zero = new Complex(0, 0);
2021-09-28 02:30:38 +00:00
/**
Computes the complex exponential `e^(iw)`.
**/
public static inline function exp(w:Float)
return new Complex(Math.cos(w), Math.sin(w));
2021-09-28 02:30:38 +00:00
}