Skip to content

Commit df6d333

Browse files
committed
into_scalar
1 parent e43fb41 commit df6d333

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/types.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,47 @@ pub trait Scalar
3535
+ Conjugate
3636
+ RandNormal
3737
+ Debug {
38-
}
39-
40-
impl Scalar for f32 {}
41-
impl Scalar for f64 {}
42-
impl Scalar for c32 {}
43-
impl Scalar for c64 {}
44-
45-
pub trait RealScalar: Scalar + Float + Sum {
4638
fn from_f64(f64) -> Self;
4739
}
4840

49-
impl RealScalar for f32 {
41+
impl Scalar for f32 {
5042
fn from_f64(f: f64) -> Self {
5143
f as f32
5244
}
5345
}
54-
impl RealScalar for f64 {
46+
47+
impl Scalar for f64 {
5548
fn from_f64(f: f64) -> Self {
5649
f
5750
}
5851
}
5952

60-
/// Convert `f64` into `RealScalar`
53+
impl Scalar for c32 {
54+
fn from_f64(f: f64) -> Self {
55+
Self::new(f as f32, 0.0)
56+
}
57+
}
58+
59+
impl Scalar for c64 {
60+
fn from_f64(f: f64) -> Self {
61+
Self::new(f, 0.0)
62+
}
63+
}
64+
65+
pub trait RealScalar: Scalar + Float + Sum {}
66+
impl RealScalar for f32 {}
67+
impl RealScalar for f64 {}
68+
69+
/// Convert `f64` into `Scalar`
6170
///
6271
/// ```rust
63-
/// fn mult2<A: RealScalar>(a: A) -> A {
64-
/// a * into_real(2.0);
72+
/// use ndarray_linalg::*;
73+
/// fn mult<A: Scalar>(a: A) -> A {
74+
/// // a * 2.0 // Error!
75+
/// a * into_scalar(2.0)
6576
/// }
6677
/// ```
67-
pub fn into_real<T: RealScalar>(f: f64) -> T {
78+
pub fn into_scalar<T: Scalar>(f: f64) -> T {
6879
T::from_f64(f)
6980
}
7081

0 commit comments

Comments
 (0)