Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/DoubleMatrix.cs @ 15353

Last change on this file since 15353 was 13841, checked in by jlodewyc, 9 years ago

#2582 More parameter datatypes, splitting fileopening service, approving users, reopen last file, change name tasks and repetitions

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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;
27using System.Linq;
28
29namespace HeuristicLab.Data {
30  [Item("DoubleMatrix", "Represents a matrix of double values.")]
31  [StorableClass]
32  public class DoubleMatrix : ValueTypeMatrix<double>, IStringConvertibleMatrix {
33    [StorableConstructor]
34    protected DoubleMatrix(bool deserializing) : base(deserializing) { }
35    protected DoubleMatrix(DoubleMatrix original, Cloner cloner)
36      : base(original, cloner) {
37    }
38    public DoubleMatrix() : base() { }
39    public DoubleMatrix(int rows, int columns) : base(rows, columns) { }
40    public DoubleMatrix(int rows, int columns, IEnumerable<string> columnNames) : base(rows, columns, columnNames) { }
41    public DoubleMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(rows, columns, columnNames, rowNames) { }
42    public DoubleMatrix(double[,] elements) : base(elements) { }
43    public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames) : base(elements, columnNames) { }
44    public DoubleMatrix(double[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(elements, columnNames, rowNames) { }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new DoubleMatrix(this, cloner);
48    }
49        public static DoubleMatrix Parse(string val)
50        {
51            try
52            {
53               
54                val = val.Replace("]", string.Empty);
55               
56                string[] parts = val.Split('[');
57                var r = parts.Length;
58                var c = parts[1].Split(';').Length;
59                val = val.Replace("\n", ";");
60                val = val.Replace("[", string.Empty);
61                string[] arr = System.Array.ConvertAll(val.Trim().Split(';'), p => p.Trim());
62                double[] a = arr.Where(s => s != "").ToList().ConvertAll(s => double.Parse(s)).ToArray();
63                double[,] b = new double[r,c];
64                var rowcount = 0;
65                for(int i = 0; i < a.Length;)
66                {
67                    for(int j = 0; j < c; j++)
68                    {
69                        b[rowcount, j] = a[i];
70                        i++;
71                    }
72                    rowcount++;
73                }
74                return new DoubleMatrix(b);
75            }
76            catch (System.FormatException e)
77            {
78                return null;
79            }
80        }
81
82        protected virtual bool Validate(string value, out string errorMessage) {
83      double val;
84      bool valid = double.TryParse(value, out val);
85      errorMessage = string.Empty;
86      if (!valid) {
87        StringBuilder sb = new StringBuilder();
88        sb.Append("Invalid Value (Valid Value Format: \"");
89        sb.Append(FormatPatterns.GetDoubleFormatPattern());
90        sb.Append("\")");
91        errorMessage = sb.ToString();
92      }
93      return valid;
94    }
95    protected virtual string GetValue(int rowIndex, int columIndex) {
96      return this[rowIndex, columIndex].ToString("r");
97    }
98    protected virtual bool SetValue(string value, int rowIndex, int columnIndex) {
99      double val;
100      if (double.TryParse(value, out val)) {
101        this[rowIndex, columnIndex] = val;
102        return true;
103      } else {
104        return false;
105      }
106    }
107
108    #region IStringConvertibleMatrix Members
109    int IStringConvertibleMatrix.Rows {
110      get { return Rows; }
111      set { Rows = value; }
112    }
113    int IStringConvertibleMatrix.Columns {
114      get { return Columns; }
115      set { Columns = value; }
116    }
117    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
118      return Validate(value, out errorMessage);
119    }
120    string IStringConvertibleMatrix.GetValue(int rowIndex, int columIndex) {
121      return GetValue(rowIndex, columIndex);
122    }
123    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
124      return SetValue(value, rowIndex, columnIndex);
125    }
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.