blob: 12f10ec10b132d6aff9004a06a9c4a4f30d1cf53 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damiend99b0522013-12-21 18:17:45 +000027#include <stdlib.h>
Damien George20beff92014-09-11 22:24:45 +010028#include <stdio.h>
Damiend99b0522013-12-21 18:17:45 +000029#include <assert.h>
30
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/obj.h"
33#include "py/parsenum.h"
34#include "py/runtime0.h"
35#include "py/runtime.h"
Damiend99b0522013-12-21 18:17:45 +000036
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +030037#if MICROPY_PY_BUILTINS_COMPLEX
Damiend99b0522013-12-21 18:17:45 +000038
Damien Georgeae491052014-04-10 20:08:11 +010039#include <math.h>
40
Damien George8767d072014-03-27 22:17:49 +000041#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
Damien George51dfcb42015-01-01 20:27:54 +000042#include "py/formatfloat.h"
Damien George8767d072014-03-27 22:17:49 +000043#endif
44
Damiend99b0522013-12-21 18:17:45 +000045typedef struct _mp_obj_complex_t {
46 mp_obj_base_t base;
47 mp_float_t real;
48 mp_float_t imag;
49} mp_obj_complex_t;
50
Damien George7f9d1d62015-04-09 23:56:15 +010051STATIC void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000052 (void)kind;
Damiend99b0522013-12-21 18:17:45 +000053 mp_obj_complex_t *o = o_in;
Damien George8767d072014-03-27 22:17:49 +000054#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
Damien George20beff92014-09-11 22:24:45 +010055 char buf[16];
Damien George8767d072014-03-27 22:17:49 +000056 if (o->real == 0) {
Damien George51dfcb42015-01-01 20:27:54 +000057 mp_format_float(o->imag, buf, sizeof(buf), 'g', 7, '\0');
Damien George7f9d1d62015-04-09 23:56:15 +010058 mp_printf(print, "%sj", buf);
Damien George8767d072014-03-27 22:17:49 +000059 } else {
Damien George51dfcb42015-01-01 20:27:54 +000060 mp_format_float(o->real, buf, sizeof(buf), 'g', 7, '\0');
Damien George7f9d1d62015-04-09 23:56:15 +010061 mp_printf(print, "(%s", buf);
Damien Georgef49782f2015-02-02 12:52:14 +000062 if (o->imag >= 0) {
Damien George7f9d1d62015-04-09 23:56:15 +010063 mp_print_str(print, "+");
Damien Georgef49782f2015-02-02 12:52:14 +000064 }
Damien George51dfcb42015-01-01 20:27:54 +000065 mp_format_float(o->imag, buf, sizeof(buf), 'g', 7, '\0');
Damien George7f9d1d62015-04-09 23:56:15 +010066 mp_printf(print, "%sj)", buf);
Damien George8767d072014-03-27 22:17:49 +000067 }
68#else
Damien George20beff92014-09-11 22:24:45 +010069 char buf[32];
Damiend99b0522013-12-21 18:17:45 +000070 if (o->real == 0) {
Damien George20beff92014-09-11 22:24:45 +010071 sprintf(buf, "%.16g", (double)o->imag);
Damien George7f9d1d62015-04-09 23:56:15 +010072 mp_printf(print, "%sj", buf);
Damiend99b0522013-12-21 18:17:45 +000073 } else {
Damien George20beff92014-09-11 22:24:45 +010074 sprintf(buf, "%.16g", (double)o->real);
Damien George7f9d1d62015-04-09 23:56:15 +010075 mp_printf(print, "(%s", buf);
Damien Georgef49782f2015-02-02 12:52:14 +000076 if (o->imag >= 0) {
Damien George7f9d1d62015-04-09 23:56:15 +010077 mp_print_str(print, "+");
Damien Georgef49782f2015-02-02 12:52:14 +000078 }
Damien George20beff92014-09-11 22:24:45 +010079 sprintf(buf, "%.16g", (double)o->imag);
Damien George7f9d1d62015-04-09 23:56:15 +010080 mp_printf(print, "%sj)", buf);
Damiend99b0522013-12-21 18:17:45 +000081 }
Damien George8767d072014-03-27 22:17:49 +000082#endif
Damiend99b0522013-12-21 18:17:45 +000083}
84
Damien Georgeecc88e92014-08-30 00:35:11 +010085STATIC mp_obj_t complex_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000086 (void)type_in;
Damien Georgeee7a8802014-05-11 18:37:21 +010087 mp_arg_check_num(n_args, n_kw, 0, 2, false);
Damien George20006db2014-01-18 14:10:48 +000088
Damien George71c51812014-01-04 20:21:15 +000089 switch (n_args) {
90 case 0:
91 return mp_obj_new_complex(0, 0);
92
93 case 1:
Damien George6e48f7f2014-03-21 11:45:46 +000094 if (MP_OBJ_IS_STR(args[0])) {
95 // a string, parse it
Damien Georged182b982014-08-30 14:19:41 +010096 mp_uint_t l;
Damien George6e48f7f2014-03-21 11:45:46 +000097 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George7d414a12015-02-08 01:57:40 +000098 return mp_parse_num_decimal(s, l, true, true, NULL);
Damien George6e48f7f2014-03-21 11:45:46 +000099 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
100 // a complex, just return it
Damien George71c51812014-01-04 20:21:15 +0000101 return args[0];
102 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000103 // something else, try to cast it to a complex
Damien George71c51812014-01-04 20:21:15 +0000104 return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
105 }
106
Damien Georgeee7a8802014-05-11 18:37:21 +0100107 case 2:
108 default: {
Damien George71c51812014-01-04 20:21:15 +0000109 mp_float_t real, imag;
Damien George0c36da02014-03-08 15:24:39 +0000110 if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
Damien George20006db2014-01-18 14:10:48 +0000111 mp_obj_complex_get(args[0], &real, &imag);
Damien George71c51812014-01-04 20:21:15 +0000112 } else {
Damien George20006db2014-01-18 14:10:48 +0000113 real = mp_obj_get_float(args[0]);
Damien George71c51812014-01-04 20:21:15 +0000114 imag = 0;
115 }
Damien George0c36da02014-03-08 15:24:39 +0000116 if (MP_OBJ_IS_TYPE(args[1], &mp_type_complex)) {
Damien George71c51812014-01-04 20:21:15 +0000117 mp_float_t real2, imag2;
Damien George20006db2014-01-18 14:10:48 +0000118 mp_obj_complex_get(args[1], &real2, &imag2);
Damien George71c51812014-01-04 20:21:15 +0000119 real -= imag2;
120 imag += real2;
121 } else {
Damien George20006db2014-01-18 14:10:48 +0000122 imag += mp_obj_get_float(args[1]);
Damien George71c51812014-01-04 20:21:15 +0000123 }
124 return mp_obj_new_complex(real, imag);
125 }
Damien George71c51812014-01-04 20:21:15 +0000126 }
127}
128
Damien Georgeecc88e92014-08-30 00:35:11 +0100129STATIC mp_obj_t complex_unary_op(mp_uint_t op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000130 mp_obj_complex_t *o = o_in;
131 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100132 case MP_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0);
133 case MP_UNARY_OP_POSITIVE: return o_in;
134 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
Damien George6ac5dce2014-05-21 19:42:43 +0100135 default: return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +0000136 }
137}
138
Damien Georgeecc88e92014-08-30 00:35:11 +0100139STATIC mp_obj_t complex_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgee2e3d112014-01-06 22:13:00 +0000140 mp_obj_complex_t *lhs = lhs_in;
141 return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
142}
143
Damien Georgeb1bbe962015-04-01 14:10:50 +0000144STATIC void complex_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
145 if (dest[0] != MP_OBJ_NULL) {
146 // not load attribute
147 return;
148 }
Damien Georgef20375e2014-08-12 19:57:52 +0100149 mp_obj_complex_t *self = self_in;
150 if (attr == MP_QSTR_real) {
151 dest[0] = mp_obj_new_float(self->real);
152 } else if (attr == MP_QSTR_imag) {
153 dest[0] = mp_obj_new_float(self->imag);
154 }
155}
156
Damien George0c36da02014-03-08 15:24:39 +0000157const mp_obj_type_t mp_type_complex = {
Damien Georgec5966122014-02-15 16:10:44 +0000158 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000159 .name = MP_QSTR_complex,
Damien George97209d32014-01-07 15:58:30 +0000160 .print = complex_print,
161 .make_new = complex_make_new,
162 .unary_op = complex_unary_op,
163 .binary_op = complex_binary_op,
Damien Georgeb1bbe962015-04-01 14:10:50 +0000164 .attr = complex_attr,
Damien Georgee2e3d112014-01-06 22:13:00 +0000165};
166
167mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
168 mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t);
Damien George0c36da02014-03-08 15:24:39 +0000169 o->base.type = &mp_type_complex;
Damien Georgee2e3d112014-01-06 22:13:00 +0000170 o->real = real;
171 o->imag = imag;
172 return o;
173}
174
175void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
Damien George0c36da02014-03-08 15:24:39 +0000176 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_complex));
Damien Georgee2e3d112014-01-06 22:13:00 +0000177 mp_obj_complex_t *self = self_in;
178 *real = self->real;
179 *imag = self->imag;
180}
181
Damien Georgeecc88e92014-08-30 00:35:11 +0100182mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) {
Damien Georgee2e3d112014-01-06 22:13:00 +0000183 mp_float_t rhs_real, rhs_imag;
184 mp_obj_get_complex(rhs_in, &rhs_real, &rhs_imag); // can be any type, this function will convert to float (if possible)
Damiend99b0522013-12-21 18:17:45 +0000185 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100186 case MP_BINARY_OP_ADD:
187 case MP_BINARY_OP_INPLACE_ADD:
Damiend99b0522013-12-21 18:17:45 +0000188 lhs_real += rhs_real;
189 lhs_imag += rhs_imag;
190 break;
Damien Georged17926d2014-03-30 13:35:08 +0100191 case MP_BINARY_OP_SUBTRACT:
192 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damiend99b0522013-12-21 18:17:45 +0000193 lhs_real -= rhs_real;
194 lhs_imag -= rhs_imag;
195 break;
Damien Georged17926d2014-03-30 13:35:08 +0100196 case MP_BINARY_OP_MULTIPLY:
Damien George5589db82014-04-09 20:21:00 +0100197 case MP_BINARY_OP_INPLACE_MULTIPLY: {
198 mp_float_t real;
199 multiply:
200 real = lhs_real * rhs_real - lhs_imag * rhs_imag;
Damiend99b0522013-12-21 18:17:45 +0000201 lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
202 lhs_real = real;
203 break;
204 }
Damien Georged17926d2014-03-30 13:35:08 +0100205 case MP_BINARY_OP_FLOOR_DIVIDE:
Damien George5589db82014-04-09 20:21:00 +0100206 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
207 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't do truncated division of a complex number"));
208
Damien Georged17926d2014-03-30 13:35:08 +0100209 case MP_BINARY_OP_TRUE_DIVIDE:
Damien George5589db82014-04-09 20:21:00 +0100210 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
211 if (rhs_imag == 0) {
212 if (rhs_real == 0) {
213 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "complex division by zero"));
214 }
215 lhs_real /= rhs_real;
216 lhs_imag /= rhs_real;
217 } else if (rhs_real == 0) {
218 mp_float_t real = lhs_imag / rhs_imag;
219 lhs_imag = -lhs_real / rhs_imag;
220 lhs_real = real;
221 } else {
222 mp_float_t rhs_len_sq = rhs_real*rhs_real + rhs_imag*rhs_imag;
223 rhs_real /= rhs_len_sq;
224 rhs_imag /= -rhs_len_sq;
225 goto multiply;
226 }
227 break;
228
Damien Georgeae491052014-04-10 20:08:11 +0100229 case MP_BINARY_OP_POWER:
230 case MP_BINARY_OP_INPLACE_POWER: {
231 // z1**z2 = exp(z2*ln(z1))
232 // = exp(z2*(ln(|z1|)+i*arg(z1)))
233 // = exp( (x2*ln1 - y2*arg1) + i*(y2*ln1 + x2*arg1) )
234 // = exp(x3 + i*y3)
235 // = exp(x3)*(cos(y3) + i*sin(y3))
236 mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real*lhs_real + lhs_imag*lhs_imag);
237 if (abs1 == 0) {
238 if (rhs_imag == 0) {
239 lhs_real = 1;
240 rhs_real = 0;
241 } else {
242 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "0.0 to a complex power"));
243 }
244 } else {
245 mp_float_t ln1 = MICROPY_FLOAT_C_FUN(log)(abs1);
246 mp_float_t arg1 = MICROPY_FLOAT_C_FUN(atan2)(lhs_imag, lhs_real);
247 mp_float_t x3 = rhs_real * ln1 - rhs_imag * arg1;
248 mp_float_t y3 = rhs_imag * ln1 + rhs_real * arg1;
249 mp_float_t exp_x3 = MICROPY_FLOAT_C_FUN(exp)(x3);
250 lhs_real = exp_x3 * MICROPY_FLOAT_C_FUN(cos)(y3);
251 lhs_imag = exp_x3 * MICROPY_FLOAT_C_FUN(sin)(y3);
252 }
253 break;
254 }
255
Damien Georgeb8a053a2014-04-11 10:10:37 +0100256 case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_real == rhs_real && lhs_imag == rhs_imag);
257
Damien George5589db82014-04-09 20:21:00 +0100258 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100259 return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +0000260 }
261 return mp_obj_new_complex(lhs_real, lhs_imag);
262}
263
Damiend99b0522013-12-21 18:17:45 +0000264#endif