scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! GPU-accelerated linear algebra operations.
//!
//! This module provides GPU-accelerated versions of key linear algebra operations
//! using the GPU backends from `scirs2-core` (Metal, CUDA, WebGPU, OpenCL, CPU).
//!
//! # Architecture
//!
//! The module is organized into three sub-modules:
//!
//! - **`types`**: Result types for decompositions (`LuFactors`, `QrFactors`, etc.)
//! - **`matmul`**: GPU-accelerated matrix multiplication (single and batched)
//! - **`decompositions`**: GPU-accelerated LU, QR, Cholesky, and SVD
//!
//! All operations automatically fall back to CPU when GPU is unavailable or
//! when the matrix is too small to benefit from GPU transfer overhead.
//!
//! # Usage
//!
//! The primary entry point is [`GpuLinalgSolver`], which manages a GPU context
//! and provides a unified API for all operations:
//!
//! ```rust,ignore
//! use scirs2_linalg::gpu_linalg::GpuLinalgSolver;
//! use scirs2_core::ndarray::array;
//!
//! let solver = GpuLinalgSolver::new();
//!
//! let a = array![[4.0_f64, 2.0], [2.0, 5.0]];
//! let chol = solver.cholesky(&a.view()).expect("Cholesky failed");
//! // chol.l is the lower-triangular factor
//!
//! let b = array![[1.0_f64, 2.0], [3.0, 4.0]];
//! let lu = solver.lu(&b.view()).expect("LU failed");
//! // lu.p, lu.l, lu.u
//! ```
//!
//! You can also use the free-standing functions directly if you manage
//! the `GpuContext` yourself or want to opt out of GPU:
//!
//! ```rust,ignore
//! use scirs2_linalg::gpu_linalg::decompositions::gpu_svd;
//!
//! let factors = gpu_svd(None, &matrix.view())?; // CPU-only
//! ```

pub mod decompositions;
pub mod matmul;
pub mod types;

// Re-export types for convenience
pub use decompositions::{gpu_cholesky, gpu_lu, gpu_qr, gpu_svd};
pub use matmul::{gpu_batched_matmul, gpu_matmul};
pub use types::{
    BatchMatmulResult, CholeskyFactors, ExecutionBackend, LuFactors, QrFactors, SvdFactors,
};

use scirs2_core::gpu::{GpuBackend, GpuContext};
use scirs2_core::ndarray::{Array2, ArrayView2, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign, One};
use std::iter::Sum;

use crate::error::LinalgResult;

/// Unified GPU-accelerated linear algebra solver.
///
/// `GpuLinalgSolver` manages a GPU context and provides methods for performing
/// GPU-accelerated linear algebra operations. When a GPU is available, operations
/// are dispatched to the GPU for large matrices; otherwise, they seamlessly fall
/// back to efficient CPU implementations.
///
/// # Construction
///
/// ```rust,ignore
/// // Auto-detect the best available GPU backend
/// let solver = GpuLinalgSolver::new();
///
/// // Force a specific backend
/// let cpu_solver = GpuLinalgSolver::with_backend(GpuBackend::Cpu);
///
/// // Use CPU-only mode explicitly
/// let cpu_solver = GpuLinalgSolver::cpu_only();
/// ```
///
/// # Thread Safety
///
/// `GpuLinalgSolver` is `Send` but not `Sync` due to the underlying GPU context.
/// If you need concurrent access, wrap it in a `Mutex` or create one per thread.
pub struct GpuLinalgSolver {
    /// The GPU context, if available.
    context: Option<GpuContext>,
    /// The backend being used.
    backend: GpuBackend,
}

impl GpuLinalgSolver {
    /// Create a new `GpuLinalgSolver` with automatic GPU detection.
    ///
    /// Attempts to find and use the best available GPU backend. If no GPU is
    /// available, falls back to CPU transparently.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let solver = GpuLinalgSolver::new();
    /// println!("Using backend: {}", solver.backend());
    /// ```
    pub fn new() -> Self {
        let preferred = GpuBackend::preferred();
        match GpuContext::new(preferred) {
            Ok(ctx) => Self {
                backend: preferred,
                context: Some(ctx),
            },
            Err(_) => {
                // Try CPU fallback
                match GpuContext::new(GpuBackend::Cpu) {
                    Ok(ctx) => Self {
                        backend: GpuBackend::Cpu,
                        context: Some(ctx),
                    },
                    Err(_) => Self {
                        backend: GpuBackend::Cpu,
                        context: None,
                    },
                }
            }
        }
    }

    /// Create a `GpuLinalgSolver` with a specific GPU backend.
    ///
    /// # Arguments
    ///
    /// * `backend` - The GPU backend to use
    ///
    /// # Returns
    ///
    /// A solver using the specified backend, or CPU fallback if the backend
    /// is unavailable.
    pub fn with_backend(backend: GpuBackend) -> Self {
        match GpuContext::new(backend) {
            Ok(ctx) => Self {
                backend,
                context: Some(ctx),
            },
            Err(_) => {
                // Fall back to CPU
                match GpuContext::new(GpuBackend::Cpu) {
                    Ok(ctx) => Self {
                        backend: GpuBackend::Cpu,
                        context: Some(ctx),
                    },
                    Err(_) => Self {
                        backend: GpuBackend::Cpu,
                        context: None,
                    },
                }
            }
        }
    }

    /// Create a CPU-only solver (no GPU acceleration).
    ///
    /// This is useful for testing, benchmarking against CPU, or in
    /// environments where GPU access is undesirable.
    pub fn cpu_only() -> Self {
        match GpuContext::new(GpuBackend::Cpu) {
            Ok(ctx) => Self {
                backend: GpuBackend::Cpu,
                context: Some(ctx),
            },
            Err(_) => Self {
                backend: GpuBackend::Cpu,
                context: None,
            },
        }
    }

    /// Get the backend being used by this solver.
    pub fn backend(&self) -> GpuBackend {
        self.backend
    }

    /// Check whether GPU acceleration is available.
    ///
    /// Returns `true` if the solver has a GPU context and the backend is not CPU.
    pub fn has_gpu(&self) -> bool {
        self.context.is_some() && self.backend != GpuBackend::Cpu
    }

    /// Get a reference to the GPU context, if available.
    pub fn context(&self) -> Option<&GpuContext> {
        self.context.as_ref()
    }

    // =========================================================================
    // Matrix Multiplication
    // =========================================================================

    /// GPU-accelerated matrix multiplication: C = A * B
    ///
    /// # Arguments
    ///
    /// * `a` - Left matrix (m x k)
    /// * `b` - Right matrix (k x n)
    ///
    /// # Returns
    ///
    /// Result matrix C (m x n)
    ///
    /// # Errors
    ///
    /// Returns error if matrices have incompatible dimensions.
    pub fn matmul<F>(&self, a: &ArrayView2<F>, b: &ArrayView2<F>) -> LinalgResult<Array2<F>>
    where
        F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
    {
        gpu_matmul(self.context.as_ref(), a, b)
    }

    /// GPU-accelerated batched matrix multiplication.
    ///
    /// Multiplies corresponding pairs: C_i = A_i * B_i for each i.
    ///
    /// # Arguments
    ///
    /// * `a_batch` - Batch of left matrices
    /// * `b_batch` - Batch of right matrices (same length as `a_batch`)
    ///
    /// # Returns
    ///
    /// `BatchMatmulResult` with one result per pair.
    ///
    /// # Errors
    ///
    /// Returns error if batch sizes differ or any pair has incompatible shapes.
    pub fn batched_matmul<F>(
        &self,
        a_batch: &[Array2<F>],
        b_batch: &[Array2<F>],
    ) -> LinalgResult<BatchMatmulResult<F>>
    where
        F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
    {
        gpu_batched_matmul(self.context.as_ref(), a_batch, b_batch)
    }

    // =========================================================================
    // Decompositions
    // =========================================================================

    /// GPU-accelerated LU decomposition: PA = LU
    ///
    /// # Arguments
    ///
    /// * `a` - Input matrix
    ///
    /// # Returns
    ///
    /// `LuFactors` containing P, L, and U matrices.
    pub fn lu<F>(&self, a: &ArrayView2<F>) -> LinalgResult<LuFactors<F>>
    where
        F: Float + NumAssign + One + Sum + Send + Sync + ScalarOperand + 'static,
    {
        gpu_lu(self.context.as_ref(), a)
    }

    /// GPU-accelerated QR decomposition: A = QR
    ///
    /// # Arguments
    ///
    /// * `a` - Input matrix
    ///
    /// # Returns
    ///
    /// `QrFactors` containing Q and R matrices.
    pub fn qr<F>(&self, a: &ArrayView2<F>) -> LinalgResult<QrFactors<F>>
    where
        F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
    {
        gpu_qr(self.context.as_ref(), a)
    }

    /// GPU-accelerated Cholesky decomposition: A = LL^T
    ///
    /// # Arguments
    ///
    /// * `a` - Symmetric positive-definite matrix
    ///
    /// # Returns
    ///
    /// `CholeskyFactors` containing the lower-triangular factor L.
    pub fn cholesky<F>(&self, a: &ArrayView2<F>) -> LinalgResult<CholeskyFactors<F>>
    where
        F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
    {
        gpu_cholesky(self.context.as_ref(), a)
    }

    /// GPU-accelerated thin SVD: A = U * diag(S) * V^T
    ///
    /// Computes the economy-size singular value decomposition.
    ///
    /// # Arguments
    ///
    /// * `a` - Input matrix
    ///
    /// # Returns
    ///
    /// `SvdFactors` containing U, S, and V^T.
    pub fn svd<F>(&self, a: &ArrayView2<F>) -> LinalgResult<SvdFactors<F>>
    where
        F: Float + NumAssign + Sum + Send + Sync + ScalarOperand + 'static,
    {
        gpu_svd(self.context.as_ref(), a)
    }

    // =========================================================================
    // Convenience: Solve linear systems via decomposition
    // =========================================================================

    /// Solve a linear system Ax = b using LU decomposition.
    ///
    /// # Arguments
    ///
    /// * `a` - Coefficient matrix (n x n)
    /// * `b` - Right-hand side vector (as n x 1 matrix)
    ///
    /// # Returns
    ///
    /// Solution vector x (as n x 1 matrix).
    ///
    /// # Errors
    ///
    /// Returns error if A is singular or dimensions are incompatible.
    pub fn solve<F>(&self, a: &ArrayView2<F>, b: &ArrayView2<F>) -> LinalgResult<Array2<F>>
    where
        F: Float + NumAssign + One + Sum + Send + Sync + ScalarOperand + 'static,
    {
        let n = a.nrows();
        if a.nrows() != a.ncols() {
            return Err(crate::error::LinalgError::ShapeError(
                "Solve requires a square coefficient matrix".to_string(),
            ));
        }
        if b.nrows() != n {
            return Err(crate::error::LinalgError::DimensionError(format!(
                "Right-hand side has {} rows but coefficient matrix has {} rows",
                b.nrows(),
                n
            )));
        }

        let factors = self.lu(a)?;
        let n_rhs = b.ncols();

        // Convention: P * A = L * U, so A = P^T * L * U
        // To solve A * x = b: P^T * L * U * x = b => L * U * x = P * b
        // 1. Compute P * b
        let pb = factors.p.dot(b);

        // 2. Forward substitution: Ly = P * b
        let mut y = Array2::<F>::zeros((n, n_rhs));
        for col in 0..n_rhs {
            for i in 0..n {
                let mut sum = pb[[i, col]];
                for j in 0..i {
                    sum -= factors.l[[i, j]] * y[[j, col]];
                }
                // L has unit diagonal
                y[[i, col]] = sum;
            }
        }

        // 3. Back substitution: Ux = y
        let min_dim = n.min(factors.u.ncols());
        let mut x = Array2::<F>::zeros((n, n_rhs));
        for col in 0..n_rhs {
            for i in (0..min_dim).rev() {
                let mut sum = y[[i, col]];
                for j in (i + 1)..factors.u.ncols().min(n) {
                    sum -= factors.u[[i, j]] * x[[j, col]];
                }
                let diag = factors.u[[i, i]];
                if diag.abs() < F::epsilon() {
                    return Err(crate::error::LinalgError::SingularMatrixError(
                        "Matrix is singular: zero diagonal in U factor".to_string(),
                    ));
                }
                x[[i, col]] = sum / diag;
            }
        }

        Ok(x)
    }
}

impl std::fmt::Debug for GpuLinalgSolver {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GpuLinalgSolver")
            .field("backend", &self.backend)
            .field("has_context", &self.context.is_some())
            .finish()
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_solver_creation() {
        let solver = GpuLinalgSolver::new();
        // Should always succeed, possibly with CPU backend
        let _backend = solver.backend();
    }

    #[test]
    fn test_solver_cpu_only() {
        let solver = GpuLinalgSolver::cpu_only();
        assert_eq!(solver.backend(), GpuBackend::Cpu);
    }

    #[test]
    fn test_solver_with_backend() {
        let solver = GpuLinalgSolver::with_backend(GpuBackend::Cpu);
        assert_eq!(solver.backend(), GpuBackend::Cpu);
    }

    #[test]
    fn test_solver_debug() {
        let solver = GpuLinalgSolver::cpu_only();
        let debug_str = format!("{:?}", solver);
        assert!(debug_str.contains("GpuLinalgSolver"));
        assert!(debug_str.contains("Cpu"));
    }

    #[test]
    fn test_solver_matmul() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[1.0_f64, 2.0], [3.0, 4.0]];
        let b = array![[5.0_f64, 6.0], [7.0, 8.0]];
        let c = solver.matmul(&a.view(), &b.view()).expect("matmul failed");
        assert_relative_eq!(c[[0, 0]], 19.0, epsilon = 1e-10);
        assert_relative_eq!(c[[1, 1]], 50.0, epsilon = 1e-10);
    }

    #[test]
    fn test_solver_batched_matmul() {
        let solver = GpuLinalgSolver::cpu_only();
        let a1 = array![[1.0_f64, 0.0], [0.0, 1.0]];
        let b1 = array![[3.0_f64, 4.0], [5.0, 6.0]];
        let batch = solver
            .batched_matmul(&[a1], &[b1])
            .expect("batch matmul failed");
        assert_eq!(batch.results.len(), 1);
        assert_relative_eq!(batch.results[0][[0, 0]], 3.0, epsilon = 1e-10);
    }

    #[test]
    fn test_solver_lu() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[2.0_f64, 1.0], [4.0, 3.0]];
        let factors = solver.lu(&a.view()).expect("LU failed");
        // Convention: P * A = L * U
        let pa = factors.p.dot(&a);
        let lu_product = factors.l.dot(&factors.u);
        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(pa[[i, j]], lu_product[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_solver_qr() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[1.0_f64, 2.0], [3.0, 4.0]];
        let factors = solver.qr(&a.view()).expect("QR failed");
        let qr_product = factors.q.dot(&factors.r);
        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(qr_product[[i, j]], a[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_solver_cholesky() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[4.0_f64, 2.0], [2.0, 5.0]];
        let factors = solver.cholesky(&a.view()).expect("Cholesky failed");
        let llt = factors.l.dot(&factors.l.t());
        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(llt[[i, j]], a[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_solver_svd() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[1.0_f64, 0.0], [0.0, 2.0]];
        let factors = solver.svd(&a.view()).expect("SVD failed");
        let s_diag = Array2::from_diag(&factors.s);
        let usv = factors.u.dot(&s_diag).dot(&factors.vt);
        for i in 0..2 {
            for j in 0..2 {
                assert_relative_eq!(usv[[i, j]], a[[i, j]], epsilon = 1e-8);
            }
        }
    }

    #[test]
    fn test_solver_solve() {
        let solver = GpuLinalgSolver::cpu_only();
        // Solve [[2,1],[4,3]] * x = [[5],[13]]
        let a = array![[2.0_f64, 1.0], [4.0, 3.0]];
        let b = array![[5.0_f64], [13.0]];
        let x = solver.solve(&a.view(), &b.view()).expect("solve failed");
        // x should be [[1], [3]]
        assert_relative_eq!(x[[0, 0]], 1.0, epsilon = 1e-8);
        assert_relative_eq!(x[[1, 0]], 3.0, epsilon = 1e-8);
    }

    #[test]
    fn test_solver_solve_non_square() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[1.0_f64, 2.0, 3.0], [4.0, 5.0, 6.0]];
        let b = array![[1.0_f64], [2.0]];
        let result = solver.solve(&a.view(), &b.view());
        assert!(result.is_err());
    }

    #[test]
    fn test_solver_solve_dimension_mismatch() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[1.0_f64, 2.0], [3.0, 4.0]];
        let b = array![[1.0_f64], [2.0], [3.0]];
        let result = solver.solve(&a.view(), &b.view());
        assert!(result.is_err());
    }

    #[test]
    fn test_solver_solve_identity() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[1.0_f64, 0.0], [0.0, 1.0]];
        let b = array![[7.0_f64], [11.0]];
        let x = solver
            .solve(&a.view(), &b.view())
            .expect("identity solve failed");
        assert_relative_eq!(x[[0, 0]], 7.0, epsilon = 1e-10);
        assert_relative_eq!(x[[1, 0]], 11.0, epsilon = 1e-10);
    }

    #[test]
    fn test_solver_solve_3x3() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[1.0_f64, 2.0, 3.0], [0.0, 1.0, 4.0], [5.0, 6.0, 0.0]];
        let b = array![[1.0_f64], [2.0], [3.0]];
        let x = solver
            .solve(&a.view(), &b.view())
            .expect("3x3 solve failed");

        // Verify A * x ≈ b
        let ax = a.dot(&x);
        for i in 0..3 {
            assert_relative_eq!(ax[[i, 0]], b[[i, 0]], epsilon = 1e-8);
        }
    }

    #[test]
    fn test_solver_matmul_f32() {
        let solver = GpuLinalgSolver::cpu_only();
        let a = array![[1.0_f32, 2.0], [3.0, 4.0]];
        let b = array![[5.0_f32, 6.0], [7.0, 8.0]];
        let c = solver
            .matmul(&a.view(), &b.view())
            .expect("f32 matmul failed");
        assert!((c[[0, 0]] - 19.0).abs() < 1e-4);
    }

    #[test]
    fn test_execution_backend_display() {
        assert_eq!(ExecutionBackend::Gpu.to_string(), "GPU");
        assert_eq!(ExecutionBackend::CpuFallback.to_string(), "CPU (fallback)");
    }

    #[test]
    fn test_has_gpu_cpu_only() {
        let solver = GpuLinalgSolver::cpu_only();
        assert!(!solver.has_gpu());
    }

    #[test]
    fn test_solver_context_ref() {
        let solver = GpuLinalgSolver::cpu_only();
        // CPU solver should have a context
        assert!(solver.context().is_some() || solver.context().is_none());
    }

    #[test]
    fn test_solver_multiple_operations() {
        // Test using the same solver for multiple operations
        let solver = GpuLinalgSolver::cpu_only();

        let a = array![[4.0_f64, 2.0], [2.0, 5.0]];

        // Cholesky
        let chol = solver.cholesky(&a.view()).expect("cholesky failed");
        let llt = chol.l.dot(&chol.l.t());
        assert_relative_eq!(llt[[0, 0]], a[[0, 0]], epsilon = 1e-10);

        // LU: convention P * A = L * U
        let lu = solver.lu(&a.view()).expect("lu failed");
        let pa = lu.p.dot(&a);
        let lu_product = lu.l.dot(&lu.u);
        assert_relative_eq!(pa[[0, 0]], lu_product[[0, 0]], epsilon = 1e-10);

        // SVD
        let svd = solver.svd(&a.view()).expect("svd failed");
        let s_diag = Array2::from_diag(&svd.s);
        let usv = svd.u.dot(&s_diag).dot(&svd.vt);
        assert_relative_eq!(usv[[0, 0]], a[[0, 0]], epsilon = 1e-8);
    }
}