#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; 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("Logarithm", "Logarithm transformation x' = log(x, Base)")] [StorableClass] public class LogarithmTransformation : Transformation { #region Parameters private FixedValueParameter BaseParameter { get { return (FixedValueParameter)Parameters["Base"]; } } #endregion #region Properties public double Base { get { return BaseParameter.Value.Value; } set { BaseParameter.Value.Value = value; } } #endregion #region Constructor, Cloning & Persistence public LogarithmTransformation() : base() { Parameters.Add(new FixedValueParameter("Base", "Base the logarithm", new DoubleValue(Math.E))); } protected LogarithmTransformation(LogarithmTransformation original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new LogarithmTransformation(this, cloner); } [StorableConstructor] protected LogarithmTransformation(bool deserializing) : base(deserializing) { } #endregion public override bool Check(IEnumerable data, out string errorMessage) { if (data.Any(x => x <= 0)) { errorMessage = "Log is not available for zero or negative values"; return false; } return base.Check(data, out errorMessage); } public override IEnumerable Apply(IEnumerable data) { return Apply(data, Base); } public override IEnumerable InverseApply(IEnumerable data) { return InverseApply(data, Base); } public static IEnumerable Apply(IEnumerable data, double @base = Math.E) { return data.Select(x => Math.Log(x, @base)); } public static IEnumerable InverseApply(IEnumerable data, double @base = Math.E) { return ExponentialTransformation.Apply(data, @base); } } }