From c5c6f66b0a4eed1046c42225c3a2161f96b46715 Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Sun, 26 May 2024 15:33:00 +0530 Subject: [PATCH 1/6] Adding Erf math function --- evaldo/builtins_math.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/evaldo/builtins_math.go b/evaldo/builtins_math.go index 97b070de..5f98441c 100644 --- a/evaldo/builtins_math.go +++ b/evaldo/builtins_math.go @@ -398,6 +398,20 @@ var Builtins_math = map[string]*env.Builtin{ } }, }, + "erf": { + Argsn: 1, + Doc: "Returns the error function of value.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + switch val := arg0.(type) { + case env.Integer: + return *env.NewDecimal(math.Erf(float64(val.Value))) + case env.Decimal: + return *env.NewDecimal(math.Erf(val.Value)) + default: + return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "erf") + } + }, + }, "pi": { Argsn: 0, Doc: "Return Pi constant.", From 31a077702c3107a0a939b0002594353c8151f6c7 Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Sun, 26 May 2024 15:40:46 +0530 Subject: [PATCH 2/6] Adding erfc math function --- evaldo/builtins_math.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/evaldo/builtins_math.go b/evaldo/builtins_math.go index 5f98441c..532cbeb3 100644 --- a/evaldo/builtins_math.go +++ b/evaldo/builtins_math.go @@ -412,6 +412,20 @@ var Builtins_math = map[string]*env.Builtin{ } }, }, + "erfc": { + Argsn: 1, + Doc: "Returns the complementary error function of value.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + switch val := arg0.(type) { + case env.Integer: + return *env.NewDecimal(math.Erfc(float64(val.Value))) + case env.Decimal: + return *env.NewDecimal(math.Erfc(val.Value)) + default: + return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "erfc") + } + }, + }, "pi": { Argsn: 0, Doc: "Return Pi constant.", From 210cd8f1cb47b73015336324bbfb0e18750b0ace Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Sun, 26 May 2024 15:50:04 +0530 Subject: [PATCH 3/6] Adding erfcinv match function --- evaldo/builtins_math.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/evaldo/builtins_math.go b/evaldo/builtins_math.go index 532cbeb3..1f795f34 100644 --- a/evaldo/builtins_math.go +++ b/evaldo/builtins_math.go @@ -426,6 +426,26 @@ var Builtins_math = map[string]*env.Builtin{ } }, }, + "erfcinv": { + Argsn: 1, + Doc: "Returns the inverse of erfc(x) function.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + switch val := arg0.(type) { + case env.Integer: + if val.Value < 0 || val.Value > 2 { + return MakeBuiltinError(ps, "Invalid input: erfcinv is only defined for 0 <= x <= 2.", "erfcinv") + } + return *env.NewDecimal(math.Erfcinv(float64(val.Value))) + case env.Decimal: + if val.Value < 0 || val.Value > 2 { + return MakeBuiltinError(ps, "Invalid input: erfcinv is only defined for 0 <= x <= 2.", "erfcinv") + } + return *env.NewDecimal(math.Erfcinv(val.Value)) + default: + return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "erfcinv") + } + }, + }, "pi": { Argsn: 0, Doc: "Return Pi constant.", From 5b6b4d55678252064094d312ce758ecf0261ef7f Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Sun, 26 May 2024 16:01:39 +0530 Subject: [PATCH 4/6] Adding Erfinv math function --- evaldo/builtins_math.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/evaldo/builtins_math.go b/evaldo/builtins_math.go index 1f795f34..f6443cf6 100644 --- a/evaldo/builtins_math.go +++ b/evaldo/builtins_math.go @@ -432,20 +432,28 @@ var Builtins_math = map[string]*env.Builtin{ Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { switch val := arg0.(type) { case env.Integer: - if val.Value < 0 || val.Value > 2 { - return MakeBuiltinError(ps, "Invalid input: erfcinv is only defined for 0 <= x <= 2.", "erfcinv") - } return *env.NewDecimal(math.Erfcinv(float64(val.Value))) case env.Decimal: - if val.Value < 0 || val.Value > 2 { - return MakeBuiltinError(ps, "Invalid input: erfcinv is only defined for 0 <= x <= 2.", "erfcinv") - } return *env.NewDecimal(math.Erfcinv(val.Value)) default: return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "erfcinv") } }, }, + "erfinv": { + Argsn: 1, + Doc: "Returns the inverse error function of value.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + switch val := arg0.(type) { + case env.Integer: + return *env.NewDecimal(math.Erfinv(float64(val.Value))) + case env.Decimal: + return *env.NewDecimal(math.Erfinv(val.Value)) + default: + return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "erfinv") + } + }, + }, "pi": { Argsn: 0, Doc: "Return Pi constant.", From 68027e55b3123405882cfb67f1e09d77fe4dd6fb Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Sun, 26 May 2024 16:25:53 +0530 Subject: [PATCH 5/6] Adding test cases for erf functions --- tests/misc.rye | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/misc.rye b/tests/misc.rye index 73f76bbf..4903b9d6 100644 --- a/tests/misc.rye +++ b/tests/misc.rye @@ -430,6 +430,40 @@ section "Math functions" equal { do\in math { dim 8 -1 } } 9.000000 equal { do\in math { dim -4 -2 } } 0.000000 } + + group "erf" + mold\nowrap "" + { { string } } + { + equal { do\in math { erf 1.5 } } 0.9661051464753108 + } + + group "erfc" + mold\nowrap "" + { { string } } + { + equal { do\in math { erfc 1.5 } } 0.033894853524689274 + } + + group "erfcinv" + mold\nowrap "" + { { string } } + { + equal { do\in math { erfcinv 1.5 } } -0.4769362762044699 + equal { do\in math { erfcinv 0 } } +Inf + equal { do\in math { erfcinv 2 } } -Inf + equal { do\in math { erfcinv 2.2 } } NaN + } + + group "erfinv" + mold\nowrap "" + { { string } } + { + equal { do\in math { erfcinv 0.5 } } 0.4769362762044699 + equal { do\in math { erfcinv 1 } } +Inf + equal { do\in math { erfcinv -1 } } -Inf + equal { do\in math { erfcinv 1.5 } } NaN + } } From fa9af19a9b421c5509316742586dad1b7442c53c Mon Sep 17 00:00:00 2001 From: Pratik Mota Date: Sun, 26 May 2024 16:49:32 +0530 Subject: [PATCH 6/6] Adding inf, and NaN values --- tests/misc.rye | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/misc.rye b/tests/misc.rye index 4903b9d6..1c1eaa7a 100644 --- a/tests/misc.rye +++ b/tests/misc.rye @@ -450,9 +450,9 @@ section "Math functions" { { string } } { equal { do\in math { erfcinv 1.5 } } -0.4769362762044699 - equal { do\in math { erfcinv 0 } } +Inf - equal { do\in math { erfcinv 2 } } -Inf - equal { do\in math { erfcinv 2.2 } } NaN + equal { do\in math { erfcinv 0 } } "+Inf" + equal { do\in math { erfcinv 2 } } "-Inf" + equal { do\in math { erfcinv 2.2 } } "NaN" } group "erfinv" @@ -460,9 +460,9 @@ section "Math functions" { { string } } { equal { do\in math { erfcinv 0.5 } } 0.4769362762044699 - equal { do\in math { erfcinv 1 } } +Inf - equal { do\in math { erfcinv -1 } } -Inf - equal { do\in math { erfcinv 1.5 } } NaN + equal { do\in math { erfcinv 1 } } "+Inf" + equal { do\in math { erfcinv -1 } } "-Inf" + equal { do\in math { erfcinv 1.5 } } "NaN" } }