Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Data/3.3/PercentMatrix.cs @ 16824

Last change on this file since 16824 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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.Collections.Generic;
23using System.Text;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Data {
29  [Item("PercentMatrix", "Represents a matrix of double values in percent.")]
30  [StorableClass]
31  public class PercentMatrix : DoubleMatrix {
32    [StorableConstructor]
33    protected PercentMatrix(bool deserializing) : base(deserializing) { }
34    protected PercentMatrix(PercentMatrix original, Cloner cloner)
35      : base(original, cloner) {
36    }
37    public PercentMatrix() : base() { }
38    public PercentMatrix(int rows, int columns) : base(rows, columns) { }
39    public PercentMatrix(int rows, int columns, IEnumerable<string> columnNames) : base(rows, columns, columnNames) { }
40    public PercentMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(rows, columns, columnNames, rowNames) { }
41    public PercentMatrix(double[,] elements) : base(elements) { }
42    public PercentMatrix(double[,] elements, IEnumerable<string> columnNames) : base(elements, columnNames) { }
43    public PercentMatrix(double[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(elements, columnNames, rowNames) { }
44
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new PercentMatrix(this, cloner);
47    }
48
49    public override string ToString() {
50      StringBuilder sb = new StringBuilder();
51      sb.Append("[");
52      if (matrix.Length > 0) {
53        for (int i = 0; i < Rows; i++) {
54          sb.Append("[").Append(matrix[i, 0].ToString("#0.#################### %"));  // percent format
55          for (int j = 1; j < Columns; j++)
56            sb.Append(";").Append(matrix[i, j].ToString("#0.#################### %"));  // percent format
57          sb.Append("]");
58        }
59      }
60      sb.Append("]");
61      return sb.ToString();
62    }
63
64    protected override bool Validate(string value, out string errorMessage) {
65      value = value.Replace("%", " ");
66      return base.Validate(value, out errorMessage);
67    }
68    protected override string GetValue(int rowIndex, int columIndex) {
69      return this[rowIndex, columIndex].ToString("#0.#################### %");  // percent format
70    }
71    protected override bool SetValue(string value, int rowIndex, int columnIndex) {
72      bool percent = value.Contains("%");
73      value = value.Replace("%", " ");
74      double val;
75      if (double.TryParse(value, out val)) {
76        if (percent) {
77          if (!(val).IsAlmost(this[rowIndex, columnIndex] * 100.0))
78            this[rowIndex, columnIndex] = val == 0 ? 0 : val / 100.0;
79        } else {
80          this[rowIndex, columnIndex] = val;
81        }
82        return true;
83      } else {
84        return false;
85      }
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.