goscript 0.0.34 → 0.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/analysis.go +28 -20
- package/compiler/analysis_test.go +14 -0
- package/compiler/assignment.go +19 -7
- package/compiler/compiler.go +15 -99
- package/compiler/composite-lit.go +60 -17
- package/compiler/decl.go +1 -1
- package/compiler/expr-call.go +233 -35
- package/compiler/expr-selector.go +28 -2
- package/compiler/expr.go +13 -37
- package/compiler/lit.go +111 -2
- package/compiler/primitive.go +6 -6
- package/compiler/protobuf.go +0 -5
- package/compiler/sanitize.go +101 -0
- package/compiler/spec-struct.go +41 -8
- package/compiler/spec-value.go +29 -18
- package/compiler/stmt-assign.go +22 -1
- package/compiler/stmt.go +26 -9
- package/compiler/type.go +3 -3
- package/dist/gs/builtin/builtin.d.ts +3 -2
- package/dist/gs/builtin/builtin.js +2 -1
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/map.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +7 -1
- package/dist/gs/builtin/slice.js +112 -22
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/errors.d.ts +13 -0
- package/dist/gs/github.com/pkg/errors/errors.js +232 -0
- package/dist/gs/github.com/pkg/errors/errors.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/go113.d.ts +4 -0
- package/dist/gs/github.com/pkg/errors/go113.js +34 -0
- package/dist/gs/github.com/pkg/errors/go113.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/index.d.ts +3 -0
- package/dist/gs/github.com/pkg/errors/index.js +4 -0
- package/dist/gs/github.com/pkg/errors/index.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/stack.d.ts +32 -0
- package/dist/gs/github.com/pkg/errors/stack.js +111 -0
- package/dist/gs/github.com/pkg/errors/stack.js.map +1 -0
- package/dist/gs/maps/index.d.ts +2 -2
- package/dist/gs/maps/index.js +2 -2
- package/dist/gs/maps/index.js.map +1 -1
- package/dist/gs/maps/iter.d.ts +7 -0
- package/dist/gs/maps/iter.js +57 -0
- package/dist/gs/maps/iter.js.map +1 -0
- package/dist/gs/maps/maps.d.ts +7 -0
- package/dist/gs/maps/maps.js +67 -0
- package/dist/gs/maps/maps.js.map +1 -0
- package/dist/gs/math/abs.gs.d.ts +1 -0
- package/dist/gs/math/abs.gs.js +10 -0
- package/dist/gs/math/abs.gs.js.map +1 -0
- package/dist/gs/math/acosh.gs.d.ts +2 -0
- package/dist/gs/math/acosh.gs.js +14 -0
- package/dist/gs/math/acosh.gs.js.map +1 -0
- package/dist/gs/math/asin.gs.d.ts +4 -0
- package/dist/gs/math/asin.gs.js +24 -0
- package/dist/gs/math/asin.gs.js.map +1 -0
- package/dist/gs/math/asinh.gs.d.ts +2 -0
- package/dist/gs/math/asinh.gs.js +14 -0
- package/dist/gs/math/asinh.gs.js.map +1 -0
- package/dist/gs/math/atan.gs.d.ts +4 -0
- package/dist/gs/math/atan.gs.js +22 -0
- package/dist/gs/math/atan.gs.js.map +1 -0
- package/dist/gs/math/atan2.gs.d.ts +2 -0
- package/dist/gs/math/atan2.gs.js +30 -0
- package/dist/gs/math/atan2.gs.js.map +1 -0
- package/dist/gs/math/atanh.gs.d.ts +2 -0
- package/dist/gs/math/atanh.gs.js +16 -0
- package/dist/gs/math/atanh.gs.js.map +1 -0
- package/dist/gs/math/bits.gs.d.ts +5 -0
- package/dist/gs/math/bits.gs.js +46 -0
- package/dist/gs/math/bits.gs.js.map +1 -0
- package/dist/gs/math/cbrt.gs.d.ts +2 -0
- package/dist/gs/math/cbrt.gs.js +14 -0
- package/dist/gs/math/cbrt.gs.js.map +1 -0
- package/dist/gs/math/const.gs.d.ts +30 -0
- package/dist/gs/math/const.gs.js +61 -0
- package/dist/gs/math/const.gs.js.map +1 -0
- package/dist/gs/math/copysign.gs.d.ts +1 -0
- package/dist/gs/math/copysign.gs.js +20 -0
- package/dist/gs/math/copysign.gs.js.map +1 -0
- package/dist/gs/math/dim.gs.d.ts +5 -0
- package/dist/gs/math/dim.gs.js +69 -0
- package/dist/gs/math/dim.gs.js.map +1 -0
- package/dist/gs/math/erf.gs.d.ts +4 -0
- package/dist/gs/math/erf.gs.js +336 -0
- package/dist/gs/math/erf.gs.js.map +1 -0
- package/dist/gs/math/erfinv.gs.d.ts +2 -0
- package/dist/gs/math/erfinv.gs.js +118 -0
- package/dist/gs/math/erfinv.gs.js.map +1 -0
- package/dist/gs/math/exp.gs.d.ts +5 -0
- package/dist/gs/math/exp.gs.js +30 -0
- package/dist/gs/math/exp.gs.js.map +1 -0
- package/dist/gs/math/expm1.gs.d.ts +2 -0
- package/dist/gs/math/expm1.gs.js +17 -0
- package/dist/gs/math/expm1.gs.js.map +1 -0
- package/dist/gs/math/floor.gs.d.ts +8 -0
- package/dist/gs/math/floor.gs.js +75 -0
- package/dist/gs/math/floor.gs.js.map +1 -0
- package/dist/gs/math/fma.gs.d.ts +1 -0
- package/dist/gs/math/fma.gs.js +8 -0
- package/dist/gs/math/fma.gs.js.map +1 -0
- package/dist/gs/math/frexp.gs.d.ts +2 -0
- package/dist/gs/math/frexp.gs.js +28 -0
- package/dist/gs/math/frexp.gs.js.map +1 -0
- package/dist/gs/math/gamma.gs.d.ts +3 -0
- package/dist/gs/math/gamma.gs.js +149 -0
- package/dist/gs/math/gamma.gs.js.map +1 -0
- package/dist/gs/math/hypot.gs.d.ts +2 -0
- package/dist/gs/math/hypot.gs.js +16 -0
- package/dist/gs/math/hypot.gs.js.map +1 -0
- package/dist/gs/math/index.d.ts +44 -0
- package/dist/gs/math/index.js +45 -0
- package/dist/gs/math/index.js.map +1 -0
- package/dist/gs/math/j0.gs.d.ts +4 -0
- package/dist/gs/math/j0.gs.js +228 -0
- package/dist/gs/math/j0.gs.js.map +1 -0
- package/dist/gs/math/j1.gs.d.ts +4 -0
- package/dist/gs/math/j1.gs.js +211 -0
- package/dist/gs/math/j1.gs.js.map +1 -0
- package/dist/gs/math/jn.gs.d.ts +2 -0
- package/dist/gs/math/jn.gs.js +412 -0
- package/dist/gs/math/jn.gs.js.map +1 -0
- package/dist/gs/math/ldexp.gs.d.ts +2 -0
- package/dist/gs/math/ldexp.gs.js +20 -0
- package/dist/gs/math/ldexp.gs.js.map +1 -0
- package/dist/gs/math/lgamma.gs.d.ts +2 -0
- package/dist/gs/math/lgamma.gs.js +243 -0
- package/dist/gs/math/lgamma.gs.js.map +1 -0
- package/dist/gs/math/log.gs.d.ts +2 -0
- package/dist/gs/math/log.gs.js +16 -0
- package/dist/gs/math/log.gs.js.map +1 -0
- package/dist/gs/math/log10.gs.d.ts +4 -0
- package/dist/gs/math/log10.gs.js +17 -0
- package/dist/gs/math/log10.gs.js.map +1 -0
- package/dist/gs/math/log1p.gs.d.ts +2 -0
- package/dist/gs/math/log1p.gs.js +17 -0
- package/dist/gs/math/log1p.gs.js.map +1 -0
- package/dist/gs/math/logb.gs.d.ts +3 -0
- package/dist/gs/math/logb.gs.js +43 -0
- package/dist/gs/math/logb.gs.js.map +1 -0
- package/dist/gs/math/mod.gs.d.ts +2 -0
- package/dist/gs/math/mod.gs.js +26 -0
- package/dist/gs/math/mod.gs.js.map +1 -0
- package/dist/gs/math/modf.gs.d.ts +2 -0
- package/dist/gs/math/modf.gs.js +24 -0
- package/dist/gs/math/modf.gs.js.map +1 -0
- package/dist/gs/math/nextafter.gs.d.ts +2 -0
- package/dist/gs/math/nextafter.gs.js +66 -0
- package/dist/gs/math/nextafter.gs.js.map +1 -0
- package/dist/gs/math/pow.gs.d.ts +3 -0
- package/dist/gs/math/pow.gs.js +40 -0
- package/dist/gs/math/pow.gs.js.map +1 -0
- package/dist/gs/math/pow10.gs.d.ts +1 -0
- package/dist/gs/math/pow10.gs.js +14 -0
- package/dist/gs/math/pow10.gs.js.map +1 -0
- package/dist/gs/math/remainder.gs.d.ts +2 -0
- package/dist/gs/math/remainder.gs.js +25 -0
- package/dist/gs/math/remainder.gs.js.map +1 -0
- package/dist/gs/math/signbit.gs.d.ts +1 -0
- package/dist/gs/math/signbit.gs.js +5 -0
- package/dist/gs/math/signbit.gs.js.map +1 -0
- package/dist/gs/math/sin.gs.d.ts +4 -0
- package/dist/gs/math/sin.gs.js +29 -0
- package/dist/gs/math/sin.gs.js.map +1 -0
- package/dist/gs/math/sincos.gs.d.ts +1 -0
- package/dist/gs/math/sincos.gs.js +11 -0
- package/dist/gs/math/sincos.gs.js.map +1 -0
- package/dist/gs/math/sinh.gs.d.ts +4 -0
- package/dist/gs/math/sinh.gs.js +27 -0
- package/dist/gs/math/sinh.gs.js.map +1 -0
- package/dist/gs/math/sqrt.gs.d.ts +2 -0
- package/dist/gs/math/sqrt.gs.js +15 -0
- package/dist/gs/math/sqrt.gs.js.map +1 -0
- package/dist/gs/math/tan.gs.d.ts +2 -0
- package/dist/gs/math/tan.gs.js +17 -0
- package/dist/gs/math/tan.gs.js.map +1 -0
- package/dist/gs/math/tanh.gs.d.ts +2 -0
- package/dist/gs/math/tanh.gs.js +17 -0
- package/dist/gs/math/tanh.gs.js.map +1 -0
- package/dist/gs/math/trig_reduce.gs.d.ts +1 -0
- package/dist/gs/math/trig_reduce.gs.js +62 -0
- package/dist/gs/math/trig_reduce.gs.js.map +1 -0
- package/dist/gs/math/unsafe.gs.d.ts +4 -0
- package/dist/gs/math/unsafe.gs.js +47 -0
- package/dist/gs/math/unsafe.gs.js.map +1 -0
- package/dist/gs/strconv/atob.gs.d.ts +4 -0
- package/dist/gs/strconv/atob.gs.js +42 -0
- package/dist/gs/strconv/atob.gs.js.map +1 -0
- package/dist/gs/strconv/atof.gs.d.ts +2 -0
- package/dist/gs/strconv/atof.gs.js +51 -0
- package/dist/gs/strconv/atof.gs.js.map +1 -0
- package/dist/gs/strconv/atoi.gs.d.ts +33 -0
- package/dist/gs/strconv/atoi.gs.js +200 -0
- package/dist/gs/strconv/atoi.gs.js.map +1 -0
- package/dist/gs/strconv/doc.gs.d.ts +1 -0
- package/dist/gs/strconv/doc.gs.js +2 -0
- package/dist/gs/strconv/doc.gs.js.map +1 -0
- package/dist/gs/strconv/ftoa.gs.d.ts +3 -0
- package/dist/gs/strconv/ftoa.gs.js +58 -0
- package/dist/gs/strconv/ftoa.gs.js.map +1 -0
- package/dist/gs/strconv/index.d.ts +6 -0
- package/dist/gs/strconv/index.js +7 -0
- package/dist/gs/strconv/index.js.map +1 -0
- package/dist/gs/strconv/itoa.gs.d.ts +6 -0
- package/dist/gs/strconv/itoa.gs.js +37 -0
- package/dist/gs/strconv/itoa.gs.js.map +1 -0
- package/dist/gs/strconv/quote.gs.d.ts +19 -0
- package/dist/gs/strconv/quote.gs.js +217 -0
- package/dist/gs/strconv/quote.gs.js.map +1 -0
- package/dist/gs/strings/index.d.ts +3 -0
- package/dist/gs/strings/index.js +4 -0
- package/dist/gs/strings/index.js.map +1 -1
- package/dist/gs/strings/replace.d.ts +0 -74
- package/dist/gs/strings/replace.js +6 -204
- package/dist/gs/strings/replace.js.map +1 -1
- package/dist/gs/strings/search.d.ts +0 -1
- package/dist/gs/strings/search.js +0 -21
- package/dist/gs/strings/search.js.map +1 -1
- package/dist/gs/sync/atomic/doc.gs.d.ts +28 -0
- package/dist/gs/sync/atomic/doc.gs.js +265 -0
- package/dist/gs/sync/atomic/doc.gs.js.map +1 -0
- package/dist/gs/sync/atomic/doc_64.gs.d.ts +15 -0
- package/dist/gs/sync/atomic/doc_64.gs.js +165 -0
- package/dist/gs/sync/atomic/doc_64.gs.js.map +1 -0
- package/dist/gs/sync/atomic/index.d.ts +4 -0
- package/dist/gs/sync/atomic/index.js +5 -0
- package/dist/gs/sync/atomic/index.js.map +1 -0
- package/dist/gs/sync/atomic/type.gs.d.ts +130 -0
- package/dist/gs/sync/atomic/type.gs.js +433 -0
- package/dist/gs/sync/atomic/type.gs.js.map +1 -0
- package/dist/gs/sync/atomic/value.gs.d.ts +19 -0
- package/dist/gs/sync/atomic/value.gs.js +116 -0
- package/dist/gs/sync/atomic/value.gs.js.map +1 -0
- package/dist/gs/unsafe/unsafe.d.ts +1 -0
- package/dist/gs/unsafe/unsafe.js +5 -0
- package/dist/gs/unsafe/unsafe.js.map +1 -1
- package/gs/builtin/builtin.ts +9 -7
- package/gs/builtin/map.ts +5 -1
- package/gs/builtin/slice.ts +152 -24
- package/gs/github.com/pkg/errors/errors.ts +307 -0
- package/gs/github.com/pkg/errors/go113.ts +39 -0
- package/gs/github.com/pkg/errors/index.ts +3 -0
- package/gs/github.com/pkg/errors/stack.ts +127 -0
- package/gs/maps/index.ts +2 -2
- package/gs/maps/iter.ts +67 -0
- package/gs/maps/maps.ts +89 -0
- package/gs/math/TODO.md +156 -0
- package/gs/math/abs.gs.test.ts +29 -0
- package/gs/math/abs.gs.ts +13 -0
- package/gs/math/acosh.gs.test.ts +39 -0
- package/gs/math/acosh.gs.ts +21 -0
- package/gs/math/asin.gs.test.ts +66 -0
- package/gs/math/asin.gs.ts +27 -0
- package/gs/math/asinh.gs.test.ts +37 -0
- package/gs/math/asinh.gs.ts +21 -0
- package/gs/math/atan.gs.test.ts +49 -0
- package/gs/math/atan.gs.ts +27 -0
- package/gs/math/atan2.gs.test.ts +55 -0
- package/gs/math/atan2.gs.ts +37 -0
- package/gs/math/atanh.gs.test.ts +47 -0
- package/gs/math/atanh.gs.ts +21 -0
- package/gs/math/bits.gs.test.ts +88 -0
- package/gs/math/bits.gs.ts +61 -0
- package/gs/math/cbrt.gs.test.ts +57 -0
- package/gs/math/cbrt.gs.ts +20 -0
- package/gs/math/const.gs.test.ts +54 -0
- package/gs/math/const.gs.ts +93 -0
- package/gs/math/copysign.gs.test.ts +44 -0
- package/gs/math/copysign.gs.ts +27 -0
- package/gs/math/dim.gs.test.ts +102 -0
- package/gs/math/dim.gs.ts +84 -0
- package/gs/math/erf.gs.test.ts +92 -0
- package/gs/math/erf.gs.ts +409 -0
- package/gs/math/erfinv.gs.test.ts +104 -0
- package/gs/math/erfinv.gs.ts +169 -0
- package/gs/math/exp.gs.test.ts +82 -0
- package/gs/math/exp.gs.ts +39 -0
- package/gs/math/expm1.gs.test.ts +48 -0
- package/gs/math/expm1.gs.ts +23 -0
- package/gs/math/floor.gs.test.ts +146 -0
- package/gs/math/floor.gs.ts +88 -0
- package/gs/math/fma.gs.test.ts +83 -0
- package/gs/math/fma.gs.ts +7 -0
- package/gs/math/frexp.gs.test.ts +146 -0
- package/gs/math/frexp.gs.ts +37 -0
- package/gs/math/gamma.gs.test.ts +66 -0
- package/gs/math/gamma.gs.ts +158 -0
- package/gs/math/hypot.gs.test.ts +73 -0
- package/gs/math/hypot.gs.ts +23 -0
- package/gs/math/index.ts +44 -0
- package/gs/math/j0.gs.test.ts +74 -0
- package/gs/math/j0.gs.ts +257 -0
- package/gs/math/j1.gs.test.ts +81 -0
- package/gs/math/j1.gs.ts +231 -0
- package/gs/math/jn.gs.test.ts +133 -0
- package/gs/math/jn.gs.ts +447 -0
- package/gs/math/ldexp.gs.test.ts +128 -0
- package/gs/math/ldexp.gs.ts +28 -0
- package/gs/math/lgamma.gs.test.ts +102 -0
- package/gs/math/lgamma.gs.ts +251 -0
- package/gs/math/log.gs.test.ts +40 -0
- package/gs/math/log.gs.ts +21 -0
- package/gs/math/log10.gs.test.ts +80 -0
- package/gs/math/log10.gs.ts +25 -0
- package/gs/math/log1p.gs.test.ts +55 -0
- package/gs/math/log1p.gs.ts +24 -0
- package/gs/math/logb.gs.test.ts +87 -0
- package/gs/math/logb.gs.ts +54 -0
- package/gs/math/mod.gs.test.ts +64 -0
- package/gs/math/mod.gs.ts +36 -0
- package/gs/math/modf.gs.test.ts +80 -0
- package/gs/math/modf.gs.ts +32 -0
- package/gs/math/nextafter.gs.test.ts +107 -0
- package/gs/math/nextafter.gs.ts +71 -0
- package/gs/math/pow.gs.test.ts +103 -0
- package/gs/math/pow.gs.ts +55 -0
- package/gs/math/pow10.gs.test.ts +58 -0
- package/gs/math/pow10.gs.ts +19 -0
- package/gs/math/remainder.gs.test.ts +70 -0
- package/gs/math/remainder.gs.ts +33 -0
- package/gs/math/signbit.gs.test.ts +33 -0
- package/gs/math/signbit.gs.ts +8 -0
- package/gs/math/sin.gs.test.ts +83 -0
- package/gs/math/sin.gs.ts +38 -0
- package/gs/math/sincos.gs.test.ts +91 -0
- package/gs/math/sincos.gs.ts +15 -0
- package/gs/math/sinh.gs.test.ts +66 -0
- package/gs/math/sinh.gs.ts +34 -0
- package/gs/math/sqrt.gs.test.ts +49 -0
- package/gs/math/sqrt.gs.ts +20 -0
- package/gs/math/tan.gs.test.ts +50 -0
- package/gs/math/tan.gs.ts +23 -0
- package/gs/math/tanh.gs.test.ts +52 -0
- package/gs/math/tanh.gs.ts +23 -0
- package/gs/math/trig_reduce.gs.ts +66 -0
- package/gs/math/unsafe.gs.ts +52 -0
- package/gs/strconv/atob.gs.ts +45 -0
- package/gs/strconv/atof.gs.ts +60 -0
- package/gs/strconv/atoi.gs.ts +243 -0
- package/gs/strconv/doc.gs.ts +2 -0
- package/gs/strconv/ftoa.gs.ts +66 -0
- package/gs/strconv/index.ts +6 -0
- package/gs/strconv/itoa.gs.ts +41 -0
- package/gs/strconv/quote.gs.ts +245 -0
- package/gs/strings/index.ts +4 -0
- package/gs/strings/replace.ts +9 -237
- package/gs/strings/search.ts +0 -28
- package/gs/sync/atomic/doc.gs.ts +276 -0
- package/gs/sync/atomic/doc_64.gs.ts +168 -0
- package/gs/sync/atomic/index.ts +4 -0
- package/gs/sync/atomic/type.gs.ts +596 -0
- package/gs/sync/atomic/value.gs.ts +158 -0
- package/gs/unsafe/unsafe.ts +6 -0
- package/package.json +1 -1
- package/gs/maps/iter.gs.ts +0 -71
- package/gs/maps/maps.gs.ts +0 -87
- package/gs/stringslite/godoc.txt +0 -17
- package/gs/stringslite/index.ts +0 -1
- package/gs/stringslite/strings.ts +0 -82
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { J0, Y0 } from './j0.gs.js'
|
|
3
|
+
|
|
4
|
+
describe('J0', () => {
|
|
5
|
+
it('should return correct values for zero and small inputs', () => {
|
|
6
|
+
expect(J0(0)).toBe(1)
|
|
7
|
+
expect(J0(1)).toBeCloseTo(0.7651976865579666, 14)
|
|
8
|
+
expect(J0(2)).toBeCloseTo(0.22389077914123567, 6)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return correct values for various inputs', () => {
|
|
12
|
+
expect(J0(3)).toBeCloseTo(-0.2600519549019335, 5)
|
|
13
|
+
expect(J0(5)).toBeCloseTo(-0.1775967713143383, 6)
|
|
14
|
+
expect(J0(10)).toBeCloseTo(-0.24593576445134832, 5)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should be an even function', () => {
|
|
18
|
+
const testValues = [1, 2, 3, 5, 10]
|
|
19
|
+
for (const x of testValues) {
|
|
20
|
+
expect(J0(-x)).toBeCloseTo(J0(x), 14)
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should handle special values', () => {
|
|
25
|
+
expect(J0(Number.POSITIVE_INFINITY)).toBe(0)
|
|
26
|
+
expect(J0(Number.NEGATIVE_INFINITY)).toBe(0)
|
|
27
|
+
expect(Number.isNaN(J0(Number.NaN))).toBe(true)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('should oscillate for large values', () => {
|
|
31
|
+
// J0 should oscillate around 0 for large x
|
|
32
|
+
const largeValues = [20, 30, 50, 100]
|
|
33
|
+
for (const x of largeValues) {
|
|
34
|
+
expect(Math.abs(J0(x))).toBeLessThan(0.5)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
describe('Y0', () => {
|
|
40
|
+
it('should return correct values for positive inputs', () => {
|
|
41
|
+
expect(Y0(1)).toBeCloseTo(0.08825696421567696, 14)
|
|
42
|
+
expect(Y0(2)).toBeCloseTo(0.5103756726497451, 5)
|
|
43
|
+
expect(Y0(5)).toBeCloseTo(-0.3085176252490338, 5)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should approach negative infinity as x approaches 0 from positive side', () => {
|
|
47
|
+
expect(Y0(0.001)).toBeLessThan(-4)
|
|
48
|
+
expect(Y0(0.0001)).toBeLessThan(-5)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should handle special values', () => {
|
|
52
|
+
expect(Y0(Number.POSITIVE_INFINITY)).toBe(0)
|
|
53
|
+
// Check if Y0(0) returns a very large negative number or -Infinity
|
|
54
|
+
const y0_zero = Y0(0)
|
|
55
|
+
expect(y0_zero < -1e10 || y0_zero === Number.NEGATIVE_INFINITY || Number.isNaN(y0_zero)).toBe(true)
|
|
56
|
+
expect(Number.isNaN(Y0(-1))).toBe(true)
|
|
57
|
+
expect(Number.isNaN(Y0(Number.NEGATIVE_INFINITY))).toBe(true)
|
|
58
|
+
expect(Number.isNaN(Y0(Number.NaN))).toBe(true)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should oscillate for large values', () => {
|
|
62
|
+
// Y0 should oscillate around 0 for large x
|
|
63
|
+
const largeValues = [20, 30, 50, 100]
|
|
64
|
+
for (const x of largeValues) {
|
|
65
|
+
expect(Math.abs(Y0(x))).toBeLessThan(0.5)
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('should be undefined for negative values', () => {
|
|
70
|
+
expect(Number.isNaN(Y0(-1))).toBe(true)
|
|
71
|
+
expect(Number.isNaN(Y0(-5))).toBe(true)
|
|
72
|
+
expect(Number.isNaN(Y0(-10))).toBe(true)
|
|
73
|
+
})
|
|
74
|
+
})
|
package/gs/math/j0.gs.ts
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import * as $ from "@goscript/builtin/builtin.js";
|
|
2
|
+
import { Abs } from "./abs.gs.js";
|
|
3
|
+
import { Inf, IsInf, IsNaN, NaN } from "./bits.gs.js";
|
|
4
|
+
import { Log } from "./log.gs.js";
|
|
5
|
+
import { Cos } from "./sin.gs.js";
|
|
6
|
+
import { Sincos } from "./sincos.gs.js";
|
|
7
|
+
import { Sqrt } from "./sqrt.gs.js";
|
|
8
|
+
|
|
9
|
+
// J0 returns the order-zero Bessel function of the first kind.
|
|
10
|
+
//
|
|
11
|
+
// Special cases are:
|
|
12
|
+
//
|
|
13
|
+
// J0(±Inf) = 0
|
|
14
|
+
// J0(0) = 1
|
|
15
|
+
// J0(NaN) = NaN
|
|
16
|
+
export function J0(x: number): number {
|
|
17
|
+
// Constants
|
|
18
|
+
const TwoM27: number = 1.0 / (1 << 27); // 2**-27
|
|
19
|
+
const TwoM13: number = 1.0 / (1 << 13); // 2**-13
|
|
20
|
+
const Two129: number = Math.pow(2, 129); // 2**129
|
|
21
|
+
|
|
22
|
+
// R0/S0 coefficients on [0, 2]
|
|
23
|
+
const R02: number = 1.56249999999999947958e-02;
|
|
24
|
+
const R03: number = -1.89979294238854721751e-04;
|
|
25
|
+
const R04: number = 1.82954049532700665670e-06;
|
|
26
|
+
const R05: number = -4.61832688532103189199e-09;
|
|
27
|
+
const S01: number = 1.56191029464890010492e-02;
|
|
28
|
+
const S02: number = 1.16926784663337450260e-04;
|
|
29
|
+
const S03: number = 5.13546550207318111446e-07;
|
|
30
|
+
const S04: number = 1.16614003333790000205e-09;
|
|
31
|
+
|
|
32
|
+
// special cases
|
|
33
|
+
if (IsNaN(x)) {
|
|
34
|
+
return x;
|
|
35
|
+
}
|
|
36
|
+
if (IsInf(x, 0)) {
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
if (x == 0) {
|
|
40
|
+
return 1;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
x = Abs(x);
|
|
44
|
+
|
|
45
|
+
// |x| >= 2.0
|
|
46
|
+
if (x >= 2) {
|
|
47
|
+
const [s, c] = Sincos(x);
|
|
48
|
+
let ss = s - c;
|
|
49
|
+
let cc = s + c;
|
|
50
|
+
|
|
51
|
+
// make sure x+x does not overflow
|
|
52
|
+
if (x < 1.79769e+308 / 2) {
|
|
53
|
+
const z = -Cos(x + x);
|
|
54
|
+
if (s * c < 0) {
|
|
55
|
+
cc = z / ss;
|
|
56
|
+
} else {
|
|
57
|
+
ss = z / cc;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let z: number;
|
|
62
|
+
// |x| > ~6.8056e+38
|
|
63
|
+
if (x > 680564733841876926926749214863536422912) {
|
|
64
|
+
z = (1 / 1.77245) * cc / Sqrt(x);
|
|
65
|
+
} else {
|
|
66
|
+
const u = pzero(x);
|
|
67
|
+
const v = qzero(x);
|
|
68
|
+
z = (1 / 1.77245) * (u * cc - v * ss) / Sqrt(x);
|
|
69
|
+
}
|
|
70
|
+
return z;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// |x| < ~1.2207e-4
|
|
74
|
+
if (x < 0.00012207) {
|
|
75
|
+
// |x| < ~7.4506e-9
|
|
76
|
+
if (x < 7.45058e-09) {
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
return 1 - 0.25 * x * x;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const z = x * x;
|
|
83
|
+
const r = z * (R02 + z * (R03 + z * (R04 + z * R05)));
|
|
84
|
+
const s = 1 + z * (S01 + z * (S02 + z * (S03 + z * S04)));
|
|
85
|
+
|
|
86
|
+
// |x| < 1.00
|
|
87
|
+
if (x < 1) {
|
|
88
|
+
return 1 + z * (-0.25 + (r / s));
|
|
89
|
+
}
|
|
90
|
+
const u = 0.5 * x;
|
|
91
|
+
return (1 + u) * (1 - u) + z * (r / s);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Y0 returns the order-zero Bessel function of the second kind.
|
|
95
|
+
//
|
|
96
|
+
// Special cases are:
|
|
97
|
+
//
|
|
98
|
+
// Y0(+Inf) = 0
|
|
99
|
+
// Y0(0) = -Inf
|
|
100
|
+
// Y0(x < 0) = NaN
|
|
101
|
+
// Y0(NaN) = NaN
|
|
102
|
+
export function Y0(x: number): number {
|
|
103
|
+
// Constants
|
|
104
|
+
const TwoM27: number = 1.0 / (1 << 27); // 2**-27
|
|
105
|
+
const Two129: number = Math.pow(2, 129); // 2**129
|
|
106
|
+
|
|
107
|
+
// U coefficients
|
|
108
|
+
const U00: number = -7.38042951086872317523e-02;
|
|
109
|
+
const U01: number = 1.76666452509181115538e-01;
|
|
110
|
+
const U02: number = -1.38185671945596898896e-02;
|
|
111
|
+
const U03: number = 3.47453432093683650238e-04;
|
|
112
|
+
const U04: number = -3.81407053724364161125e-06;
|
|
113
|
+
const U05: number = 1.95590137035022920206e-08;
|
|
114
|
+
const U06: number = -3.98205194132103398453e-11;
|
|
115
|
+
|
|
116
|
+
// V coefficients
|
|
117
|
+
const V01: number = 1.27304834834123699328e-02;
|
|
118
|
+
const V02: number = 7.60068627350353253702e-05;
|
|
119
|
+
const V03: number = 2.59150851840457805467e-07;
|
|
120
|
+
const V04: number = 4.41110311332675467403e-10;
|
|
121
|
+
|
|
122
|
+
// special cases
|
|
123
|
+
if (x < 0 || IsNaN(x)) {
|
|
124
|
+
return NaN();
|
|
125
|
+
}
|
|
126
|
+
if (IsInf(x, 1)) {
|
|
127
|
+
return 0;
|
|
128
|
+
}
|
|
129
|
+
if (x == 0) {
|
|
130
|
+
return Inf(-1);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// |x| >= 2.0
|
|
134
|
+
if (x >= 2) {
|
|
135
|
+
const [s, c] = Sincos(x);
|
|
136
|
+
let ss = s - c;
|
|
137
|
+
let cc = s + c;
|
|
138
|
+
|
|
139
|
+
// make sure x+x does not overflow
|
|
140
|
+
if (x < 1.79769e+308 / 2) {
|
|
141
|
+
const z = -Cos(x + x);
|
|
142
|
+
if (s * c < 0) {
|
|
143
|
+
cc = z / ss;
|
|
144
|
+
} else {
|
|
145
|
+
ss = z / cc;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let z: number;
|
|
150
|
+
// |x| > ~6.8056e+38
|
|
151
|
+
if (x > 680564733841876926926749214863536422912) {
|
|
152
|
+
z = (1 / 1.77245) * ss / Sqrt(x);
|
|
153
|
+
} else {
|
|
154
|
+
const u = pzero(x);
|
|
155
|
+
const v = qzero(x);
|
|
156
|
+
z = (1 / 1.77245) * (u * ss + v * cc) / Sqrt(x);
|
|
157
|
+
}
|
|
158
|
+
return z;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// |x| < ~7.4506e-9
|
|
162
|
+
if (x <= 7.45058e-09) {
|
|
163
|
+
return U00 + (2 / Math.PI) * Log(x);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const z = x * x;
|
|
167
|
+
const u = U00 + z * (U01 + z * (U02 + z * (U03 + z * (U04 + z * (U05 + z * U06)))));
|
|
168
|
+
const v = 1 + z * (V01 + z * (V02 + z * (V03 + z * V04)));
|
|
169
|
+
return u / v + (2 / Math.PI) * J0(x) * Log(x);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const p0R8: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([0.00000000000000000000e+00, -7.03124999999900357484e-02, -8.08167041275349795626e+00, -2.57063105679704847262e+02, -2.48521641009428822144e+03, -5.25304380490729545272e+03]));
|
|
173
|
+
|
|
174
|
+
const p0S8: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([1.16534364619668181717e+02, 3.83374475364121826715e+03, 4.05978572648472545552e+04, 1.16752972564375915681e+05, 4.76277284146730962675e+04]));
|
|
175
|
+
|
|
176
|
+
const p0R5: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([-1.14125464691894502584e-11, -7.03124940873599280078e-02, -4.15961064470587782438e+00, -6.76747652265167261021e+01, -3.31231299649172967747e+02, -3.46433388365604912451e+02]));
|
|
177
|
+
|
|
178
|
+
const p0S5: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([6.07539382692300335975e+01, 1.05125230595704579173e+03, 5.97897094333855784498e+03, 9.62544514357774460223e+03, 2.40605815922939109441e+03]));
|
|
179
|
+
|
|
180
|
+
const p0R3: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([-2.54704601771951915620e-09, -7.03119616381481654654e-02, -2.40903221549529611423e+00, -2.19659774734883086467e+01, -5.80791704701737572236e+01, -3.14479470594888503854e+01]));
|
|
181
|
+
|
|
182
|
+
const p0S3: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([3.58560338055209726349e+01, 3.61513983050303863820e+02, 1.19360783792111533330e+03, 1.12799679856907414432e+03, 1.73580930813335754692e+02]));
|
|
183
|
+
|
|
184
|
+
const p0R2: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([-8.87534333032526411254e-08, -7.03030995483624743247e-02, -1.45073846780952986357e+00, -7.63569613823527770791e+00, -1.11931668860356747786e+01, -3.23364579351335335033e+00]));
|
|
185
|
+
|
|
186
|
+
const p0S2: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([2.22202997532088808441e+01, 1.36206794218215208048e+02, 2.70470278658083486789e+02, 1.53875394208320329881e+02, 1.46576176948256193810e+01]));
|
|
187
|
+
|
|
188
|
+
export function pzero(x: number): number {
|
|
189
|
+
let p: $.VarRef<$.Slice<number>>;
|
|
190
|
+
let q: $.VarRef<$.Slice<number>>;
|
|
191
|
+
|
|
192
|
+
if (x >= 8) {
|
|
193
|
+
p = p0R8;
|
|
194
|
+
q = p0S8;
|
|
195
|
+
} else if (x >= 4.5454) {
|
|
196
|
+
p = p0R5;
|
|
197
|
+
q = p0S5;
|
|
198
|
+
} else if (x >= 2.8571) {
|
|
199
|
+
p = p0R3;
|
|
200
|
+
q = p0S3;
|
|
201
|
+
} else if (x >= 2) {
|
|
202
|
+
p = p0R2;
|
|
203
|
+
q = p0S2;
|
|
204
|
+
} else {
|
|
205
|
+
// This should not happen based on the calling code, but we need to handle it
|
|
206
|
+
throw new Error("pzero: x must be >= 2");
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const z = 1 / (x * x);
|
|
210
|
+
const r = p.value![0] + z * (p.value![1] + z * (p.value![2] + z * (p.value![3] + z * (p.value![4] + z * p.value![5]))));
|
|
211
|
+
const s = 1 + z * (q.value![0] + z * (q.value![1] + z * (q.value![2] + z * (q.value![3] + z * q.value![4]))));
|
|
212
|
+
return 1 + r / s;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const q0R8: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([0.00000000000000000000e+00, 7.32421874999935051953e-02, 1.17682064682252693899e+01, 5.57673380256401856059e+02, 8.85919720756468632317e+03, 3.70146267776887834771e+04]));
|
|
216
|
+
|
|
217
|
+
const q0S8: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([1.63776026895689824414e+02, 8.09834494656449805916e+03, 1.42538291419120476348e+05, 8.03309257119514397345e+05, 8.40501579819060512818e+05, -3.43899293537866615225e+05]));
|
|
218
|
+
|
|
219
|
+
const q0R5: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([1.84085963594515531381e-11, 7.32421766612684765896e-02, 5.83563508962056953777e+00, 1.35111577286449829671e+02, 1.02724376596164097464e+03, 1.98997785864605384631e+03]));
|
|
220
|
+
|
|
221
|
+
const q0S5: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([8.27766102236537761883e+01, 2.07781416421392987104e+03, 1.88472887785718085070e+04, 5.67511122894947329769e+04, 3.59767538425114471465e+04, -5.35434275601944773371e+03]));
|
|
222
|
+
|
|
223
|
+
const q0R3: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([4.37741014089738620906e-09, 7.32411180042911447163e-02, 3.34423137516170720929e+00, 4.26218440745412650017e+01, 1.70808091340565596283e+02, 1.66733948696651168575e+02]));
|
|
224
|
+
|
|
225
|
+
const q0S3: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([4.87588729724587182091e+01, 7.09689221056606015736e+02, 3.70414822620111362994e+03, 6.46042516752568917582e+03, 2.51633368920368957333e+03, -1.49247451836156386662e+02]));
|
|
226
|
+
|
|
227
|
+
const q0R2: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([1.50444444886983272379e-07, 7.32234265963079278272e-02, 1.99819174093815998816e+00, 1.44956029347885735348e+01, 3.16662317504781540833e+01, 1.62527075710929267416e+01]));
|
|
228
|
+
|
|
229
|
+
const q0S2: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([3.03655848355219184498e+01, 2.69348118608049844624e+02, 8.44783757595320139444e+02, 8.82935845112488550512e+02, 2.12666388511798828631e+02, -5.31095493882666946917e+00]));
|
|
230
|
+
|
|
231
|
+
export function qzero(x: number): number {
|
|
232
|
+
let p: $.VarRef<$.Slice<number>>;
|
|
233
|
+
let q: $.VarRef<$.Slice<number>>;
|
|
234
|
+
|
|
235
|
+
if (x >= 8) {
|
|
236
|
+
p = q0R8;
|
|
237
|
+
q = q0S8;
|
|
238
|
+
} else if (x >= 4.5454) {
|
|
239
|
+
p = q0R5;
|
|
240
|
+
q = q0S5;
|
|
241
|
+
} else if (x >= 2.8571) {
|
|
242
|
+
p = q0R3;
|
|
243
|
+
q = q0S3;
|
|
244
|
+
} else if (x >= 2) {
|
|
245
|
+
p = q0R2;
|
|
246
|
+
q = q0S2;
|
|
247
|
+
} else {
|
|
248
|
+
// This should not happen based on the calling code, but we need to handle it
|
|
249
|
+
throw new Error("qzero: x must be >= 2");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const z = 1 / (x * x);
|
|
253
|
+
const r = p.value![0] + z * (p.value![1] + z * (p.value![2] + z * (p.value![3] + z * (p.value![4] + z * p.value![5]))));
|
|
254
|
+
const s = 1 + z * (q.value![0] + z * (q.value![1] + z * (q.value![2] + z * (q.value![3] + z * (q.value![4] + z * q.value![5])))));
|
|
255
|
+
return (-0.125 + r / s) / x;
|
|
256
|
+
}
|
|
257
|
+
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { J1, Y1 } from './j1.gs.js'
|
|
3
|
+
|
|
4
|
+
describe('J1', () => {
|
|
5
|
+
it('should return correct values for zero and small inputs', () => {
|
|
6
|
+
expect(J1(0)).toBe(0)
|
|
7
|
+
expect(J1(1)).toBeCloseTo(0.4400505857449335, 7)
|
|
8
|
+
expect(J1(2)).toBeCloseTo(0.5767248077568733, 5)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return correct values for various inputs', () => {
|
|
12
|
+
expect(J1(3)).toBeCloseTo(0.33905895852593637, 5)
|
|
13
|
+
expect(J1(5)).toBeCloseTo(-0.32757913759146523, 5)
|
|
14
|
+
expect(J1(10)).toBeCloseTo(0.043472746168861424, 6)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should be an odd function', () => {
|
|
18
|
+
const testValues = [1, 2, 3, 5, 10]
|
|
19
|
+
for (const x of testValues) {
|
|
20
|
+
expect(J1(-x)).toBeCloseTo(-J1(x), 14)
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should handle special values', () => {
|
|
25
|
+
expect(J1(Number.POSITIVE_INFINITY)).toBe(0)
|
|
26
|
+
expect(J1(Number.NEGATIVE_INFINITY)).toBe(0)
|
|
27
|
+
expect(Number.isNaN(J1(Number.NaN))).toBe(true)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('should oscillate for large values', () => {
|
|
31
|
+
// J1 should oscillate around 0 for large x
|
|
32
|
+
const largeValues = [20, 30, 50, 100]
|
|
33
|
+
for (const x of largeValues) {
|
|
34
|
+
expect(Math.abs(J1(x))).toBeLessThan(0.5)
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should approach x/2 for small x', () => {
|
|
39
|
+
const smallValues = [0.01, 0.001, 0.0001]
|
|
40
|
+
for (const x of smallValues) {
|
|
41
|
+
expect(J1(x)).toBeCloseTo(x / 2, 6)
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe('Y1', () => {
|
|
47
|
+
it('should return correct values for positive inputs', () => {
|
|
48
|
+
expect(Y1(1)).toBeCloseTo(-0.7812128213002887, 6)
|
|
49
|
+
expect(Y1(2)).toBeCloseTo(-0.10703243154093754, 6)
|
|
50
|
+
expect(Y1(5)).toBeCloseTo(0.1478631433912268, 6)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('should approach negative infinity as x approaches 0 from positive side', () => {
|
|
54
|
+
expect(Y1(0.001)).toBeLessThan(-300)
|
|
55
|
+
expect(Y1(0.0001)).toBeLessThan(-3000)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('should handle special values', () => {
|
|
59
|
+
expect(Y1(Number.POSITIVE_INFINITY)).toBe(0)
|
|
60
|
+
// Check if Y1(0) returns a very large negative number or -Infinity
|
|
61
|
+
const y1_zero = Y1(0)
|
|
62
|
+
expect(y1_zero < -1e10 || y1_zero === Number.NEGATIVE_INFINITY || Number.isNaN(y1_zero)).toBe(true)
|
|
63
|
+
expect(Number.isNaN(Y1(-1))).toBe(true)
|
|
64
|
+
expect(Number.isNaN(Y1(Number.NEGATIVE_INFINITY))).toBe(true)
|
|
65
|
+
expect(Number.isNaN(Y1(Number.NaN))).toBe(true)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('should oscillate for large values', () => {
|
|
69
|
+
// Y1 should oscillate around 0 for large x
|
|
70
|
+
const largeValues = [20, 30, 50, 100]
|
|
71
|
+
for (const x of largeValues) {
|
|
72
|
+
expect(Math.abs(Y1(x))).toBeLessThan(0.5)
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('should be undefined for negative values', () => {
|
|
77
|
+
expect(Number.isNaN(Y1(-1))).toBe(true)
|
|
78
|
+
expect(Number.isNaN(Y1(-5))).toBe(true)
|
|
79
|
+
expect(Number.isNaN(Y1(-10))).toBe(true)
|
|
80
|
+
})
|
|
81
|
+
})
|
package/gs/math/j1.gs.ts
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import * as $ from "@goscript/builtin/builtin.js";
|
|
2
|
+
import { Inf, IsInf, IsNaN, NaN } from "./bits.gs.js";
|
|
3
|
+
import { Log } from "./log.gs.js";
|
|
4
|
+
import { Cos } from "./sin.gs.js";
|
|
5
|
+
import { Sincos } from "./sincos.gs.js";
|
|
6
|
+
import { Sqrt } from "./sqrt.gs.js";
|
|
7
|
+
|
|
8
|
+
// J1 returns the order-one Bessel function of the first kind.
|
|
9
|
+
//
|
|
10
|
+
// Special cases are:
|
|
11
|
+
//
|
|
12
|
+
// J1(±Inf) = 0
|
|
13
|
+
// J1(NaN) = NaN
|
|
14
|
+
export function J1(x: number): number {
|
|
15
|
+
// special cases
|
|
16
|
+
switch (true) {
|
|
17
|
+
case IsNaN(x):
|
|
18
|
+
return x;
|
|
19
|
+
case IsInf(x, 0) || x == 0:
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let sign = false;
|
|
24
|
+
if (x < 0) {
|
|
25
|
+
x = -x;
|
|
26
|
+
sign = true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// j1(x) = 1/sqrt(pi) * (P(1,x)*cc - Q(1,x)*ss) / sqrt(x)
|
|
30
|
+
// y1(x) = 1/sqrt(pi) * (P(1,x)*ss + Q(1,x)*cc) / sqrt(x)
|
|
31
|
+
if (x >= 2) {
|
|
32
|
+
let [s, c] = Sincos(x);
|
|
33
|
+
let ss = -s - c;
|
|
34
|
+
let cc = s - c;
|
|
35
|
+
|
|
36
|
+
// make sure x+x does not overflow
|
|
37
|
+
if (x < 1.79769e+308 / 2) {
|
|
38
|
+
let z = Cos(x + x);
|
|
39
|
+
if (s * c > 0) {
|
|
40
|
+
cc = z / ss;
|
|
41
|
+
} else {
|
|
42
|
+
ss = z / cc;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// j1(x) = 1/sqrt(pi) * (P(1,x)*cc - Q(1,x)*ss) / sqrt(x)
|
|
47
|
+
// y1(x) = 1/sqrt(pi) * (P(1,x)*ss + Q(1,x)*cc) / sqrt(x)
|
|
48
|
+
|
|
49
|
+
let z: number = 0;
|
|
50
|
+
if (x > 680564733841876926926749214863536422912) {
|
|
51
|
+
z = (1 / 1.77245) * cc / Sqrt(x);
|
|
52
|
+
} else {
|
|
53
|
+
let u = pone(x);
|
|
54
|
+
let v = qone(x);
|
|
55
|
+
z = (1 / 1.77245) * (u * cc - v * ss) / Sqrt(x);
|
|
56
|
+
}
|
|
57
|
+
if (sign) {
|
|
58
|
+
return -z;
|
|
59
|
+
}
|
|
60
|
+
return z;
|
|
61
|
+
}
|
|
62
|
+
// |x|<2**-27
|
|
63
|
+
// inexact if x!=0 necessary
|
|
64
|
+
if (x < 7.45058e-09) {
|
|
65
|
+
// |x|<2**-27
|
|
66
|
+
return 0.5 * x;
|
|
67
|
+
}
|
|
68
|
+
let z = x * x;
|
|
69
|
+
let r = z * (-0.0625 + z * (0.00140706 + z * (-1.59956e-05 + z * 4.96728e-08)));
|
|
70
|
+
let s = 1.0 + z * (0.0191538 + z * (0.000185947 + z * (1.17718e-06 + z * (5.04636e-09 + z * 1.23542e-11))));
|
|
71
|
+
r *= x;
|
|
72
|
+
z = 0.5 * x + r / s;
|
|
73
|
+
if (sign) {
|
|
74
|
+
return -z;
|
|
75
|
+
}
|
|
76
|
+
return z;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Y1 returns the order-one Bessel function of the second kind.
|
|
80
|
+
//
|
|
81
|
+
// Special cases are:
|
|
82
|
+
//
|
|
83
|
+
// Y1(+Inf) = 0
|
|
84
|
+
// Y1(0) = -Inf
|
|
85
|
+
// Y1(x < 0) = NaN
|
|
86
|
+
// Y1(NaN) = NaN
|
|
87
|
+
export function Y1(x: number): number {
|
|
88
|
+
// special cases
|
|
89
|
+
switch (true) {
|
|
90
|
+
case x < 0 || IsNaN(x):
|
|
91
|
+
return NaN();
|
|
92
|
+
case IsInf(x, 1):
|
|
93
|
+
return 0;
|
|
94
|
+
case x == 0:
|
|
95
|
+
return Inf(-1);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x0)+q1(x)*cos(x0))
|
|
99
|
+
// where x0 = x-3pi/4
|
|
100
|
+
// Better formula:
|
|
101
|
+
// cos(x0) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4)
|
|
102
|
+
// = 1/sqrt(2) * (sin(x) - cos(x))
|
|
103
|
+
// sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
|
|
104
|
+
// = -1/sqrt(2) * (cos(x) + sin(x))
|
|
105
|
+
// To avoid cancellation, use
|
|
106
|
+
// sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
|
|
107
|
+
// to compute the worse one.
|
|
108
|
+
if (x >= 2) {
|
|
109
|
+
let [s, c] = Sincos(x);
|
|
110
|
+
let ss = -s - c;
|
|
111
|
+
let cc = s - c;
|
|
112
|
+
|
|
113
|
+
// make sure x+x does not overflow
|
|
114
|
+
if (x < 1.79769e+308 / 2) {
|
|
115
|
+
let z = Cos(x + x);
|
|
116
|
+
if (s * c > 0) {
|
|
117
|
+
cc = z / ss;
|
|
118
|
+
} else {
|
|
119
|
+
ss = z / cc;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let z: number = 0;
|
|
124
|
+
if (x > 680564733841876926926749214863536422912) {
|
|
125
|
+
z = (1 / 1.77245) * ss / Sqrt(x);
|
|
126
|
+
} else {
|
|
127
|
+
let u = pone(x);
|
|
128
|
+
let v = qone(x);
|
|
129
|
+
z = (1 / 1.77245) * (u * ss + v * cc) / Sqrt(x);
|
|
130
|
+
}
|
|
131
|
+
return z;
|
|
132
|
+
}
|
|
133
|
+
// x < 2**-54
|
|
134
|
+
if (x <= 5.55112e-17) {
|
|
135
|
+
// x < 2**-54
|
|
136
|
+
return -(2 / 3.14159) / x;
|
|
137
|
+
}
|
|
138
|
+
let z = x * x;
|
|
139
|
+
let u = -0.196057 + z * (0.0504439 + z * (-0.00191257 + z * (2.35253e-05 + z * -9.19099e-08)));
|
|
140
|
+
let v = 1 + z * (0.0199167 + z * (0.000202553 + z * (1.35609e-06 + z * (6.22741e-09 + z * 1.66559e-11))));
|
|
141
|
+
return x * (u / v) + (2 / 3.14159) * (J1(x) * Log(x) - 1 / x);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let p1R8: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([0.00000000000000000000e+00, 1.17187499999988647970e-01, 1.32394806593073575129e+01, 4.12051854307378562225e+02, 3.87474538913960532227e+03, 7.91447954031891731574e+03]));
|
|
145
|
+
|
|
146
|
+
let p1S8: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([1.14207370375678408436e+02, 3.65093083420853463394e+03, 3.69562060269033463555e+04, 9.76027935934950801311e+04, 3.08042720627888811578e+04]));
|
|
147
|
+
|
|
148
|
+
let p1R5: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([1.31990519556243522749e-11, 1.17187493190614097638e-01, 6.80275127868432871736e+00, 1.08308182990189109773e+02, 5.17636139533199752805e+02, 5.28715201363337541807e+02]));
|
|
149
|
+
|
|
150
|
+
let p1S5: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([5.92805987221131331921e+01, 9.91401418733614377743e+02, 5.35326695291487976647e+03, 7.84469031749551231769e+03, 1.50404688810361062679e+03]));
|
|
151
|
+
|
|
152
|
+
let p1R3: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([3.02503916137373618024e-09, 1.17186865567253592491e-01, 3.93297750033315640650e+00, 3.51194035591636932736e+01, 9.10550110750781271918e+01, 4.85590685197364919645e+01]));
|
|
153
|
+
|
|
154
|
+
let p1S3: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([3.47913095001251519989e+01, 3.36762458747825746741e+02, 1.04687139975775130551e+03, 8.90811346398256432622e+02, 1.03787932439639277504e+02]));
|
|
155
|
+
|
|
156
|
+
let p1R2: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([1.07710830106873743082e-07, 1.17176219462683348094e-01, 2.36851496667608785174e+00, 1.22426109148261232917e+01, 1.76939711271687727390e+01, 5.07352312588818499250e+00]));
|
|
157
|
+
|
|
158
|
+
let p1S2: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([2.14364859363821409488e+01, 1.25290227168402751090e+02, 2.32276469057162813669e+02, 1.17679373287147100768e+02, 8.36463893371618283368e+00]));
|
|
159
|
+
|
|
160
|
+
export function pone(x: number): number {
|
|
161
|
+
let p: $.VarRef<$.Slice<number>>;
|
|
162
|
+
let q: $.VarRef<$.Slice<number>>;
|
|
163
|
+
|
|
164
|
+
if (x >= 8) {
|
|
165
|
+
p = p1R8;
|
|
166
|
+
q = p1S8;
|
|
167
|
+
} else if (x >= 4.5454) {
|
|
168
|
+
p = p1R5;
|
|
169
|
+
q = p1S5;
|
|
170
|
+
} else if (x >= 2.8571) {
|
|
171
|
+
p = p1R3;
|
|
172
|
+
q = p1S3;
|
|
173
|
+
} else if (x >= 2) {
|
|
174
|
+
p = p1R2;
|
|
175
|
+
q = p1S2;
|
|
176
|
+
} else {
|
|
177
|
+
// Default case to ensure p and q are always assigned
|
|
178
|
+
p = p1R2;
|
|
179
|
+
q = p1S2;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let z = 1 / (x * x);
|
|
183
|
+
let r = p.value![0] + z * (p.value![1] + z * (p.value![2] + z * (p.value![3] + z * (p.value![4] + z * p.value![5]))));
|
|
184
|
+
let s = 1.0 + z * (q.value![0] + z * (q.value![1] + z * (q.value![2] + z * (q.value![3] + z * q.value![4]))));
|
|
185
|
+
return 1 + r / s;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
let q1R8: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([0.00000000000000000000e+00, -1.02539062499992714161e-01, -1.62717534544589987888e+01, -7.59601722513950107896e+02, -1.18498066702429587167e+04, -4.84385124285750353010e+04]));
|
|
189
|
+
|
|
190
|
+
let q1S8: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([1.61395369700722909556e+02, 7.82538599923348465381e+03, 1.33875336287249578163e+05, 7.19657723683240939863e+05, 6.66601232617776375264e+05, -2.94490264303834643215e+05]));
|
|
191
|
+
|
|
192
|
+
let q1R5: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([-2.08979931141764104297e-11, -1.02539050241375426231e-01, -8.05644828123936029840e+00, -1.83669607474888380239e+02, -1.37319376065508163265e+03, -2.61244440453215656817e+03]));
|
|
193
|
+
|
|
194
|
+
let q1S5: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([8.12765501384335777857e+01, 1.99179873460485964642e+03, 1.74684851924908907677e+04, 4.98514270910352279316e+04, 2.79480751638918118260e+04, -4.71918354795128470869e+03]));
|
|
195
|
+
|
|
196
|
+
let q1R3: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([-5.07831226461766561369e-09, -1.02537829820837089745e-01, -4.61011581139473403113e+00, -5.78472216562783643212e+01, -2.28244540737631695038e+02, -2.19210128478909325622e+02]));
|
|
197
|
+
|
|
198
|
+
let q1S3: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([4.76651550323729509273e+01, 6.73865112676699709482e+02, 3.38015286679526343505e+03, 5.54772909720722782367e+03, 1.90311919338810798763e+03, -1.35201191444307340817e+02]));
|
|
199
|
+
|
|
200
|
+
let q1R2: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([-1.78381727510958865572e-07, -1.02517042607985553460e-01, -2.75220568278187460720e+00, -1.96636162643703720221e+01, -4.23253133372830490089e+01, -2.13719211703704061733e+01]));
|
|
201
|
+
|
|
202
|
+
let q1S2: $.VarRef<$.Slice<number>> = $.varRef($.arrayToSlice<number>([2.95333629060523854548e+01, 2.52981549982190529136e+02, 7.57502834868645436472e+02, 7.39393205320467245656e+02, 1.55949003336666123687e+02, -4.95949898822628210127e+00]));
|
|
203
|
+
|
|
204
|
+
export function qone(x: number): number {
|
|
205
|
+
let p: $.VarRef<$.Slice<number>>;
|
|
206
|
+
let q: $.VarRef<$.Slice<number>>;
|
|
207
|
+
|
|
208
|
+
if (x >= 8) {
|
|
209
|
+
p = q1R8;
|
|
210
|
+
q = q1S8;
|
|
211
|
+
} else if (x >= 4.5454) {
|
|
212
|
+
p = q1R5;
|
|
213
|
+
q = q1S5;
|
|
214
|
+
} else if (x >= 2.8571) {
|
|
215
|
+
p = q1R3;
|
|
216
|
+
q = q1S3;
|
|
217
|
+
} else if (x >= 2) {
|
|
218
|
+
p = q1R2;
|
|
219
|
+
q = q1S2;
|
|
220
|
+
} else {
|
|
221
|
+
// Default case to ensure p and q are always assigned
|
|
222
|
+
p = q1R2;
|
|
223
|
+
q = q1S2;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let z = 1 / (x * x);
|
|
227
|
+
let r = p.value![0] + z * (p.value![1] + z * (p.value![2] + z * (p.value![3] + z * (p.value![4] + z * p.value![5]))));
|
|
228
|
+
let s = 1 + z * (q.value![0] + z * (q.value![1] + z * (q.value![2] + z * (q.value![3] + z * (q.value![4] + z * q.value![5])))));
|
|
229
|
+
return (0.375 + r / s) / x;
|
|
230
|
+
}
|
|
231
|
+
|