forum.alglib.net
http://forum.alglib.net/

Please add partial on alglib.complex
http://forum.alglib.net/viewtopic.php?f=2&t=4909
Page 1 of 1

Author:  where [ Thu Jul 09, 2026 4:17 am ]
Post subject:  Please add partial on alglib.complex

I want to add INumberBase and IEquatable interface on alglib.complex, but the alglib.complex is not partial. So that I need to change the source code of it. It will be better if the alglib.complex struct can be partial.
And this is the code what I add. It will be better if it can merge into alglib.complex.
Code:
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

public partial class alglib
{
    public partial struct complex : IEquatable<complex>, IFormattable
    {
        public readonly bool Equals(complex value) => this == value;
        public override readonly string ToString() => ToString(null, null);
        public readonly string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string format) => ToString(format, null);
        public readonly string ToString(IFormatProvider provider) => ToString(null, provider);
        public readonly string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string format, IFormatProvider provider)
        {
#if NET6_0_OR_GREATER
            System.Runtime.CompilerServices.DefaultInterpolatedStringHandler handler = new(4, 2, provider, stackalloc char[512]);
            handler.AppendLiteral("<");
            handler.AppendFormatted(x, format);
            handler.AppendLiteral("; ");
            handler.AppendFormatted(y, format);
            handler.AppendLiteral(">");
            return handler.ToStringAndClear();
#else
            return $"<{x.ToString(format, provider)}; {y.ToString(format, provider)}>";
#endif
        }
    }

#if !NETFRAMEWORK || NET40_OR_GREATER
    public partial struct complex
    {
        public complex(System.Numerics.Complex complex) : this(complex.Real, complex.Imaginary) { }

        public static implicit operator System.Numerics.Complex(complex complex) => new complex(complex.x, complex.y);
        public static implicit operator complex(System.Numerics.Complex complex) => new complex(complex);
    }
#endif

#if NET7_0_OR_GREATER
    public partial struct complex
        : System.Numerics.IAdditionOperators<complex, complex, complex>,
        System.Numerics.IAdditiveIdentity<complex, complex>,
        System.Numerics.IDecrementOperators<complex>,
        System.Numerics.IDivisionOperators<complex, complex, complex>,
        System.Numerics.IEqualityOperators<complex, complex, bool>,
        System.Numerics.IIncrementOperators<complex>,
        System.Numerics.IMultiplicativeIdentity<complex, complex>,
        System.Numerics.IMultiplyOperators<complex, complex, complex>,
        System.Numerics.ISubtractionOperators<complex, complex, complex>,
        System.Numerics.IUnaryNegationOperators<complex, complex>,
        System.Numerics.IUnaryPlusOperators<complex, complex>
    {
        static complex System.Numerics.IAdditiveIdentity<complex, complex>.AdditiveIdentity => new(0.0, 0.0);
        public static complex operator --(complex value) => value with { x = value.x - 1 };
        public static complex operator ++(complex value) => value with { x = value.x + 1 };
        static complex System.Numerics.IMultiplicativeIdentity<complex, complex>.MultiplicativeIdentity => new(1.0, 0.0);
    }
#endif
}

With this, we can have collection calculation for double and complex with one write:
Code:
extension<T>(IEnumerable<T> source) where T : IAdditionOperators<T, T, T>
{
    public static IEnumerable<T> operator +(IEnumerable<T> a, T b) => a.Select(x => x + b);
}

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/