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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
// ジェネリックテンソル統一API
// Generic tensor unified API

use super::complex_tensor::ComplexTensor;
use super::core::F32Tensor;
use super::f64_tensor::F64Tensor;
use super::type_conversion::{TensorConversion, TensorVariant};
use crate::common::RusTorchResult;
use std::fmt::Debug;
use std::ops::{Add, Div, Mul, Neg, Sub};

/// テンソルの基本操作を定義するトレイト
/// Trait defining basic tensor operations
pub trait TensorOps: Debug + Clone {
    type Scalar: Copy + Debug;

    /// 新しいテンソルを作成
    /// Create a new tensor
    fn zeros(shape: &[usize]) -> RusTorchResult<Self>;
    fn ones(shape: &[usize]) -> RusTorchResult<Self>;
    fn randn(shape: &[usize]) -> RusTorchResult<Self>;

    /// 基本プロパティ
    /// Basic properties
    fn shape(&self) -> &[usize];
    fn ndim(&self) -> usize;
    fn numel(&self) -> usize;
    fn dtype(&self) -> &'static str;

    /// 勾配設定
    /// Gradient settings
    fn requires_grad_(self, requires_grad: bool) -> Self;

    /// 形状操作
    /// Shape operations
    fn reshape(&self, new_shape: &[usize]) -> RusTorchResult<Self>;
    fn transpose(&self) -> RusTorchResult<Self>;
    fn unsqueeze(&self, dim: usize) -> RusTorchResult<Self>;
    fn expand(&self, new_shape: &[usize]) -> RusTorchResult<Self>;
    fn transpose_dims(&self, dim1: usize, dim2: usize) -> RusTorchResult<Self>;

    /// 統計操作
    /// Statistical operations
    fn sum_scalar(&self) -> Self::Scalar;
    fn mean_scalar(&self) -> Self::Scalar;
}

/// 行列演算を定義するトレイト
/// Trait defining matrix operations
pub trait MatrixOps: TensorOps {
    /// 行列積
    /// Matrix multiplication
    fn matmul(&self, other: &Self) -> RusTorchResult<Self>;

    /// 転置
    /// Transpose
    fn t(&self) -> RusTorchResult<Self> {
        self.transpose()
    }
}

/// 数学関数を定義するトレイト
/// Trait defining mathematical functions
pub trait MathOps: TensorOps {
    /// 指数関数
    /// Exponential function
    fn exp(&self) -> RusTorchResult<Self>;

    /// 対数関数
    /// Logarithm function
    fn log(&self) -> RusTorchResult<Self>;

    /// べき乗
    /// Power function
    fn pow(&self, exponent: f64) -> RusTorchResult<Self>;

    /// 平方根
    /// Square root
    fn sqrt(&self) -> RusTorchResult<Self>;

    /// 三角関数
    /// Trigonometric functions
    fn sin(&self) -> RusTorchResult<Self>;
    fn cos(&self) -> RusTorchResult<Self>;
    fn tan(&self) -> RusTorchResult<Self>;

    /// ソフトマックス
    /// Softmax
    fn softmax(&self, dim: Option<usize>) -> RusTorchResult<Self>;
}

/// 複素数専用操作を定義するトレイト
/// Trait defining complex-specific operations
pub trait ComplexOps: TensorOps {
    type RealTensor: TensorOps;

    /// 実部を取得
    /// Get real part
    fn real(&self) -> Self::RealTensor;

    /// 虚部を取得
    /// Get imaginary part
    fn imag(&self) -> Self::RealTensor;

    /// 絶対値を取得
    /// Get absolute value
    fn abs(&self) -> Self::RealTensor;

    /// 位相を取得
    /// Get phase
    fn angle(&self) -> Self::RealTensor;

    /// 共役複素数を取得
    /// Get complex conjugate
    fn conj(&self) -> Self;
}

// F32Tensor用実装
impl TensorOps for F32Tensor {
    type Scalar = f32;

    fn zeros(shape: &[usize]) -> RusTorchResult<Self> {
        F32Tensor::zeros(shape)
    }

    fn ones(shape: &[usize]) -> RusTorchResult<Self> {
        F32Tensor::ones(shape)
    }

    fn randn(shape: &[usize]) -> RusTorchResult<Self> {
        F32Tensor::randn(shape)
    }

    fn shape(&self) -> &[usize] {
        self.shape()
    }

    fn ndim(&self) -> usize {
        self.ndim()
    }

    fn numel(&self) -> usize {
        self.numel()
    }

    fn dtype(&self) -> &'static str {
        "f32"
    }

    fn requires_grad_(self, requires_grad: bool) -> Self {
        let mut result = self;
        result.requires_grad = requires_grad;
        result
    }

    fn reshape(&self, new_shape: &[usize]) -> RusTorchResult<Self> {
        self.reshape(new_shape)
    }

    fn transpose(&self) -> RusTorchResult<Self> {
        self.transpose()
    }

    fn unsqueeze(&self, dim: usize) -> RusTorchResult<Self> {
        self.unsqueeze(dim)
    }

    fn expand(&self, new_shape: &[usize]) -> RusTorchResult<Self> {
        self.expand(new_shape)
    }

    fn transpose_dims(&self, dim1: usize, dim2: usize) -> RusTorchResult<Self> {
        self.transpose_dims(dim1, dim2)
    }

    fn sum_scalar(&self) -> Self::Scalar {
        self.sum().unwrap_or(0.0)
    }

    fn mean_scalar(&self) -> Self::Scalar {
        // mean関数がないので、sum/lengthで計算
        let sum_val = self.sum().unwrap_or(0.0);
        let len = self.data.len() as f32;
        if len > 0.0 {
            sum_val / len
        } else {
            0.0
        }
    }
}

impl MatrixOps for F32Tensor {
    fn matmul(&self, other: &Self) -> RusTorchResult<Self> {
        self.matmul(other)
    }
}

impl MathOps for F32Tensor {
    fn exp(&self) -> RusTorchResult<Self> {
        let exp_data = self.data.mapv(|x| x.exp());
        let (data_vec, _offset) = exp_data.into_raw_vec_and_offset();
        let shape = self.shape();
        let mut result = F32Tensor::new(data_vec, &shape)?;
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn log(&self) -> RusTorchResult<Self> {
        let log_data = self.data.mapv(|x| x.ln());
        let (data_vec, _offset) = log_data.into_raw_vec_and_offset();
        let shape = self.shape();
        let mut result = F32Tensor::new(data_vec, &shape)?;
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn pow(&self, exponent: f64) -> RusTorchResult<Self> {
        let pow_data = self.data.mapv(|x| x.powf(exponent as f32));
        let (data_vec, _offset) = pow_data.into_raw_vec_and_offset();
        let shape = self.shape();
        let mut result = F32Tensor::new(data_vec, &shape)?;
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn sqrt(&self) -> RusTorchResult<Self> {
        let sqrt_data = self.data.mapv(|x| x.sqrt());
        let (data_vec, _offset) = sqrt_data.into_raw_vec_and_offset();
        let shape = self.shape();
        let mut result = F32Tensor::new(data_vec, &shape)?;
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn sin(&self) -> RusTorchResult<Self> {
        let sin_data = self.data.mapv(|x| x.sin());
        let (data_vec, _offset) = sin_data.into_raw_vec_and_offset();
        let shape = self.shape();
        let mut result = F32Tensor::new(data_vec, &shape)?;
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn cos(&self) -> RusTorchResult<Self> {
        let cos_data = self.data.mapv(|x| x.cos());
        let (data_vec, _offset) = cos_data.into_raw_vec_and_offset();
        let shape = self.shape();
        let mut result = F32Tensor::new(data_vec, &shape)?;
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn tan(&self) -> RusTorchResult<Self> {
        let tan_data = self.data.mapv(|x| x.tan());
        let (data_vec, _offset) = tan_data.into_raw_vec_and_offset();
        let shape = self.shape();
        let mut result = F32Tensor::new(data_vec, &shape)?;
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn softmax(&self, dim: Option<usize>) -> RusTorchResult<Self> {
        self.softmax(dim)
    }
}

// F64Tensor用実装
impl TensorOps for F64Tensor {
    type Scalar = f64;

    fn zeros(shape: &[usize]) -> RusTorchResult<Self> {
        F64Tensor::zeros(shape)
    }

    fn ones(shape: &[usize]) -> RusTorchResult<Self> {
        F64Tensor::ones(shape)
    }

    fn randn(shape: &[usize]) -> RusTorchResult<Self> {
        F64Tensor::randn(shape)
    }

    fn shape(&self) -> &[usize] {
        self.shape()
    }

    fn ndim(&self) -> usize {
        self.ndim()
    }

    fn numel(&self) -> usize {
        self.numel()
    }

    fn dtype(&self) -> &'static str {
        "f64"
    }

    fn requires_grad_(self, requires_grad: bool) -> Self {
        let mut result = self;
        result.requires_grad = requires_grad;
        result
    }

    fn reshape(&self, new_shape: &[usize]) -> RusTorchResult<Self> {
        self.reshape(new_shape)
    }

    fn transpose(&self) -> RusTorchResult<Self> {
        self.transpose()
    }

    fn unsqueeze(&self, dim: usize) -> RusTorchResult<Self> {
        self.unsqueeze(dim)
    }

    fn expand(&self, new_shape: &[usize]) -> RusTorchResult<Self> {
        self.expand(new_shape)
    }

    fn transpose_dims(&self, dim1: usize, dim2: usize) -> RusTorchResult<Self> {
        self.transpose_dims(dim1, dim2)
    }

    fn sum_scalar(&self) -> Self::Scalar {
        self.sum()
    }

    fn mean_scalar(&self) -> Self::Scalar {
        // mean関数がないので、sum/lengthで計算
        let sum_val = self.sum();
        let len = self.data.len() as f64;
        if len > 0.0 {
            sum_val / len
        } else {
            0.0
        }
    }
}

impl MatrixOps for F64Tensor {
    fn matmul(&self, other: &Self) -> RusTorchResult<Self> {
        self.matmul(other)
    }
}

impl MathOps for F64Tensor {
    fn exp(&self) -> RusTorchResult<Self> {
        let exp_data = self.data.mapv(|x| x.exp());
        let mut result = F64Tensor::new(exp_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn log(&self) -> RusTorchResult<Self> {
        let log_data = self.data.mapv(|x| x.ln());
        let mut result = F64Tensor::new(log_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn pow(&self, exponent: f64) -> RusTorchResult<Self> {
        let pow_data = self.data.mapv(|x| x.powf(exponent));
        let mut result = F64Tensor::new(pow_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn sqrt(&self) -> RusTorchResult<Self> {
        let sqrt_data = self.data.mapv(|x| x.sqrt());
        let mut result = F64Tensor::new(sqrt_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn sin(&self) -> RusTorchResult<Self> {
        let sin_data = self.data.mapv(|x| x.sin());
        let mut result = F64Tensor::new(sin_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn cos(&self) -> RusTorchResult<Self> {
        let cos_data = self.data.mapv(|x| x.cos());
        let mut result = F64Tensor::new(cos_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn tan(&self) -> RusTorchResult<Self> {
        let tan_data = self.data.mapv(|x| x.tan());
        let mut result = F64Tensor::new(tan_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn softmax(&self, dim: Option<usize>) -> RusTorchResult<Self> {
        self.softmax(dim)
    }
}

// ComplexTensor用実装
impl TensorOps for ComplexTensor {
    type Scalar = num_complex::Complex64;

    fn zeros(shape: &[usize]) -> RusTorchResult<Self> {
        ComplexTensor::zeros(shape)
    }

    fn ones(shape: &[usize]) -> RusTorchResult<Self> {
        ComplexTensor::ones(shape)
    }

    fn randn(shape: &[usize]) -> RusTorchResult<Self> {
        // 複素数のランダムテンソル:実部と虚部が独立した正規分布
        use ndarray::Array;
        use ndarray_rand::rand_distr::StandardNormal;
        use ndarray_rand::RandomExt;
        use num_complex::Complex64;

        let real_part: Array<f64, _> = Array::random(shape, StandardNormal);
        let imag_part: Array<f64, _> = Array::random(shape, StandardNormal);

        let complex_data = real_part
            .iter()
            .zip(imag_part.iter())
            .map(|(&r, &i)| Complex64::new(r, i))
            .collect::<Vec<_>>();

        let data = Array::from_shape_vec(shape, complex_data)
            .map_err(|e| crate::error::RusTorchError::tensor_op(e.to_string()))?;

        Ok(ComplexTensor::new(data))
    }

    fn shape(&self) -> &[usize] {
        self.shape()
    }

    fn ndim(&self) -> usize {
        self.ndim()
    }

    fn numel(&self) -> usize {
        self.numel()
    }

    fn dtype(&self) -> &'static str {
        "complex64"
    }

    fn requires_grad_(self, requires_grad: bool) -> Self {
        let mut result = self;
        result.requires_grad = requires_grad;
        result
    }

    fn reshape(&self, new_shape: &[usize]) -> RusTorchResult<Self> {
        self.reshape(new_shape)
    }

    fn transpose(&self) -> RusTorchResult<Self> {
        self.transpose()
    }

    fn unsqueeze(&self, dim: usize) -> RusTorchResult<Self> {
        self.unsqueeze(dim)
    }

    fn expand(&self, new_shape: &[usize]) -> RusTorchResult<Self> {
        self.expand(new_shape)
    }

    fn transpose_dims(&self, dim1: usize, dim2: usize) -> RusTorchResult<Self> {
        self.transpose_dims(dim1, dim2)
    }

    fn sum_scalar(&self) -> Self::Scalar {
        self.sum()
    }

    fn mean_scalar(&self) -> Self::Scalar {
        use num_complex::Complex64;
        // mean関数がないので、sum/lengthで計算
        let sum_val = self.sum();
        let len = self.data.len() as f64;
        if len > 0.0 {
            sum_val / len
        } else {
            Complex64::new(0.0, 0.0)
        }
    }
}

impl MatrixOps for ComplexTensor {
    fn matmul(&self, other: &Self) -> RusTorchResult<Self> {
        self.matmul(other)
    }
}

impl MathOps for ComplexTensor {
    fn exp(&self) -> RusTorchResult<Self> {
        let exp_data = self.data.mapv(|x| x.exp());
        let mut result = ComplexTensor::new(exp_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn log(&self) -> RusTorchResult<Self> {
        let log_data = self.data.mapv(|x| x.ln());
        let mut result = ComplexTensor::new(log_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn pow(&self, exponent: f64) -> RusTorchResult<Self> {
        use num_complex::Complex64;
        let exp_complex = Complex64::new(exponent, 0.0);
        let pow_data = self.data.mapv(|x| x.powc(exp_complex));
        let mut result = ComplexTensor::new(pow_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn sqrt(&self) -> RusTorchResult<Self> {
        let sqrt_data = self.data.mapv(|x| x.sqrt());
        let mut result = ComplexTensor::new(sqrt_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn sin(&self) -> RusTorchResult<Self> {
        let sin_data = self.data.mapv(|x| x.sin());
        let mut result = ComplexTensor::new(sin_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn cos(&self) -> RusTorchResult<Self> {
        let cos_data = self.data.mapv(|x| x.cos());
        let mut result = ComplexTensor::new(cos_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn tan(&self) -> RusTorchResult<Self> {
        let tan_data = self.data.mapv(|x| x.tan());
        let mut result = ComplexTensor::new(tan_data);
        result.requires_grad = self.requires_grad;
        Ok(result)
    }

    fn softmax(&self, _dim: Option<usize>) -> RusTorchResult<Self> {
        // 複素数のソフトマックスは定義が複雑なため、実部のみに適用
        Err(crate::error::RusTorchError::InvalidOperation(
            "Softmax is not well-defined for complex tensors".to_string(),
        ))
    }
}

impl ComplexOps for ComplexTensor {
    type RealTensor = F64Tensor;

    fn real(&self) -> Self::RealTensor {
        F64Tensor::new(self.real())
    }

    fn imag(&self) -> Self::RealTensor {
        F64Tensor::new(self.imag())
    }

    fn abs(&self) -> Self::RealTensor {
        F64Tensor::new(self.abs())
    }

    fn angle(&self) -> Self::RealTensor {
        F64Tensor::new(self.angle())
    }

    fn conj(&self) -> Self {
        self.conj()
    }
}

/// ジェネリックテンソル操作のヘルパー関数
/// Helper functions for generic tensor operations
pub struct TensorUtils;

impl TensorUtils {
    /// 型安全な加算
    /// Type-safe addition
    pub fn add<T>(a: &T, b: &T) -> RusTorchResult<T>
    where
        T: TensorOps + Add<Output = T> + Clone,
    {
        Ok(a.clone() + b.clone())
    }

    /// 型安全な行列積
    /// Type-safe matrix multiplication
    pub fn matmul<T>(a: &T, b: &T) -> RusTorchResult<T>
    where
        T: MatrixOps,
    {
        a.matmul(b)
    }

    /// 型を指定したテンソル作成
    /// Create tensor with specified type
    pub fn zeros_like<T>(tensor: &T) -> RusTorchResult<T>
    where
        T: TensorOps,
    {
        T::zeros(tensor.shape())
    }

    /// 型を指定したテンソル作成
    /// Create tensor with specified type
    pub fn ones_like<T>(tensor: &T) -> RusTorchResult<T>
    where
        T: TensorOps,
    {
        T::ones(tensor.shape())
    }

    /// テンソルの型情報を表示
    /// Display tensor type information
    pub fn tensor_info<T>(tensor: &T) -> String
    where
        T: TensorOps,
    {
        format!(
            "Tensor(dtype={}, shape={:?}, numel={})",
            tensor.dtype(),
            tensor.shape(),
            tensor.numel()
        )
    }
}

/// ジェネリックテンソルファクトリ
/// Generic tensor factory
pub struct TensorFactory;

impl TensorFactory {
    /// 指定した精度でゼロテンソルを作成
    /// Create zero tensor with specified precision
    pub fn zeros(dtype: &str, shape: &[usize]) -> RusTorchResult<TensorVariant> {
        match dtype {
            "f32" => Ok(TensorVariant::F32(F32Tensor::zeros(shape)?)),
            "f64" => Ok(TensorVariant::F64(F64Tensor::zeros(shape)?)),
            "complex64" => Ok(TensorVariant::Complex(ComplexTensor::zeros(shape)?)),
            _ => Err(crate::error::RusTorchError::InvalidOperation(format!(
                "Unsupported dtype: {}",
                dtype
            ))),
        }
    }

    /// 指定した精度でワンテンソルを作成
    /// Create ones tensor with specified precision
    pub fn ones(dtype: &str, shape: &[usize]) -> RusTorchResult<TensorVariant> {
        match dtype {
            "f32" => Ok(TensorVariant::F32(F32Tensor::ones(shape)?)),
            "f64" => Ok(TensorVariant::F64(F64Tensor::ones(shape)?)),
            "complex64" => Ok(TensorVariant::Complex(ComplexTensor::ones(shape)?)),
            _ => Err(crate::error::RusTorchError::InvalidOperation(format!(
                "Unsupported dtype: {}",
                dtype
            ))),
        }
    }

    /// 指定した精度でランダムテンソルを作成
    /// Create random tensor with specified precision
    pub fn randn(dtype: &str, shape: &[usize]) -> RusTorchResult<TensorVariant> {
        match dtype {
            "f32" => Ok(TensorVariant::F32(F32Tensor::randn(shape)?)),
            "f64" => Ok(TensorVariant::F64(F64Tensor::randn(shape)?)),
            "complex64" => Ok(TensorVariant::Complex(ComplexTensor::randn(shape)?)),
            _ => Err(crate::error::RusTorchError::InvalidOperation(format!(
                "Unsupported dtype: {}",
                dtype
            ))),
        }
    }

    /// 最適な精度を自動選択
    /// Automatically select optimal precision
    pub fn auto_precision(
        shape: &[usize],
        requires_high_precision: bool,
        is_complex: bool,
    ) -> RusTorchResult<TensorVariant> {
        let dtype = match (requires_high_precision, is_complex) {
            (true, true) => "complex64",
            (true, false) => "f64",
            (false, true) => "complex64", // 複素数は常に高精度
            (false, false) => "f32",
        };
        Self::zeros(dtype, shape)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hybrid_f32::tensor::{ComplexTensor, F32Tensor, F64Tensor};

    #[test]
    fn test_generic_tensor_ops() {
        let f32_tensor = F32Tensor::ones(&[2, 2]).unwrap();
        let f64_tensor = F64Tensor::ones(&[2, 2]).unwrap();

        assert_eq!(f32_tensor.dtype(), "f32");
        assert_eq!(f64_tensor.dtype(), "f64");
        assert_eq!(f32_tensor.shape(), f64_tensor.shape());
    }

    #[test]
    fn test_tensor_factory() {
        let f32_zeros = TensorFactory::zeros("f32", &[2, 2]).unwrap();
        let f64_ones = TensorFactory::ones("f64", &[3, 3]).unwrap();
        let complex_randn = TensorFactory::randn("complex64", &[2, 2]).unwrap();

        assert_eq!(f32_zeros.dtype(), "f32");
        assert_eq!(f64_ones.dtype(), "f64");
        assert_eq!(complex_randn.dtype(), "complex64");
    }

    #[test]
    fn test_tensor_utils() {
        let tensor = F32Tensor::ones(&[2, 2]).unwrap();
        let info = TensorUtils::tensor_info(&tensor);

        assert!(info.contains("f32"));
        assert!(info.contains("[2, 2]"));
        assert!(info.contains("4"));
    }

    #[test]
    fn test_math_ops() {
        let tensor = F32Tensor::ones(&[2, 2]).unwrap();
        let exp_tensor = tensor.exp().unwrap();
        let log_tensor = tensor.log().unwrap();

        // e^1 ≈ 2.718
        assert!((exp_tensor.sum().unwrap() - 4.0 * std::f32::consts::E).abs() < 1e-5);
        // ln(1) = 0
        assert!(log_tensor.sum().unwrap().abs() < 1e-5);
    }

    #[test]
    fn test_complex_ops() {
        let complex_tensor = ComplexTensor::ones(&[2, 2]).unwrap();
        let real_part = complex_tensor.real();
        let imag_part = complex_tensor.imag();
        let conj_tensor = complex_tensor.conj();

        // Note: real/imag return ArrayBase, not tensor objects
        // Just verify they return data
        assert!(real_part.len() > 0);
        assert!(imag_part.len() > 0);
        assert_eq!(conj_tensor.dtype(), "complex64");
    }
}