3434
3535#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH
3636
37+ /// \module cmath - mathematical functions for complex numbers
38+ ///
39+ /// The `cmath` module provides some basic mathematical funtions for
40+ /// working with complex numbers.
41+
3742// These are defined in modmath.c
43+ /// \constant e - base of the natural logarithm
3844extern const mp_obj_float_t mp_math_e_obj ;
45+ /// \constant pi - the ratio of a circle's circumference to its diameter
3946extern const mp_obj_float_t mp_math_pi_obj ;
4047
48+ /// \function phase(z)
49+ /// Returns the phase of the number `z`, in the range (-pi, +pi].
4150mp_obj_t mp_cmath_phase (mp_obj_t z_obj ) {
4251 mp_float_t real , imag ;
4352 mp_obj_get_complex (z_obj , & real , & imag );
4453 return mp_obj_new_float (MICROPY_FLOAT_C_FUN (atan2 )(imag , real ));
4554}
4655STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_cmath_phase_obj , mp_cmath_phase );
4756
57+ /// \function polar(z)
58+ /// Returns, as a tuple, the polar form of `z`.
4859mp_obj_t mp_cmath_polar (mp_obj_t z_obj ) {
4960 mp_float_t real , imag ;
5061 mp_obj_get_complex (z_obj , & real , & imag );
@@ -56,13 +67,16 @@ mp_obj_t mp_cmath_polar(mp_obj_t z_obj) {
5667}
5768STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_cmath_polar_obj , mp_cmath_polar );
5869
70+ /// \function rect(r, phi)
71+ /// Returns the complex number with modules `r` and phase `phi`.
5972mp_obj_t mp_cmath_rect (mp_obj_t r_obj , mp_obj_t phi_obj ) {
6073 mp_float_t r = mp_obj_get_float (r_obj );
6174 mp_float_t phi = mp_obj_get_float (phi_obj );
6275 return mp_obj_new_complex (r * MICROPY_FLOAT_C_FUN (cos )(phi ), r * MICROPY_FLOAT_C_FUN (sin )(phi ));
6376}
6477STATIC MP_DEFINE_CONST_FUN_OBJ_2 (mp_cmath_rect_obj , mp_cmath_rect );
6578
79+ /// \function exp(z)
6680mp_obj_t mp_cmath_exp (mp_obj_t z_obj ) {
6781 mp_float_t real , imag ;
6882 mp_obj_get_complex (z_obj , & real , & imag );
@@ -71,6 +85,7 @@ mp_obj_t mp_cmath_exp(mp_obj_t z_obj) {
7185}
7286STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_cmath_exp_obj , mp_cmath_exp );
7387
88+ /// \function log(z)
7489// TODO can take second argument, being the base
7590mp_obj_t mp_cmath_log (mp_obj_t z_obj ) {
7691 mp_float_t real , imag ;
@@ -79,13 +94,15 @@ mp_obj_t mp_cmath_log(mp_obj_t z_obj) {
7994}
8095STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_cmath_log_obj , mp_cmath_log );
8196
97+ /// \function log10(z)
8298mp_obj_t mp_cmath_log10 (mp_obj_t z_obj ) {
8399 mp_float_t real , imag ;
84100 mp_obj_get_complex (z_obj , & real , & imag );
85101 return mp_obj_new_complex (0.5 * MICROPY_FLOAT_C_FUN (log10 )(real * real + imag * imag ), MICROPY_FLOAT_C_FUN (atan2 )(imag , real ));
86102}
87103STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_cmath_log10_obj , mp_cmath_log10 );
88104
105+ /// \function sqrt(z)
89106mp_obj_t mp_cmath_sqrt (mp_obj_t z_obj ) {
90107 mp_float_t real , imag ;
91108 mp_obj_get_complex (z_obj , & real , & imag );
@@ -95,13 +112,15 @@ mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) {
95112}
96113STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_cmath_sqrt_obj , mp_cmath_sqrt );
97114
115+ /// \function cos(z)
98116mp_obj_t mp_cmath_cos (mp_obj_t z_obj ) {
99117 mp_float_t real , imag ;
100118 mp_obj_get_complex (z_obj , & real , & imag );
101119 return mp_obj_new_complex (MICROPY_FLOAT_C_FUN (cos )(real ) * MICROPY_FLOAT_C_FUN (cosh )(imag ), - MICROPY_FLOAT_C_FUN (sin )(real ) * MICROPY_FLOAT_C_FUN (sinh )(imag ));
102120}
103121STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_cmath_cos_obj , mp_cmath_cos );
104122
123+ /// \function sin(z)
105124mp_obj_t mp_cmath_sin (mp_obj_t z_obj ) {
106125 mp_float_t real , imag ;
107126 mp_obj_get_complex (z_obj , & real , & imag );
0 commit comments