blob: d58b53463c5be22538e8d1699867127d80e3b49a [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>
Damiend99b0522013-12-21 18:17:45 +000028#include <assert.h>
29
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000031#include "nlr.h"
32#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000034#include "obj.h"
Damien George6e48f7f2014-03-21 11:45:46 +000035#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000036#include "runtime0.h"
Damien Georgeee7a8802014-05-11 18:37:21 +010037#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000038
Damien Georgefb510b32014-06-01 13:32:54 +010039#if MICROPY_PY_BUILTINS_FLOAT
Damiend99b0522013-12-21 18:17:45 +000040
Damien Georgeae491052014-04-10 20:08:11 +010041#include <math.h>
42
Damien George8767d072014-03-27 22:17:49 +000043#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
44#include "formatfloat.h"
45#endif
46
Damiend99b0522013-12-21 18:17:45 +000047typedef struct _mp_obj_complex_t {
48 mp_obj_base_t base;
49 mp_float_t real;
50 mp_float_t imag;
51} mp_obj_complex_t;
52
53mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
54
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020055STATIC void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000056 mp_obj_complex_t *o = o_in;
Damien George8767d072014-03-27 22:17:49 +000057#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
58 char buf[32];
59 if (o->real == 0) {
60 format_float(o->imag, buf, sizeof(buf), 'g', 6, '\0');
Damien George6e54fcf2014-03-29 11:41:38 +000061 print(env, "%sj", buf);
Damien George8767d072014-03-27 22:17:49 +000062 } else {
63 format_float(o->real, buf, sizeof(buf), 'g', 6, '\0');
64 print(env, "(%s+", buf);
Damien George01b877d2014-03-27 23:35:31 +000065 format_float(o->imag, buf, sizeof(buf), 'g', 6, '\0');
Damien George8767d072014-03-27 22:17:49 +000066 print(env, "%sj)", buf);
67 }
68#else
Damiend99b0522013-12-21 18:17:45 +000069 if (o->real == 0) {
mux00a4da92014-01-27 10:21:42 +020070 print(env, "%.8gj", (double) o->imag);
Damiend99b0522013-12-21 18:17:45 +000071 } else {
mux00a4da92014-01-27 10:21:42 +020072 print(env, "(%.8g+%.8gj)", (double) o->real, (double) o->imag);
Damiend99b0522013-12-21 18:17:45 +000073 }
Damien George8767d072014-03-27 22:17:49 +000074#endif
Damiend99b0522013-12-21 18:17:45 +000075}
76
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020077STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien Georgeee7a8802014-05-11 18:37:21 +010078 mp_arg_check_num(n_args, n_kw, 0, 2, false);
Damien George20006db2014-01-18 14:10:48 +000079
Damien George71c51812014-01-04 20:21:15 +000080 switch (n_args) {
81 case 0:
82 return mp_obj_new_complex(0, 0);
83
84 case 1:
Damien George6e48f7f2014-03-21 11:45:46 +000085 if (MP_OBJ_IS_STR(args[0])) {
86 // a string, parse it
87 uint l;
88 const char *s = mp_obj_str_get_data(args[0], &l);
89 return mp_parse_num_decimal(s, l, true, true);
90 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
91 // a complex, just return it
Damien George71c51812014-01-04 20:21:15 +000092 return args[0];
93 } else {
Damien George6e48f7f2014-03-21 11:45:46 +000094 // something else, try to cast it to a complex
Damien George71c51812014-01-04 20:21:15 +000095 return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
96 }
97
Damien Georgeee7a8802014-05-11 18:37:21 +010098 case 2:
99 default: {
Damien George71c51812014-01-04 20:21:15 +0000100 mp_float_t real, imag;
Damien George0c36da02014-03-08 15:24:39 +0000101 if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
Damien George20006db2014-01-18 14:10:48 +0000102 mp_obj_complex_get(args[0], &real, &imag);
Damien George71c51812014-01-04 20:21:15 +0000103 } else {
Damien George20006db2014-01-18 14:10:48 +0000104 real = mp_obj_get_float(args[0]);
Damien George71c51812014-01-04 20:21:15 +0000105 imag = 0;
106 }
Damien George0c36da02014-03-08 15:24:39 +0000107 if (MP_OBJ_IS_TYPE(args[1], &mp_type_complex)) {
Damien George71c51812014-01-04 20:21:15 +0000108 mp_float_t real2, imag2;
Damien George20006db2014-01-18 14:10:48 +0000109 mp_obj_complex_get(args[1], &real2, &imag2);
Damien George71c51812014-01-04 20:21:15 +0000110 real -= imag2;
111 imag += real2;
112 } else {
Damien George20006db2014-01-18 14:10:48 +0000113 imag += mp_obj_get_float(args[1]);
Damien George71c51812014-01-04 20:21:15 +0000114 }
115 return mp_obj_new_complex(real, imag);
116 }
Damien George71c51812014-01-04 20:21:15 +0000117 }
118}
119
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200120STATIC mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000121 mp_obj_complex_t *o = o_in;
122 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100123 case MP_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0);
124 case MP_UNARY_OP_POSITIVE: return o_in;
125 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
Damien George6ac5dce2014-05-21 19:42:43 +0100126 default: return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +0000127 }
128}
129
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200130STATIC mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgee2e3d112014-01-06 22:13:00 +0000131 mp_obj_complex_t *lhs = lhs_in;
132 return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
133}
134
Damien George0c36da02014-03-08 15:24:39 +0000135const mp_obj_type_t mp_type_complex = {
Damien Georgec5966122014-02-15 16:10:44 +0000136 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000137 .name = MP_QSTR_complex,
Damien George97209d32014-01-07 15:58:30 +0000138 .print = complex_print,
139 .make_new = complex_make_new,
140 .unary_op = complex_unary_op,
141 .binary_op = complex_binary_op,
Damien Georgee2e3d112014-01-06 22:13:00 +0000142};
143
144mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
145 mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t);
Damien George0c36da02014-03-08 15:24:39 +0000146 o->base.type = &mp_type_complex;
Damien Georgee2e3d112014-01-06 22:13:00 +0000147 o->real = real;
148 o->imag = imag;
149 return o;
150}
151
152void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
Damien George0c36da02014-03-08 15:24:39 +0000153 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_complex));
Damien Georgee2e3d112014-01-06 22:13:00 +0000154 mp_obj_complex_t *self = self_in;
155 *real = self->real;
156 *imag = self->imag;
157}
158
159mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) {
160 mp_float_t rhs_real, rhs_imag;
161 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 +0000162 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100163 case MP_BINARY_OP_ADD:
164 case MP_BINARY_OP_INPLACE_ADD:
Damiend99b0522013-12-21 18:17:45 +0000165 lhs_real += rhs_real;
166 lhs_imag += rhs_imag;
167 break;
Damien Georged17926d2014-03-30 13:35:08 +0100168 case MP_BINARY_OP_SUBTRACT:
169 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damiend99b0522013-12-21 18:17:45 +0000170 lhs_real -= rhs_real;
171 lhs_imag -= rhs_imag;
172 break;
Damien Georged17926d2014-03-30 13:35:08 +0100173 case MP_BINARY_OP_MULTIPLY:
Damien George5589db82014-04-09 20:21:00 +0100174 case MP_BINARY_OP_INPLACE_MULTIPLY: {
175 mp_float_t real;
176 multiply:
177 real = lhs_real * rhs_real - lhs_imag * rhs_imag;
Damiend99b0522013-12-21 18:17:45 +0000178 lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
179 lhs_real = real;
180 break;
181 }
Damien Georged17926d2014-03-30 13:35:08 +0100182 case MP_BINARY_OP_FLOOR_DIVIDE:
Damien George5589db82014-04-09 20:21:00 +0100183 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
184 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't do truncated division of a complex number"));
185
Damien Georged17926d2014-03-30 13:35:08 +0100186 case MP_BINARY_OP_TRUE_DIVIDE:
Damien George5589db82014-04-09 20:21:00 +0100187 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
188 if (rhs_imag == 0) {
189 if (rhs_real == 0) {
190 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "complex division by zero"));
191 }
192 lhs_real /= rhs_real;
193 lhs_imag /= rhs_real;
194 } else if (rhs_real == 0) {
195 mp_float_t real = lhs_imag / rhs_imag;
196 lhs_imag = -lhs_real / rhs_imag;
197 lhs_real = real;
198 } else {
199 mp_float_t rhs_len_sq = rhs_real*rhs_real + rhs_imag*rhs_imag;
200 rhs_real /= rhs_len_sq;
201 rhs_imag /= -rhs_len_sq;
202 goto multiply;
203 }
204 break;
205
Damien Georgeae491052014-04-10 20:08:11 +0100206 case MP_BINARY_OP_POWER:
207 case MP_BINARY_OP_INPLACE_POWER: {
208 // z1**z2 = exp(z2*ln(z1))
209 // = exp(z2*(ln(|z1|)+i*arg(z1)))
210 // = exp( (x2*ln1 - y2*arg1) + i*(y2*ln1 + x2*arg1) )
211 // = exp(x3 + i*y3)
212 // = exp(x3)*(cos(y3) + i*sin(y3))
213 mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real*lhs_real + lhs_imag*lhs_imag);
214 if (abs1 == 0) {
215 if (rhs_imag == 0) {
216 lhs_real = 1;
217 rhs_real = 0;
218 } else {
219 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "0.0 to a complex power"));
220 }
221 } else {
222 mp_float_t ln1 = MICROPY_FLOAT_C_FUN(log)(abs1);
223 mp_float_t arg1 = MICROPY_FLOAT_C_FUN(atan2)(lhs_imag, lhs_real);
224 mp_float_t x3 = rhs_real * ln1 - rhs_imag * arg1;
225 mp_float_t y3 = rhs_imag * ln1 + rhs_real * arg1;
226 mp_float_t exp_x3 = MICROPY_FLOAT_C_FUN(exp)(x3);
227 lhs_real = exp_x3 * MICROPY_FLOAT_C_FUN(cos)(y3);
228 lhs_imag = exp_x3 * MICROPY_FLOAT_C_FUN(sin)(y3);
229 }
230 break;
231 }
232
Damien Georgeb8a053a2014-04-11 10:10:37 +0100233 case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_real == rhs_real && lhs_imag == rhs_imag);
234
Damien George5589db82014-04-09 20:21:00 +0100235 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100236 return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +0000237 }
238 return mp_obj_new_complex(lhs_real, lhs_imag);
239}
240
Damiend99b0522013-12-21 18:17:45 +0000241#endif