This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
Wave.cs
117 lines (106 loc) · 3.32 KB
/
Wave.cs
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
// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.Statistics.Kernels
{
using System;
using Accord.Compat;
/// <summary>
/// Wave Kernel.
/// </summary>
///
/// <remarks>
/// The Wave kernel is symmetric positive semi-definite (Huang, 2008).
/// </remarks>
///
[Serializable]
public sealed class Wave : KernelBase, IKernel, IRadialBasisKernel, ICloneable
{
private double sigma;
/// <summary>
/// Constructs a new Wave Kernel.
/// </summary>
///
/// <param name="sigma">Value for sigma.</param>
///
public Wave(double sigma)
{
this.sigma = sigma;
}
/// <summary>
/// Gets or sets the kernel's sigma value.
/// </summary>
///
public double Sigma
{
get { return sigma; }
set { sigma = value; }
}
/// <summary>
/// Wave Kernel Function.
/// </summary>
///
/// <param name="x">Vector <c>x</c> in input space.</param>
/// <param name="y">Vector <c>y</c> in input space.</param>
///
/// <returns>Dot product in feature (kernel) space.</returns>
///
public override double Function(double[] x, double[] y)
{
double norm = 0.0;
for (int i = 0; i < x.Length; i++)
{
double d = x[i] - y[i];
norm += d * d;
}
norm = System.Math.Sqrt(norm);
if (sigma == 0 || norm == 0)
return 0;
return (sigma / norm) * System.Math.Sin(norm / sigma);
}
/// <summary>
/// Wave Kernel Function.
/// </summary>
///
/// <param name="z">Distance <c>z</c> in input space.</param>
///
/// <returns>Dot product in feature (kernel) space.</returns>
///
public double Function(double z)
{
if (sigma == 0 || z == 0)
return 0;
return (sigma / z) * System.Math.Sin(z / sigma);
}
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
///
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
///
public object Clone()
{
return MemberwiseClone();
}
}
}