Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/LinearTransformation.cs @ 16671

Last change on this file since 16671 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HEAL.Attic;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  [StorableType("203E6A27-4430-4A9E-825A-22C1B254F5E8")]
34  [Item("Linear Transformation", "f(x) = k * x + d | Represents a linear transformation with multiplication and addition.")]
35  public class LinearTransformation : Transformation<double> {
36    protected const string MultiplierParameterName = "Multiplier";
37    protected const string AddendParameterName = "Addend";
38
39    #region Parameters
40    public IValueParameter<DoubleValue> MultiplierParameter {
41      get { return (IValueParameter<DoubleValue>)Parameters[MultiplierParameterName]; }
42    }
43    public IValueParameter<DoubleValue> AddendParameter {
44      get { return (IValueParameter<DoubleValue>)Parameters[AddendParameterName]; }
45    }
46    #endregion
47
48    #region properties
49    public override string ShortName {
50      get { return "Lin"; }
51    }
52    public double Multiplier {
53      get { return MultiplierParameter.Value.Value; }
54      set {
55        MultiplierParameter.Value.Value = value;
56      }
57    }
58
59    public double Addend {
60      get { return AddendParameter.Value.Value; }
61      set {
62        AddendParameter.Value.Value = value;
63      }
64    }
65    #endregion
66
67    [StorableConstructor]
68    protected LinearTransformation(StorableConstructorFlag _) : base(_) { }
69    protected LinearTransformation(LinearTransformation original, Cloner cloner)
70      : base(original, cloner) {
71    }
72    public LinearTransformation(IEnumerable<string> allowedColumns)
73      : base(allowedColumns) {
74      Parameters.Add(new ValueParameter<DoubleValue>(MultiplierParameterName, "k | Multiplier for linear transformation", new DoubleValue(1.0)));
75      Parameters.Add(new ValueParameter<DoubleValue>(AddendParameterName, "d | Addend for linear transformation", new DoubleValue(0.0)));
76    }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new LinearTransformation(this, cloner);
80    }
81
82    public override IEnumerable<double> Apply(IEnumerable<double> data) {
83      var m = Multiplier;
84      var a = Addend;
85      return data.Select(e => e * m + a);
86    }
87
88    public override bool Check(IEnumerable<double> data, out string errorMsg) {
89      errorMsg = null;
90      if (Multiplier.IsAlmost(0.0)) {
91        errorMsg = String.Format("Multiplicand is 0, all {0} entries will be set to {1}. Inverse apply will not be possible (division by 0).", data.Count(), Addend);
92        return false;
93      }
94      return true;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.