C# 如何将-转换为+以创建虚部的共轭?

C# 如何将-转换为+以创建虚部的共轭?,c#,.net,complex-numbers,C#,.net,Complex Numbers,我正在尝试重载a/divide运算符来除掉两个复数。如何反转复数中虚部的极性,使其成为复数的共轭,从而实现复数及其共轭的分配乘法。 例如: 将这两个复数除以: (6 + 3i) / (7 - 5i) [(6 + 3i)*(7 + 5i)] / [(7 - 5i)*(7 + 5i)] //have to create conjugate by inverting signs 7(6 + 3i) + 5i(6 + 3i) / 7(7 - 5i) + 5i(7 + 5i) Where i² =

我正在尝试重载a/divide运算符来除掉两个复数。如何反转复数中虚部的极性,使其成为复数的共轭,从而实现复数及其共轭的分配乘法。 例如:

将这两个复数除以:

(6 + 3i) / (7 - 5i) [(6 + 3i)*(7 + 5i)] / [(7 - 5i)*(7 + 5i)] //have to create conjugate by inverting signs 7(6 + 3i) + 5i(6 + 3i) / 7(7 - 5i) + 5i(7 + 5i) Where i² = -1 (42 + 51i + 15) / (49 + 25) (27 + 5i) / (74) => (27/74) + (51i/74) 给你:

public class Complex
{
    public double Real { get; set; }
    public double Imaginary { get; set; }

    public static Complex operator *(Complex a, Complex b) =>
        new Complex()
        {
            Real = a.Real * b.Real - a.Imaginary * b.Imaginary,
            Imaginary = a.Real * b.Imaginary + a.Imaginary * b.Real,
        };

    public static Complex Conjugate(Complex a) =>
            new Complex() { Real = a.Real, Imaginary = -a.Imaginary };

    public static Complex operator /(Complex a, double b) =>
            new Complex() { Real = a.Real / b, Imaginary = a.Imaginary / b };

    public static Complex operator /(Complex a, Complex b) =>
            a * Conjugate(b) / (b * Conjugate(b)).Real;
}
如果您想获得奖励积分,请尝试以下方法:

public struct Complex : IEquatable<Complex>
{
    public double R { get; private set; }
    public double I { get; private set; }

    public static Complex Create(double r, double i) => new Complex() { R = r, I = i };

    public static Complex operator +(Complex a, Complex b) => Complex.Create(a.R + b.R, a.I + b.I);
    public static Complex operator -(Complex a, Complex b) => Complex.Create(a.R - b.R, a.I - b.I);
    public static Complex operator *(Complex a, Complex b) => Complex.Create(a.R * b.R - a.I * b.I, a.R * b.I + a.I * b.R);
    public static Complex operator /(Complex a, Complex b) => a * b.Conjugate() / (b * b.Conjugate()).R;
    public static Complex operator /(Complex a, double b) => Complex.Create(a.R / b, a.I / b);

    public override bool Equals(object obj)
    {
        if (obj is Complex)
            return Equals((Complex)obj);
        return false;
    }

    public bool Equals(Complex obj)
    {
        if (obj == null) return false;
        if (!EqualityComparer<double>.Default.Equals(R, obj.R)) return false;
        if (!EqualityComparer<double>.Default.Equals(I, obj.I)) return false;
        return true;
    }

    public override int GetHashCode()
    {
        int hash = 0;
        hash ^= EqualityComparer<double>.Default.GetHashCode(R);
        hash ^= EqualityComparer<double>.Default.GetHashCode(I);
        return hash;
    }

    public override string ToString()
    {
        return $"{R}{(I >= 0 ? "+" : "")}{I}";
    }

    public static bool operator ==(Complex left, Complex right)
    {
        if (object.ReferenceEquals(left, null))
        {
            return object.ReferenceEquals(right, null);
        }

        return left.Equals(right);
    }

    public static bool operator !=(Complex left, Complex right)
    {
        return !(left == right);
    }
}

public static class ComplexEx
{
    public static Complex Conjugate(this Complex a) => Complex.Create(a.R, -a.I);
}

我会使用nuget软件包-也许这些人能帮上忙?我必须不使用外部软件包来完成它。@learningtocode-你已经解决了这个问题。尝试实现静态复数运算符*复数a、复数b、静态复数共轭复数a和静态复数运算符/复数a、双b,然后静态复数运算符/复数a、复数b变得微不足道,我不能确定这其中的大部分是如何工作的,但我已经在mathjax之外的其他东西中寻找复数的例子有一段时间了@ocæon-这就是编码很酷的地方。完成简单任务的小部件,组合在一起使复杂的整个工作变得简单。@ocæon-我喜欢使用“闪亮”这个词。布朗考茨团结起来!
public struct Complex : IEquatable<Complex>
{
    public double R { get; private set; }
    public double I { get; private set; }

    public static Complex Create(double r, double i) => new Complex() { R = r, I = i };

    public static Complex operator +(Complex a, Complex b) => Complex.Create(a.R + b.R, a.I + b.I);
    public static Complex operator -(Complex a, Complex b) => Complex.Create(a.R - b.R, a.I - b.I);
    public static Complex operator *(Complex a, Complex b) => Complex.Create(a.R * b.R - a.I * b.I, a.R * b.I + a.I * b.R);
    public static Complex operator /(Complex a, Complex b) => a * b.Conjugate() / (b * b.Conjugate()).R;
    public static Complex operator /(Complex a, double b) => Complex.Create(a.R / b, a.I / b);

    public override bool Equals(object obj)
    {
        if (obj is Complex)
            return Equals((Complex)obj);
        return false;
    }

    public bool Equals(Complex obj)
    {
        if (obj == null) return false;
        if (!EqualityComparer<double>.Default.Equals(R, obj.R)) return false;
        if (!EqualityComparer<double>.Default.Equals(I, obj.I)) return false;
        return true;
    }

    public override int GetHashCode()
    {
        int hash = 0;
        hash ^= EqualityComparer<double>.Default.GetHashCode(R);
        hash ^= EqualityComparer<double>.Default.GetHashCode(I);
        return hash;
    }

    public override string ToString()
    {
        return $"{R}{(I >= 0 ? "+" : "")}{I}";
    }

    public static bool operator ==(Complex left, Complex right)
    {
        if (object.ReferenceEquals(left, null))
        {
            return object.ReferenceEquals(right, null);
        }

        return left.Equals(right);
    }

    public static bool operator !=(Complex left, Complex right)
    {
        return !(left == right);
    }
}

public static class ComplexEx
{
    public static Complex Conjugate(this Complex a) => Complex.Create(a.R, -a.I);
}