From 917da99e4896f1cb2dd556f74b5c5c814fb05c85 Mon Sep 17 00:00:00 2001 From: jamesjlong Date: Sun, 26 Apr 2015 15:37:56 -0400 Subject: [PATCH 1/5] Update modmath.c Added DFT implementation under 'mp_math_custom' --- py/modmath.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/py/modmath.c b/py/modmath.c index 46423d2b5cf49..5c54c619da2b7 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -25,7 +25,17 @@ */ #include "py/builtin.h" +#include "objlist.h" +#include +#include + +#include "py/nlr.h" +#include "py/objlist.h" +#include "py/obj.h" +#include "py/runtime0.h" +#include "py/runtime.h" +#include "py/stackctrl.h" #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH #include @@ -120,7 +130,6 @@ MATH_FUN_1_TO_BOOL(isnan, isnan) MATH_FUN_1_TO_INT(trunc, trunc) /// \function ldexp(x, exp) MATH_FUN_2(ldexp, ldexp) -#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS /// \function erf(x) /// Return the error function of `x`. MATH_FUN_1(erf, erf) @@ -133,7 +142,6 @@ MATH_FUN_1(gamma, tgamma) /// \function lgamma(x) /// return the natural logarithm of the gamma function of `x`. MATH_FUN_1(lgamma, lgamma) -#endif //TODO: factorial, fsum // Functions that return a tuple @@ -171,10 +179,36 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_radians_obj, mp_math_radians); /// \function degrees(x) STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) { - return mp_obj_new_float(mp_obj_get_float(x_obj) * 180.0 / M_PI); + float one_eighty = 0.0; + for (int i = 0; i<2; i++){one_eighty+=2.0;} + return mp_obj_new_float(mp_obj_get_float(x_obj) * one_eighty); } + STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees); +STATIC mp_obj_t mp_math_custom(mp_obj_t x_obj){ + //implement a naive DFT algorithm x_obj is a python list, which is implememnted as a struct as follows: + + mp_obj_list_t *o = ((void*)x_obj); // + int length = o->len; + mp_obj_t list = mp_obj_new_list(0.0, NULL); + for (int i = 0; i items[i])*mp_obj_get_float(o->items[i]); + float real = 0.0; + for (int j = 0; jitems[j] ); + mp_float_t factor = cos(-2.0*M_PI*i*j/length) ; + real += item * factor; + } + mp_obj_list_append(list, mp_obj_new_float(real)); + } + return list; + } + +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_custom_obj, mp_math_custom); + STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) }, { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj }, @@ -213,12 +247,11 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_radians), (mp_obj_t)&mp_math_radians_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_degrees), (mp_obj_t)&mp_math_degrees_obj }, - #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS + { MP_OBJ_NEW_QSTR(MP_QSTR_custom), (mp_obj_t)&mp_math_custom_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&mp_math_erf_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&mp_math_erfc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_gamma), (mp_obj_t)&mp_math_gamma_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_lgamma), (mp_obj_t)&mp_math_lgamma_obj }, - #endif }; STATIC MP_DEFINE_CONST_DICT(mp_module_math_globals, mp_module_math_globals_table); From f5bd0b0344327754d55b476f988e00f450da5ed5 Mon Sep 17 00:00:00 2001 From: james Date: Sun, 26 Apr 2015 20:40:43 -0400 Subject: [PATCH 2/5] added DFT function to modmath.c, and updated to include in qstrdefs.h --- py/modmath.c | 28 ++++++++++++++++++++++++++-- py/qstrdefs.h | 1 + 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/py/modmath.c b/py/modmath.c index 46423d2b5cf49..6086f10aa224f 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -1,5 +1,4 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ +/* * This file is part of the Micro Python project, http://micropython.org/ * * The MIT License (MIT) * @@ -29,6 +28,7 @@ #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH #include +#include /// \module math - mathematical functions /// @@ -175,6 +175,29 @@ STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees); +//function DFT(list) +STATIC mp_obj_t mp_math_DFT(mp_obj_t x_obj){ + //implement a naive DFT algorithm x_obj is a python list, which is implememnted as a struct as follows: + + mp_obj_list_t *o = ((void*)x_obj); // + int length = o->len; + mp_obj_t list = mp_obj_new_list(0.0, NULL); + for (int i = 0; i items[i])*mp_obj_get_float(o->items[i]); + float real = 0.0; + for (int j = 0; jitems[j] ); + mp_float_t factor = cos(-2.0*M_PI*i*j/length) ; + real += item * factor; + } + mp_obj_list_append(list, mp_obj_new_float(real)); + } + return list; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_DFT_obj, mp_math_DFT); + STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) }, { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj }, @@ -213,6 +236,7 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_radians), (mp_obj_t)&mp_math_radians_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_degrees), (mp_obj_t)&mp_math_degrees_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_DFT), (mp_obj_t)&mp_math_DFT_obj }, #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS { MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&mp_math_erf_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&mp_math_erfc_obj }, diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 8b1c9297dd0b9..b97c5c817a122 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -391,6 +391,7 @@ Q(frexp) Q(ldexp) Q(degrees) Q(radians) +Q(DFT) #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS Q(erf) Q(erfc) From 96259a8f60e2d23c8016c8d040d39d0cae57b646 Mon Sep 17 00:00:00 2001 From: james Date: Mon, 27 Apr 2015 01:28:55 -0400 Subject: [PATCH 3/5] -a --- py/modmath.c | 26 +++ py/modmath.c~ | 282 ++++++++++++++++++++++++ py/qstrdefs.h | 1 + py/qstrdefs.h~ | 586 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 895 insertions(+) create mode 100644 py/modmath.c~ create mode 100644 py/qstrdefs.h~ diff --git a/py/modmath.c b/py/modmath.c index 6086f10aa224f..20e9d09275b19 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -197,6 +197,31 @@ STATIC mp_obj_t mp_math_DFT(mp_obj_t x_obj){ return list; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_DFT_obj, mp_math_DFT); + +//function dot returns matrix vector product of a list of lists times a list +STATIC mp_obj_t mp_math_dot(mp_obj_t matrix_obj, mp_obj_t vector_obj){ + //implement a matrix vector dot product + mp_obj_list_t *M = ((void*)matrix_obj); // + mp_obj_list_t *v = ((void*)vector_obj); // + int length = v->len; + mp_obj_list_t *r = mp_obj_new_list(length, NULL); //list for result + int i; + int j; + for(i=0;iitems[i]); + //r->items[i] = mp_obj_new_float(0.0); + float summation = 0.0; + for(j=0;jitems[j]) * mp_obj_get_float(v->items[j]); + //r->items[i] += current_multiply; + } + r->items[i] = mp_obj_new_float(summation); + } + return r; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_dot_obj, mp_math_dot); STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) }, @@ -237,6 +262,7 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_radians), (mp_obj_t)&mp_math_radians_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_degrees), (mp_obj_t)&mp_math_degrees_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_DFT), (mp_obj_t)&mp_math_DFT_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_dot), (mp_obj_t)&mp_math_dot_obj }, #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS { MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&mp_math_erf_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&mp_math_erfc_obj }, diff --git a/py/modmath.c~ b/py/modmath.c~ new file mode 100644 index 0000000000000..20e9d09275b19 --- /dev/null +++ b/py/modmath.c~ @@ -0,0 +1,282 @@ +/* * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/builtin.h" + +#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH + +#include +#include + +/// \module math - mathematical functions +/// +/// The `math` module provides some basic mathematical funtions for +/// working with floating-point numbers. + +//TODO: Change macros to check for overflow and raise OverflowError or RangeError +#define MATH_FUN_1(py_name, c_name) \ + STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj))); } \ + STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); + +#define MATH_FUN_2(py_name, c_name) \ + STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj, mp_obj_t y_obj) { return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj), mp_obj_get_float(y_obj))); } \ + STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name); + +#define MATH_FUN_1_TO_BOOL(py_name, c_name) \ + STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return MP_BOOL(c_name(mp_obj_get_float(x_obj))); } \ + STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); + +#define MATH_FUN_1_TO_INT(py_name, c_name) \ + STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { mp_int_t x = MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj)); return mp_obj_new_int(x); } \ + STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); + +// These are also used by cmath.c +/// \constant e - base of the natural logarithm +const mp_obj_float_t mp_math_e_obj = {{&mp_type_float}, M_E}; +/// \constant pi - the ratio of a circle's circumference to its diameter +const mp_obj_float_t mp_math_pi_obj = {{&mp_type_float}, M_PI}; + +/// \function sqrt(x) +/// Returns the square root of `x`. +MATH_FUN_1(sqrt, sqrt) +/// \function pow(x, y) +/// Returns `x` to the power of `y`. +MATH_FUN_2(pow, pow) +/// \function exp(x) +MATH_FUN_1(exp, exp) +/// \function expm1(x) +MATH_FUN_1(expm1, expm1) +/// \function log(x) +MATH_FUN_1(log, log) +/// \function log2(x) +MATH_FUN_1(log2, log2) +/// \function log10(x) +MATH_FUN_1(log10, log10) +/// \function cosh(x) +MATH_FUN_1(cosh, cosh) +/// \function sinh(x) +MATH_FUN_1(sinh, sinh) +/// \function tanh(x) +MATH_FUN_1(tanh, tanh) +/// \function acosh(x) +MATH_FUN_1(acosh, acosh) +/// \function asinh(x) +MATH_FUN_1(asinh, asinh) +/// \function atanh(x) +MATH_FUN_1(atanh, atanh) +/// \function cos(x) +MATH_FUN_1(cos, cos) +/// \function sin(x) +MATH_FUN_1(sin, sin) +/// \function tan(x) +MATH_FUN_1(tan, tan) +/// \function acos(x) +MATH_FUN_1(acos, acos) +/// \function asin(x) +MATH_FUN_1(asin, asin) +/// \function atan(x) +MATH_FUN_1(atan, atan) +/// \function atan2(y, x) +MATH_FUN_2(atan2, atan2) +/// \function ceil(x) +MATH_FUN_1_TO_INT(ceil, ceil) +/// \function copysign(x, y) +MATH_FUN_2(copysign, copysign) +/// \function fabs(x) +MATH_FUN_1(fabs, fabs) +/// \function floor(x) +MATH_FUN_1_TO_INT(floor, floor) //TODO: delegate to x.__floor__() if x is not a float +/// \function fmod(x, y) +MATH_FUN_2(fmod, fmod) +/// \function isfinite(x) +MATH_FUN_1_TO_BOOL(isfinite, isfinite) +/// \function isinf(x) +MATH_FUN_1_TO_BOOL(isinf, isinf) +/// \function isnan(x) +MATH_FUN_1_TO_BOOL(isnan, isnan) +/// \function trunc(x) +MATH_FUN_1_TO_INT(trunc, trunc) +/// \function ldexp(x, exp) +MATH_FUN_2(ldexp, ldexp) +#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS +/// \function erf(x) +/// Return the error function of `x`. +MATH_FUN_1(erf, erf) +/// \function erfc(x) +/// Return the complementary error function of `x`. +MATH_FUN_1(erfc, erfc) +/// \function gamma(x) +/// Return the gamma function of `x`. +MATH_FUN_1(gamma, tgamma) +/// \function lgamma(x) +/// return the natural logarithm of the gamma function of `x`. +MATH_FUN_1(lgamma, lgamma) +#endif +//TODO: factorial, fsum + +// Functions that return a tuple + +/// \function frexp(x) +/// Converts a floating-point number to fractional and integral components. +STATIC mp_obj_t mp_math_frexp(mp_obj_t x_obj) { + int int_exponent = 0; + mp_float_t significand = MICROPY_FLOAT_C_FUN(frexp)(mp_obj_get_float(x_obj), &int_exponent); + mp_obj_t tuple[2]; + tuple[0] = mp_obj_new_float(significand); + tuple[1] = mp_obj_new_int(int_exponent); + return mp_obj_new_tuple(2, tuple); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_frexp_obj, mp_math_frexp); + +/// \function modf(x) +STATIC mp_obj_t mp_math_modf(mp_obj_t x_obj) { + mp_float_t int_part = 0.0; + mp_float_t fractional_part = MICROPY_FLOAT_C_FUN(modf)(mp_obj_get_float(x_obj), &int_part); + mp_obj_t tuple[2]; + tuple[0] = mp_obj_new_float(fractional_part); + tuple[1] = mp_obj_new_float(int_part); + return mp_obj_new_tuple(2, tuple); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_modf_obj, mp_math_modf); + +// Angular conversions + +/// \function radians(x) +STATIC mp_obj_t mp_math_radians(mp_obj_t x_obj) { + return mp_obj_new_float(mp_obj_get_float(x_obj) * M_PI / 180.0); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_radians_obj, mp_math_radians); + +/// \function degrees(x) +STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) { + return mp_obj_new_float(mp_obj_get_float(x_obj) * 180.0 / M_PI); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees); + +//function DFT(list) +STATIC mp_obj_t mp_math_DFT(mp_obj_t x_obj){ + //implement a naive DFT algorithm x_obj is a python list, which is implememnted as a struct as follows: + + mp_obj_list_t *o = ((void*)x_obj); // + int length = o->len; + mp_obj_t list = mp_obj_new_list(0.0, NULL); + for (int i = 0; i items[i])*mp_obj_get_float(o->items[i]); + float real = 0.0; + for (int j = 0; jitems[j] ); + mp_float_t factor = cos(-2.0*M_PI*i*j/length) ; + real += item * factor; + } + mp_obj_list_append(list, mp_obj_new_float(real)); + } + return list; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_DFT_obj, mp_math_DFT); + +//function dot returns matrix vector product of a list of lists times a list +STATIC mp_obj_t mp_math_dot(mp_obj_t matrix_obj, mp_obj_t vector_obj){ + //implement a matrix vector dot product + mp_obj_list_t *M = ((void*)matrix_obj); // + mp_obj_list_t *v = ((void*)vector_obj); // + int length = v->len; + mp_obj_list_t *r = mp_obj_new_list(length, NULL); //list for result + int i; + int j; + for(i=0;iitems[i]); + //r->items[i] = mp_obj_new_float(0.0); + float summation = 0.0; + for(j=0;jitems[j]) * mp_obj_get_float(v->items[j]); + //r->items[i] += current_multiply; + } + r->items[i] = mp_obj_new_float(summation); + } + return r; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_dot_obj, mp_math_dot); + +STATIC const mp_map_elem_t mp_module_math_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_pi), (mp_obj_t)&mp_math_pi_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&mp_math_sqrt_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_math_pow_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&mp_math_exp_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_expm1), (mp_obj_t)&mp_math_expm1_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_log), (mp_obj_t)&mp_math_log_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_log2), (mp_obj_t)&mp_math_log2_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_log10), (mp_obj_t)&mp_math_log10_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_cosh), (mp_obj_t)&mp_math_cosh_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_sinh), (mp_obj_t)&mp_math_sinh_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_tanh), (mp_obj_t)&mp_math_tanh_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_acosh), (mp_obj_t)&mp_math_acosh_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_asinh), (mp_obj_t)&mp_math_asinh_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_atanh), (mp_obj_t)&mp_math_atanh_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_cos), (mp_obj_t)&mp_math_cos_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_sin), (mp_obj_t)&mp_math_sin_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_tan), (mp_obj_t)&mp_math_tan_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_acos), (mp_obj_t)&mp_math_acos_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&mp_math_asin_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&mp_math_atan_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_atan2), (mp_obj_t)&mp_math_atan2_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ceil), (mp_obj_t)&mp_math_ceil_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_copysign), (mp_obj_t)&mp_math_copysign_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_fabs), (mp_obj_t)&mp_math_fabs_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_floor), (mp_obj_t)&mp_math_floor_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_fmod), (mp_obj_t)&mp_math_fmod_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_frexp), (mp_obj_t)&mp_math_frexp_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ldexp), (mp_obj_t)&mp_math_ldexp_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_modf), (mp_obj_t)&mp_math_modf_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_isfinite), (mp_obj_t)&mp_math_isfinite_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_isinf), (mp_obj_t)&mp_math_isinf_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_isnan), (mp_obj_t)&mp_math_isnan_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_radians), (mp_obj_t)&mp_math_radians_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_degrees), (mp_obj_t)&mp_math_degrees_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_DFT), (mp_obj_t)&mp_math_DFT_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_dot), (mp_obj_t)&mp_math_dot_obj }, + #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS + { MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&mp_math_erf_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&mp_math_erfc_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_gamma), (mp_obj_t)&mp_math_gamma_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_lgamma), (mp_obj_t)&mp_math_lgamma_obj }, + #endif +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_math_globals, mp_module_math_globals_table); + +const mp_obj_module_t mp_module_math = { + .base = { &mp_type_module }, + .name = MP_QSTR_math, + .globals = (mp_obj_dict_t*)&mp_module_math_globals, +}; + +#endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH diff --git a/py/qstrdefs.h b/py/qstrdefs.h index b97c5c817a122..9f9999a213371 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -392,6 +392,7 @@ Q(ldexp) Q(degrees) Q(radians) Q(DFT) +Q(dot) #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS Q(erf) Q(erfc) diff --git a/py/qstrdefs.h~ b/py/qstrdefs.h~ new file mode 100644 index 0000000000000..b97c5c817a122 --- /dev/null +++ b/py/qstrdefs.h~ @@ -0,0 +1,586 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mpconfig.h" + +// All the qstr definitions in this file are available as constants. +// That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx. + +// qstr configuration passed to makeqstrdata.py of the form QCFG(key, value) +QCFG(BYTES_IN_LEN, MICROPY_QSTR_BYTES_IN_LEN) + +Q() +Q(*) +Q(__build_class__) +Q(__class__) +Q(__doc__) +Q(__import__) +Q(__init__) +Q(__new__) +Q(__locals__) +Q(__main__) +Q(__module__) +Q(__name__) +Q(__hash__) +Q(__next__) +Q(__qualname__) +Q(__path__) +Q(__repl_print__) +#if MICROPY_PY___FILE__ +Q(__file__) +#endif + +Q(__bool__) +Q(__contains__) +Q(__enter__) +Q(__exit__) +Q(__len__) +Q(__iter__) +Q(__getitem__) +Q(__setitem__) +Q(__delitem__) +Q(__add__) +Q(__sub__) +Q(__repr__) +Q(__str__) +#if MICROPY_PY_DESCRIPTORS +Q(__get__) +Q(__set__) +Q(__delete__) +#endif +Q(__getattr__) +Q(__del__) +Q(__call__) +Q(__lt__) +Q(__gt__) +Q(__eq__) +Q(__le__) +Q(__ge__) +Q(__reversed__) +#if MICROPY_PY_ALL_SPECIAL_METHODS +Q(__mul__) +Q(__truediv__) +Q(__floordiv__) +Q(__iadd__) +Q(__isub__) +Q(__invert__) +Q(__neg__) +Q(__pos__) +#endif + +Q(micropython) +Q(bytecode) +Q(const) + +#if MICROPY_EMIT_NATIVE +Q(native) +Q(viper) +Q(uint) +Q(ptr) +Q(ptr8) +Q(ptr16) +#endif + +#if MICROPY_EMIT_INLINE_THUMB +Q(asm_thumb) +Q(label) +Q(align) +Q(data) +#endif + +Q(builtins) + +Q(Ellipsis) +Q(StopIteration) + +Q(BaseException) +Q(ArithmeticError) +Q(AssertionError) +Q(AttributeError) +Q(BufferError) +Q(EOFError) +Q(Exception) +Q(FileExistsError) +Q(FileNotFoundError) +Q(FloatingPointError) +Q(GeneratorExit) +Q(ImportError) +Q(IndentationError) +Q(IndexError) +Q(KeyboardInterrupt) +Q(KeyError) +Q(LookupError) +Q(MemoryError) +Q(NameError) +Q(NotImplementedError) +Q(OSError) +Q(OverflowError) +Q(RuntimeError) +Q(SyntaxError) +Q(SystemExit) +Q(TypeError) +Q(UnboundLocalError) +Q(ValueError) +#if MICROPY_EMIT_NATIVE +Q(ViperTypeError) +#endif +Q(ZeroDivisionError) +#if MICROPY_PY_BUILTINS_STR_UNICODE +Q(UnicodeError) +#endif + +Q(None) +Q(False) +Q(True) +Q(object) + +Q(NoneType) + +#if MICROPY_PY_COLLECTIONS_ORDEREDDICT +Q(OrderedDict) +#endif + +Q(abs) +Q(all) +Q(any) +Q(args) +#if MICROPY_PY_ARRAY +Q(array) +#endif +Q(bin) +Q({:#b}) +Q(bool) +#if MICROPY_PY_BUILTINS_BYTEARRAY +Q(bytearray) +#endif +#if MICROPY_PY_BUILTINS_MEMORYVIEW +Q(memoryview) +#endif +Q(bytes) +Q(callable) +#if MICROPY_PY_STRUCT +Q(calcsize) +#endif +Q(chr) +Q(classmethod) +Q(_collections) +#if MICROPY_PY_BUILTINS_COMPLEX +Q(complex) +Q(real) +Q(imag) +#endif +Q(dict) +Q(dir) +Q(divmod) +Q(enumerate) +Q(eval) +Q(exec) +#if MICROPY_PY_BUILTINS_EXECFILE +Q(execfile) +#endif +Q(filter) +#if MICROPY_PY_BUILTINS_FLOAT +Q(float) +#endif +Q(from_bytes) +Q(getattr) +Q(setattr) +Q(globals) +Q(hasattr) +Q(hash) +Q(hex) +Q(%#x) +Q(id) +Q(int) +Q(isinstance) +Q(issubclass) +Q(iter) +Q(len) +Q(list) +Q(locals) +Q(map) +Q(max) +Q(min) +Q(namedtuple) +Q(next) +Q(oct) +Q(%#o) +Q(open) +Q(ord) +Q(path) +Q(pow) +Q(print) +Q(range) +Q(read) +Q(repr) +Q(reversed) +Q(round) +Q(sorted) +Q(staticmethod) +Q(sum) +Q(super) +Q(str) +Q(sys) +Q(to_bytes) +Q(tuple) +Q(type) +Q(value) +Q(write) +Q(zip) + +#if MICROPY_PY_BUILTINS_COMPILE +Q(compile) +Q(code) +Q(single) +#endif + +Q(sep) +Q(end) + +#if MICROPY_PY_BUILTINS_RANGE_ATTRS +Q(step) +Q(stop) +#endif + +Q(clear) +Q(copy) +Q(fromkeys) +Q(get) +Q(items) +Q(keys) +Q(pop) +Q(popitem) +Q(setdefault) +Q(update) +Q(values) +Q(append) +Q(close) +Q(send) +Q(throw) +Q(count) +Q(extend) +Q(index) +Q(remove) +Q(insert) +Q(pop) +Q(sort) +Q(join) +Q(strip) +Q(lstrip) +Q(rstrip) +Q(format) +Q(key) +Q(reverse) +Q(add) +Q(clear) +Q(copy) +Q(pop) +Q(remove) +Q(find) +Q(rfind) +Q(rindex) +Q(split) +#if MICROPY_PY_BUILTINS_STR_SPLITLINES +Q(splitlines) +Q(keepends) +Q(\n) +#endif +Q(rsplit) +Q(startswith) +Q(endswith) +Q(replace) +Q(partition) +Q(rpartition) +Q(lower) +Q(upper) +Q(isspace) +Q(isalpha) +Q(isdigit) +Q(isupper) +Q(islower) +Q(iterable) +Q(start) + +Q(bound_method) +Q(closure) +Q(dict_view) +Q(function) +Q(generator) +Q(iterator) +Q(module) +Q(slice) + +#if MICROPY_PY_BUILTINS_SET +Q(discard) +Q(difference) +Q(difference_update) +Q(intersection) +Q(intersection_update) +Q(isdisjoint) +Q(issubset) +Q(issuperset) +Q(set) +Q(symmetric_difference) +Q(symmetric_difference_update) +Q(union) +Q(update) +#endif + +#if MICROPY_PY_BUILTINS_FROZENSET +Q(frozenset) +#endif + +#if MICROPY_PY_MATH || MICROPY_PY_CMATH +Q(math) +Q(e) +Q(pi) +Q(sqrt) +Q(pow) +Q(exp) +Q(expm1) +Q(log) +Q(log2) +Q(log10) +Q(cosh) +Q(sinh) +Q(tanh) +Q(acosh) +Q(asinh) +Q(atanh) +Q(cos) +Q(sin) +Q(tan) +Q(acos) +Q(asin) +Q(atan) +Q(atan2) +Q(ceil) +Q(copysign) +Q(fabs) +Q(fmod) +Q(floor) +Q(isfinite) +Q(isinf) +Q(isnan) +Q(trunc) +Q(modf) +Q(frexp) +Q(ldexp) +Q(degrees) +Q(radians) +Q(DFT) +#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS +Q(erf) +Q(erfc) +Q(gamma) +Q(lgamma) +#endif +#endif + +#if MICROPY_PY_CMATH +Q(cmath) +Q(phase) +Q(polar) +Q(rect) +#endif + +#if MICROPY_PY_MICROPYTHON_MEM_INFO +#if MICROPY_MEM_STATS +Q(mem_total) +Q(mem_current) +Q(mem_peak) +#endif +Q(mem_info) +Q(qstr_info) +#endif + +#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && (MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0) +Q(alloc_emergency_exception_buf) +#endif +Q(maximum recursion depth exceeded) + +Q() +Q() +Q() +Q() +Q() +Q() +Q() +Q() + +#if MICROPY_CPYTHON_COMPAT +Q(encode) +Q(decode) +Q(utf-8) +#endif + +#if MICROPY_PY_SYS +Q(argv) +Q(byteorder) +Q(big) +Q(exit) +Q(little) +#ifdef MICROPY_PY_SYS_PLATFORM +Q(platform) +#endif +Q(stdin) +Q(stdout) +Q(stderr) +Q(version) +Q(version_info) +#if MICROPY_PY_ATTRTUPLE +Q(name) +#endif +Q(implementation) +#if MICROPY_PY_SYS_MAXSIZE +Q(maxsize) +#endif +#if MICROPY_PY_SYS_EXC_INFO +Q(exc_info) +#endif +Q(print_exception) +#endif + +#if MICROPY_PY_STRUCT +Q(struct) +Q(pack) +Q(unpack) +#endif + +#if MICROPY_PY_UCTYPES +Q(uctypes) +Q(sizeof) +Q(addressof) +Q(bytes_at) +Q(bytearray_at) + +Q(NATIVE) +Q(LITTLE_ENDIAN) +Q(BIG_ENDIAN) + +Q(VOID) + +Q(UINT8) +Q(INT8) +Q(UINT16) +Q(INT16) +Q(UINT32) +Q(INT32) +Q(UINT64) +Q(INT64) + +Q(BFUINT8) +Q(BFINT8) +Q(BFUINT16) +Q(BFINT16) +Q(BFUINT32) +Q(BFINT32) + +Q(FLOAT32) +Q(FLOAT64) + +Q(ARRAY) +Q(PTR) +//Q(BITFIELD) + +Q(BF_POS) +Q(BF_LEN) +#endif + +#if MICROPY_PY_IO +Q(_io) +Q(readall) +Q(readinto) +Q(readline) +Q(readlines) +Q(seek) +Q(FileIO) +Q(TextIOWrapper) +Q(StringIO) +Q(BytesIO) +Q(getvalue) +Q(file) +Q(mode) +Q(r) +Q(encoding) +#endif + +#if MICROPY_PY_GC +Q(gc) +Q(collect) +Q(disable) +Q(enable) +Q(isenabled) +Q(mem_free) +Q(mem_alloc) +#endif + +#if MICROPY_PY_BUILTINS_PROPERTY +Q(property) +Q(getter) +Q(setter) +Q(deleter) +#endif + +#if MICROPY_PY_UZLIB +Q(uzlib) +Q(decompress) +#endif + +#if MICROPY_PY_UJSON +Q(ujson) +Q(dumps) +Q(loads) +#endif + +#if MICROPY_PY_URE +Q(ure) +Q(compile) +Q(match) +Q(search) +Q(group) +Q(DEBUG) +#endif + +#if MICROPY_PY_UHEAPQ +Q(uheapq) +Q(heappush) +Q(heappop) +Q(heapify) +#endif + +#if MICROPY_PY_UHASHLIB +Q(uhashlib) +Q(update) +Q(digest) +Q(hexdigest) +Q(sha256) +#endif + +#if MICROPY_PY_UBINASCII +Q(ubinascii) +Q(hexlify) +#endif From 39ebd3c1cd3143b176124912cbc7372904ca4109 Mon Sep 17 00:00:00 2001 From: james Date: Mon, 27 Apr 2015 01:34:15 -0400 Subject: [PATCH 4/5] adding mat-vet product --- py/modmath.c | 60 ++++++++++++++++++++++++++++++++------------------- py/modmath.c~ | 47 +++------------------------------------- 2 files changed, 41 insertions(+), 66 deletions(-) diff --git a/py/modmath.c b/py/modmath.c index e2d9de2046a16..20e9d09275b19 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -1,6 +1,4 @@ - -/* - * This file is part of the Micro Python project, http://micropython.org/ +/* * This file is part of the Micro Python project, http://micropython.org/ * * The MIT License (MIT) * @@ -26,20 +24,11 @@ */ #include "py/builtin.h" -#include "objlist.h" - -#include -#include -#include "py/nlr.h" -#include "py/objlist.h" -#include "py/obj.h" -#include "py/runtime0.h" -#include "py/runtime.h" -#include "py/stackctrl.h" #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH #include +#include /// \module math - mathematical functions /// @@ -131,6 +120,7 @@ MATH_FUN_1_TO_BOOL(isnan, isnan) MATH_FUN_1_TO_INT(trunc, trunc) /// \function ldexp(x, exp) MATH_FUN_2(ldexp, ldexp) +#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS /// \function erf(x) /// Return the error function of `x`. MATH_FUN_1(erf, erf) @@ -143,6 +133,7 @@ MATH_FUN_1(gamma, tgamma) /// \function lgamma(x) /// return the natural logarithm of the gamma function of `x`. MATH_FUN_1(lgamma, lgamma) +#endif //TODO: factorial, fsum // Functions that return a tuple @@ -180,14 +171,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_radians_obj, mp_math_radians); /// \function degrees(x) STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) { - float one_eighty = 0.0; - for (int i = 0; i<2; i++){one_eighty+=2.0;} - return mp_obj_new_float(mp_obj_get_float(x_obj) * one_eighty); + return mp_obj_new_float(mp_obj_get_float(x_obj) * 180.0 / M_PI); } - STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees); -STATIC mp_obj_t mp_math_custom(mp_obj_t x_obj){ +//function DFT(list) +STATIC mp_obj_t mp_math_DFT(mp_obj_t x_obj){ //implement a naive DFT algorithm x_obj is a python list, which is implememnted as a struct as follows: mp_obj_list_t *o = ((void*)x_obj); // @@ -206,10 +195,34 @@ STATIC mp_obj_t mp_math_custom(mp_obj_t x_obj){ mp_obj_list_append(list, mp_obj_new_float(real)); } return list; - } - -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_custom_obj, mp_math_custom); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_DFT_obj, mp_math_DFT); +//function dot returns matrix vector product of a list of lists times a list +STATIC mp_obj_t mp_math_dot(mp_obj_t matrix_obj, mp_obj_t vector_obj){ + //implement a matrix vector dot product + mp_obj_list_t *M = ((void*)matrix_obj); // + mp_obj_list_t *v = ((void*)vector_obj); // + int length = v->len; + mp_obj_list_t *r = mp_obj_new_list(length, NULL); //list for result + int i; + int j; + for(i=0;iitems[i]); + //r->items[i] = mp_obj_new_float(0.0); + float summation = 0.0; + for(j=0;jitems[j]) * mp_obj_get_float(v->items[j]); + //r->items[i] += current_multiply; + } + r->items[i] = mp_obj_new_float(summation); + } + return r; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_dot_obj, mp_math_dot); + STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) }, { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj }, @@ -248,11 +261,14 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_radians), (mp_obj_t)&mp_math_radians_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_degrees), (mp_obj_t)&mp_math_degrees_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_custom), (mp_obj_t)&mp_math_custom_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_DFT), (mp_obj_t)&mp_math_DFT_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_dot), (mp_obj_t)&mp_math_dot_obj }, + #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS { MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&mp_math_erf_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&mp_math_erfc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_gamma), (mp_obj_t)&mp_math_gamma_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_lgamma), (mp_obj_t)&mp_math_lgamma_obj }, + #endif }; STATIC MP_DEFINE_CONST_DICT(mp_module_math_globals, mp_module_math_globals_table); diff --git a/py/modmath.c~ b/py/modmath.c~ index 508f830e5af0e..e2d9de2046a16 100644 --- a/py/modmath.c~ +++ b/py/modmath.c~ @@ -1,4 +1,6 @@ -/* * This file is part of the Micro Python project, http://micropython.org/ + +/* + * This file is part of the Micro Python project, http://micropython.org/ * * The MIT License (MIT) * @@ -38,7 +40,6 @@ #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH #include -#include /// \module math - mathematical functions /// @@ -186,12 +187,7 @@ STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees); -<<<<<<< HEAD -//function DFT(list) -STATIC mp_obj_t mp_math_DFT(mp_obj_t x_obj){ -======= STATIC mp_obj_t mp_math_custom(mp_obj_t x_obj){ ->>>>>>> 917da99e4896f1cb2dd556f74b5c5c814fb05c85 //implement a naive DFT algorithm x_obj is a python list, which is implememnted as a struct as follows: mp_obj_list_t *o = ((void*)x_obj); // @@ -210,41 +206,10 @@ STATIC mp_obj_t mp_math_custom(mp_obj_t x_obj){ mp_obj_list_append(list, mp_obj_new_float(real)); } return list; -<<<<<<< HEAD -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_DFT_obj, mp_math_DFT); - -//function dot returns matrix vector product of a list of lists times a list -STATIC mp_obj_t mp_math_dot(mp_obj_t matrix_obj, mp_obj_t vector_obj){ - //implement a matrix vector dot product - mp_obj_list_t *M = ((void*)matrix_obj); // - mp_obj_list_t *v = ((void*)vector_obj); // - int length = v->len; - mp_obj_list_t *r = mp_obj_new_list(length, NULL); //list for result - int i; - int j; - for(i=0;iitems[i]); - //r->items[i] = mp_obj_new_float(0.0); - float summation = 0.0; - for(j=0;jitems[j]) * mp_obj_get_float(v->items[j]); - //r->items[i] += current_multiply; - } - r->items[i] = mp_obj_new_float(summation); - } - return r; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_dot_obj, mp_math_dot); - -======= } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_custom_obj, mp_math_custom); ->>>>>>> 917da99e4896f1cb2dd556f74b5c5c814fb05c85 STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) }, { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj }, @@ -283,13 +248,7 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_radians), (mp_obj_t)&mp_math_radians_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_degrees), (mp_obj_t)&mp_math_degrees_obj }, -<<<<<<< HEAD - { MP_OBJ_NEW_QSTR(MP_QSTR_DFT), (mp_obj_t)&mp_math_DFT_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_dot), (mp_obj_t)&mp_math_dot_obj }, - #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS -======= { MP_OBJ_NEW_QSTR(MP_QSTR_custom), (mp_obj_t)&mp_math_custom_obj }, ->>>>>>> 917da99e4896f1cb2dd556f74b5c5c814fb05c85 { MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&mp_math_erf_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&mp_math_erfc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_gamma), (mp_obj_t)&mp_math_gamma_obj }, From b3322b108ded7576f0b732d515dfbfae234cdc2a Mon Sep 17 00:00:00 2001 From: james Date: Mon, 27 Apr 2015 01:44:16 -0400 Subject: [PATCH 5/5] tidying up DFT --- py/modmath.c | 3 +-- py/modmath.c~ | 62 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/py/modmath.c b/py/modmath.c index 20e9d09275b19..4eddeb660915a 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -177,14 +177,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees); //function DFT(list) STATIC mp_obj_t mp_math_DFT(mp_obj_t x_obj){ - //implement a naive DFT algorithm x_obj is a python list, which is implememnted as a struct as follows: + //implement a naive DFT algorithm x_obj is a python list, and a list is also returned mp_obj_list_t *o = ((void*)x_obj); // int length = o->len; mp_obj_t list = mp_obj_new_list(0.0, NULL); for (int i = 0; i items[i])*mp_obj_get_float(o->items[i]); float real = 0.0; for (int j = 0; j -#include -#include "py/nlr.h" -#include "py/objlist.h" -#include "py/obj.h" -#include "py/runtime0.h" -#include "py/runtime.h" -#include "py/stackctrl.h" #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH #include +#include /// \module math - mathematical functions /// @@ -131,6 +120,7 @@ MATH_FUN_1_TO_BOOL(isnan, isnan) MATH_FUN_1_TO_INT(trunc, trunc) /// \function ldexp(x, exp) MATH_FUN_2(ldexp, ldexp) +#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS /// \function erf(x) /// Return the error function of `x`. MATH_FUN_1(erf, erf) @@ -143,6 +133,7 @@ MATH_FUN_1(gamma, tgamma) /// \function lgamma(x) /// return the natural logarithm of the gamma function of `x`. MATH_FUN_1(lgamma, lgamma) +#endif //TODO: factorial, fsum // Functions that return a tuple @@ -180,15 +171,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_radians_obj, mp_math_radians); /// \function degrees(x) STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) { - float one_eighty = 0.0; - for (int i = 0; i<2; i++){one_eighty+=2.0;} - return mp_obj_new_float(mp_obj_get_float(x_obj) * one_eighty); + return mp_obj_new_float(mp_obj_get_float(x_obj) * 180.0 / M_PI); } - STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees); -STATIC mp_obj_t mp_math_custom(mp_obj_t x_obj){ - //implement a naive DFT algorithm x_obj is a python list, which is implememnted as a struct as follows: +//function DFT(list) +STATIC mp_obj_t mp_math_DFT(mp_obj_t x_obj){ + //implement a naive DFT algorithm x_obj is a python list, and a list is also returned mp_obj_list_t *o = ((void*)x_obj); // int length = o->len; @@ -206,10 +195,34 @@ STATIC mp_obj_t mp_math_custom(mp_obj_t x_obj){ mp_obj_list_append(list, mp_obj_new_float(real)); } return list; - } - -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_custom_obj, mp_math_custom); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_DFT_obj, mp_math_DFT); +//function dot returns matrix vector product of a list of lists times a list +STATIC mp_obj_t mp_math_dot(mp_obj_t matrix_obj, mp_obj_t vector_obj){ + //implement a matrix vector dot product + mp_obj_list_t *M = ((void*)matrix_obj); // + mp_obj_list_t *v = ((void*)vector_obj); // + int length = v->len; + mp_obj_list_t *r = mp_obj_new_list(length, NULL); //list for result + int i; + int j; + for(i=0;iitems[i]); + //r->items[i] = mp_obj_new_float(0.0); + float summation = 0.0; + for(j=0;jitems[j]) * mp_obj_get_float(v->items[j]); + //r->items[i] += current_multiply; + } + r->items[i] = mp_obj_new_float(summation); + } + return r; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_dot_obj, mp_math_dot); + STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) }, { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj }, @@ -248,11 +261,14 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_radians), (mp_obj_t)&mp_math_radians_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_degrees), (mp_obj_t)&mp_math_degrees_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_custom), (mp_obj_t)&mp_math_custom_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_DFT), (mp_obj_t)&mp_math_DFT_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_dot), (mp_obj_t)&mp_math_dot_obj }, + #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS { MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&mp_math_erf_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&mp_math_erfc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_gamma), (mp_obj_t)&mp_math_gamma_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_lgamma), (mp_obj_t)&mp_math_lgamma_obj }, + #endif }; STATIC MP_DEFINE_CONST_DICT(mp_module_math_globals, mp_module_math_globals_table);