rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use std::os::raw::c_char;

use crate::ffi_apis::common::from_json_string;
use crate::ffi_apis::common::to_c_string;
use crate::ffi_apis::common::to_json_string;
use crate::symbolic::complex_analysis::MobiusTransformation;
use crate::symbolic::complex_analysis::PathContinuation;
use crate::symbolic::core::Expr;

/// Creates a new `PathContinuation` object.
/// Takes JSON-serialized inputs for the function, variable, start point, and order.
/// Returns a JSON-serialized `PathContinuation` object.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn path_continuation_new_json(
    func_json: *const c_char,
    var: *const c_char,
    start_point_json: *const c_char,
    order: usize,
) -> *mut c_char {
    unsafe {
        let func: Expr = match from_json_string(func_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let var_str = std::ffi::CStr::from_ptr(var).to_str().unwrap();

        let start_point: Expr = match from_json_string(start_point_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let path_continuation = PathContinuation::new(&func, var_str, &start_point, order);

        to_json_string(&path_continuation)
    }
}

/// Continues the analytic continuation along a given path.
///
/// Takes a JSON-serialized `PathContinuation` object and a JSON-serialized `Vec<Expr>` representing the path points.
/// Returns a C-style string "OK" on success, or an error message on failure.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn path_continuation_continue_along_path_json(
    pc_json: *const c_char,
    path_points_json: *const c_char,
) -> *mut c_char {
    let mut pc: PathContinuation = match from_json_string(pc_json) {
        | Some(e) => e,
        | None => return std::ptr::null_mut(),
    };

    let path_points: Vec<Expr> = match from_json_string(path_points_json) {
        | Some(e) => e,
        | None => return std::ptr::null_mut(),
    };

    match pc.continue_along_path(&path_points) {
        | Ok(()) => to_c_string("OK".to_string()),
        | Err(e) => to_c_string(e),
    }
}

/// Gets the final expression after analytic continuation.
/// Takes a JSON-serialized `PathContinuation` object.
/// Returns a JSON-serialized `Expr` representing the final expression.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn path_continuation_get_final_expression_json(
    pc_json: *const c_char
) -> *mut c_char {
    let pc: PathContinuation = match from_json_string(pc_json) {
        | Some(e) => e,
        | None => return std::ptr::null_mut(),
    };

    to_json_string(&pc.get_final_expression())
}

/// Estimates the radius of convergence of a series.
///
/// Takes a JSON-serialized `Expr` (the series), a C-style string for the variable,
/// a JSON-serialized `Expr` for the center, and an integer for the order.
/// Returns an `f64` representing the estimated radius of convergence.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn estimate_radius_of_convergence_json(
    series_expr_json: *const c_char,
    var: *const c_char,
    center_json: *const c_char,
    order: usize,
) -> f64 {
    unsafe {
        let series_expr: Expr = match from_json_string(series_expr_json) {
            | Some(e) => e,
            | None => return 0.0,
        };

        let var_str = std::ffi::CStr::from_ptr(var).to_str().unwrap();

        let center: Expr = match from_json_string(center_json) {
            | Some(e) => e,
            | None => return 0.0,
        };

        crate::symbolic::complex_analysis::estimate_radius_of_convergence(
            &series_expr,
            var_str,
            &center,
            order,
        )
        .unwrap_or(0.0)
    }
}

/// Calculates the distance between two complex numbers.
/// Takes two JSON-serialized `Expr` representing the complex numbers.
/// Returns an `f64` representing the distance.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn complex_distance_json(
    p1_json: *const c_char,
    p2_json: *const c_char,
) -> f64 {
    let p1: Expr = match from_json_string(p1_json) {
        | Some(e) => e,
        | None => return 0.0,
    };

    let p2: Expr = match from_json_string(p2_json) {
        | Some(e) => e,
        | None => return 0.0,
    };

    crate::symbolic::complex_analysis::complex_distance(&p1, &p2).unwrap_or(0.0)
}

/// Classifies the singularity of a function at a given point.
///
/// Takes a JSON-serialized `Expr` (the function), a C-style string for the variable,
/// a JSON-serialized `Expr` for the singularity point, and an integer for the order.
/// Returns a JSON-serialized `SingularityType` enum.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn classify_singularity_json(
    func_json: *const c_char,
    var: *const c_char,
    singularity_json: *const c_char,
    order: usize,
) -> *mut c_char {
    unsafe {
        let func: Expr = match from_json_string(func_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let var_str = std::ffi::CStr::from_ptr(var).to_str().unwrap();

        let singularity: Expr = match from_json_string(singularity_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let singularity_type = crate::symbolic::complex_analysis::classify_singularity(
            &func,
            var_str,
            &singularity,
            order,
        );

        to_json_string(&singularity_type)
    }
}

/// Computes the Laurent series of a function.
///
/// Takes a JSON-serialized `Expr` (the function), a C-style string for the variable,
/// a JSON-serialized `Expr` for the center, and an integer for the order.
/// Returns a JSON-serialized `Expr` representing the Laurent series.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn laurent_series_json(
    func_json: *const c_char,
    var: *const c_char,
    center_json: *const c_char,
    order: usize,
) -> *mut c_char {
    unsafe {
        let func: Expr = match from_json_string(func_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let var_str = std::ffi::CStr::from_ptr(var).to_str().unwrap();

        let center: Expr = match from_json_string(center_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let series =
            crate::symbolic::complex_analysis::laurent_series(&func, var_str, &center, order);

        to_json_string(&series)
    }
}

/// Calculates the residue of a function at a given singularity.
///
/// Takes a JSON-serialized `Expr` (the function), a C-style string for the variable,
/// and a JSON-serialized `Expr` for the singularity.
/// Returns a JSON-serialized `Expr` representing the residue.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn calculate_residue_json(
    func_json: *const c_char,
    var: *const c_char,
    singularity_json: *const c_char,
) -> *mut c_char {
    unsafe {
        let func: Expr = match from_json_string(func_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let var_str = std::ffi::CStr::from_ptr(var).to_str().unwrap();

        let singularity: Expr = match from_json_string(singularity_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let residue =
            crate::symbolic::complex_analysis::calculate_residue(&func, var_str, &singularity);

        to_json_string(&residue)
    }
}

/// Calculates a contour integral using the residue theorem.
///
/// Takes a JSON-serialized `Expr` (the function), a C-style string for the variable,
/// and a JSON-serialized `Vec<Expr>` for the singularities.
/// Returns a JSON string representing the `Expr` of the integral.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn contour_integral_residue_theorem_json(
    func_json: *const c_char,
    var: *const c_char,
    singularities_json: *const c_char,
) -> *mut c_char {
    unsafe {
        let func: Expr = match from_json_string(func_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let var_str = std::ffi::CStr::from_ptr(var).to_str().unwrap();

        let singularities: Vec<Expr> = match from_json_string(singularities_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let result = crate::symbolic::complex_analysis::contour_integral_residue_theorem(
            &func,
            var_str,
            &singularities,
        );

        to_json_string(&result)
    }
}

/// Creates a new `MobiusTransformation` object from JSON-serialized coefficients.
///
/// Takes JSON-serialized `Expr` for coefficients `a`, `b`, `c`, and `d`.
/// Returns a JSON-serialized `MobiusTransformation` object.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn mobius_transformation_new_json(
    a_json: *const c_char,

    b_json: *const c_char,

    c_json: *const c_char,

    d_json: *const c_char,
) -> *mut c_char {
    let a: Expr = match from_json_string(a_json) {
        | Some(e) => e,
        | None => {
            return std::ptr::null_mut();
        },
    };

    let b: Expr = match from_json_string(b_json) {
        | Some(e) => e,
        | None => {
            return std::ptr::null_mut();
        },
    };

    let c: Expr = match from_json_string(c_json) {
        | Some(e) => e,
        | None => {
            return std::ptr::null_mut();
        },
    };

    let d: Expr = match from_json_string(d_json) {
        | Some(e) => e,
        | None => {
            return std::ptr::null_mut();
        },
    };

    let mobius = MobiusTransformation::new(a, b, c, d);

    to_json_string(&mobius)
}

/// Creates an identity `MobiusTransformation` object.
/// Returns a JSON-serialized identity `MobiusTransformation` object.
#[unsafe(no_mangle)]
pub extern "C" fn mobius_transformation_identity_json() -> *mut c_char {
    let mobius = MobiusTransformation::identity();

    to_json_string(&mobius)
}

/// Applies a Mobius Transformation to a complex number.
///
/// Takes a JSON-serialized `MobiusTransformation` object and a JSON-serialized `Expr` representing the complex number `z`.
/// Returns a JSON-serialized `Expr` representing the result of the transformation.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn mobius_transformation_apply_json(
    mobius_json: *const c_char,

    z_json: *const c_char,
) -> *mut c_char {
    let mobius: MobiusTransformation = match from_json_string(mobius_json) {
        | Some(e) => e,
        | None => return std::ptr::null_mut(),
    };

    let z: Expr = match from_json_string(z_json) {
        | Some(e) => e,
        | None => {
            return std::ptr::null_mut();
        },
    };

    let result = mobius.apply(&z);

    to_json_string(&result)
}

/// Composes two Mobius Transformations.
/// Takes two JSON-serialized `MobiusTransformation` objects.
/// Returns a JSON-serialized `MobiusTransformation` object representing their composition.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn mobius_transformation_compose_json(
    mobius1_json: *const c_char,

    mobius2_json: *const c_char,
) -> *mut c_char {
    let mobius1: MobiusTransformation = match from_json_string(mobius1_json) {
        | Some(e) => e,
        | None => return std::ptr::null_mut(),
    };

    let mobius2: MobiusTransformation = match from_json_string(mobius2_json) {
        | Some(e) => e,
        | None => return std::ptr::null_mut(),
    };

    let result = mobius1.compose(&mobius2);

    to_json_string(&result)
}

/// Computes the inverse of a Mobius Transformation.
/// Takes a JSON-serialized `MobiusTransformation` object.
/// Returns a JSON-serialized `MobiusTransformation` object representing the inverse.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn mobius_transformation_inverse_json(
    mobius_json: *const c_char
) -> *mut c_char {
    let mobius: MobiusTransformation = match from_json_string(mobius_json) {
        | Some(e) => e,
        | None => return std::ptr::null_mut(),
    };

    let result = mobius.inverse();

    to_json_string(&result)
}

/// Applies Cauchy's Integral Formula to a function at a given point.
///
/// Takes a JSON-serialized `Expr` (the function), a C-style string for the variable,
/// and a JSON-serialized `Expr` for the point `z0`.
/// Returns a JSON-serialized `Expr` representing the result of the integral.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cauchy_integral_formula_json(
    func_json: *const c_char,

    var: *const c_char,

    z0_json: *const c_char,
) -> *mut c_char {
    unsafe {
        let func: Expr = match from_json_string(func_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let var_str = std::ffi::CStr::from_ptr(var).to_str().unwrap();

        let z0: Expr = match from_json_string(z0_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let result =
            crate::symbolic::complex_analysis::cauchy_integral_formula(&func, var_str, &z0);

        to_json_string(&result)
    }
}

/// Applies Cauchy's Derivative Formula to compute the nth derivative of a function at a given point.
///
/// Takes a JSON-serialized `Expr` (the function), a C-style string for the variable,
/// a JSON-serialized `Expr` for the point `z0`, and an integer `n` for the order of the derivative.
/// Returns a JSON-serialized `Expr` representing the nth derivative.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
///
/// # Panics
///
/// This function may panic if the FFI input is malformed, null where not expected,
/// or if internal state synchronization fails (e.g., poisoned locks).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn cauchy_derivative_formula_json(
    func_json: *const c_char,

    var: *const c_char,

    z0_json: *const c_char,

    n: usize,
) -> *mut c_char {
    unsafe {
        let func: Expr = match from_json_string(func_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let var_str = std::ffi::CStr::from_ptr(var).to_str().unwrap();

        let z0: Expr = match from_json_string(z0_json) {
            | Some(e) => e,
            | None => return std::ptr::null_mut(),
        };

        let result =
            crate::symbolic::complex_analysis::cauchy_derivative_formula(&func, var_str, &z0, n);

        to_json_string(&result)
    }
}

/// Computes the complex exponential of a given complex number.
/// Takes a JSON-serialized `Expr` representing the complex number `z`.
/// Returns a JSON-serialized `Expr` representing `e^z`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn complex_exp_json(z_json: *const c_char) -> *mut c_char {
    let z: Expr = match from_json_string(z_json) {
        | Some(e) => e,
        | None => {
            return std::ptr::null_mut();
        },
    };

    let result = crate::symbolic::complex_analysis::complex_exp(&z);

    to_json_string(&result)
}

/// Computes the complex natural logarithm of a given complex number.
/// Takes a JSON-serialized `Expr` representing the complex number `z`.
/// Returns a JSON-serialized `Expr` representing `ln(z)`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn complex_log_json(z_json: *const c_char) -> *mut c_char {
    let z: Expr = match from_json_string(z_json) {
        | Some(e) => e,
        | None => {
            return std::ptr::null_mut();
        },
    };

    let result = crate::symbolic::complex_analysis::complex_log(&z);

    to_json_string(&result)
}

/// Computes the argument (phase angle) of a given complex number.
/// Takes a JSON-serialized `Expr` representing the complex number `z`.
/// Returns a JSON-serialized `Expr` representing the argument of `z`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn complex_arg_json(z_json: *const c_char) -> *mut c_char {
    let z: Expr = match from_json_string(z_json) {
        | Some(e) => e,
        | None => {
            return std::ptr::null_mut();
        },
    };

    let result = crate::symbolic::complex_analysis::complex_arg(&z);

    to_json_string(&result)
}

/// Computes the modulus (magnitude) of a given complex number.
/// Takes a JSON-serialized `Expr` representing the complex number `z`.
/// Returns a JSON-serialized `Expr` representing the modulus of `z`.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers as part of the FFI boundary.
/// The caller must ensure:
/// 1. All pointer arguments are valid and point to initialized memory.
/// 2. The memory layout of passed structures matches the expected C-ABI layout.
/// 3. Any pointers returned by this function are managed according to the API's ownership rules.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn complex_modulus_json(z_json: *const c_char) -> *mut c_char {
    let z: Expr = match from_json_string(z_json) {
        | Some(e) => e,
        | None => {
            return std::ptr::null_mut();
        },
    };

    let result = crate::symbolic::complex_analysis::complex_modulus(&z);

    to_json_string(&result)
}