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

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

#2906 Refactoring

  • Moved transformation-specific parts out of existing interfaces.
  • Moved all Transformation logic to DataAnalysisTransformation.
  • Simplified (Inverse)Transformation of Dataset/ProblemData/Model/Solution.
File size: 3.6 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  [Item("Linear", "Linear transformation x' = slope * x + intercept")]
33  [StorableClass]
34  public class LinearTransformation : Transformation<double> {
35
36    #region Parameters
37    private FixedValueParameter<DoubleValue> SlopeParameter {
38      get { return (FixedValueParameter<DoubleValue>)Parameters["Slope"]; }
39    }
40
41    private FixedValueParameter<DoubleValue> InterceptParameter {
42      get { return (FixedValueParameter<DoubleValue>)Parameters["Intercept"]; }
43    }
44    #endregion
45
46    #region Properties
47    public double Slope {
48      get { return SlopeParameter.Value.Value; }
49      set { SlopeParameter.Value.Value = value; }
50    }
51
52    public double Intercept {
53      get { return InterceptParameter.Value.Value; }
54      set { InterceptParameter.Value.Value = value; }
55    }
56    #endregion
57
58    #region Constructor, Cloning & Persistence
59    public LinearTransformation()
60      : base() {
61      Parameters.Add(new FixedValueParameter<DoubleValue>("Slope", "Slope (multiplicative factor)", new DoubleValue(1.0)));
62      Parameters.Add(new FixedValueParameter<DoubleValue>("Intercept", "Intercept (additive factor)", new DoubleValue(0.0)));
63    }
64
65    protected LinearTransformation(LinearTransformation original, Cloner cloner)
66      : base(original, cloner) {
67    }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new LinearTransformation(this, cloner);
70    }
71
72    [StorableConstructor]
73    protected LinearTransformation(bool deserializing)
74      : base(deserializing) {
75    }
76    #endregion
77
78    public override IEnumerable<double> Apply(IEnumerable<double> data) {
79      return Apply(data, Slope, Intercept);
80    }
81
82    public override IEnumerable<double> InverseApply(IEnumerable<double> data) {
83      return InverseApply(data, Slope, Intercept);
84    }
85
86
87    public static IEnumerable<double> Apply(IEnumerable<double> data, double slope = 1.0, double intercept = 0.0) {
88      if (slope.IsAlmost(0.0))
89        throw new InvalidOperationException($"Cannot transform with a {nameof(slope)} of zero because inverse transformation would be invalid.");
90
91      return data.Select(x => slope * x + intercept);
92    }
93
94    public static IEnumerable<double> InverseApply(IEnumerable<double> data, double slope = 1.0, double intercept = 0.0) {
95      if (slope.IsAlmost(0.0))
96        throw new InvalidOperationException($"Cannot inverse transform with a {nameof(slope)} of zero.");
97
98      return data.Select(x => (x - intercept) / slope);
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.