blob: 924420964785dcf20223a72a3b7fe8e2a7839860 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
Damiend99b0522013-12-21 18:17:45 +00002#include <assert.h>
3
4#include "nlr.h"
5#include "misc.h"
6#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00007#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "obj.h"
Damien George6e48f7f2014-03-21 11:45:46 +00009#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "runtime0.h"
Damiend99b0522013-12-21 18:17:45 +000011
12#if MICROPY_ENABLE_FLOAT
13
Damien Georgeae491052014-04-10 20:08:11 +010014#include <math.h>
15
Damien George8767d072014-03-27 22:17:49 +000016#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
17#include "formatfloat.h"
18#endif
19
Damiend99b0522013-12-21 18:17:45 +000020typedef struct _mp_obj_complex_t {
21 mp_obj_base_t base;
22 mp_float_t real;
23 mp_float_t imag;
24} mp_obj_complex_t;
25
26mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
27
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020028STATIC 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 +000029 mp_obj_complex_t *o = o_in;
Damien George8767d072014-03-27 22:17:49 +000030#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
31 char buf[32];
32 if (o->real == 0) {
33 format_float(o->imag, buf, sizeof(buf), 'g', 6, '\0');
Damien George6e54fcf2014-03-29 11:41:38 +000034 print(env, "%sj", buf);
Damien George8767d072014-03-27 22:17:49 +000035 } else {
36 format_float(o->real, buf, sizeof(buf), 'g', 6, '\0');
37 print(env, "(%s+", buf);
Damien George01b877d2014-03-27 23:35:31 +000038 format_float(o->imag, buf, sizeof(buf), 'g', 6, '\0');
Damien George8767d072014-03-27 22:17:49 +000039 print(env, "%sj)", buf);
40 }
41#else
Damiend99b0522013-12-21 18:17:45 +000042 if (o->real == 0) {
mux00a4da92014-01-27 10:21:42 +020043 print(env, "%.8gj", (double) o->imag);
Damiend99b0522013-12-21 18:17:45 +000044 } else {
mux00a4da92014-01-27 10:21:42 +020045 print(env, "(%.8g+%.8gj)", (double) o->real, (double) o->imag);
Damiend99b0522013-12-21 18:17:45 +000046 }
Damien George8767d072014-03-27 22:17:49 +000047#endif
Damiend99b0522013-12-21 18:17:45 +000048}
49
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020050STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +000051 // TODO check n_kw == 0
52
Damien George71c51812014-01-04 20:21:15 +000053 switch (n_args) {
54 case 0:
55 return mp_obj_new_complex(0, 0);
56
57 case 1:
Damien George6e48f7f2014-03-21 11:45:46 +000058 if (MP_OBJ_IS_STR(args[0])) {
59 // a string, parse it
60 uint l;
61 const char *s = mp_obj_str_get_data(args[0], &l);
62 return mp_parse_num_decimal(s, l, true, true);
63 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
64 // a complex, just return it
Damien George71c51812014-01-04 20:21:15 +000065 return args[0];
66 } else {
Damien George6e48f7f2014-03-21 11:45:46 +000067 // something else, try to cast it to a complex
Damien George71c51812014-01-04 20:21:15 +000068 return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
69 }
70
Damien George6e48f7f2014-03-21 11:45:46 +000071 case 2: {
Damien George71c51812014-01-04 20:21:15 +000072 mp_float_t real, imag;
Damien George0c36da02014-03-08 15:24:39 +000073 if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
Damien George20006db2014-01-18 14:10:48 +000074 mp_obj_complex_get(args[0], &real, &imag);
Damien George71c51812014-01-04 20:21:15 +000075 } else {
Damien George20006db2014-01-18 14:10:48 +000076 real = mp_obj_get_float(args[0]);
Damien George71c51812014-01-04 20:21:15 +000077 imag = 0;
78 }
Damien George0c36da02014-03-08 15:24:39 +000079 if (MP_OBJ_IS_TYPE(args[1], &mp_type_complex)) {
Damien George71c51812014-01-04 20:21:15 +000080 mp_float_t real2, imag2;
Damien George20006db2014-01-18 14:10:48 +000081 mp_obj_complex_get(args[1], &real2, &imag2);
Damien George71c51812014-01-04 20:21:15 +000082 real -= imag2;
83 imag += real2;
84 } else {
Damien George20006db2014-01-18 14:10:48 +000085 imag += mp_obj_get_float(args[1]);
Damien George71c51812014-01-04 20:21:15 +000086 }
87 return mp_obj_new_complex(real, imag);
88 }
89
90 default:
Damien Georgeea13f402014-04-05 18:32:08 +010091 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "complex takes at most 2 arguments, %d given", n_args));
Damien George71c51812014-01-04 20:21:15 +000092 }
93}
94
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020095STATIC mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000096 mp_obj_complex_t *o = o_in;
97 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010098 case MP_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0);
99 case MP_UNARY_OP_POSITIVE: return o_in;
100 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100101 default: return MP_OBJ_NOT_SUPPORTED;
Damiend99b0522013-12-21 18:17:45 +0000102 }
103}
104
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200105STATIC 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 +0000106 mp_obj_complex_t *lhs = lhs_in;
107 return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
108}
109
Damien George0c36da02014-03-08 15:24:39 +0000110const mp_obj_type_t mp_type_complex = {
Damien Georgec5966122014-02-15 16:10:44 +0000111 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000112 .name = MP_QSTR_complex,
Damien George97209d32014-01-07 15:58:30 +0000113 .print = complex_print,
114 .make_new = complex_make_new,
115 .unary_op = complex_unary_op,
116 .binary_op = complex_binary_op,
Damien Georgee2e3d112014-01-06 22:13:00 +0000117};
118
119mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
120 mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t);
Damien George0c36da02014-03-08 15:24:39 +0000121 o->base.type = &mp_type_complex;
Damien Georgee2e3d112014-01-06 22:13:00 +0000122 o->real = real;
123 o->imag = imag;
124 return o;
125}
126
127void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
Damien George0c36da02014-03-08 15:24:39 +0000128 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_complex));
Damien Georgee2e3d112014-01-06 22:13:00 +0000129 mp_obj_complex_t *self = self_in;
130 *real = self->real;
131 *imag = self->imag;
132}
133
134mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) {
135 mp_float_t rhs_real, rhs_imag;
136 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 +0000137 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100138 case MP_BINARY_OP_ADD:
139 case MP_BINARY_OP_INPLACE_ADD:
Damiend99b0522013-12-21 18:17:45 +0000140 lhs_real += rhs_real;
141 lhs_imag += rhs_imag;
142 break;
Damien Georged17926d2014-03-30 13:35:08 +0100143 case MP_BINARY_OP_SUBTRACT:
144 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damiend99b0522013-12-21 18:17:45 +0000145 lhs_real -= rhs_real;
146 lhs_imag -= rhs_imag;
147 break;
Damien Georged17926d2014-03-30 13:35:08 +0100148 case MP_BINARY_OP_MULTIPLY:
Damien George5589db82014-04-09 20:21:00 +0100149 case MP_BINARY_OP_INPLACE_MULTIPLY: {
150 mp_float_t real;
151 multiply:
152 real = lhs_real * rhs_real - lhs_imag * rhs_imag;
Damiend99b0522013-12-21 18:17:45 +0000153 lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
154 lhs_real = real;
155 break;
156 }
Damien Georged17926d2014-03-30 13:35:08 +0100157 case MP_BINARY_OP_FLOOR_DIVIDE:
Damien George5589db82014-04-09 20:21:00 +0100158 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
159 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "can't do truncated division of a complex number"));
160
Damien Georged17926d2014-03-30 13:35:08 +0100161 case MP_BINARY_OP_TRUE_DIVIDE:
Damien George5589db82014-04-09 20:21:00 +0100162 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
163 if (rhs_imag == 0) {
164 if (rhs_real == 0) {
165 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "complex division by zero"));
166 }
167 lhs_real /= rhs_real;
168 lhs_imag /= rhs_real;
169 } else if (rhs_real == 0) {
170 mp_float_t real = lhs_imag / rhs_imag;
171 lhs_imag = -lhs_real / rhs_imag;
172 lhs_real = real;
173 } else {
174 mp_float_t rhs_len_sq = rhs_real*rhs_real + rhs_imag*rhs_imag;
175 rhs_real /= rhs_len_sq;
176 rhs_imag /= -rhs_len_sq;
177 goto multiply;
178 }
179 break;
180
Damien Georgeae491052014-04-10 20:08:11 +0100181 case MP_BINARY_OP_POWER:
182 case MP_BINARY_OP_INPLACE_POWER: {
183 // z1**z2 = exp(z2*ln(z1))
184 // = exp(z2*(ln(|z1|)+i*arg(z1)))
185 // = exp( (x2*ln1 - y2*arg1) + i*(y2*ln1 + x2*arg1) )
186 // = exp(x3 + i*y3)
187 // = exp(x3)*(cos(y3) + i*sin(y3))
188 mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real*lhs_real + lhs_imag*lhs_imag);
189 if (abs1 == 0) {
190 if (rhs_imag == 0) {
191 lhs_real = 1;
192 rhs_real = 0;
193 } else {
194 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "0.0 to a complex power"));
195 }
196 } else {
197 mp_float_t ln1 = MICROPY_FLOAT_C_FUN(log)(abs1);
198 mp_float_t arg1 = MICROPY_FLOAT_C_FUN(atan2)(lhs_imag, lhs_real);
199 mp_float_t x3 = rhs_real * ln1 - rhs_imag * arg1;
200 mp_float_t y3 = rhs_imag * ln1 + rhs_real * arg1;
201 mp_float_t exp_x3 = MICROPY_FLOAT_C_FUN(exp)(x3);
202 lhs_real = exp_x3 * MICROPY_FLOAT_C_FUN(cos)(y3);
203 lhs_imag = exp_x3 * MICROPY_FLOAT_C_FUN(sin)(y3);
204 }
205 break;
206 }
207
Damien Georgeb8a053a2014-04-11 10:10:37 +0100208 case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_real == rhs_real && lhs_imag == rhs_imag);
209
Damien George5589db82014-04-09 20:21:00 +0100210 default:
Damien Georgeea8d06c2014-04-17 23:19:36 +0100211 return MP_OBJ_NOT_SUPPORTED;
Damiend99b0522013-12-21 18:17:45 +0000212 }
213 return mp_obj_new_complex(lhs_real, lhs_imag);
214}
215
Damiend99b0522013-12-21 18:17:45 +0000216#endif