@@ -30,8 +30,6 @@ pub mod svr;
3030
3131use core:: fmt:: Debug ;
3232
33- #[ cfg( feature = "serde" ) ]
34- use serde:: ser:: { SerializeStruct , Serializer } ;
3533#[ cfg( feature = "serde" ) ]
3634use serde:: { Deserialize , Serialize } ;
3735
@@ -40,36 +38,20 @@ use crate::linalg::basic::arrays::{Array1, ArrayView1};
4038
4139/// Defines a kernel function.
4240/// This is a object-safe trait.
43- pub trait Kernel {
41+ #[ cfg_attr(
42+ all( feature = "serde" , not( target_arch = "wasm32" ) ) ,
43+ typetag:: serde( tag = "type" )
44+ ) ]
45+ pub trait Kernel : Debug {
4446 #[ allow( clippy:: ptr_arg) ]
4547 /// Apply kernel function to x_i and x_j
4648 fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > ;
47- /// Return a serializable name
48- fn name ( & self ) -> & ' static str ;
49- }
50-
51- impl Debug for dyn Kernel {
52- fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
53- write ! ( f, "Kernel<f64>" )
54- }
55- }
56-
57- #[ cfg( feature = "serde" ) ]
58- impl Serialize for dyn Kernel {
59- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
60- where
61- S : Serializer ,
62- {
63- let mut s = serializer. serialize_struct ( "Kernel" , 1 ) ?;
64- s. serialize_field ( "type" , & self . name ( ) ) ?;
65- s. end ( )
66- }
6749}
6850
6951/// Pre-defined kernel functions
7052#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
7153#[ derive( Debug , Clone ) ]
72- pub struct Kernels { }
54+ pub struct Kernels ;
7355
7456impl Kernels {
7557 /// Return a default linear
@@ -211,15 +193,14 @@ impl SigmoidKernel {
211193 }
212194}
213195
196+ #[ cfg_attr( all( feature = "serde" , not( target_arch = "wasm32" ) ) , typetag:: serde) ]
214197impl Kernel for LinearKernel {
215198 fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > {
216199 Ok ( x_i. dot ( x_j) )
217200 }
218- fn name ( & self ) -> & ' static str {
219- "Linear"
220- }
221201}
222202
203+ #[ cfg_attr( all( feature = "serde" , not( target_arch = "wasm32" ) ) , typetag:: serde) ]
223204impl Kernel for RBFKernel {
224205 fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > {
225206 if self . gamma . is_none ( ) {
@@ -231,11 +212,9 @@ impl Kernel for RBFKernel {
231212 let v_diff = x_i. sub ( x_j) ;
232213 Ok ( ( -self . gamma . unwrap ( ) * v_diff. mul ( & v_diff) . sum ( ) ) . exp ( ) )
233214 }
234- fn name ( & self ) -> & ' static str {
235- "RBF"
236- }
237215}
238216
217+ #[ cfg_attr( all( feature = "serde" , not( target_arch = "wasm32" ) ) , typetag:: serde) ]
239218impl Kernel for PolynomialKernel {
240219 fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > {
241220 if self . gamma . is_none ( ) || self . coef0 . is_none ( ) || self . degree . is_none ( ) {
@@ -247,11 +226,9 @@ impl Kernel for PolynomialKernel {
247226 let dot = x_i. dot ( x_j) ;
248227 Ok ( ( self . gamma . unwrap ( ) * dot + self . coef0 . unwrap ( ) ) . powf ( self . degree . unwrap ( ) ) )
249228 }
250- fn name ( & self ) -> & ' static str {
251- "Polynomial"
252- }
253229}
254230
231+ #[ cfg_attr( all( feature = "serde" , not( target_arch = "wasm32" ) ) , typetag:: serde) ]
255232impl Kernel for SigmoidKernel {
256233 fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > {
257234 if self . gamma . is_none ( ) || self . coef0 . is_none ( ) {
@@ -263,9 +240,6 @@ impl Kernel for SigmoidKernel {
263240 let dot = x_i. dot ( x_j) ;
264241 Ok ( self . gamma . unwrap ( ) * dot + self . coef0 . unwrap ( ) . tanh ( ) )
265242 }
266- fn name ( & self ) -> & ' static str {
267- "Sigmoid"
268- }
269243}
270244
271245#[ cfg( test) ]
0 commit comments