1 impl Abs for f32{fn abs(self)->Self::Output{f32::abs(self)}
4 type Output=f32;
5}
6impl Rank for f32{
7 fn dynamic_rank(&self)->usize{0}
8 fn type_rank()->usize{0}
9}
10impl Squeeze for Vec<f32>{
11 fn squeeze(self,dim:i32)->Self::Output{
12 if dim!=-1&&dim!=0{panic!("squeeze dim out of bounds")}
13 if self.len()!=1{panic!("cannot squeeze a dim whose size is not 1")}
14 self[0]
15 }
16 type Output=f32;
17}
18impl SquaredError for f32{
19 fn squared_error(self,rhs:f32)->Self::Output{
20 let d=self-rhs;
21 d*d
22 }
23 type Output=f32;
24}
25impl Unsqueeze for f32{
26 fn unsqueeze(self,dim:i32)->UnsqueezeScalar<f32>{
27 if dim==-1||dim==0{UnsqueezeScalar(self)}else{panic!("unsqueeze dim out of bounds")}
28 }
29 type Output=UnsqueezeScalar<f32>;
30}
31impl<T:Rank> Rank for Vec<T>{
32 fn dynamic_rank(&self)->usize{self.first().map(T::dynamic_rank).unwrap_or_else(T::type_rank)+1}
33 fn type_rank()->usize{T::type_rank()+1}
34}
35impl<T:Squeeze> Squeeze for Vec<Vec<T>> where Vec<T>:Squeeze<Output=T>+Rank{
36 fn squeeze(self,mut dim:i32)->Self::Output{
37 let rank=self.rank() as i32;
38
39 if !(-rank..rank).contains(&dim){panic!("squeeze dim out of bounds")}
40 if dim==0||dim==-rank{
41 if self.len()!=1{panic!("cannot squeeze a dim whose size is not 1")}
42 self.into_iter().next().unwrap()
43 }else{
44 if dim>0{dim-=1}
45 self.into_iter().map(|x|x.squeeze(dim)).collect()
46 }
47 }
48 type Output=Vec<T>;
49}
50impl<T:Unsqueeze<Output=U>,U> Stack for Vec<T> where Vec<U>:Cat<Output=Vec<T>>{
51 fn stack(self,dim:i32)->Self::Output{
52 let unsqueezed:Vec<U>=self.into_iter().map(|x|x.unsqueeze(dim)).collect();
53 unsqueezed.cat(dim)
54 }
55 type Output=Self;
56}
57impl<T:Unsqueeze> Unsqueeze for Vec<T> where T::Output:Into<Vec<T>>,Vec<T>:Rank{
58 fn unsqueeze(self,mut dim:i32)->Self::Output{
59 let rank=self.rank() as i32;
60
61 if !(-rank..rank+1).contains(&dim){panic!("unsqueeze dim out of bounds")}
62 if dim==0||dim==-rank{return vec![self]}else if dim>0{dim-=1}
63 self.into_iter().map(|x|x.unsqueeze(dim).into()).collect()
64 }
65 type Output=Vec<Vec<T>>;
66}
67impl<T> From<UnsqueezeScalar<T>> for Vec<T>{
68 fn from(value:UnsqueezeScalar<T>)->Vec<T>{vec![value.0]}
69}
70#[derive(Clone,Copy,Debug,Default,Eq,Hash,Ord,PartialEq,PartialOrd)]
71pub struct UnsqueezeScalar<T>(pub T);
73pub trait Abs{
75 fn _apply(self)->Self::Output where Self:Sized{self.abs()}
77 fn abs(self)->Self::Output;
79 type Output;
81}
82pub trait Cat{
84 fn _apply(self,dim:i32)->Self::Output where Self:Sized{self.cat(dim)}
86 fn cat(self,dim:i32)->Self::Output;
88 type Output;
90}
91pub trait Flatten<R>{
93 fn _apply(self,args:R)->Self::Output where Self:Sized{self.flatten(args)}
95 fn flatten(self,args:R)->Self::Output;
97 type Output;
99}
100pub trait Rank{
102 fn dynamic_rank(&self)->usize;
104 fn rank(&self)->usize{self.dynamic_rank()}
106 fn type_rank()->usize where Self:Sized;
108}
109pub trait Reshape<R>{
111 fn _apply(self,args:R)->Self::Output where Self:Sized{self.reshape(args)}
113 fn reshape(self,args:R)->Self::Output;
115 type Output;
117}
118pub trait Squeeze{
120 fn _apply(self,dim:i32)->Self::Output where Self:Sized{self.squeeze(dim)}
122 fn squeeze(self,dim:i32)->Self::Output;
124 type Output;
126}
127pub trait SwapDims{
129 fn _apply(self,a:i32,b:i32)->Self::Output where Self:Sized{self.swap_dims(a,b)}
131 fn swap_dims(self,a:i32,b:i32)->Self::Output;
133 type Output;
135}
136pub trait SquaredError<R=Self>{
138 fn _apply(self,rhs:R)->Self::Output where Self:Sized{self.squared_error(rhs)}
140 fn squared_error(self,rhs:R)->Self::Output;
142 type Output;
144}
145pub trait Stack{
147 fn _apply(self,dim:i32)->Self::Output where Self:Sized{self.stack(dim)}
149 fn stack(self,dim:i32)->Self::Output;
151 type Output;
153}
154pub trait Unsqueeze{
156 fn _apply(self,dim:i32)->Self::Output where Self:Sized{self.unsqueeze(dim)}
158 fn unsqueeze(self,dim:i32)->Self::Output;
160 type Output;
162}