// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Numerics; namespace Spludlow { public class Maths { public static UInt64 NextUInt64(Random random) { byte[] buffer = new byte[sizeof(UInt64)]; random.NextBytes(buffer); return BitConverter.ToUInt64(buffer, 0); } public static Int64 NextInt64(Random random) { byte[] buffer = new byte[sizeof(Int64)]; random.NextBytes(buffer); return BitConverter.ToInt64(buffer, 0); } public static double ModulusSquared(Complex complex) { return complex.Real * complex.Real + complex.Imaginary * complex.Imaginary; } public static bool IsCloseTo(Complex x, Complex y) { const double tiny = 0.001; return (Math.Abs(y.Real - x.Real) + Math.Abs(y.Imaginary - x.Imaginary) < tiny); } public static Complex Pow(Complex complex, double exponent) // Exponent power { double x = complex.Real; double y = complex.Imaginary; double modulus = Math.Pow(x * x + y * y, exponent * 0.5); double argument = Math.Atan2(y, x) * exponent; double real = modulus * Math.Cos(argument); double imaginary = modulus * Math.Sin(argument); return new Complex(real, imaginary); } public static Complex Power(Complex a, Complex b) // raise to the power { double r = Math.Sqrt(a.Real * a.Real + a.Imaginary * a.Imaginary); double theta = Math.Atan2(a.Imaginary, a.Real); double factor = Math.Pow(r, b.Real) * (Math.Pow(Math.E, -b.Imaginary * theta)); double angle = b.Real * theta + b.Imaginary * Math.Log(r); return new Complex(factor * Math.Cos(angle), factor * Math.Sin(angle)); } } }