blob: 1036bae420b4aea0e1d80427606c4efbd25e300b [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
24void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
25 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 George71c51812014-01-04 20:21:15 +000033// args are reverse in the array
34static mp_obj_t complex_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
35 switch (n_args) {
36 case 0:
37 return mp_obj_new_complex(0, 0);
38
39 case 1:
40 // TODO allow string as first arg and parse it
41 if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
42 return args[0];
43 } else {
44 return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
45 }
46
47 case 2:
48 {
49 mp_float_t real, imag;
50 if (MP_OBJ_IS_TYPE(args[1], &complex_type)) {
Damien Georgee2e3d112014-01-06 22:13:00 +000051 mp_obj_complex_get(args[1], &real, &imag);
Damien George71c51812014-01-04 20:21:15 +000052 } else {
53 real = mp_obj_get_float(args[1]);
54 imag = 0;
55 }
56 if (MP_OBJ_IS_TYPE(args[0], &complex_type)) {
57 mp_float_t real2, imag2;
Damien Georgee2e3d112014-01-06 22:13:00 +000058 mp_obj_complex_get(args[0], &real2, &imag2);
Damien George71c51812014-01-04 20:21:15 +000059 real -= imag2;
60 imag += real2;
61 } else {
62 imag += mp_obj_get_float(args[0]);
63 }
64 return mp_obj_new_complex(real, imag);
65 }
66
67 default:
68 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));
69 }
70}
71
72static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000073 mp_obj_complex_t *o = o_in;
74 switch (op) {
75 case RT_UNARY_OP_NOT: if (o->real != 0 || o->imag != 0) { return mp_const_true;} else { return mp_const_false; }
76 case RT_UNARY_OP_POSITIVE: return o_in;
77 case RT_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
78 default: return MP_OBJ_NULL; // op not supported
79 }
80}
81
Damien George71c51812014-01-04 20:21:15 +000082static 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 +000083 mp_obj_complex_t *lhs = lhs_in;
84 return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
85}
86
87const mp_obj_type_t complex_type = {
88 { &mp_const_type },
89 "complex",
Damien George97209d32014-01-07 15:58:30 +000090 .print = complex_print,
91 .make_new = complex_make_new,
92 .unary_op = complex_unary_op,
93 .binary_op = complex_binary_op,
Damien Georgee2e3d112014-01-06 22:13:00 +000094};
95
96mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
97 mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t);
98 o->base.type = &complex_type;
99 o->real = real;
100 o->imag = imag;
101 return o;
102}
103
104void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
105 assert(MP_OBJ_IS_TYPE(self_in, &complex_type));
106 mp_obj_complex_t *self = self_in;
107 *real = self->real;
108 *imag = self->imag;
109}
110
111mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) {
112 mp_float_t rhs_real, rhs_imag;
113 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 +0000114 switch (op) {
115 case RT_BINARY_OP_ADD:
116 case RT_BINARY_OP_INPLACE_ADD:
117 lhs_real += rhs_real;
118 lhs_imag += rhs_imag;
119 break;
120 case RT_BINARY_OP_SUBTRACT:
121 case RT_BINARY_OP_INPLACE_SUBTRACT:
122 lhs_real -= rhs_real;
123 lhs_imag -= rhs_imag;
124 break;
125 case RT_BINARY_OP_MULTIPLY:
126 case RT_BINARY_OP_INPLACE_MULTIPLY:
127 {
128 mp_float_t real = lhs_real * rhs_real - lhs_imag * rhs_imag;
129 lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
130 lhs_real = real;
131 break;
132 }
133 /* TODO floor(?) the value
134 case RT_BINARY_OP_FLOOR_DIVIDE:
135 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break;
136 */
137 /* TODO
138 case RT_BINARY_OP_TRUE_DIVIDE:
139 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: val = lhs_val / rhs_val; break;
140 */
141 return NULL; // op not supported
142 }
143 return mp_obj_new_complex(lhs_real, lhs_imag);
144}
145
Damiend99b0522013-12-21 18:17:45 +0000146#endif