using System; using System.Collections.Generic; namespace MathNet.Numerics.Distributions { public class Gamma { public static double CDF(double shape, double rate, double x) { if (shape < 0.0 || rate < 0.0) { throw new ArgumentException("Invalid parametrization for the distribution."); } if (double.IsPositiveInfinity(rate)) { return x >= shape ? 1.0 : 0.0; } if (shape == 0.0 && rate == 0.0) { return 0.0; } return SpecialFunctions.GammaLowerRegularized(shape, x*rate); } } } namespace MathNet.Numerics { public static partial class Precision { const int DoubleWidth = 53; const int SingleWidth = 24; public static readonly double DoublePrecision = Math.Pow(2, -DoubleWidth); public static readonly double SinglePrecision = Math.Pow(2, -SingleWidth); static readonly double DefaultDoubleAccuracy = DoublePrecision*10; static readonly float DefaultSingleAccuracy = (float) (SinglePrecision*10); public static bool AlmostEqualNorm(this double a, double b, double diff, double maximumAbsoluteError) { if (double.IsInfinity(a) || double.IsInfinity(b)) { return a == b; } if (double.IsNaN(a) || double.IsNaN(b)) { return false; } return Math.Abs(diff) < maximumAbsoluteError; } public static bool AlmostEqual(this double a, double b) { return AlmostEqualNorm(a, b, a - b, DefaultDoubleAccuracy); } public static bool AlmostEqual(this float a, float b) { return AlmostEqualNorm(a, b, a - b, DefaultSingleAccuracy); } } public static class Constants { public const double E = 2.71828182845905; public const double Pi = 3.14159265358979; public const double LnPi = 1.1447298858494; public const double TwoSqrtEOverPi = 1.86038273420527; public const double LogTwoSqrtEOverPi = 0.620782237635245; } public static partial class SpecialFunctions { const int GammaN = 10; const double GammaR = 10.900511; static readonly double[] GammaDk = { 2.48574089138753565546e-5, 1.05142378581721974210, -3.45687097222016235469, 4.51227709466894823700, -2.98285225323576655721, 1.05639711577126713077, -1.95428773191645869583e-1, 1.70970543404441224307e-2, -5.71926117404305781283e-4, 4.63399473359905636708e-6, -2.71994908488607703910e-9 }; public static double GammaLn(double z) { if (z < 0.5) { double s = GammaDk[0]; for (int i = 1; i <= GammaN; i++) { s += GammaDk[i]/(i - z); } return Constants.LnPi - Math.Log(Math.Sin(Math.PI*z)) - Math.Log(s) - Constants.LogTwoSqrtEOverPi - ((0.5 - z)*Math.Log((0.5 - z + GammaR)/Math.E)); } else { double s = GammaDk[0]; for (int i = 1; i <= GammaN; i++) { s += GammaDk[i]/(z + i - 1.0); } return Math.Log(s) + Constants.LogTwoSqrtEOverPi + ((z - 0.5)*Math.Log((z - 0.5 + GammaR)/Math.E)); } } public static double Gamma(double z) { if (z < 0.5) { double s = GammaDk[0]; for (int i = 1; i <= GammaN; i++) { s += GammaDk[i]/(i - z); } return Math.PI/(Math.Sin(Math.PI*z) *s *Constants.TwoSqrtEOverPi *Math.Pow((0.5 - z + GammaR)/Math.E, 0.5 - z)); } else { double s = GammaDk[0]; for (int i = 1; i <= GammaN; i++) { s += GammaDk[i]/(z + i - 1.0); } return s*Constants.TwoSqrtEOverPi*Math.Pow((z - 0.5 + GammaR)/Math.E, z - 0.5); } } public static double GammaLowerRegularized(double a, double x) { const double epsilon = 0.000000000000001; const double big = 4503599627370496.0; const double bigInv = 2.22044604925031308085e-16; if (a < 0d) { //throw new ArgumentOutOfRangeException(nameof(a), "Value must not be negative (zero is ok)."); } if (x < 0d) { //throw new ArgumentOutOfRangeException(nameof(x), "Value must not be negative (zero is ok)."); } if (a.AlmostEqual(0.0)) { if (x.AlmostEqual(0.0)) { //use right hand limit value because so that regularized upper/lower gamma definition holds. return 1d; } return 1d; } if (x.AlmostEqual(0.0)) { return 0d; } double ax = (a*Math.Log(x)) - x - GammaLn(a); if (ax < -709.78271289338399) { return a < x ? 1d : 0d; } if (x <= 1 || x <= a) { double r2 = a; double c2 = 1; double ans2 = 1; do { r2 = r2 + 1; c2 = c2*x/r2; ans2 += c2; } while ((c2/ans2) > epsilon); return Math.Exp(ax)*ans2/a; } int c = 0; double y = 1 - a; double z = x + y + 1; double p3 = 1; double q3 = x; double p2 = x + 1; double q2 = z*x; double ans = p2/q2; double error; do { c++; y += 1; z += 2; double yc = y*c; double p = (p2*z) - (p3*yc); double q = (q2*z) - (q3*yc); if (q != 0) { double nextans = p/q; error = Math.Abs((ans - nextans)/nextans); ans = nextans; } else { // zero div, skip error = 1; } // shift p3 = p2; p2 = p; q3 = q2; q2 = q; // normalize fraction when the numerator becomes large if (Math.Abs(p) > big) { p3 *= bigInv; p2 *= bigInv; q3 *= bigInv; q2 *= bigInv; } } while (error > epsilon); return 1d - (Math.Exp(ax)*ans); } } }
Write, Run & Share C# code online using OneCompiler's C# online compiler for free. It's one of the robust, feature-rich online compilers for C# language, running on the latest version 8.0. Getting started with the OneCompiler's C# compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C#
and start coding.
OneCompiler's C# online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
string name;
name = Console.ReadLine();
Console.WriteLine("Hello {0} ", name);
}
}
}
C# is a general purpose object-oriented programming language by Microsoft. Though initially it was developed as part of .net but later it was approved by ECMA and ISO standards.
You can use C# to create variety of applications, like web, windows, mobile, console applications and much more using Visual studio.
Data Type | Description | Range | size |
---|---|---|---|
int | To store integers | -2,147,483,648 to 2,147,483,647 | 4 bytes |
double | to store large floating point numbers with decimals | can store 15 decimal digits | 8 bytes |
float | to store floating point numbers with decimals | can store upto 7 decimal digits | 4 bytes |
char | to store single characters | - | 2 bytes |
string | to stores text | - | 2 bytes per character |
bool | to stores either true or false | - | 1 bit |
datatype variable-name = value;
When ever you want to perform a set of operations based on a condition or set of few conditions IF-ELSE is used.
if(conditional-expression) {
// code
}
else {
// code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement) {
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type[] array-name;
Method is a set of statements which gets executed only when they are called. Call the method name in the main function to execute the method.
static void method-name()
{
// code to be executed
}