blob: b56f75c4cd5bd2687117dc626e69c94fd7b6a12b [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
2#include <stdint.h>
3#include <string.h>
4#include <assert.h>
5
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
Damien George71c51812014-01-04 20:21:15 +00009#include "mpqstr.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "obj.h"
11#include "runtime0.h"
12#include "map.h"
13
14#if MICROPY_ENABLE_FLOAT
15
16typedef struct _mp_obj_complex_t {
17 mp_obj_base_t base;
18 mp_float_t real;
19 mp_float_t imag;
20} mp_obj_complex_t;
21
22mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
23
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020024void 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 +000025 mp_obj_complex_t *o = o_in;
26 if (o->real == 0) {
27 print(env, "%.8gj", o->imag);
28 } else {
29 print(env, "(%.8g+%.8gj)", o->real, o->imag);
30 }
31}
32
Damien George20006db2014-01-18 14:10:48 +000033static mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
34 // TODO check n_kw == 0
35
Damien George71c51812014-01-04 20:21:15 +000036 switch (n_args) {
37 case 0:
38 return mp_obj_new_complex(0, 0);
39
40 case 1:
41 // TODO allow string as first arg and parse it
42 if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
43 return args[0];
44 } else {
45 return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
46 }
47
48 case 2:
49 {
50 mp_float_t real, imag;
Damien George20006db2014-01-18 14:10:48 +000051 if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
52 mp_obj_complex_get(args[0], &real, &imag);
Damien George71c51812014-01-04 20:21:15 +000053 } else {
Damien George20006db2014-01-18 14:10:48 +000054 real = mp_obj_get_float(args[0]);
Damien George71c51812014-01-04 20:21:15 +000055 imag = 0;
56 }
Damien George20006db2014-01-18 14:10:48 +000057 if (MP_OBJ_IS_TYPE(args[1], &complex_type)) {
Damien George71c51812014-01-04 20:21:15 +000058 mp_float_t real2, imag2;
Damien George20006db2014-01-18 14:10:48 +000059 mp_obj_complex_get(args[1], &real2, &imag2);
Damien George71c51812014-01-04 20:21:15 +000060 real -= imag2;
61 imag += real2;
62 } else {
Damien George20006db2014-01-18 14:10:48 +000063 imag += mp_obj_get_float(args[1]);
Damien George71c51812014-01-04 20:21:15 +000064 }
65 return mp_obj_new_complex(real, imag);
66 }
67
68 default:
69 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "complex takes at most 2 arguments, %d given", (void*)(machine_int_t)n_args));
70 }
71}
72
73static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000074 mp_obj_complex_t *o = o_in;
75 switch (op) {
76 case RT_UNARY_OP_NOT: if (o->real != 0 || o->imag != 0) { return mp_const_true;} else { return mp_const_false; }
77 case RT_UNARY_OP_POSITIVE: return o_in;
78 case RT_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
79 default: return MP_OBJ_NULL; // op not supported
80 }
81}
82
Damien George71c51812014-01-04 20:21:15 +000083static 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 +000084 mp_obj_complex_t *lhs = lhs_in;
85 return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
86}
87
88const mp_obj_type_t complex_type = {
89 { &mp_const_type },
90 "complex",
Damien George97209d32014-01-07 15:58:30 +000091 .print = complex_print,
92 .make_new = complex_make_new,
93 .unary_op = complex_unary_op,
94 .binary_op = complex_binary_op,
Damien Georgee2e3d112014-01-06 22:13:00 +000095};
96
97mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
98 mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t);
99 o->base.type = &complex_type;
100 o->real = real;
101 o->imag = imag;
102 return o;
103}
104
105void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
106 assert(MP_OBJ_IS_TYPE(self_in, &complex_type));
107 mp_obj_complex_t *self = self_in;
108 *real = self->real;
109 *imag = self->imag;
110}
111
112mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) {
113 mp_float_t rhs_real, rhs_imag;
114 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 +0000115 switch (op) {
116 case RT_BINARY_OP_ADD:
117 case RT_BINARY_OP_INPLACE_ADD:
118 lhs_real += rhs_real;
119 lhs_imag += rhs_imag;
120 break;
121 case RT_BINARY_OP_SUBTRACT:
122 case RT_BINARY_OP_INPLACE_SUBTRACT:
123 lhs_real -= rhs_real;
124 lhs_imag -= rhs_imag;
125 break;
126 case RT_BINARY_OP_MULTIPLY:
127 case RT_BINARY_OP_INPLACE_MULTIPLY:
128 {
129 mp_float_t real = lhs_real * rhs_real - lhs_imag * rhs_imag;
130 lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
131 lhs_real = real;
132 break;
133 }
134 /* TODO floor(?) the value
135 case RT_BINARY_OP_FLOOR_DIVIDE:
136 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break;
137 */
138 /* TODO
139 case RT_BINARY_OP_TRUE_DIVIDE:
140 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: val = lhs_val / rhs_val; break;
141 */
142 return NULL; // op not supported
143 }
144 return mp_obj_new_complex(lhs_real, lhs_imag);
145}
146
Damiend99b0522013-12-21 18:17:45 +0000147#endif