forum.alglib.net

ALGLIB forum
It is currently Sat Jul 25, 2026 3:33 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Please add partial on alglib.complex
PostPosted: Thu Jul 09, 2026 4:17 am 
Offline

Joined: Thu Jun 04, 2026 11:17 am
Posts: 1
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);
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 46 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group