From d01d6f45db3bcdebd99b5edf03422bb0537b6310 Mon Sep 17 00:00:00 2001 From: kimikage Date: Tue, 11 Jun 2013 03:09:39 +0900 Subject: [PATCH] =?utf8?q?MathEx=E3=82=AF=E3=83=A9=E3=82=B9=E3=81=AE?= =?utf8?q?=E8=BF=BD=E5=8A=A0=EF=BC=8C=E8=AA=A4=E5=B7=AE=E9=96=A2=E6=95=B0?= =?utf8?q?=E3=81=AE=E5=AE=9F=E8=A3=85(#23869)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Karinto/Karinto.csproj | 1 + Karinto/MathEx.cs | 231 +++++++++++++++++++++++++++++++++++++++++ KarintoTest/KarintoTest.csproj | 1 + KarintoTest/MathExTest.cs | 174 +++++++++++++++++++++++++++++++ 4 files changed, 407 insertions(+) create mode 100755 Karinto/MathEx.cs create mode 100755 KarintoTest/MathExTest.cs diff --git a/Karinto/Karinto.csproj b/Karinto/Karinto.csproj index e306798..6a88398 100755 --- a/Karinto/Karinto.csproj +++ b/Karinto/Karinto.csproj @@ -47,6 +47,7 @@ + diff --git a/Karinto/MathEx.cs b/Karinto/MathEx.cs new file mode 100755 index 0000000..0453747 --- /dev/null +++ b/Karinto/MathEx.cs @@ -0,0 +1,231 @@ +/* + * Karinto Library Project + * + * This software is distributed under a zlib-style license. + * See license.txt for more information. + */ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace Karinto +{ + public static class MathEx + { + #region fields + private static readonly Polynomial[] erfcTable = + { + new Polynomial( //[0]: 1 <= x < 2 + 4.7019003296642450457550391704e-1, + -5.4129916560282193120142282350e-4, + 3.2466045488989207057247716494e-2, + -1.5551574967900995569013129661e-3, + 1.2991371652846980287617056420e-3, + -1.2476738559919909068871115854e-4, + 4.2164948133638407169423314936e-5, + -5.5944555649744218134732620248e-6, + 1.2391258283306184550290288637e-6, + -1.8629332352031266041799875695e-7, + 3.3508489330180889238527074083e-8, + -5.1453759994172323811411458769e-9, + 8.2778054007509566713316121080e-10, + -1.2419584612081402943997565209e-10, + 1.8628225410158766603724913483e-11, + -2.6916642541339090011279256698e-12, + 3.8321798025710167662264220379e-13, + -5.2985573947392393836199793226e-14, + 7.2214593891835651145391699616e-15, + -1.0552316968012789176241975783e-15, + 1.3848365749828243613160210837e-16 + ), + new Polynomial( //[1]: 2 <= x < 4 + 0.28246919208993532,//2.8246919208993525100243945507e-1, + 1.0295429295263581287408466969e-4, + 2.6352174925955979906965931429e-2, + -2.0840262584872572824603082922e-3, + 1.6107317263933041407504226538e-3, + -2.6413257091217055241174636048e-4, + 9.3720096524725604112989062675e-5, + -2.0394452777501772979594808178e-5, + 5.4818843984699154429750547408e-6, + -1.2769242863171188612896799307e-6, + 3.0855108190100308040276886416e-7, + -7.1110449954162265412516889674e-8, + 1.6277854940656666077194365644e-8, + -3.6388350444865150940102551512e-9, + 8.0125451384130244729739875906e-10, + -1.7333139256038089278659882792e-10, + 3.6913251414732819339098560977e-11, + -7.7391112861863152629855204763e-12, + 1.5985771117554783386097643078e-12, + -3.2558812235239520370430316160e-13, + 6.5474397002885039401566856717e-14, + -1.2908539064863451579569117003e-14, + 2.4663418649675516649805424542e-15, + -5.0102905606542145531985288605e-16, + 1.1637629874200535139854357149e-16, + -1.7702372520803693617903759643e-17 + ), + new Polynomial( //[2]: 4 <= x < 8 + 2.6609916698112647641230096605e-1, + 1.0052997692961233689565319255e-1, + 0.063819586227166714,//6.3819586227166696717161903119e-2, + 1.6510111747337704484097091109e-2, + 6.7745775475928297665975539362e-3, + 1.2613799574673550665257600345e-3, + 4.6314050254419785813179921962e-4, + 5.4816124629655721757719475954e-5, + 2.5069740734847835425277961803e-5, + 7.6494811710229433994592919775e-7, + 1.3069891449561428965827894398e-6, + -1.1211722505095090109921762128e-7, + 7.7627072370605473720667209070e-8, + -1.5067764445724103557487307330e-8, + 5.3921138750029687859330065848e-9, + -1.3591247804716958040748207366e-9, + 4.0341965879492537779635899830e-10, + -1.0884398300756496354533131958e-10, + 3.0363217123340332343882922235e-11, + -8.2439513886349055414081918841e-12, + 2.2756305305902268859393373330e-12, + -6.1246789459726400033402401708e-13, + 1.4333003386843162197015125496e-13, + -3.7815369812690037058307445308e-14, + 1.7401564599128446023009328203e-14, + -4.5875278594765951588791904401e-15 + ), + new Polynomial( //[3]: 8 <= x < 16 + 4.6854221014893762619745618301e-2, + -1.5511450952249084104775598329e-2, + 5.1178905303441648577722059649e-3, + -1.6829798529769531049332840624e-3, + 5.5160777130644620308102757353e-4, + -1.8020184996880086091090661904e-4, + 5.8678514133519807272309470851e-5, + -1.9045977453678276537004114523e-5, + 6.1623270905278656151589976968e-6, + -1.9875419915464967477768116933e-6, + 6.3904356643246431611609250453e-7, + -2.0483278667408340516487849749e-7, + 6.5453904816333940501972558207e-8, + -2.0852118199579889358624923805e-8, + 6.6229050456699429842283832512e-9, + -2.0972627311983894339808366077e-9, + 6.6237821648144864340143708663e-10, + -2.0853485442305791192334927397e-10, + 6.5162153190849185531320932146e-11, + -2.0380108335416826794297357698e-11, + 6.6463403803777970165860627602e-12, + -2.0777605497301746376639943675e-12, + 4.6614566309518191650253075708e-13, + -1.4074713019480904125485292343e-13, + 1.0831110710204089408511450325e-13, + -3.3906934201035073659517665140e-14 + ), + new Polynomial( //[4]: 16 <= x < 28 + 2.8262089312268317148835811995e-2, + -5.4152623609484073274229170763e-3, + 1.6241399412435775154107409724e-3, + -5.0744105591774430405597196162e-3, + -1.7616157737971578130497741968e-2, + 2.3083427166129394399962687197e-2, + 1.1164742403248709989628167597e-1, + -3.5273994341997794105095275190e-2, + -3.0869428535741202544195312887e-1, + -1.3020536214165453885942308553e-2, + 4.3332671745834241123676787595e-1, + 9.4385604975718760529994282754e-2, + -3.0238469418692319645741714650e-1, + -9.5995136295717931771565803096e-2, + 8.3313274670729987166299100438e-2, + 3.1593997744278794785645602547e-2 + ) + }; + #endregion + + #region public methods + public static double Erf(double x) + { + double a = Math.Abs(x); + if (a >= 1.0) + { + return 1.0 - Erfc(x); + } + double y; + double aa = a * a; + if (a < 0.0625) + { + double c1 = a * aa; + double c2 = c1 * aa; + double c3 = c2 * aa; + double c4 = c3 * aa; + + double s1 = c1 * -315.0;// (3*5*7*9 / -3 / 1!); + double s2 = s1 * 0 + c2 * 189.0 / 2.0;// (3*5*7*9 / +5 / 2!) + double s3 = s2 * 0 + c3 * -45.0 / 2.0;// (3*5*7*9 / -7 / 3!) + double s4 = s3 * 0 + c4 * 35.0 / 8.0;// (3*5*7*9 / 9 / 4!) + y = a + (s1 + s2 + s3 + s4) / 945.0; //(3*5*7*9); + } + else + { + double c = a; + double s = 0; + double b = 1.0; + for (double i = -1.0; i > -20.0; i-=1.0) + { + c *= aa / i; + b += 2.0; + s += c / b; + } + y = a + s; + } + y *= 1.1283791670955126; // 2 / pi^0.5 + return Math.Sign(x) > 0 ? y : -y; + } + + public static double Erfc(double x) + { + double a = Math.Abs(x); + if (a < 1.0) + { + return 1.0 - Erf(x); + } + if (a > 27.226017111108501) + { + return x > 0 ? 0 : 2.0; + } + double y = 0.0; + + if (a < 2.0) + { + y = Math.Exp(-1.1688327773406599 * a * a); + y *= erfcTable[0].GetValueAt(a + a - 3.0); + } + else if (a < 4.0) + { + decimal b = -1.05068636145441m * (decimal)a * (decimal)a; + y = Math.Exp((double)b); + y *= erfcTable[1].GetValueAt(a - 3.0); + } + else if (a < 8.0) + { + decimal b = -1.0292687483818601m * (decimal)a * (decimal)a; + y = Math.Exp((double)b); + y *= erfcTable[2].GetValueAt(a * 0.5 - 3); + } + else if (a < 16.0) + { + y = Math.Exp(-a * a); + y *= erfcTable[3].GetValueAt(a * 0.25 - 3); + } + else + { + y = Math.Exp(-a * a); + y *= erfcTable[4].GetValueAt(a * 0.125 - 3); + } + return x > 0 ? y : 2.0 - y; + } + #endregion + } +} diff --git a/KarintoTest/KarintoTest.csproj b/KarintoTest/KarintoTest.csproj index deb59f6..bcacb2b 100755 --- a/KarintoTest/KarintoTest.csproj +++ b/KarintoTest/KarintoTest.csproj @@ -50,6 +50,7 @@ + diff --git a/KarintoTest/MathExTest.cs b/KarintoTest/MathExTest.cs new file mode 100755 index 0000000..4a0425c --- /dev/null +++ b/KarintoTest/MathExTest.cs @@ -0,0 +1,174 @@ +/* + * Karinto Library Project + * + * This software is distributed under a zlib-style license. + * See license.txt for more information. + */ + +using System; +using System.Collections.Generic; +using System.Text; +using NUnit.Framework; +using Karinto; +using System.Diagnostics; + +namespace KarintoTest +{ + class MathExTest + { + private string DoubleToRational(double d) + { + int e; + ulong a; + unsafe + { + ulong i64; + *(&i64) = *(ulong*)((void*)&d); + e = (int)(i64 >> 52) & 0x7FF; + a = i64 & 0xFFFFFFFFFFFFFul | 0x10000000000000ul; + } + string s = d < 0 ? "-" : ""; + return String.Format("{0}{1} * 2b0^{2}", s, a, e -52 - 1023); + } + + [Test] + public void Erf() + { + PointList p = new PointList(); + p.Add(-6.0, -1.0); + p.Add(-1.0, -0.84270079294971489); + p.Add(0.0, 0.0); + p.Add(1e-300, 1.1283791670955126e-300); + p.Add(1e-100, 1.1283791670955126e-100); + p.Add(1e-50, 1.1283791670955126e-50); + p.Add(1e-10, 1.1283791670955126e-10); + p.Add(1e-9, 1.1283791670955126e-9); + p.Add(1e-8, 1.1283791670955126e-8); + p.Add(3e-8, 3.3851375012865365e-8); + p.Add(1e-7, 1.1283791670955088e-7); + p.Add(3e-7, 3.3851375012864358e-7); + p.Add(1e-6, 1.1283791670951364e-6); + p.Add(3e-6, 3.3851375012763826e-6); + p.Add(1e-5, 1.1283791670579000e-5); + p.Add(3e-5, 3.3851375002709968e-5); + p.Add(5e-5, 5.6418958307759834e-5); + p.Add(1e-4, 1.1283791633342487e-4); + p.Add(3e-4, 3.3851373997324151e-4); + p.Add(5e-4, 5.6418953653196121e-4); + p.Add(1e-3, 1.1283787909692363e-3); + p.Add(3e-3, 3.3851273459014537e-3); + p.Add(5e-3, 5.6418488200315501e-3); + p.Add(1e-2, 1.1283415555849616e-2); + p.Add(3e-2, 3.3841222341735429e-2); + p.Add(5e-2, 5.6371977797016630e-2); + p.Add(1e-1, 1.1246291601828490e-1); + p.Add(0.2, 0.22270258921047847); + p.Add(0.3, 0.32862675945912739); + p.Add(0.4, 0.42839235504666845); + p.Add(0.5, 0.52049987781304652); + p.Add(0.6, 0.60385609084792591); + p.Add(0.7, 0.67780119383741844); + p.Add(0.8, 0.74210096470766052); + p.Add(0.9, 0.79690821242283216); + p.Add(0.99999999999999989, 0.84270079294971478); + p.Add(1.0, 0.84270079294971489); + p.Add(1.0000000000000002, 0.84270079294971501); + p.Add(2.0, 0.99532226501895271); + p.Add(3.0, 0.99997790950300136); + p.Add(4.0, 0.99999998458274209); + p.Add(5.0, 0.99999999999846256); + p.Add(6.0, 1.0); + foreach (Point t in p) + { + double erf = MathEx.Erf(t.X); + string m = String.Format("x = {0}, y = {1}", + t.X, DoubleToRational(erf)); + Assert.AreEqual(t.Y, erf, Math.Abs(t.Y) * 1.7763568394002505e-15, m); + } + } + + [Test] + public void Erfc() + { + PointList p = new PointList(); + p.Add(-28.0, 2.0); + p.Add(-1.0, 1.8427007929497148); + p.Add(0.0, 1.0); + p.Add(0.1, 0.88753708398171505); + p.Add(0.5, 0.47950012218695348); + p.Add(1.0, 0.15729920705028513); + p.Add(1.0000000000000002, 0.15729920705028505); + p.Add(1.1, 0.11979493042591827); + p.Add(1.2, 0.089686021770364638); + p.Add(1.3, 0.065992055059347549); + p.Add(1.4, 0.047714880237351202); + p.Add(1.5, 0.033894853524689274); + p.Add(1.6, 0.023651616655355985); + p.Add(1.7, 0.016209541409225439); + p.Add(1.8, 0.010909498364269283); + p.Add(1.9, 0.0072095707647425325); + p.Add(1.9999999999999998, 0.0046777349810472706); + p.Add(2.0, 0.0046777349810472662); + p.Add(2.1, 0.0029794666563329841); + p.Add(2.2, 0.0018628462979818898); + p.Add(2.3, 0.0011431765973566525); + p.Add(2.4, 0.0006885138966450789); + p.Add(2.5, 0.00040695201744495892); + p.Add(2.6, 0.00023603441652934908); + p.Add(2.7, 0.00013433273994052419); + p.Add(2.8, 7.5013194665459108e-05); + p.Add(2.9, 4.1097878099458858e-05); + p.Add(3.0, 2.2090496998585441e-05); + p.Add(3.1, 1.1648657367199589e-05); + p.Add(3.2, 6.0257611517620876e-06); + p.Add(3.3, 3.0577097964381650e-06); + p.Add(3.4, 1.5219933628622864e-06); + p.Add(3.5, 7.4309837234141278e-07); + p.Add(4.0, 1.5417257900280020e-08); + + foreach (Point t in p) + { + double erfc = MathEx.Erfc(t.X); + string m = String.Format("x = {0}, y = {1}", + t.X, DoubleToRational(erfc)); + Assert.AreEqual(t.Y, erfc, Math.Abs(t.Y) * 1.7763568394002505e-15, m); + } + + p.Clear(); + p.Add(4.5, 1.9661604415428876e-10); + p.Add(5.0, 1.5374597944280349e-12); + p.Add(5.5, 7.3578479179743983e-15); + p.Add(6.0, 2.1519736712498913e-17); + p.Add(6.5, 3.8421483271206475e-20); + p.Add(7.0, 4.1838256077794142e-23); + p.Add(7.5, 2.7766493860305689e-26); + p.Add(8.0, 1.1224297172982926e-29); + p.Add(10.0, 2.0884875837625449e-45); + p.Add(12.0, 1.3562611692059042e-64); + p.Add(14.0, 3.0372298477503115e-87); + + foreach (Point t in p) + { + double erfc = MathEx.Erfc(t.X); + string m = String.Format("x = {0}, y = {1}", + t.X, DoubleToRational(erfc)); + Assert.AreEqual(t.Y, erfc, Math.Abs(t.Y) * 1e-14, m); + } + + p.Clear(); + p.Add(16.0, 2.3284857515715299e-113); + p.Add(20.0, 5.3958656116079012e-176); + p.Add(24.0, 1.6489825831519335e-252); + p.Add(28.0, 0.0); + p.Add(100.0, 0.0); + + foreach (Point t in p) + { + double erfc = MathEx.Erfc(t.X); + string m = String.Format("x = {0}, y = {1}", + t.X, DoubleToRational(erfc)); + Assert.AreEqual(t.Y, erfc, Math.Abs(t.Y) * 0.5, m); + } + } + } +} -- 2.11.0