rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
//! Utility operations for tensors
//! テンソルのユーティリティ演算
//!
//! This module provides utility functions including mapping, batch operations,
//! and helper functions for tensor manipulation.
//! このモジュールはマッピング、バッチ操作、テンソル操作のヘルパー関数を含むユーティリティ関数を提供します。

use crate::tensor::Tensor;
use num_traits::Float;

impl<T: Float + 'static + ndarray::ScalarOperand + num_traits::FromPrimitive> Tensor<T> {
    /// Get the batch size (first dimension)
    /// バッチサイズを取得(最初の次元)
    pub fn batch_size(&self) -> usize {
        self.shape().get(0).copied().unwrap_or(1)
    }

    /// Apply function to each element
    /// 各要素に関数を適用
    pub fn map<F>(&self, f: F) -> Tensor<T>
    where
        F: Fn(T) -> T,
    {
        let mapped_data: Vec<T> = self.data.iter().map(|&x| f(x)).collect();
        Tensor::from_vec(mapped_data, self.shape().to_vec())
    }

    /// Apply function with index to each element
    /// インデックス付き関数を各要素に適用
    pub fn map_with_index<F>(&self, f: F) -> Tensor<T>
    where
        F: Fn(T, usize) -> T,
    {
        let mapped_data: Vec<T> = self.data.iter().enumerate()
            .map(|(i, &x)| f(x, i))
            .collect();
        Tensor::from_vec(mapped_data, self.shape().to_vec())
    }

    /// Apply function element-wise between two tensors
    /// 2つのテンソル間で要素ごとに関数を適用
    pub fn zip_with<F>(&self, other: &Tensor<T>, f: F) -> Result<Tensor<T>, String>
    where
        F: Fn(T, T) -> T,
    {
        if self.shape() != other.shape() {
            return Err("Shape mismatch for zip_with operation".to_string());
        }

        let result_data: Vec<T> = self.data.iter()
            .zip(other.data.iter())
            .map(|(&a, &b)| f(a, b))
            .collect();
        
        Ok(Tensor::from_vec(result_data, self.shape().to_vec()))
    }

    /// Select elements based on condition
    /// 条件に基づいて要素を選択
    pub fn select<F>(&self, condition: F) -> Tensor<T>
    where
        F: Fn(T) -> bool,
    {
        let selected_data: Vec<T> = self.data.iter()
            .filter(|&&x| condition(x))
            .copied()
            .collect();
        
        let selected_len = selected_data.len();
        Tensor::from_vec(selected_data, vec![selected_len])
    }

    /// Count elements satisfying condition
    /// 条件を満たす要素をカウント
    pub fn count_where<F>(&self, condition: F) -> usize
    where
        F: Fn(T) -> bool,
    {
        self.data.iter().filter(|&&x| condition(x)).count()
    }

    /// Check if all elements satisfy condition
    /// 全要素が条件を満たすかチェック
    pub fn all<F>(&self, condition: F) -> bool
    where
        F: Fn(T) -> bool,
    {
        self.data.iter().all(|&x| condition(x))
    }

    /// Check if any element satisfies condition
    /// いずれかの要素が条件を満たすかチェック
    pub fn any<F>(&self, condition: F) -> bool
    where
        F: Fn(T) -> bool,
    {
        self.data.iter().any(|&x| condition(x))
    }

    /// Find first index where condition is true
    /// 条件が真となる最初のインデックスを検索
    pub fn find_first<F>(&self, condition: F) -> Option<usize>
    where
        F: Fn(T) -> bool,
    {
        self.data.iter().position(|&x| condition(x))
    }

    /// Find all indices where condition is true
    /// 条件が真となる全インデックスを検索
    pub fn find_all<F>(&self, condition: F) -> Vec<usize>
    where
        F: Fn(T) -> bool,
    {
        self.data.iter()
            .enumerate()
            .filter_map(|(i, &x)| if condition(x) { Some(i) } else { None })
            .collect()
    }

    /// Replace elements based on condition
    /// 条件に基づいて要素を置換
    pub fn replace_where<F>(&self, condition: F, replacement: T) -> Tensor<T>
    where
        F: Fn(T) -> bool,
    {
        let replaced_data: Vec<T> = self.data.iter()
            .map(|&x| if condition(x) { replacement } else { x })
            .collect();
        
        Tensor::from_vec(replaced_data, self.shape().to_vec())
    }

    // Note: where_tensor function removed due to boolean tensor type constraints
    // ブールテンソル型制約のためwhere_tensor関数を削除

    /// Get unique elements in tensor (simplified implementation)
    /// テンソル内のユニーク要素を取得(簡略化実装)
    pub fn unique(&self) -> Tensor<T>
    where
        T: PartialOrd + Copy,
    {
        let mut unique_data: Vec<T> = self.data.to_vec();
        unique_data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        unique_data.dedup();
        
        Tensor::from_vec(unique_data, vec![unique_data.len()])
    }

    /// Split tensor into chunks along specified dimension
    /// 指定次元でテンソルをチャンクに分割
    pub fn chunk(&self, chunks: usize, dim: usize) -> Result<Vec<Tensor<T>>, String> {
        if dim >= self.ndim() {
            return Err(format!("Dimension {} is out of bounds", dim));
        }

        let shape = self.shape();
        let dim_size = shape[dim];
        
        if chunks == 0 {
            return Err("Number of chunks must be positive".to_string());
        }

        let chunk_size = dim_size.div_ceil(chunks);
        let mut result = Vec::new();
        
        for i in 0..chunks {
            let start = i * chunk_size;
            let end = std::cmp::min(start + chunk_size, dim_size);
            
            if start >= dim_size {
                break;
            }

            let chunk_tensor = self.slice_along_dim(dim, start, end)?;
            result.push(chunk_tensor);
        }
        
        Ok(result)
    }

    /// Split tensor at specified indices along dimension
    /// 指定インデックスで次元に沿ってテンソルを分割
    pub fn split(&self, split_size_or_sections: &[usize], dim: usize) -> Result<Vec<Tensor<T>>, String> {
        if dim >= self.ndim() {
            return Err(format!("Dimension {} is out of bounds", dim));
        }

        let mut result = Vec::new();
        let mut start_idx = 0;

        for &size in split_size_or_sections {
            let end_idx = start_idx + size;
            if end_idx > self.shape()[dim] {
                return Err("Split sizes exceed dimension size".to_string());
            }

            let chunk = self.slice_along_dim(dim, start_idx, end_idx)?;
            result.push(chunk);
            start_idx = end_idx;
        }

        // Handle remaining elements if any
        if start_idx < self.shape()[dim] {
            let chunk = self.slice_along_dim(dim, start_idx, self.shape()[dim])?;
            result.push(chunk);
        }

        Ok(result)
    }

    /// Slice tensor along specified dimension
    /// 指定次元に沿ってテンソルをスライス
    fn slice_along_dim(&self, dim: usize, start: usize, end: usize) -> Result<Tensor<T>, String> {
        let shape = self.shape();
        let mut new_shape = shape.to_vec();
        new_shape[dim] = end - start;

        if start >= end || end > shape[dim] {
            return Err("Invalid slice indices".to_string());
        }

        let data = self.as_slice().unwrap();
        let mut result_data = Vec::new();

        // Calculate strides
        let mut strides = vec![1; shape.len()];
        for i in (0..shape.len() - 1).rev() {
            strides[i] = strides[i + 1] * shape[i + 1];
        }

        let dim_stride = strides[dim];
        let outer_size = if dim == 0 { 1 } else { shape[..dim].iter().product() };
        let inner_size = if dim == shape.len() - 1 { 1 } else { shape[dim + 1..].iter().product() };

        for outer in 0..outer_size {
            for i in start..end {
                for inner in 0..inner_size {
                    let idx = outer * (shape[dim] * inner_size) + i * inner_size + inner;
                    result_data.push(data[idx]);
                }
            }
        }

        Ok(Tensor::from_vec(result_data, new_shape))
    }

    /// Concatenate tensors along specified dimension
    /// 指定次元に沿ってテンソルを連結
    pub fn concat(tensors: &[&Tensor<T>], dim: usize) -> Result<Tensor<T>, String> {
        if tensors.is_empty() {
            return Err("Cannot concatenate empty list of tensors".to_string());
        }

        let first_shape = tensors[0].shape();
        
        if dim >= first_shape.len() {
            return Err(format!("Dimension {} is out of bounds", dim));
        }

        // Verify that all tensors have compatible shapes
        for (i, tensor) in tensors.iter().enumerate() {
            if tensor.shape().len() != first_shape.len() {
                return Err(format!("Tensor {} has different number of dimensions", i));
            }

            for (j, (&dim_size, &first_dim_size)) in tensor.shape().iter().zip(first_shape.iter()).enumerate() {
                if j != dim && dim_size != first_dim_size {
                    return Err(format!("Tensors have incompatible shapes at dimension {}", j));
                }
            }
        }

        // Calculate new shape
        let mut new_shape = first_shape.to_vec();
        new_shape[dim] = tensors.iter().map(|t| t.shape()[dim]).sum();

        // Concatenate data
        let mut result_data = Vec::new();
        let outer_size = first_shape[..dim].iter().product::<usize>();
        let inner_size = first_shape[dim + 1..].iter().product::<usize>();

        for outer in 0..outer_size {
            for tensor in tensors {
                let tensor_data = tensor.as_slice().unwrap();
                let tensor_dim_size = tensor.shape()[dim];

                for i in 0..tensor_dim_size {
                    for inner in 0..inner_size {
                        let idx = outer * (tensor_dim_size * inner_size) + i * inner_size + inner;
                        result_data.push(tensor_data[idx]);
                    }
                }
            }
        }

        Ok(Tensor::from_vec(result_data, new_shape))
    }

    /// Squeeze tensor (remove dimensions of size 1)
    /// テンソルを絞る(サイズ1の次元を削除)
    pub fn squeeze(&self, dim: Option<usize>) -> Result<Tensor<T>, String> {
        let shape = self.shape();
        let new_shape: Vec<usize> = if let Some(d) = dim {
            if d >= shape.len() {
                return Err(format!("Dimension {} is out of bounds", d));
            }
            if shape[d] != 1 {
                return Err(format!("Cannot squeeze dimension {} with size {}", d, shape[d]));
            }
            shape.iter().enumerate()
                .filter_map(|(i, &size)| if i != d { Some(size) } else { None })
                .collect()
        } else {
            shape.iter().copied().filter(|&size| size != 1).collect()
        };

        if new_shape.is_empty() {
            // Result is scalar
            Ok(Tensor::from_vec(self.as_slice().unwrap().to_vec(), vec![]))
        } else {
            Ok(Tensor::from_vec(self.as_slice().unwrap().to_vec(), new_shape))
        }
    }

    /// Unsqueeze tensor (add dimension of size 1)
    /// テンソルを展開(サイズ1の次元を追加)
    pub fn unsqueeze(&self, dim: usize) -> Result<Tensor<T>, String> {
        let shape = self.shape();
        
        if dim > shape.len() {
            return Err(format!("Dimension {} is out of bounds", dim));
        }

        let mut new_shape = shape.to_vec();
        new_shape.insert(dim, 1);

        Ok(Tensor::from_vec(self.as_slice().unwrap().to_vec(), new_shape))
    }

    /// Permute dimensions
    /// 次元を置換
    pub fn permute(&self, dims: &[usize]) -> Result<Tensor<T>, String> {
        let shape = self.shape();
        
        if dims.len() != shape.len() {
            return Err("Number of dimensions must match tensor dimensions".to_string());
        }

        // Check that dims is a valid permutation
        let mut sorted_dims = dims.to_vec();
        sorted_dims.sort();
        let expected: Vec<usize> = (0..shape.len()).collect();
        if sorted_dims != expected {
            return Err("Invalid permutation of dimensions".to_string());
        }

        let permuted = self.data.clone().permuted_axes(dims);
        Ok(Tensor::new(permuted))
    }

    /// Expand tensor (repeat along dimensions of size 1)
    /// テンソルを拡張(サイズ1の次元に沿って繰り返し)
    pub fn expand(&self, sizes: &[usize]) -> Result<Tensor<T>, String> {
        let shape = self.shape();
        
        if sizes.len() != shape.len() {
            return Err("Number of sizes must match tensor dimensions".to_string());
        }

        // Check that expansion is valid
        for (&current_size, &target_size) in shape.iter().zip(sizes.iter()) {
            if current_size != 1 && current_size != target_size {
                return Err(format!("Cannot expand size {} to size {}", current_size, target_size));
            }
        }

        // If no expansion needed, return clone
        if shape == sizes {
            return Ok(self.clone());
        }

        // Use tile-like expansion
        let reps: Vec<usize> = shape.iter()
            .zip(sizes.iter())
            .map(|(&current, &target)| target / current)
            .collect();

        Self::tile(self, &reps)
    }
}

// Note: Boolean tensor implementation removed due to Float trait constraint
// Float特性制約のためブールテンソル実装を削除

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_batch_size() {
        let tensor = Tensor::from_vec(vec![1.0f32; 12], vec![3, 4]);
        assert_eq!(tensor.batch_size(), 3);
        
        let scalar = Tensor::from_vec(vec![1.0f32], vec![]);
        assert_eq!(scalar.batch_size(), 1);
    }

    #[test]
    fn test_map() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0], vec![3]);
        let mapped = tensor.map(|x| x * 2.0);
        
        assert_eq!(mapped.as_slice().unwrap(), &[2.0f32, 4.0, 6.0]);
    }

    #[test]
    fn test_map_with_index() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0], vec![3]);
        let mapped = tensor.map_with_index(|x, i| x + i as f32);
        
        assert_eq!(mapped.as_slice().unwrap(), &[1.0f32, 3.0, 5.0]);
    }

    #[test]
    fn test_zip_with() {
        let a = Tensor::from_vec(vec![1.0f32, 2.0, 3.0], vec![3]);
        let b = Tensor::from_vec(vec![4.0f32, 5.0, 6.0], vec![3]);
        
        let result = a.zip_with(&b, |x, y| x + y).unwrap();
        assert_eq!(result.as_slice().unwrap(), &[5.0f32, 7.0, 9.0]);
    }

    #[test]
    fn test_select() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0, 4.0, 5.0], vec![5]);
        let selected = tensor.select(|x| x > 3.0);
        
        assert_eq!(selected.as_slice().unwrap(), &[4.0f32, 5.0]);
    }

    #[test]
    fn test_count_where() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0, 4.0, 5.0], vec![5]);
        let count = tensor.count_where(|x| x > 3.0);
        
        assert_eq!(count, 2);
    }

    #[test]
    fn test_all_any() {
        let tensor = Tensor::from_vec(vec![2.0f32, 4.0, 6.0, 8.0], vec![4]);
        
        assert!(tensor.all(|x| x > 0.0));
        assert!(!tensor.all(|x| x > 5.0));
        
        assert!(tensor.any(|x| x > 7.0));
        assert!(!tensor.any(|x| x > 10.0));
    }

    #[test]
    fn test_find() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0, 4.0, 5.0], vec![5]);
        
        assert_eq!(tensor.find_first(|x| x > 3.0), Some(3));
        assert_eq!(tensor.find_first(|x| x > 10.0), None);
        
        let indices = tensor.find_all(|x| x > 3.0);
        assert_eq!(indices, vec![3, 4]);
    }

    #[test]
    fn test_replace_where() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0, 4.0, 5.0], vec![5]);
        let replaced = tensor.replace_where(|x| x > 3.0, 0.0);
        
        assert_eq!(replaced.as_slice().unwrap(), &[1.0f32, 2.0, 3.0, 0.0, 0.0]);
    }

    #[test]
    fn test_unique() {
        let tensor = Tensor::from_vec(vec![3.0f32, 1.0, 4.0, 1.0, 5.0, 3.0], vec![6]);
        let unique = tensor.unique();
        
        assert_eq!(unique.as_slice().unwrap(), &[1.0f32, 3.0, 4.0, 5.0]);
    }

    #[test]
    fn test_chunk() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], vec![6]);
        let chunks = tensor.chunk(3, 0).unwrap();
        
        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0].as_slice().unwrap(), &[1.0f32, 2.0]);
        assert_eq!(chunks[1].as_slice().unwrap(), &[3.0f32, 4.0]);
        assert_eq!(chunks[2].as_slice().unwrap(), &[5.0f32, 6.0]);
    }

    #[test]
    fn test_concat() {
        let a = Tensor::from_vec(vec![1.0f32, 2.0], vec![2]);
        let b = Tensor::from_vec(vec![3.0f32, 4.0], vec![2]);
        let c = Tensor::from_vec(vec![5.0f32, 6.0], vec![2]);
        
        let concatenated = Tensor::concat(&[&a, &b, &c], 0).unwrap();
        assert_eq!(concatenated.shape(), &[6]);
        assert_eq!(concatenated.as_slice().unwrap(), &[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0]);
    }

    #[test]
    fn test_squeeze_unsqueeze() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0], vec![1, 3, 1]);
        
        let squeezed = tensor.squeeze(None).unwrap();
        assert_eq!(squeezed.shape(), &[3]);
        
        let unsqueezed = squeezed.unsqueeze(1).unwrap();
        assert_eq!(unsqueezed.shape(), &[3, 1]);
    }

    #[test]
    fn test_permute() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], vec![2, 3]);
        let permuted = tensor.permute(&[1, 0]).unwrap();
        
        assert_eq!(permuted.shape(), &[3, 2]);
    }

    #[test]
    fn test_expand() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0], vec![1, 3]);
        let expanded = tensor.expand(&[2, 3]).unwrap();
        
        assert_eq!(expanded.shape(), &[2, 3]);
        assert_eq!(expanded.as_slice().unwrap(), &[1.0f32, 2.0, 3.0, 1.0, 2.0, 3.0]);
    }

    #[test]
    fn test_boolean_tensor() {
        let tensor = Tensor::from_vec(vec![1.0f32, 2.0, 3.0, 4.0, 5.0], vec![5]);
        let bool_tensor = Tensor::from_condition(&tensor, |x| x > 3.0);
        
        assert_eq!(bool_tensor.as_slice().unwrap(), &[false, false, false, true, true]);
    }
}