Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 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 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
Damien George | 20beff9 | 2014-09-11 22:24:45 +0100 | [diff] [blame] | 28 | #include <stdio.h> |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 29 | #include <assert.h> |
| 30 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 31 | #include "py/parsenum.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 32 | #include "py/runtime.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 33 | |
Paul Sokolovsky | 3b6f7b9 | 2014-06-20 01:48:35 +0300 | [diff] [blame] | 34 | #if MICROPY_PY_BUILTINS_COMPLEX |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 35 | |
Damien George | ae49105 | 2014-04-10 20:08:11 +0100 | [diff] [blame] | 36 | #include <math.h> |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 37 | #include "py/formatfloat.h" |
Damien George | 8767d07 | 2014-03-27 22:17:49 +0000 | [diff] [blame] | 38 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 39 | typedef struct _mp_obj_complex_t { |
| 40 | mp_obj_base_t base; |
| 41 | mp_float_t real; |
| 42 | mp_float_t imag; |
| 43 | } mp_obj_complex_t; |
| 44 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 45 | static void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 46 | (void)kind; |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 47 | mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in); |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 48 | #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT |
Damien George | 20beff9 | 2014-09-11 22:24:45 +0100 | [diff] [blame] | 49 | char buf[16]; |
Damien George | 7a72c0d | 2017-04-19 18:46:53 +1000 | [diff] [blame] | 50 | #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C |
| 51 | const int precision = 6; |
| 52 | #else |
stijn | 861670b | 2015-05-16 10:54:19 +0200 | [diff] [blame] | 53 | const int precision = 7; |
Damien George | 7a72c0d | 2017-04-19 18:46:53 +1000 | [diff] [blame] | 54 | #endif |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 55 | #else |
Damien George | 20beff9 | 2014-09-11 22:24:45 +0100 | [diff] [blame] | 56 | char buf[32]; |
stijn | 861670b | 2015-05-16 10:54:19 +0200 | [diff] [blame] | 57 | const int precision = 16; |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 58 | #endif |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 59 | if (o->real == 0) { |
stijn | 861670b | 2015-05-16 10:54:19 +0200 | [diff] [blame] | 60 | mp_format_float(o->imag, buf, sizeof(buf), 'g', precision, '\0'); |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 61 | mp_printf(print, "%sj", buf); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 62 | } else { |
stijn | 861670b | 2015-05-16 10:54:19 +0200 | [diff] [blame] | 63 | mp_format_float(o->real, buf, sizeof(buf), 'g', precision, '\0'); |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 64 | mp_printf(print, "(%s", buf); |
stijn | 709955b | 2015-05-13 21:35:58 +0200 | [diff] [blame] | 65 | if (o->imag >= 0 || isnan(o->imag)) { |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 66 | mp_print_str(print, "+"); |
Damien George | f49782f | 2015-02-02 12:52:14 +0000 | [diff] [blame] | 67 | } |
stijn | 861670b | 2015-05-16 10:54:19 +0200 | [diff] [blame] | 68 | mp_format_float(o->imag, buf, sizeof(buf), 'g', precision, '\0'); |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 69 | mp_printf(print, "%sj)", buf); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 73 | static mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 74 | (void)type_in; |
Damien George | ee7a880 | 2014-05-11 18:37:21 +0100 | [diff] [blame] | 75 | mp_arg_check_num(n_args, n_kw, 0, 2, false); |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 76 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 77 | switch (n_args) { |
| 78 | case 0: |
| 79 | return mp_obj_new_complex(0, 0); |
| 80 | |
| 81 | case 1: |
Damien George | eee1e88 | 2019-01-30 18:49:52 +1100 | [diff] [blame] | 82 | if (mp_obj_is_str(args[0])) { |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 83 | // a string, parse it |
Damien George | 6b34107 | 2017-03-25 19:48:18 +1100 | [diff] [blame] | 84 | size_t l; |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 85 | const char *s = mp_obj_str_get_data(args[0], &l); |
Damien George | 627ba38 | 2022-06-22 10:37:48 +1000 | [diff] [blame] | 86 | return mp_parse_num_complex(s, l, NULL); |
Damien George | eee1e88 | 2019-01-30 18:49:52 +1100 | [diff] [blame] | 87 | } else if (mp_obj_is_type(args[0], &mp_type_complex)) { |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 88 | // a complex, just return it |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 89 | return args[0]; |
| 90 | } else { |
Andrew Leech | 1e87b56 | 2022-07-04 17:35:46 +1000 | [diff] [blame] | 91 | mp_float_t real, imag; |
Damien George | 4fe3e49 | 2022-07-25 15:23:48 +1000 | [diff] [blame] | 92 | mp_obj_get_complex(args[0], &real, &imag); |
| 93 | return mp_obj_new_complex(real, imag); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Damien George | ee7a880 | 2014-05-11 18:37:21 +0100 | [diff] [blame] | 96 | case 2: |
| 97 | default: { |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 98 | mp_float_t real, imag; |
Damien George | eee1e88 | 2019-01-30 18:49:52 +1100 | [diff] [blame] | 99 | if (mp_obj_is_type(args[0], &mp_type_complex)) { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 100 | mp_obj_complex_get(args[0], &real, &imag); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 101 | } else { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 102 | real = mp_obj_get_float(args[0]); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 103 | imag = 0; |
| 104 | } |
Damien George | eee1e88 | 2019-01-30 18:49:52 +1100 | [diff] [blame] | 105 | if (mp_obj_is_type(args[1], &mp_type_complex)) { |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 106 | mp_float_t real2, imag2; |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 107 | mp_obj_complex_get(args[1], &real2, &imag2); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 108 | real -= imag2; |
| 109 | imag += real2; |
| 110 | } else { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 111 | imag += mp_obj_get_float(args[1]); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 112 | } |
| 113 | return mp_obj_new_complex(real, imag); |
| 114 | } |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 118 | static mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) { |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 119 | mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 120 | switch (op) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 121 | case MP_UNARY_OP_BOOL: |
| 122 | return mp_obj_new_bool(o->real != 0 || o->imag != 0); |
| 123 | case MP_UNARY_OP_HASH: |
| 124 | return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag)); |
| 125 | case MP_UNARY_OP_POSITIVE: |
| 126 | return o_in; |
| 127 | case MP_UNARY_OP_NEGATIVE: |
| 128 | return mp_obj_new_complex(-o->real, -o->imag); |
Damien George | fdb2aa8 | 2017-09-18 14:31:03 +1000 | [diff] [blame] | 129 | case MP_UNARY_OP_ABS: |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 130 | return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(o->real * o->real + o->imag * o->imag)); |
| 131 | default: |
| 132 | return MP_OBJ_NULL; // op not supported |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 136 | static mp_obj_t complex_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 137 | mp_obj_complex_t *lhs = MP_OBJ_TO_PTR(lhs_in); |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 138 | return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in); |
| 139 | } |
| 140 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 141 | static void complex_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { |
Damien George | b1bbe96 | 2015-04-01 14:10:50 +0000 | [diff] [blame] | 142 | if (dest[0] != MP_OBJ_NULL) { |
| 143 | // not load attribute |
| 144 | return; |
| 145 | } |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 146 | mp_obj_complex_t *self = MP_OBJ_TO_PTR(self_in); |
Damien George | f20375e | 2014-08-12 19:57:52 +0100 | [diff] [blame] | 147 | if (attr == MP_QSTR_real) { |
| 148 | dest[0] = mp_obj_new_float(self->real); |
| 149 | } else if (attr == MP_QSTR_imag) { |
| 150 | dest[0] = mp_obj_new_float(self->imag); |
| 151 | } |
| 152 | } |
| 153 | |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 154 | MP_DEFINE_CONST_OBJ_TYPE( |
Jim Mussared | 94beeab | 2022-09-17 00:31:23 +1000 | [diff] [blame] | 155 | mp_type_complex, MP_QSTR_complex, MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE, |
| 156 | make_new, complex_make_new, |
Jim Mussared | 662b976 | 2021-07-14 14:38:38 +1000 | [diff] [blame] | 157 | print, complex_print, |
| 158 | unary_op, complex_unary_op, |
| 159 | binary_op, complex_binary_op, |
| 160 | attr, complex_attr |
| 161 | ); |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 162 | |
| 163 | mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) { |
Jim Mussared | 0e7bfc8 | 2022-04-22 17:09:15 +1000 | [diff] [blame] | 164 | mp_obj_complex_t *o = mp_obj_malloc(mp_obj_complex_t, &mp_type_complex); |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 165 | o->real = real; |
| 166 | o->imag = imag; |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 167 | return MP_OBJ_FROM_PTR(o); |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) { |
Damien George | eee1e88 | 2019-01-30 18:49:52 +1100 | [diff] [blame] | 171 | assert(mp_obj_is_type(self_in, &mp_type_complex)); |
Damien George | 999cedb | 2015-11-27 17:01:44 +0000 | [diff] [blame] | 172 | mp_obj_complex_t *self = MP_OBJ_TO_PTR(self_in); |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 173 | *real = self->real; |
| 174 | *imag = self->imag; |
| 175 | } |
| 176 | |
Damien George | 58321dd | 2017-08-29 13:04:01 +1000 | [diff] [blame] | 177 | mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) { |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 178 | mp_float_t rhs_real, rhs_imag; |
Damien George | 9f911d8 | 2020-06-22 10:21:02 +1000 | [diff] [blame] | 179 | if (!mp_obj_get_complex_maybe(rhs_in, &rhs_real, &rhs_imag)) { |
| 180 | return MP_OBJ_NULL; // op not supported |
| 181 | } |
| 182 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 183 | switch (op) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 184 | case MP_BINARY_OP_ADD: |
| 185 | case MP_BINARY_OP_INPLACE_ADD: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 186 | lhs_real += rhs_real; |
| 187 | lhs_imag += rhs_imag; |
| 188 | break; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 189 | case MP_BINARY_OP_SUBTRACT: |
| 190 | case MP_BINARY_OP_INPLACE_SUBTRACT: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 191 | lhs_real -= rhs_real; |
| 192 | lhs_imag -= rhs_imag; |
| 193 | break; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 194 | case MP_BINARY_OP_MULTIPLY: |
Damien George | 5589db8 | 2014-04-09 20:21:00 +0100 | [diff] [blame] | 195 | case MP_BINARY_OP_INPLACE_MULTIPLY: { |
| 196 | mp_float_t real; |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 197 | multiply: |
Damien George | 5589db8 | 2014-04-09 20:21:00 +0100 | [diff] [blame] | 198 | real = lhs_real * rhs_real - lhs_imag * rhs_imag; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 199 | lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real; |
| 200 | lhs_real = real; |
| 201 | break; |
| 202 | } |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 203 | case MP_BINARY_OP_FLOOR_DIVIDE: |
Damien George | 5589db8 | 2014-04-09 20:21:00 +0100 | [diff] [blame] | 204 | case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 205 | mp_raise_TypeError(MP_ERROR_TEXT("can't truncate-divide a complex number")); |
Damien George | 5589db8 | 2014-04-09 20:21:00 +0100 | [diff] [blame] | 206 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 207 | case MP_BINARY_OP_TRUE_DIVIDE: |
Damien George | 5589db8 | 2014-04-09 20:21:00 +0100 | [diff] [blame] | 208 | case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: |
| 209 | if (rhs_imag == 0) { |
| 210 | if (rhs_real == 0) { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 211 | mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("complex divide by zero")); |
Damien George | 5589db8 | 2014-04-09 20:21:00 +0100 | [diff] [blame] | 212 | } |
| 213 | lhs_real /= rhs_real; |
| 214 | lhs_imag /= rhs_real; |
| 215 | } else if (rhs_real == 0) { |
| 216 | mp_float_t real = lhs_imag / rhs_imag; |
| 217 | lhs_imag = -lhs_real / rhs_imag; |
| 218 | lhs_real = real; |
| 219 | } else { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 220 | mp_float_t rhs_len_sq = rhs_real * rhs_real + rhs_imag * rhs_imag; |
Damien George | 5589db8 | 2014-04-09 20:21:00 +0100 | [diff] [blame] | 221 | rhs_real /= rhs_len_sq; |
| 222 | rhs_imag /= -rhs_len_sq; |
| 223 | goto multiply; |
| 224 | } |
| 225 | break; |
| 226 | |
Damien George | ae49105 | 2014-04-10 20:08:11 +0100 | [diff] [blame] | 227 | case MP_BINARY_OP_POWER: |
| 228 | case MP_BINARY_OP_INPLACE_POWER: { |
| 229 | // z1**z2 = exp(z2*ln(z1)) |
| 230 | // = exp(z2*(ln(|z1|)+i*arg(z1))) |
| 231 | // = exp( (x2*ln1 - y2*arg1) + i*(y2*ln1 + x2*arg1) ) |
| 232 | // = exp(x3 + i*y3) |
| 233 | // = exp(x3)*(cos(y3) + i*sin(y3)) |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 234 | mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real * lhs_real + lhs_imag * lhs_imag); |
Damien George | ae49105 | 2014-04-10 20:08:11 +0100 | [diff] [blame] | 235 | if (abs1 == 0) { |
Damien George | 3ed0e5e | 2017-02-03 00:01:45 +1100 | [diff] [blame] | 236 | if (rhs_imag == 0 && rhs_real >= 0) { |
Damien George | bd04ed3 | 2017-02-04 00:23:56 +1100 | [diff] [blame] | 237 | lhs_real = (rhs_real == 0); |
Damien George | ae49105 | 2014-04-10 20:08:11 +0100 | [diff] [blame] | 238 | } else { |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 239 | mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("0.0 to a complex power")); |
Damien George | ae49105 | 2014-04-10 20:08:11 +0100 | [diff] [blame] | 240 | } |
| 241 | } else { |
| 242 | mp_float_t ln1 = MICROPY_FLOAT_C_FUN(log)(abs1); |
| 243 | mp_float_t arg1 = MICROPY_FLOAT_C_FUN(atan2)(lhs_imag, lhs_real); |
| 244 | mp_float_t x3 = rhs_real * ln1 - rhs_imag * arg1; |
| 245 | mp_float_t y3 = rhs_imag * ln1 + rhs_real * arg1; |
| 246 | mp_float_t exp_x3 = MICROPY_FLOAT_C_FUN(exp)(x3); |
| 247 | lhs_real = exp_x3 * MICROPY_FLOAT_C_FUN(cos)(y3); |
| 248 | lhs_imag = exp_x3 * MICROPY_FLOAT_C_FUN(sin)(y3); |
| 249 | } |
| 250 | break; |
| 251 | } |
| 252 | |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 253 | case MP_BINARY_OP_EQUAL: |
| 254 | return mp_obj_new_bool(lhs_real == rhs_real && lhs_imag == rhs_imag); |
Damien George | b8a053a | 2014-04-11 10:10:37 +0100 | [diff] [blame] | 255 | |
Damien George | 5589db8 | 2014-04-09 20:21:00 +0100 | [diff] [blame] | 256 | default: |
Damien George | 6ac5dce | 2014-05-21 19:42:43 +0100 | [diff] [blame] | 257 | return MP_OBJ_NULL; // op not supported |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 258 | } |
| 259 | return mp_obj_new_complex(lhs_real, lhs_imag); |
| 260 | } |
| 261 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 262 | #endif |