#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis {
[Item("Linear", "Linear transformation x' = slope * x + intercept")]
[StorableClass]
public class LinearTransformation : Transformation {
#region Parameters
private FixedValueParameter SlopeParameter {
get { return (FixedValueParameter)Parameters["Slope"]; }
}
private FixedValueParameter InterceptParameter {
get { return (FixedValueParameter)Parameters["Intercept"]; }
}
#endregion
#region Properties
public double Slope {
get { return SlopeParameter.Value.Value; }
set { SlopeParameter.Value.Value = value; } //TODO: check for != 0?
}
public double Intercept {
get { return InterceptParameter.Value.Value; }
set { InterceptParameter.Value.Value = value; }
}
#endregion
#region Constructor, Cloning & Persistence
public LinearTransformation()
: base() {
Parameters.Add(new FixedValueParameter("Slope", "Slope (multiplicative factor)", new DoubleValue(1.0)));
Parameters.Add(new FixedValueParameter("Intercept", "Intercept (additive factor)", new DoubleValue(0.0)));
}
protected LinearTransformation(LinearTransformation original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new LinearTransformation(this, cloner);
}
[StorableConstructor]
protected LinearTransformation(bool deserializing)
: base(deserializing) {
}
#endregion
public override IEnumerable Apply(IEnumerable data) {
return Apply(data, Slope, Intercept);
}
public override IEnumerable InverseApply(IEnumerable data) {
return InverseApply(data, Slope, Intercept);
}
public static IEnumerable Apply(IEnumerable data, double slope = 1.0, double intercept = 0.0) {
return data.Select(x => slope * x + intercept);
}
public static IEnumerable InverseApply(IEnumerable data, double slope = 1.0, double intercept = 0.0) {
return data.Select(x => (x - intercept) / slope);
}
}
}