LMMC
Lamina Mathematic core
载入中...
搜索中...
未找到
stdlib_complex_sets.c
浏览该文件的文档.
1#include "lmmc/stdlib.h"
2
3#include "memory_bridge.h"
4
5#include "stdlib_internal.h"
6
8 lmmc_real_t imag,
10{
11 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
12 if (!lmmc_std_real_is_finite(real) || !lmmc_std_real_is_finite(imag)) {
14 }
15 return lmmc_complex_create(real, imag, out);
16}
17
19{
20 if (!z || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
21 if (!lmmc_std_complex_is_finite(z)) { return LMMC_STATUS_NUMERICAL_FAILURE; }
22 return lmmc_std_store_finite_real(z->real, out);
23}
24
26{
27 if (!z || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
28 if (!lmmc_std_complex_is_finite(z)) { return LMMC_STATUS_NUMERICAL_FAILURE; }
29 return lmmc_std_store_finite_real(z->imag, out);
30}
31
33{
34 lmmc_status_t status;
35 if (!z || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
36 if (!lmmc_std_complex_is_finite(z)) { return LMMC_STATUS_NUMERICAL_FAILURE; }
37 status = lmmc_complex_conj(z, out);
38 if (status != LMMC_STATUS_OK) { return status; }
39 return lmmc_std_complex_is_finite(out) ? LMMC_STATUS_OK
41}
42
44 lmmc_real_t* out)
45{
46 lmmc_real_t value;
47 lmmc_status_t status;
48 if (!z || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
49 if (!lmmc_std_complex_is_finite(z)) { return LMMC_STATUS_NUMERICAL_FAILURE; }
50 status = lmmc_complex_modulus(z, &value);
51 if (status != LMMC_STATUS_OK) { return status; }
52 return lmmc_std_store_finite_real(value, out);
53}
54
56 const lmmc_complex_t* rhs,
57 int* out)
58{
59 if (!lhs || !rhs || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
60 if (!lmmc_std_complex_is_finite(lhs) ||
61 !lmmc_std_complex_is_finite(rhs)) {
63 }
64 *out = (lhs->real == rhs->real && lhs->imag == rhs->imag) ? 1 : 0;
65 return LMMC_STATUS_OK;
66}
67
69 uint64_t* out)
70{
71 uint64_t real_hash;
72 uint64_t imag_hash;
73 if (!z || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
74 if (!lmmc_std_complex_is_finite(z)) {
76 }
77 real_hash = lmmc_std_real_hash_bits(z->real);
78 imag_hash = lmmc_std_real_hash_bits(z->imag);
79 *out = lmmc_std_hash_mix64(real_hash ^
80 (imag_hash + UINT64_C(0x9e3779b97f4a7c15) +
81 (real_hash << 6) + (real_hash >> 2)));
82 return LMMC_STATUS_OK;
83}
84
85static lmmc_complex_t lmmc_std_complex_set_key(const lmmc_complex_t* value)
86{
87 lmmc_complex_t key = *value;
88 key.real = lmmc_std_num_set_key(key.real);
89 key.imag = lmmc_std_num_set_key(key.imag);
90 return key;
91}
92
93static int lmmc_std_complex_set_valid(const lmmc_std_complex_set_t* set)
94{
95 return set && (set->size == 0 || set->data);
96}
97
98static lmmc_status_t lmmc_std_complex_set_require_finite(
99 const lmmc_std_complex_set_t* set)
100{
101 if (!lmmc_std_complex_set_valid(set)) { return LMMC_STATUS_INVALID_ARGUMENT; }
102 for (size_t i = 0; i < set->size; ++i) {
103 if (!lmmc_std_complex_is_finite(&set->data[i])) {
105 }
106 }
107 return LMMC_STATUS_OK;
108}
109
110static int lmmc_std_complex_values_equal(const lmmc_complex_t* lhs,
111 const lmmc_complex_t* rhs)
112{
113 lmmc_complex_t left = lmmc_std_complex_set_key(lhs);
114 lmmc_complex_t right = lmmc_std_complex_set_key(rhs);
115 return left.real == right.real && left.imag == right.imag;
116}
117
118static int lmmc_std_complex_set_contains_value(
119 const lmmc_std_complex_set_t* set,
120 const lmmc_complex_t* value)
121{
122 for (size_t i = 0; i < set->size; ++i) {
123 if (lmmc_std_complex_values_equal(&set->data[i], value)) { return 1; }
124 }
125 return 0;
126}
127
128static lmmc_status_t lmmc_std_complex_set_alloc(
129 size_t capacity,
131{
132 size_t bytes = 0;
133 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
134 out->size = 0;
135 out->data = NULL;
136 out->owns_data = 0;
137 if (capacity == 0) { return LMMC_STATUS_OK; }
138 if (!lmmc_safe_mul_size(capacity, sizeof(lmmc_complex_t), &bytes)) {
140 }
141 out->data = (lmmc_complex_t*)lmmc_memory_alloc(bytes);
142 if (!out->data) { return LMMC_STATUS_ALLOCATION_FAILED; }
143 out->owns_data = 1;
144 return LMMC_STATUS_OK;
145}
146
147static lmmc_status_t lmmc_std_complex_set_append_unique(
149 const lmmc_complex_t* value)
150{
151 if (!set || !lmmc_std_complex_is_finite(value)) {
153 }
154 if (lmmc_std_complex_set_contains_value(set, value)) {
155 return LMMC_STATUS_OK;
156 }
157 if (!set->data) { return LMMC_STATUS_INVALID_ARGUMENT; }
158 set->data[set->size++] = lmmc_std_complex_set_key(value);
159 return LMMC_STATUS_OK;
160}
161
163 size_t count,
165{
166 lmmc_status_t status;
167 if (!out || (count > 0 && !values)) { return LMMC_STATUS_INVALID_ARGUMENT; }
168 status = lmmc_std_complex_set_alloc(count, out);
169 if (status != LMMC_STATUS_OK) { return status; }
170 for (size_t i = 0; i < count; ++i) {
171 status = lmmc_std_complex_set_append_unique(out, &values[i]);
172 if (status != LMMC_STATUS_OK) {
174 return status;
175 }
176 }
177 return LMMC_STATUS_OK;
178}
179
181{
182 if (!set) return;
183 if (set->owns_data && set->data) {
185 }
186 set->size = 0;
187 set->data = NULL;
188 set->owns_data = 0;
189}
190
192 const lmmc_std_complex_set_t* set,
193 const lmmc_complex_t* value,
194 int* out)
195{
196 lmmc_status_t status;
197 if (!value || !out) { return LMMC_STATUS_INVALID_ARGUMENT; }
198 if (!lmmc_std_complex_is_finite(value)) {
200 }
201 status = lmmc_std_complex_set_require_finite(set);
202 if (status != LMMC_STATUS_OK) { return status; }
203 *out = lmmc_std_complex_set_contains_value(set, value);
204 return LMMC_STATUS_OK;
205}
206
208 const lmmc_std_complex_set_t* lhs,
209 const lmmc_std_complex_set_t* rhs,
210 int* out)
211{
212 lmmc_status_t status;
213 if (!out) { return LMMC_STATUS_INVALID_ARGUMENT; }
214 status = lmmc_std_complex_set_require_finite(lhs);
215 if (status != LMMC_STATUS_OK) { return status; }
216 status = lmmc_std_complex_set_require_finite(rhs);
217 if (status != LMMC_STATUS_OK) { return status; }
218 for (size_t i = 0; i < lhs->size; ++i) {
219 if (!lmmc_std_complex_set_contains_value(rhs, &lhs->data[i])) {
220 *out = 0;
221 return LMMC_STATUS_OK;
222 }
223 }
224 *out = 1;
225 return LMMC_STATUS_OK;
226}
227
228static lmmc_status_t lmmc_std_complex_set_binary_alloc(
229 const lmmc_std_complex_set_t* lhs,
230 const lmmc_std_complex_set_t* rhs,
232{
233 lmmc_status_t status;
234 if (!out || (const void*)out == (const void*)lhs ||
235 (const void*)out == (const void*)rhs) {
237 }
238 status = lmmc_std_complex_set_require_finite(lhs);
239 if (status != LMMC_STATUS_OK) { return status; }
240 status = lmmc_std_complex_set_require_finite(rhs);
241 if (status != LMMC_STATUS_OK) { return status; }
242 if (lhs->size > ((size_t)-1) - rhs->size) {
244 }
245 return lmmc_std_complex_set_alloc(lhs->size + rhs->size, out);
246}
247
249 const lmmc_std_complex_set_t* lhs,
250 const lmmc_std_complex_set_t* rhs,
252{
253 lmmc_status_t status = lmmc_std_complex_set_binary_alloc(lhs, rhs, out);
254 if (status != LMMC_STATUS_OK) { return status; }
255 for (size_t i = 0; i < lhs->size; ++i) {
256 status = lmmc_std_complex_set_append_unique(out, &lhs->data[i]);
257 if (status != LMMC_STATUS_OK) {
258 goto fail;
259 }
260 }
261 for (size_t i = 0; i < rhs->size; ++i) {
262 status = lmmc_std_complex_set_append_unique(out, &rhs->data[i]);
263 if (status != LMMC_STATUS_OK) {
264 goto fail;
265 }
266 }
267 return LMMC_STATUS_OK;
268fail:
270 return status;
271}
272
274 const lmmc_std_complex_set_t* lhs,
275 const lmmc_std_complex_set_t* rhs,
277{
278 lmmc_status_t status = lmmc_std_complex_set_binary_alloc(lhs, rhs, out);
279 if (status != LMMC_STATUS_OK) { return status; }
280 for (size_t i = 0; i < lhs->size; ++i) {
281 if (lmmc_std_complex_set_contains_value(rhs, &lhs->data[i])) {
282 status = lmmc_std_complex_set_append_unique(out, &lhs->data[i]);
283 if (status != LMMC_STATUS_OK) {
284 goto fail;
285 }
286 }
287 }
288 return LMMC_STATUS_OK;
289fail:
291 return status;
292}
293
295 const lmmc_std_complex_set_t* lhs,
296 const lmmc_std_complex_set_t* rhs,
298{
299 lmmc_status_t status = lmmc_std_complex_set_binary_alloc(lhs, rhs, out);
300 if (status != LMMC_STATUS_OK) { return status; }
301 for (size_t i = 0; i < lhs->size; ++i) {
302 if (!lmmc_std_complex_set_contains_value(rhs, &lhs->data[i])) {
303 status = lmmc_std_complex_set_append_unique(out, &lhs->data[i]);
304 if (status != LMMC_STATUS_OK) {
305 goto fail;
306 }
307 }
308 }
309 return LMMC_STATUS_OK;
310fail:
312 return status;
313}
314
316 const lmmc_std_complex_set_t* lhs,
317 const lmmc_std_complex_set_t* rhs,
319{
320 lmmc_status_t status = lmmc_std_complex_set_binary_alloc(lhs, rhs, out);
321 if (status != LMMC_STATUS_OK) { return status; }
322 for (size_t i = 0; i < lhs->size; ++i) {
323 if (!lmmc_std_complex_set_contains_value(rhs, &lhs->data[i])) {
324 status = lmmc_std_complex_set_append_unique(out, &lhs->data[i]);
325 if (status != LMMC_STATUS_OK) {
326 goto fail;
327 }
328 }
329 }
330 for (size_t i = 0; i < rhs->size; ++i) {
331 if (!lmmc_std_complex_set_contains_value(lhs, &rhs->data[i])) {
332 status = lmmc_std_complex_set_append_unique(out, &rhs->data[i]);
333 if (status != LMMC_STATUS_OK) {
334 goto fail;
335 }
336 }
337 }
338 return LMMC_STATUS_OK;
339fail:
341 return status;
342}
lmmc_status_t lmmc_complex_create(lmmc_real_t real, lmmc_real_t imag, lmmc_complex_t *out)
从笛卡尔坐标构造复数.
lmmc_status_t lmmc_complex_conj(const lmmc_complex_t *z, lmmc_complex_t *out)
复数共轭: .
lmmc_real_t real
lmmc_status_t lmmc_complex_modulus(const lmmc_complex_t *z, lmmc_real_t *out)
采用缩放计算复数模: .
lmmc_real_t imag
复数类型,包含实部与虚部.
double lmmc_real_t
Public and internal LMMC real type.
void * lmmc_memory_alloc(size_t size)
void lmmc_memory_free(void *pointer)
Private recoverable allocator for persistent LMMC ownership.
lmmc_status_t
LMMC 库统一返回的状态码.
@ LMMC_STATUS_OK
@ LMMC_STATUS_NUMERICAL_FAILURE
@ LMMC_STATUS_ALLOCATION_FAILED
@ LMMC_STATUS_INVALID_ARGUMENT
lmmc_status_t lmmc_std_math_complex_abs(const lmmc_complex_t *z, lmmc_real_t *out)
为 std.math.abs 计算复数模。
lmmc_status_t lmmc_std_math_conj(const lmmc_complex_t *z, lmmc_complex_t *out)
为 std.math.conj 计算复共轭。
lmmc_status_t lmmc_std_math_imag(const lmmc_complex_t *z, lmmc_real_t *out)
提取复数的虚部。
lmmc_status_t lmmc_std_math_real(const lmmc_complex_t *z, lmmc_real_t *out)
提取复数的实部。
lmmc_status_t lmmc_std_math_complex(lmmc_real_t real, lmmc_real_t imag, lmmc_complex_t *out)
由实部与虚部构造复数。
void lmmc_std_complex_set_destroy(lmmc_std_complex_set_t *set)
lmmc_status_t lmmc_std_complex_set_union(const lmmc_std_complex_set_t *lhs, const lmmc_std_complex_set_t *rhs, lmmc_std_complex_set_t *out)
lmmc_status_t lmmc_std_complex_set_subset(const lmmc_std_complex_set_t *lhs, const lmmc_std_complex_set_t *rhs, int *out)
lmmc_status_t lmmc_std_complex_set_difference(const lmmc_std_complex_set_t *lhs, const lmmc_std_complex_set_t *rhs, lmmc_std_complex_set_t *out)
lmmc_status_t lmmc_std_math_complex_equal(const lmmc_complex_t *lhs, const lmmc_complex_t *rhs, int *out)
按 LSR 规范的结构键语义比较复数值。
lmmc_status_t lmmc_std_complex_set_intersection(const lmmc_std_complex_set_t *lhs, const lmmc_std_complex_set_t *rhs, lmmc_std_complex_set_t *out)
lmmc_status_t lmmc_std_math_complex_hash(const lmmc_complex_t *z, uint64_t *out)
为 LSR 规范的表键计算有限复数哈希。
lmmc_status_t lmmc_std_complex_set_contains(const lmmc_std_complex_set_t *set, const lmmc_complex_t *value, int *out)
lmmc_status_t lmmc_std_complex_set_make(const lmmc_complex_t *values, size_t count, lmmc_std_complex_set_t *out)
构造去重的有限复数集合。
lmmc_status_t lmmc_std_complex_set_symmetric_difference(const lmmc_std_complex_set_t *lhs, const lmmc_std_complex_set_t *rhs, lmmc_std_complex_set_t *out)