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
//! Handle-based FFI API for symbolic calculus functions.

use std::ffi::CStr;
use std::os::raw::c_char;

use crate::symbolic::calculus;
use crate::symbolic::core::Expr;

/// # 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 fn c_str_to_str<'a>(s: *const c_char) -> Option<&'a str> {
    unsafe {
        if s.is_null() {
            None
        } else {
            CStr::from_ptr(s).to_str().ok()
        }
    }
}

/// Differentiates an expression: d/d(var) expr.
///
/// # Safety
/// The caller must ensure `expr` is a valid Expr pointer and `var` is a valid C string.
///
/// # 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 rssn_differentiate(
    expr: *const Expr,
    var: *const c_char,
) -> *mut Expr {
    unsafe {
        if expr.is_null() || var.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return std::ptr::null_mut(),
        };

        Box::into_raw(Box::new(calculus::differentiate(expr_ref, var_str)))
    }
}

/// Integrates an expression: int(expr) d(var).
///
/// # Safety
/// The caller must ensure `expr` is a valid Expr pointer and `var` is a valid C string.
///
/// # 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 rssn_integrate(
    expr: *const Expr,
    var: *const c_char,
) -> *mut Expr {
    unsafe {
        if expr.is_null() || var.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return std::ptr::null_mut(),
        };

        Box::into_raw(Box::new(calculus::integrate(expr_ref, var_str, None, None)))
    }
}

/// Checks if an expression is analytic with respect to a variable.
///
/// # Safety
/// The caller must ensure `expr` is a valid Expr pointer and `var` is a valid C string.
///
/// # 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 rssn_check_analytic(
    expr: *const Expr,
    var: *const c_char,
) -> bool {
    unsafe {
        if expr.is_null() || var.is_null() {
            return false;
        }

        let expr_ref = &*expr;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return false,
        };

        calculus::check_analytic(expr_ref, var_str)
    }
}

/// Computes the limit of an expression: limit(expr, var -> point).
///
/// # Safety
/// The caller must ensure `expr` and `point` are valid Expr pointers and `var` is a valid C string.
///
/// # 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 rssn_limit(
    expr: *const Expr,
    var: *const c_char,
    point: *const Expr,
) -> *mut Expr {
    unsafe {
        if expr.is_null() || var.is_null() || point.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let point_ref = &*point;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return std::ptr::null_mut(),
        };

        Box::into_raw(Box::new(calculus::limit(expr_ref, var_str, point_ref)))
    }
}

/// Computes the definite integral of an 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 rssn_definite_integrate(
    expr: *const Expr,
    var: *const c_char,
    lower: *const Expr,
    upper: *const Expr,
) -> *mut Expr {
    unsafe {
        if expr.is_null() || var.is_null() || lower.is_null() || upper.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let lower_ref = &*lower;

        let upper_ref = &*upper;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return std::ptr::null_mut(),
        };

        Box::into_raw(Box::new(calculus::definite_integrate(
            expr_ref, var_str, lower_ref, upper_ref,
        )))
    }
}

/// Evaluates an expression at a given point.
///
/// # 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 rssn_evaluate_at_point(
    expr: *const Expr,
    var: *const c_char,
    value: *const Expr,
) -> *mut Expr {
    unsafe {
        if expr.is_null() || var.is_null() || value.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let value_ref = &*value;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return std::ptr::null_mut(),
        };

        Box::into_raw(Box::new(calculus::evaluate_at_point(
            expr_ref, var_str, value_ref,
        )))
    }
}

/// Finds poles of an 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 rssn_find_poles(
    expr: *const Expr,
    var: *const c_char,
) -> *mut Vec<Expr> {
    unsafe {
        if expr.is_null() || var.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return std::ptr::null_mut(),
        };

        Box::into_raw(Box::new(calculus::find_poles(expr_ref, var_str)))
    }
}

/// Returns the number of poles found for a given expression.
///
/// # Arguments
///
/// * `poles` - Pointer to a vector of pole expressions as returned by `rssn_find_poles`.
///
/// # Returns
///
/// The number of poles in the vector, or 0 if the pointer is null.
///
/// # Safety
///
/// This function is unsafe because it dereferences a raw pointer to a `Vec<Expr>`.
///
/// # 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 const unsafe extern "C" fn rssn_poles_len(poles: *const Vec<Expr>) -> usize {
    unsafe {
        if poles.is_null() {
            0
        } else {
            (*poles).len()
        }
    }
}

/// Returns a cloned pole expression at the given index.
///
/// # Arguments
///
/// * `poles` - Pointer to a vector of pole expressions as returned by `rssn_find_poles`.
/// * `index` - Zero-based index of the pole to retrieve.
///
/// # Returns
///
/// A newly allocated `Expr` pointer to the selected pole, or null if the pointer is
/// null or the index is out of bounds.
///
/// # Safety
///
/// This function is unsafe because it dereferences a raw pointer to a `Vec<Expr>` and
/// returns ownership of a heap-allocated `Expr` to the caller.
///
/// # 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 rssn_poles_get(
    poles: *const Vec<Expr>,
    index: usize,
) -> *mut Expr {
    unsafe {
        if poles.is_null() {
            return std::ptr::null_mut();
        }

        let poles_ref = &*poles;

        if index >= poles_ref.len() {
            return std::ptr::null_mut();
        }

        Box::into_raw(Box::new(poles_ref[index].clone()))
    }
}

/// Frees a vector of pole expressions previously returned by `rssn_find_poles`.
///
/// # Arguments
///
/// * `poles` - Mutable pointer to a heap-allocated `Vec<Expr>`.
///
/// # Returns
///
/// This function does not return a value.
///
/// # Safety
///
/// This function is unsafe because it takes ownership of a raw pointer and frees the
/// underlying allocation. The pointer must have been created by this library and must
/// not be used after this call.
///
/// # 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 rssn_free_poles(poles: *mut Vec<Expr>) {
    unsafe {
        if !poles.is_null() {
            let _ = Box::from_raw(poles);
        }
    }
}

/// Calculates the residue of a complex function at a given pole.
///
/// # 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 rssn_calculate_residue(
    expr: *const Expr,
    var: *const c_char,
    pole: *const Expr,
) -> *mut Expr {
    unsafe {
        if expr.is_null() || var.is_null() || pole.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let pole_ref = &*pole;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return std::ptr::null_mut(),
        };

        Box::into_raw(Box::new(calculus::calculate_residue(
            expr_ref, var_str, pole_ref,
        )))
    }
}

/// Finds the order of a pole.
///
/// # 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 rssn_find_pole_order(
    expr: *const Expr,
    var: *const c_char,
    pole: *const Expr,
) -> usize {
    unsafe {
        if expr.is_null() || var.is_null() || pole.is_null() {
            return 0;
        }

        let expr_ref = &*expr;

        let pole_ref = &*pole;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return 0,
        };

        calculus::find_pole_order(expr_ref, var_str, pole_ref)
    }
}

/// Substitutes a variable with an 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 rssn_substitute(
    expr: *const Expr,
    var: *const c_char,
    replacement: *const Expr,
) -> *mut Expr {
    unsafe {
        if expr.is_null() || var.is_null() || replacement.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let replacement_ref = &*replacement;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return std::ptr::null_mut(),
        };

        Box::into_raw(Box::new(calculus::substitute(
            expr_ref,
            var_str,
            replacement_ref,
        )))
    }
}

/// Gets real and imaginary parts of an expression.
///
/// Returns a pointer to a tuple (Expr, Expr) - represented as `Vec<Expr>` of size 2 for simplicity?
/// Or return two out pointers?
/// I'll return a `Vec<Expr>` of size 2.
///
/// # 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 rssn_get_real_imag_parts(expr: *const Expr) -> *mut Vec<Expr> {
    unsafe {
        if expr.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let (re, im) = calculus::get_real_imag_parts(expr_ref);

        Box::into_raw(Box::new(vec![re, im]))
    }
}

/// Computes a path 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.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn rssn_path_integrate(
    expr: *const Expr,
    var: *const c_char,
    contour: *const Expr,
) -> *mut Expr {
    unsafe {
        if expr.is_null() || var.is_null() || contour.is_null() {
            return std::ptr::null_mut();
        }

        let expr_ref = &*expr;

        let contour_ref = &*contour;

        let var_str = match c_str_to_str(var) {
            | Some(s) => s,
            | None => return std::ptr::null_mut(),
        };

        Box::into_raw(Box::new(calculus::path_integrate(
            expr_ref,
            var_str,
            contour_ref,
        )))
    }
}