Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Data/3.3/DoubleMatrix.cs @ 9306

Last change on this file since 9306 was 9306, checked in by sforsten, 11 years ago

#2018:

  • renamed the structs and methods in IStringConvertibleMatrix
  • added MatrixValuesChangedEventArgs in IStringConvertibleMatrix
  • added methods SetValues(MatrixValues<T>) in ValueTypeMatrix
  • fixed bugs in StringConvertibleMatrixView: DataGridView has now at least one column and dataGridView_CellValidating does not set e.Cancel to true anymore.
File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Data {
31  [Item("DoubleMatrix", "Represents a matrix of double values.")]
32  [StorableClass]
33  public class DoubleMatrix : ValueTypeMatrix<double> {
34    [StorableConstructor]
35    protected DoubleMatrix(bool deserializing) : base(deserializing) { }
36    protected DoubleMatrix(DoubleMatrix original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public DoubleMatrix() : base() { }
40    public DoubleMatrix(int rows, int columns) : base(rows, columns) { }
41    public DoubleMatrix(int rows, int columns, IEnumerable<string> columnNames) : base(rows, columns, columnNames) { }
42    public DoubleMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(rows, columns, columnNames, rowNames) { }
43    public DoubleMatrix(double[,] elements) : base(elements) { }
44    public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames) : base(elements, columnNames) { }
45    public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(elements, columnNames, rowNames) { }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new DoubleMatrix(this, cloner);
49    }
50
51    protected override bool Validate(string value, out string errorMessage) {
52      double val;
53      bool valid = double.TryParse(value, out val);
54      errorMessage = string.Empty;
55      if (!valid) {
56        StringBuilder sb = new StringBuilder();
57        sb.Append("Invalid Value (Valid Value Format: \"");
58        sb.Append(FormatPatterns.GetDoubleFormatPattern());
59        sb.Append("\")");
60        errorMessage = sb.ToString();
61      }
62      return valid;
63    }
64    protected override string GetValue(int rowIndex, int columIndex) {
65      return this[rowIndex, columIndex].ToString();
66    }
67    protected override bool SetValue(string value, int rowIndex, int columnIndex) {
68      double val;
69      if (double.TryParse(value, out val)) {
70        this[rowIndex, columnIndex] = val;
71        return true;
72      } else {
73        return false;
74      }
75    }
76    protected override bool SetValues(IEnumerable<MatrixValue<string>> matrixValues) {
77      if (ReadOnly) throw new NotSupportedException("Item cannot be set. DoubleMatrix is read-only.");
78
79      double parsed;
80      if (!matrixValues.All(v => double.TryParse(v.Value, out parsed))) return false;
81      List<MatrixPosition> positions = new List<MatrixPosition>();
82      foreach (var v in matrixValues) {
83        parsed = double.Parse(v.Value);
84        if (matrix[v.Position.Row, v.Position.Column] != parsed) {
85          matrix[v.Position.Row, v.Position.Column] = parsed;
86          positions.Add(v.Position);
87        }
88      }
89      return true;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.