package com.njcn.gather.tools.comtrade.comparewave.core.model;

import com.njcn.gather.tools.comtrade.comparewave.core.algorithm.FloatMath;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 复数类
 * 对应C代码：fft_vec_struct
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Complex {

    /**
     *  实部
     */
    private float real;

    /**
     * 虚部
     */
    private float imag;
    
    /**
     * 复制构造函数
     */
    public Complex(Complex other) {
        this.real = other.real;
        this.imag = other.imag;
    }
    
    /**
     * 复数加法
     */
    public Complex add(Complex other) {
        return new Complex(
            this.real + other.real,
            this.imag + other.imag
        );
    }
    
    /**
     * 复数减法
     */
    public Complex subtract(Complex other) {
        return new Complex(
            this.real - other.real,
            this.imag - other.imag
        );
    }
    
    /**
     * 复数乘法
     * (a+bi)*(c+di) = (ac-bd) + (ad+bc)i
     */
    public Complex multiply(Complex other) {
        return new Complex(
            this.real * other.real - this.imag * other.imag,
            this.real * other.imag + this.imag * other.real
        );
    }
    
    /**
     * 复数乘以实数
     */
    public Complex multiply(float scalar) {
        return new Complex(
            this.real * scalar,
            this.imag * scalar
        );
    }
    
    /**
     * 计算复数的模
     */
    public float magnitude() {
        return FloatMath.sqrt(real * real + imag * imag);
    }
    
    /**
     * 计算复数的相角（弧度）
     */
    public float phase() {
        return FloatMath.atan2(imag, real);
    }
    
    /**
     * 计算复数的相角（度）
     */
    public float phaseDegrees() {
        return FloatMath.toDegrees(phase());
    }
}