Exemplo n.º 1
0
        /// <inheritdoc/>
        public bool TryCompile(IReadOnlyList <MathExpression> arguments, ICompilationContext <object?> context, ITypeHintHandler typeHintHandler, [MaybeNullWhen(false)] out Expression expr)
        {
            if (arguments.Count != 1)
            {
                expr = null;
                return(false);
            }

            var argExpr = context.Transform(arguments.First());

            if (argExpr.Type == typeof(float) || argExpr.Type == typeof(double))
            { // if this is a floating point
                var method = Helpers.GetMethod <Action <double> >(d => Math.Log(d)) !;

                expr = Expression.Call(method, CompilerHelpers.ConvertToType(argExpr, typeof(double)));
                return(true);
            }
            else if (CompilerHelpers.IsFloating(argExpr.Type) || CompilerHelpers.IsIntegral(argExpr.Type))
            { // if this is a built-in integer or decimal
                var method = Helpers.GetMethod <Action <decimal> >(d => DecimalMath.Ln(d)) !;

                expr = Expression.Call(method, CompilerHelpers.ConvertToType(argExpr, typeof(decimal)));
                return(true);
            }

            expr = null;
            return(false);
        }
Exemplo n.º 2
0
        public override Vector2 <decimal> EquatorialCoordinates(DateTime date)
        {
            var pos = EclipticEarth3D(date);

#if DEBUG
            Console.WriteLine($"Sun ecliptic position: X={pos.X} Y={pos.Y} Z={pos.Z}");
#endif

            var tiltRad = CommonCalculations.GetAxialTilt(date) * DecimalMath.DegToRad;
            var Xe      = pos.X;
            var Ye      = pos.Y * DecimalMath.Cos(tiltRad);
            var Ze      = pos.Y * DecimalMath.Sin(tiltRad);

#if DEBUG
            Console.WriteLine($"Sun equatorial position: X={Xe} Y={Ye} Z={Ze} with tilt {tiltRad}");
#endif

            var RA  = DecimalMath.Atan2(Ye, Xe) * DecimalMath.RadToDeg;
            var Dec = DecimalMath.Atan2(Ze, DecimalMath.Sqrt(Xe * Xe + Ye * Ye)) * DecimalMath.RadToDeg;

#if DEBUG
            Console.WriteLine($"Sun RA = {RA} Dec={Dec}");
#endif
            return(new Vector2 <decimal>(CommonCalculations.From0To360(RA), Dec));
        }
Exemplo n.º 3
0
        public void PowNegativeDecimal()
        {
            const decimal expected = 0.5380018430410047863745228634M;
            decimal       actual   = DecimalMath.Pow(1.2M, -3.4M);

            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 4
0
        public void FloatingPower(decimal bas, decimal exp, decimal expect, decimal error)
        {
            var actual      = DecimalMath.Pow(bas, exp);
            var actualError = Math.Abs(expect - actual);

            Assert.True(error >= actualError, $"Error of {actualError}, expected no more than error of {error}");
        }
Exemplo n.º 5
0
        public void PowDecimal()
        {
            const decimal expected = 1.8587296919794811670420219951M;
            decimal       actual   = DecimalMath.Pow(1.2M, 3.4M);

            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 6
0
        public void NaturalLog(decimal arg, decimal expect, decimal error)
        {
            var actual      = DecimalMath.Ln(arg);
            var actualError = Math.Abs(expect - actual);

            Assert.True(error >= actualError, $"Error of {actualError}, expected no more than error of {error}");
        }
Exemplo n.º 7
0
        public override Vector2 <decimal> EquatorialCoordinates(DateTime date)
        {
            var pos = EclipticEarth3D(date);
            var X = pos.X; var Y = pos.Y; var Z = pos.Z;
            var R         = DecimalMath.Sqrt(X * X + Y * Y + Z * Z);
            var lambda    = DecimalMath.Atan2(Y, X);
            var sinLambda = DecimalMath.Sin(lambda);
            var cosLambda = DecimalMath.Cos(lambda);
            var beta      = DecimalMath.Asin(Z / R);
            var tanBeta   = DecimalMath.Tan(beta);
            var sinBeta   = DecimalMath.Sin(beta);
            var cosBeta   = DecimalMath.Cos(beta);

            var tilt    = CommonCalculations.GetAxialTilt(date) * DecimalMath.DegToRad;
            var cosTilt = DecimalMath.Cos(tilt);
            var sinTilt = DecimalMath.Sin(tilt);
            var RA      = DecimalMath.Atan2(sinLambda * cosTilt - tanBeta * sinTilt, cosLambda) * DecimalMath.RadToDeg;
            var Dec     = DecimalMath.Asin(sinBeta * cosTilt + cosBeta * sinTilt * sinLambda) * DecimalMath.RadToDeg;

            RA = CommonCalculations.From0To360(RA);
#if DEBUG
            Console.WriteLine($"R = {R} lambda = {lambda}, beta = {beta}, tilt = {tilt}, RA = {RA}, dec = {Dec}");
#endif

            return(new Vector2 <decimal>(RA, Dec));
        }
Exemplo n.º 8
0
        public Vector2 <decimal> EquatorialCoordinates(DateTime date)
        {
            var DegToRad = DecimalMath.DegToRad;

            var d = JulianDateCalculator.ToJulianDaysJ2000(date);
            var L = FL(d); var Lrad = L * DegToRad;
            var M = FMMoon(d); var Mrad = M * DegToRad;
            var F = FF(d); var Frad = F * DegToRad;

            var lambda = (L + 6.289M * DecimalMath.Sin(Mrad)) * DegToRad;
            var beta   = (5.128m * DecimalMath.Sin(Frad)) * DegToRad;
            var tilt   = CommonCalculations.GetAxialTilt(date) * DegToRad;

            var sinLambda = DecimalMath.Sin(lambda);
            var cosLambda = DecimalMath.Cos(lambda);

            var sinBeta = DecimalMath.Sin(beta);
            var cosBeta = DecimalMath.Cos(beta);
            var tanBeta = DecimalMath.Tan(beta);

            var sinTilt = DecimalMath.Sin(tilt);
            var cosTilt = DecimalMath.Cos(tilt);

            var RA  = DecimalMath.Atan2(sinLambda * cosTilt - tanBeta * sinTilt, cosLambda) * DecimalMath.RadToDeg;
            var Dec = DecimalMath.Asin(sinBeta * cosTilt + cosBeta * sinTilt * sinLambda) * DecimalMath.RadToDeg;

            return(new Vector2 <decimal>(CommonCalculations.From0To360(RA), Dec));
        }
Exemplo n.º 9
0
        public void CosZero()
        {
            decimal expected = 0.0M;
            decimal actual   = DecimalMath.PI / 2.0M;

            actual = DecimalMath.Cos(actual);
            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 10
0
        public void Root()
        {
            const decimal expected = 100.0M;
            decimal       actual   = expected * expected * expected;

            actual = DecimalMath.Root(actual, 3);
            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 11
0
        public void Sqrt()
        {
            const decimal expected = 9.0M;
            decimal       actual   = 81.0M;

            actual = DecimalMath.Sqrt(actual);
            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 12
0
        public void PowNegativeInt()
        {
            const decimal expected = 0.012345679012345679012345679M;
            decimal       actual   = 9.0M;

            actual = DecimalMath.Pow(actual, -2);
            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 13
0
        public void PowInt()
        {
            const decimal expected = 81.0M;
            decimal       actual   = 9.0M;

            actual = DecimalMath.Pow(actual, 2);
            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 14
0
        public void SinZero()
        {
            decimal expected = 0.0M;
            decimal actual   = DecimalMath.PI;

            actual = DecimalMath.Sin(actual);
            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 15
0
        public void SinOne()
        {
            decimal expected = 1.0M;
            decimal actual   = DecimalMath.PI / 2.0M;

            actual = DecimalMath.Sin(actual);
            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 16
0
        public void CosNegative()
        {
            decimal expected = -1.0M;
            decimal actual   = DecimalMath.PI;

            actual = DecimalMath.Cos(actual);
            Assert.AreEqual <decimal>(expected, actual);
        }
Exemplo n.º 17
0
        public override Vector2 <decimal> EclipticCoordinates(DateTime date)
        {
            var pos = EclipticEarth3D(date);
            var lon = DecimalMath.Atan2(pos.Y, pos.X) * DecimalMath.RadToDeg;
            var lat = DecimalMath.Asin(pos.Z / DecimalMath.Sqrt(pos.X * pos.X + pos.Y * pos.Y + pos.Z * pos.Z)) * DecimalMath.RadToDeg;

            return(new Vector2 <decimal>(CommonCalculations.From0To360(lon), lat));
        }
Exemplo n.º 18
0
 public decimal GetValue(decimal from0To1)
 {
     if (from0To1 < eps)
     {
         return(0m);
     }
     return(DecimalMath.Sqrt(from0To1));
 }
Exemplo n.º 19
0
        // Wolfram command
        // plot 23.439291111111 - 0.0130041666667*t - 1.63888888889e-07*t^2 + 5.959274797e-09*t^3 t from -1 to 1
        // where t is fraction of Julian Century passed from J2000
        /// <summary>
        /// Get current axial tilt (epsilon) based on date.
        /// Value of 23.4 degrees is slowly decreasing.
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static decimal GetAxialTilt(DateTime dateTime)
        {
            var julianCenturies = JulianDateCalculator.ToJulianCenturiesJ2000(dateTime);

            return(t0
                   + t1 * julianCenturies
                   + t2 * DecimalMath.Power(julianCenturies, 2)
                   + t3 * DecimalMath.Power(julianCenturies, 3));
        }
Exemplo n.º 20
0
        public override Vector2 <decimal> ApparentEclipticEarth(DateTime date)
        {
            var pos = ApparentEclipticEarth3D(date);
            var lon = DecimalMath.Atan2(pos.Y, pos.X) * DecimalMath.RadToDeg;
            var lat = DecimalMath.Asin(pos.Z / DecimalMath.Sqrt(pos.X * pos.X + pos.Y * pos.Y + pos.Z * pos.Z)) * DecimalMath.RadToDeg;

            lon = CommonCalculations.From0To360(lon);
            lat = CommonCalculations.From0To360(lat);
            return(new Vector2 <decimal>(lon, lat));
        }
Exemplo n.º 21
0
        public void TanSqrt3()
        {
            const decimal THREE    = 3.0M;
            decimal       expected = DecimalMath.Sqrt(THREE);
            decimal       actual   = DecimalMath.PI / THREE;

            actual = DecimalMath.Tan(actual);

            expected = Math.Round(expected, 22);    //1.7320508075688772935274463415
            actual   = Math.Round(actual, 22);      //1.7320508075688772935274463414

            Assert.AreEqual <decimal>(expected, actual);
        }
        public static decimal GetAscendant(double longi, double lat, DateTime dateTime, bool isVedic = true)
        {
            var Pi          = DecimalMath.Pi;
            var DegToRad    = Pi / 180;
            var longitude   = Convert.ToDecimal(-longi); // East should be positive, West negative
            var latitudeRad = Convert.ToDecimal(lat) * DegToRad;
            var tilt        = CommonCalculations.GetAxialTilt(dateTime);
            var tiltRad     = tilt * DegToRad;

#if DEBUG
            Console.WriteLine($"tilt: {tilt}");
#endif
            var siderealTime    = SiderealTime.Calculate(longitude, dateTime);
            var siderealTimeRad = siderealTime * DegToRad;
#if DEBUG
            Console.WriteLine($"sidereal Time: {siderealTime}");
#endif
            var y = -DecimalMath.Cos(siderealTimeRad);
#if DEBUG
            Console.WriteLine($"y :{y}");
            Console.WriteLine(
                $"sin(RAMC) = {DecimalMath.Sin(siderealTimeRad)}" +
                $"\ncos(TILT) = {DecimalMath.Cos(tiltRad)}" +
                $"\ntan(LAT) = {DecimalMath.Tan(latitudeRad)}" +
                $"\nsin(TILT) = {DecimalMath.Sin(tiltRad)}");
#endif
            var x = DecimalMath.Sin(siderealTimeRad) * DecimalMath.Cos(tiltRad)
                    + DecimalMath.Tan(latitudeRad) * DecimalMath.Sin(tiltRad);
#if DEBUG
            Console.WriteLine($"x :{x}");
            Console.WriteLine($"Decimal y/x: {y / x}");
            Console.WriteLine($"Atan(y/x): {DecimalMath.ATan(y / x)}");
#endif
            var output = DecimalMath.ATan(y / x) * 180 / Pi;

            if (output < 0)
            {
                output += 180;
            }
            if (siderealTimeRad > Pi / 2 && siderealTimeRad < 3 * Pi / 2)
            {
                output += 180;
                output %= 360;
            }
#if DEBUG
            Console.WriteLine($"output before applying sidereal diff{output}");
#endif
            return(output);
        }
Exemplo n.º 23
0
        /// <summary>
        /// Berechnung des Abschreibungs- und Restbuchwertes (<see cref="CalculationResult"/>) für das Abschreibungsjahr (<paramref name="period"/>).
        /// </summary>
        /// <param name="data">Die grundlegenden Daten für die Abschreibungsberechnung</param>
        /// <param name="period">Das Abschreibungsjahr für das die Daten errechnet werden sollen.</param>
        /// <returns>Die errechneten Abschreibungs- und Restbuchwerte</returns>
        public CalculationResult CalculateDepreciation(CalculationData data, int period)
        {
            if (period < 1 || period > data.DepreciationRange)
            {
                throw new ArgumentOutOfRangeException(nameof(period), "The period must be greater than 0 and less than the value of depreciationRange.");
            }

            var factor = CalculateFactor(data);

            var factorForOldValue = DecimalMath.Pow(factor, period - 1);
            var oldValue          = data.AcquisitionValue * factorForOldValue;
            var remainingValue    = factor * oldValue;
            var depreciation      = oldValue - remainingValue;

            return(new CalculationResult(period, depreciation, remainingValue));
        }
Exemplo n.º 24
0
        private Vector3 <decimal> EclipticSunXYZ(DateTime date)
        {
            var days = JulianDateCalculator.ToJulianDaysJ2000(date);

            SetTrigonometry(days);
            var S      = FS(days);
            var P      = FP(days);
            var lonecl = 238.9508m + 0.00400703m * days
                         - 19.799m * sinP + 19.848m * cosP
                         + 0.897m * sin2P - 4.956m * cos2P
                         + 0.610m * sin3P + 1.211m * cos3P
                         - 0.341m * sin4P - 0.190m * cos4P
                         + 0.128m * sin5P - 0.034m * cos5P
                         - 0.038m * sin6P + 0.031m * cos6P
                         + 0.020m * sinS_P - 0.010m * cosS_P;

            var latecl = -3.9082m
                         - 5.453m * sinP - 14.975m * cosP
                         + 3.527m * sin2P + 1.673m * cos2P
                         - 1.051m * sin3P + 0.328m * cos3P
                         + 0.179m * sin4P - 0.292m * cos4P
                         + 0.019m * sin5P + 0.100m * cos5P
                         - 0.031m * sin6P - 0.026m * cos6P
                         + 0.011m * cosS_P;
            var r = 40.72m
                    + 6.68m * sinP + 6.90m * cosP
                    - 1.18m * sin2P - 0.03m * cos2P
                    + 0.15m * sin3P - 0.14m * cos3P;

#if DEBUG
            Console.WriteLine($"Pluto lonecl = {lonecl}, latecl = {latecl}, r = {r}");
#endif

            var sinlon = DecimalMath.Sin(lonecl * DecimalMath.DegToRad);
            var coslon = DecimalMath.Cos(lonecl * DecimalMath.DegToRad);
            var sinlat = DecimalMath.Sin(latecl * DecimalMath.DegToRad);
            var coslat = DecimalMath.Cos(latecl * DecimalMath.DegToRad);

            var xe = r * coslon * coslat;
            var ye = r * sinlon * coslat;
            var ze = r * sinlat;

#if DEBUG
            Console.WriteLine($"Pluto ecliptic Sun  Xe= {xe}, Ye = {ye}, Ze = {ze}");
#endif
            return(new Vector3 <decimal>(xe, ye, ze));
        }
        public void OrbitalElemets_Success(DateTime date, string planetName, OrbitalElements expected)
        {
            var epsilon = 2m;
            // arrange & act
            var actual = PlanetManager.GetInstance().GetPlanet(planetName).GetOrbitalElements(date);

            // assert
            string output = string.Empty;

            output += !DecimalMath.IsClose(actual.LongAscedingNode, expected.LongAscedingNode, epsilon) ? $"\nLongAscendingNode\texpected : {expected.LongAscedingNode} actual {actual.LongAscedingNode}" : string.Empty;
            output += !DecimalMath.IsClose(actual.Inclination, expected.Inclination, epsilon) ? $"\nInclination\t\texpected : {expected.Inclination} actual {actual.Inclination}" : string.Empty;
            output += !DecimalMath.IsClose(actual.Peryhelion, expected.Peryhelion, epsilon) ? $"\nPeryhelion\t\texpected : {expected.Peryhelion} actual {actual.Peryhelion}" : string.Empty;
            output += !DecimalMath.IsClose(actual.Eccentricity, expected.Eccentricity, epsilon) ? $"\nEccentricity\t\texpected : {expected.Eccentricity} actual {actual.Eccentricity}" : string.Empty;
            output += !DecimalMath.IsClose(actual.MeanAnomaly, expected.MeanAnomaly, epsilon) ? $"\nMeanAnomaly\t\texpected : {expected.MeanAnomaly} actual {actual.MeanAnomaly}" : string.Empty;
            output += !DecimalMath.IsClose(actual.SemimajorAxis, expected.SemimajorAxis, epsilon) ? $"\nSemimajorAxis\t\texpected : {expected.SemimajorAxis} actual {actual.SemimajorAxis}" : string.Empty;
            Assert.True(string.IsNullOrEmpty(output), output);
        }
Exemplo n.º 26
0
        public static decimal CalculateSumLongitude(DateTime date)
        {
            var T   = JulianDateCalculator.ToJulianCenturiesJ2000(date);
            var sum = AL(T);

            foreach (var term in terms)
            {
                var e         = Fe(T, term.m);
                var sinFs     = DecimalMath.Sin(Fs(T, term));
                var component = e * term.S * sinFs;
                sum += component;
#if DEBUG
                Console.WriteLine($"Component no{term.no}\t{component}\te {e} \tLCoeff\t{e * term.S}\tSinFs{sinFs}");
#endif
            }
#if DEBUG
            Console.WriteLine($"Sum SIGMA_L = {sum}");
#endif
            return(sum);
        }
Exemplo n.º 27
0
        public Vector2 <decimal> EclipticCoordinates(DateTime date)
        {
            var DegToRad = DecimalMath.DegToRad;

            var T = JulianDateCalculator.ToJulianCenturiesJ2000(date);
            var L = CommonCalculations.From0To360(FL(T));
            var M = CommonCalculations.From0To360(FMMoon(T)); var Mrad = M * DegToRad;
            var F = CommonCalculations.From0To360(FF(T)); var Frad = F * DegToRad;

            var lambda = CommonCalculations.From0To360(L + 1e-6M * CalculateSumLongitude(date));
            var beta   = (5.128m * DecimalMath.Sin(Frad));

#if DEBUG
            Console.WriteLine($"Julian centuries passed: {T}, L = {L}, M = {M}, F = {F}");
            Console.WriteLine($"Lambda: {lambda}, Beta : {beta}");
#endif


            return(new Vector2 <decimal>(lambda, beta));
        }
Exemplo n.º 28
0
        private void SetTrigonometry(decimal days)
        {
            var S = FS(days) * DecimalMath.DegToRad;
            var P = FP(days) * DecimalMath.DegToRad;

            sinP   = DecimalMath.Sin(P);
            sin2P  = DecimalMath.Sin(2 * P);
            sin3P  = DecimalMath.Sin(3 * P);
            sin4P  = DecimalMath.Sin(4 * P);
            sin5P  = DecimalMath.Sin(5 * P);
            sin6P  = DecimalMath.Sin(6 * P);
            sinS_P = DecimalMath.Sin(S - P);

            cosP   = DecimalMath.Cos(P);
            cos2P  = DecimalMath.Cos(2 * P);
            cos3P  = DecimalMath.Cos(3 * P);
            cos4P  = DecimalMath.Cos(4 * P);
            cos5P  = DecimalMath.Cos(5 * P);
            cos6P  = DecimalMath.Cos(6 * P);
            cosS_P = DecimalMath.Cos(S - P);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Berechnung des Abschreibungs- und Restbuchwertes (<see cref="CalculationResult"/>) für das Abschreibungsjahr (<paramref name="period"/>).
        /// </summary>
        /// <param name="data">Die grundlegenden Daten für die Abschreibungsberechnung</param>
        /// <param name="period">Das Abschreibungsjahr für das die Daten errechnet werden sollen.</param>
        /// <returns>Die errechneten Abschreibungs- und Restbuchwerte</returns>
        public CalculationResult CalculateDepreciation(CalculationData data, int period)
        {
            if (period < 1 || period > data.DepreciationRange)
            {
                throw new ArgumentOutOfRangeException(nameof(period), "The period must be greater than 0 and less than the value of depreciationRange.");
            }

            var factor = CalculateFactor(data);

            var depreciations =
                Enumerable.Range(0, 6)
                .Select(x => DecimalMath.Pow(factor, x))
                .Select(x => data.AcquisitionValue * x)
                .SelectWithPrevious((prev, current) => prev - current)
                .Reverse()
                .ToList();

            var depreciation   = depreciations[period - 1];
            var remainingValue = data.AcquisitionValue - depreciations.Take(period).Sum();

            return(new CalculationResult(period, depreciation, remainingValue));
        }
Exemplo n.º 30
0
        public override Vector3 <decimal> ApparentEclipticEarth3D(DateTime date)
        {
            var E = EarthPosition.EarthInstance.EclipticSun3D(date);
            Vector3 <decimal> P;

            var tempDate = date;
            var TT       = decimal.MaxValue;

            while (true)
            {
                P = EclipticSun3D(tempDate);
                var s = Vector3 <decimal> .Distance(E, P);

                var newTT = s / CommonCalculations.LIGHTSPEED_AUperTick;
                if (DecimalMath.Abs(newTT - TT) < TimeSpan.TicksPerSecond) // try with date up to 1 second.
                {
                    break;
                }
                tempDate -= new TimeSpan(Convert.ToInt64(newTT));
                TT        = newTT;
            }
            return(new Vector3 <decimal>(P.X - E.X, P.Y - E.Y, P.Z - E.Z));
        }