source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/LinearTransformation.cs @ 15865

Last change on this file since 15865 was 15865, checked in by pfleck, 15 months ago

#2906 Added PreprocessingTransformation as a custom view-model for transformations in preprocessing.

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  [Item("Linear", "Linear transformation x' = slope * x + intercept")]
32  [StorableClass]
33  public class LinearTransformation : Transformation<double> {
34
35    #region Parameters
36    private FixedValueParameter<DoubleValue> SlopeParameter {
37      get { return (FixedValueParameter<DoubleValue>)Parameters["Slope"]; }
38    }
39
40    private FixedValueParameter<DoubleValue> InterceptParameter {
41      get { return (FixedValueParameter<DoubleValue>)Parameters["Intercept"]; }
42    }
43    #endregion
44
45    #region Properties
46    public double Slope {
47      get { return SlopeParameter.Value.Value; }
48      set { SlopeParameter.Value.Value = value; } //TODO: check for != 0?
49    }
50
51    public double Intercept {
52      get { return InterceptParameter.Value.Value; }
53      set { InterceptParameter.Value.Value = value; }
54    }
55    #endregion
56
57    #region Constructor, Cloning & Persistence
58    public LinearTransformation()
59      : base() {
60      Parameters.Add(new FixedValueParameter<DoubleValue>("Slope", "Slope (multiplicative factor)", new DoubleValue(1.0)));
61      Parameters.Add(new FixedValueParameter<DoubleValue>("Intercept", "Intercept (additive factor)", new DoubleValue(0.0)));
62    }
63
64    protected LinearTransformation(LinearTransformation original, Cloner cloner)
65      : base(original, cloner) {
66    }
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new LinearTransformation(this, cloner);
69    }
70
71    [StorableConstructor]
72    protected LinearTransformation(bool deserializing)
73      : base(deserializing) {
74    }
75    #endregion
76
77    public override IEnumerable<double> Apply(IEnumerable<double> data) {
78      return Apply(data, Slope, Intercept);
79    }
80
81    public override IEnumerable<double> InverseApply(IEnumerable<double> data) {
82      return InverseApply(data, Slope, Intercept);
83    }
84
85
86    public static IEnumerable<double> Apply(IEnumerable<double> data, double slope = 1.0, double intercept = 0.0) {
87      return data.Select(x => slope * x + intercept);
88    }
89
90    public static IEnumerable<double> InverseApply(IEnumerable<double> data, double slope = 1.0, double intercept = 0.0) {
91      return data.Select(x => (x - intercept) / slope);
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.