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
use std::ffi::CStr;
use std::os::raw::c_char;

use crate::symbolic::core::Expr;
use crate::symbolic::functional_analysis::BanachSpace;
use crate::symbolic::functional_analysis::HilbertSpace;
use crate::symbolic::functional_analysis::LinearOperator;
use crate::symbolic::functional_analysis::are_orthogonal;
use crate::symbolic::functional_analysis::banach_norm;
use crate::symbolic::functional_analysis::gram_schmidt;
use crate::symbolic::functional_analysis::inner_product;
use crate::symbolic::functional_analysis::norm;
use crate::symbolic::functional_analysis::project;

// --- HilbertSpace ---

/// Creates a new Hilbert space over a specified interval.
///
/// # Arguments
/// * `var` - The name of the independent variable defining the space.
/// * `lower_bound` - Symbolic expression for the lower bound of the interval.
/// * `upper_bound` - Symbolic expression for the upper bound of the interval.
///
/// # Returns
/// A raw pointer to the newly created `HilbertSpace`.
///
/// # 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 rssn_hilbert_space_create(
    var: *const c_char,
    lower_bound: *const Expr,
    upper_bound: *const Expr,
) -> *mut HilbertSpace {
    unsafe {
        let var_str = CStr::from_ptr(var).to_str().unwrap();

        let space = HilbertSpace::new(var_str, (*lower_bound).clone(), (*upper_bound).clone());

        Box::into_raw(Box::new(space))
    }
}

/// Frees a Hilbert space handle.
///
/// # Arguments
/// * `ptr` - Pointer to the `HilbertSpace` to free.
///
/// # 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_hilbert_space_free(ptr: *mut HilbertSpace) {
    unsafe {
        if !ptr.is_null() {
            let _ = Box::from_raw(ptr);
        }
    }
}

// --- BanachSpace ---

/// Creates a new Banach space $L^p$ over a specified interval.
///
/// # Arguments
/// * `var` - The name of the independent variable.
/// * `lower_bound` - Symbolic expression for the lower bound.
/// * `upper_bound` - Symbolic expression for the upper bound.
/// * `p` - Symbolic expression representing the $p$ parameter of the $L^p$ norm.
///
/// # Returns
/// A raw pointer to the newly created `BanachSpace`.
///
/// # 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 rssn_banach_space_create(
    var: *const c_char,
    lower_bound: *const Expr,
    upper_bound: *const Expr,
    p: *const Expr,
) -> *mut BanachSpace {
    unsafe {
        let var_str = CStr::from_ptr(var).to_str().unwrap();

        let space = BanachSpace::new(
            var_str,
            (*lower_bound).clone(),
            (*upper_bound).clone(),
            (*p).clone(),
        );

        Box::into_raw(Box::new(space))
    }
}

/// Frees a Banach space handle.
///
/// # Arguments
/// * `ptr` - Pointer to the `BanachSpace` to free.
///
/// # 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_banach_space_free(ptr: *mut BanachSpace) {
    unsafe {
        if !ptr.is_null() {
            let _ = Box::from_raw(ptr);
        }
    }
}

// --- LinearOperator ---

/// Creates a linear derivative operator handle.
///
/// # Arguments
/// * `var` - The name of the variable with respect to which differentiation is performed.
///
/// # Returns
/// A raw pointer to the `LinearOperator`.
///
/// # 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 rssn_linear_operator_derivative_create(
    var: *const c_char
) -> *mut LinearOperator {
    unsafe {
        let var_str = CStr::from_ptr(var).to_str().unwrap();

        let op = LinearOperator::Derivative(var_str.to_string());

        Box::into_raw(Box::new(op))
    }
}

/// Creates a linear integral operator handle.
///
/// # Arguments
/// * `lower_bound` - Symbolic expression for the lower integration limit.
/// * `var` - The integration variable.
///
/// # Returns
/// A raw pointer to the `LinearOperator`.
///
/// # 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 rssn_linear_operator_integral_create(
    lower_bound: *const Expr,
    var: *const c_char,
) -> *mut LinearOperator {
    unsafe {
        let var_str = CStr::from_ptr(var).to_str().unwrap();

        let op = LinearOperator::Integral((*lower_bound).clone(), var_str.to_string());

        Box::into_raw(Box::new(op))
    }
}

/// Applies a linear operator to a symbolic expression.
///
/// # Arguments
/// * `op` - Handle to the linear operator.
/// * `expr` - Handle to the expression to operate on.
///
/// # Returns
/// A raw pointer to the resulting symbolic 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_linear_operator_apply(
    op: *const LinearOperator,
    expr: *const Expr,
) -> *mut Expr {
    unsafe {
        let result = (*op).apply(&*expr);

        Box::into_raw(Box::new(result))
    }
}

/// Frees a linear operator handle.
///
/// # Arguments
/// * `ptr` - Pointer to the `LinearOperator` to free.
///
/// # 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_linear_operator_free(ptr: *mut LinearOperator) {
    unsafe {
        if !ptr.is_null() {
            let _ = Box::from_raw(ptr);
        }
    }
}

// --- Functions ---

/// Computes the inner product of two functions in a Hilbert space.
///
/// # Arguments
/// * `space` - Handle to the Hilbert space.
/// * `f` - Handle to the first expression.
/// * `g` - Handle to the second expression.
///
/// # Returns
/// A raw pointer to the symbolic expression representing the inner product.
///
/// # 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_inner_product(
    space: *const HilbertSpace,
    f: *const Expr,
    g: *const Expr,
) -> *mut Expr {
    unsafe {
        let result = inner_product(&*space, &*f, &*g);

        Box::into_raw(Box::new(result))
    }
}

/// Computes the $L^2$ norm of a function in a Hilbert space.
///
/// # Arguments
/// * `space` - Handle to the Hilbert space.
/// * `f` - Handle to the expression.
///
/// # Returns
/// A raw pointer to the symbolic expression representing the norm.
///
/// # 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_norm(
    space: *const HilbertSpace,
    f: *const Expr,
) -> *mut Expr {
    unsafe {
        let result = norm(&*space, &*f);

        Box::into_raw(Box::new(result))
    }
}

/// Computes the $L^p$ norm of a function in a Banach space.
///
/// # Arguments
/// * `space` - Handle to the Banach space.
/// * `f` - Handle to the expression.
///
/// # Returns
/// A raw pointer to the symbolic expression representing the norm.
///
/// # 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_banach_norm(
    space: *const BanachSpace,
    f: *const Expr,
) -> *mut Expr {
    unsafe {
        let result = banach_norm(&*space, &*f);

        Box::into_raw(Box::new(result))
    }
}

/// Checks if two functions are orthogonal in a Hilbert space.
///
/// # Arguments
/// * `space` - Handle to the Hilbert space.
/// * `f` - Handle to the first expression.
/// * `g` - Handle to the second expression.
///
/// # Returns
/// `true` if orthogonal, `false` otherwise.
///
/// # 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_are_orthogonal(
    space: *const HilbertSpace,
    f: *const Expr,
    g: *const Expr,
) -> bool {
    unsafe { are_orthogonal(&*space, &*f, &*g) }
}

/// Projects one function onto another within a Hilbert space.
///
/// # Arguments
/// * `space` - Handle to the Hilbert space.
/// * `f` - Handle to the function to project.
/// * `g` - Handle to the function onto which $f$ is projected.
///
/// # Returns
/// A raw pointer to the resulting projection 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_project(
    space: *const HilbertSpace,
    f: *const Expr,
    g: *const Expr,
) -> *mut Expr {
    unsafe {
        let result = project(&*space, &*f, &*g);

        Box::into_raw(Box::new(result))
    }
}

/// Performs the Gram-Schmidt process to produce an orthogonal basis.
///
/// # Arguments
/// * `space` - Handle to the Hilbert space.
/// * `basis_ptr` - Array of handles to the input basis expressions.
/// * `basis_len` - Number of input expressions.
/// * `out_len` - Pointer to store the number of orthogonal expressions produced.
///
/// # Returns
/// A raw pointer to an array of handles to the orthogonalized basis expressions.
///
/// # 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_gram_schmidt(
    space: *const HilbertSpace,
    basis_ptr: *const *const Expr,
    basis_len: usize,
    out_len: *mut usize,
) -> *mut *mut Expr {
    unsafe {
        let basis_slice = std::slice::from_raw_parts(basis_ptr, basis_len);

        let basis: Vec<Expr> = basis_slice.iter().map(|&p| (*p).clone()).collect();

        let orthogonal_basis = gram_schmidt(&*space, &basis);

        *out_len = orthogonal_basis.len();

        let mut out_ptrs = Vec::with_capacity(orthogonal_basis.len());

        for expr in orthogonal_basis {
            out_ptrs.push(Box::into_raw(Box::new(expr)));
        }

        let ptr = out_ptrs.as_mut_ptr();

        std::mem::forget(out_ptrs);

        ptr
    }
}