From 19b1f73aa7eec9f0d8834889661b2e98ebc64041 Mon Sep 17 00:00:00 2001 From: Stefan Israelsson Tampe Date: Mon, 9 Apr 2018 14:50:28 +0200 Subject: complex math --- modules/language/python/module/cmath.scm | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 modules/language/python/module/cmath.scm (limited to 'modules/language/python/module/cmath.scm') diff --git a/modules/language/python/module/cmath.scm b/modules/language/python/module/cmath.scm new file mode 100644 index 0000000..b02be2d --- /dev/null +++ b/modules/language/python/module/cmath.scm @@ -0,0 +1,63 @@ +(define-module (language python module cmath) + #:export (polar rect exp log log10 sqrt pi e tau inf nan infj nanj isclose + isnan isinf isfinite sin cos tan asin acos atan + sinh cosh tanh asinh acosh atanh)) + +(define (polar x) + (list (magnitude x) (angle x))) + +(define (rect r phi) + (make-polar r phi)) + +;; pow log + +(define exp (@ (guile) exp)) +(define log (@ (guile) log)) +(define log10 (@ (guile) log10)) +(define sqrt (@ (guile) sqrt)) + +;; trig +(define sin (@ (guile) sin)) +(define cos (@ (guile) cos)) +(define tan (@ (guile) tan)) +(define asin (@ (guile) asin)) +(define acos (@ (guile) acos)) +(define atan (@ (guile) atan)) + +;; hyp +(define sinh (@ (guile) sinh)) +(define cosh (@ (guile) cosh)) +(define tanh (@ (guile) tanh)) +(define asinh (@ (guile) asinh)) +(define acosh (@ (guile) acosh)) +(define atanh (@ (guile) atanh)) + +;; classification +(define isfinite1 (@ (language python module math) isfinite)) + +(define (isfinite x) + (and (isfinite1 (real-part x)) + (isfinite1 (imag-part x)))) + +(define (isinf x) + (or (inf? (real-part x)) + (inf? (imag-part x)))) + +(define (isnan x) + (or (nan? (real-part x)) + (nan? (imag-part x)))) + +(define* (isclose a b #:key (rel_tol 1e-9) (abs_tol 0.0)) + (define e-abs (magnitude (- a b))) + (if (< e-abs (max (* rel_tol (max (magnitude a) (magnitude b))) abs_tol)) + #t + #f)) + +(define pi (@ (language python module math) pi)) +(define e (@ (language python module math) e)) +(define tau (@ (language python module math) tau)) +(define inf (@ (language python module math) inf)) +(define nan (@ (language python module math) nan)) +(define infj (make-rectangular 0 inf)) +(define nanj (make-rectangular 0 nan)) + -- cgit v1.2.3