1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 | using System.Collections.Generic;
|
---|
23 | using System.Text;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Data {
|
---|
29 | [Item("DoubleMatrix", "Represents a matrix of double values.")]
|
---|
30 | [StorableType("EFF3CA0C-100C-4DD4-9EFF-2EF3CB0DE793")]
|
---|
31 | public class DoubleMatrix : ValueTypeMatrix<double>, IStringConvertibleMatrix {
|
---|
32 | [StorableConstructor]
|
---|
33 | protected DoubleMatrix(StorableConstructorFlag _) : base(_) { }
|
---|
34 | protected DoubleMatrix(DoubleMatrix original, Cloner cloner)
|
---|
35 | : base(original, cloner) {
|
---|
36 | }
|
---|
37 | public DoubleMatrix() : base() { }
|
---|
38 | public DoubleMatrix(int rows, int columns) : base(rows, columns) { }
|
---|
39 | public DoubleMatrix(int rows, int columns, IEnumerable<string> columnNames) : base(rows, columns, columnNames) { }
|
---|
40 | public DoubleMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(rows, columns, columnNames, rowNames) { }
|
---|
41 | public DoubleMatrix(double[,] elements, bool @readonly = false) : base(elements, @readonly) { }
|
---|
42 | public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames, bool @readonly = false) : base(elements, columnNames, @readonly) { }
|
---|
43 | public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames, bool @readonly = false) : base(elements, columnNames, rowNames, @readonly) { }
|
---|
44 |
|
---|
45 | public static DoubleMatrix FromRows(IList<double[]> elements, bool @readonly = false, IEnumerable<string> columnNames = null, IEnumerable<string> rowNames = null) {
|
---|
46 | if (elements.Count == 0) return new DoubleMatrix(0, 0);
|
---|
47 | var mat = new double[elements.Count, elements[0].Length];
|
---|
48 | for (var r = 0; r < mat.GetLength(0); r++)
|
---|
49 | for (var c = 0; c < mat.GetLength(1); c++)
|
---|
50 | mat[r, c] = elements[r][c];
|
---|
51 | // TODO: We should avoid the memory copy in this case
|
---|
52 | return new DoubleMatrix(mat, columnNames, rowNames, @readonly);
|
---|
53 | }
|
---|
54 | public static DoubleMatrix FromColumns(IList<double[]> elements, bool @readonly = false, IEnumerable<string> columnNames = null, IEnumerable<string> rowNames = null) {
|
---|
55 | if (elements.Count == 0) return new DoubleMatrix(0, 0);
|
---|
56 | var mat = new double[elements[0].Length, elements.Count];
|
---|
57 | for (var c = 0; c < mat.GetLength(1); c++)
|
---|
58 | for (var r = 0; r < mat.GetLength(0); r++)
|
---|
59 | mat[r, c] = elements[c][r];
|
---|
60 | // TODO: We should avoid the memory copy in this case
|
---|
61 | return new DoubleMatrix(mat, columnNames, rowNames, @readonly);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
65 | return new DoubleMatrix(this, cloner);
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
69 | double val;
|
---|
70 | bool valid = double.TryParse(value, out val);
|
---|
71 | errorMessage = string.Empty;
|
---|
72 | if (!valid) {
|
---|
73 | StringBuilder sb = new StringBuilder();
|
---|
74 | sb.Append("Invalid Value (Valid Value Format: \"");
|
---|
75 | sb.Append(FormatPatterns.GetDoubleFormatPattern());
|
---|
76 | sb.Append("\")");
|
---|
77 | errorMessage = sb.ToString();
|
---|
78 | }
|
---|
79 | return valid;
|
---|
80 | }
|
---|
81 | protected virtual string GetValue(int rowIndex, int columIndex) {
|
---|
82 | return this[rowIndex, columIndex].ToString("r");
|
---|
83 | }
|
---|
84 | protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
|
---|
85 | double val;
|
---|
86 | if (double.TryParse(value, out val)) {
|
---|
87 | this[rowIndex, columnIndex] = val;
|
---|
88 | return true;
|
---|
89 | } else {
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public new DoubleMatrix AsReadOnly() {
|
---|
95 | return (DoubleMatrix)base.AsReadOnly();
|
---|
96 | }
|
---|
97 |
|
---|
98 | #region IStringConvertibleMatrix Members
|
---|
99 | int IStringConvertibleMatrix.Rows {
|
---|
100 | get { return Rows; }
|
---|
101 | set { Rows = value; }
|
---|
102 | }
|
---|
103 | int IStringConvertibleMatrix.Columns {
|
---|
104 | get { return Columns; }
|
---|
105 | set { Columns = value; }
|
---|
106 | }
|
---|
107 | bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
|
---|
108 | return Validate(value, out errorMessage);
|
---|
109 | }
|
---|
110 | string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
|
---|
111 | return GetValue(rowIndex, columIndex);
|
---|
112 | }
|
---|
113 | bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
|
---|
114 | return SetValue(value, rowIndex, columnIndex);
|
---|
115 | }
|
---|
116 | #endregion
|
---|
117 | }
|
---|
118 | }
|
---|