Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Matrix.cs @ 9562

Last change on this file since 9562 was 8471, checked in by abeham, 12 years ago

#1913: integrated branch into trunk

File size: 7.1 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;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [NonDiscoverableType]
31  internal class Matrix : IEnumerable<double>, IDeepCloneable {
32    // this type is immutable
33    private readonly IEnumerable<double> values;
34    public readonly int Rows;
35    public readonly int Columns;
36
37    protected Matrix(Matrix original, Cloner cloner) {
38      this.values = original.values.ToArray();
39      this.Rows = original.Rows;
40      this.Columns = original.Columns;
41      cloner.RegisterClonedObject(original, this);
42    }
43    public Matrix(IEnumerable<double> vector) {
44      this.values = vector;
45      Rows = 1;
46      Columns = vector.Count();
47    }
48    public Matrix(IEnumerable<double> vector, int length) {
49      this.values = vector;
50      Rows = 1;
51      Columns = length;
52    }
53    public Matrix(double[,] matrix) {
54      this.values = GetOnlineValues(matrix);
55      Rows = matrix.GetLength(0);
56      Columns = matrix.GetLength(1);
57    }
58    public Matrix(IEnumerable<double> matrix, int rows, int columns) {
59      this.values = matrix;
60      Rows = rows;
61      Columns = columns;
62    }
63
64    public object Clone() {
65      return Clone(new Cloner());
66    }
67    public IDeepCloneable Clone(Cloner cloner) {
68      return new Matrix(this, cloner);
69    }
70
71    public Matrix Transpose() {
72      var result = new Matrix(Transpose(values, Columns, Rows), Columns, Rows);
73      return result;
74    }
75
76    private IEnumerable<double> Transpose(IEnumerable<double> values, int rows, int columns) {
77      // vectors don't need to be transposed
78      if (rows == 1 || columns == 1) {
79        foreach (var v in values) yield return v;
80        yield break;
81      }
82
83      int skip = 0;
84      var iter = values.GetEnumerator();
85      if (!iter.MoveNext()) yield break;
86      while (skip < rows) {
87        for (int i = 0; i < skip; i++) iter.MoveNext();
88        yield return iter.Current;
89        for (int j = 0; j < columns - 1; j++) {
90          for (int i = 0; i < rows; i++) iter.MoveNext();
91          yield return iter.Current;
92        }
93        skip++;
94        if (skip < rows) {
95          iter = values.GetEnumerator();
96          iter.MoveNext();
97        }
98      }
99    }
100
101    public Matrix Add(Matrix other) {
102      return new Matrix(AddOnline(other), Rows, Columns);
103    }
104
105    public void AddTo(double[,] matrix) {
106      if (Rows != matrix.GetLength(0) || Columns != matrix.GetLength(1)) throw new ArgumentException("unequal size", "matrix");
107      var iter = values.GetEnumerator();
108      for (int i = 0; i < Rows; i++)
109        for (int j = 0; j < Columns; j++) {
110          iter.MoveNext();
111          matrix[i, j] += iter.Current;
112        }
113    }
114
115    public Matrix Subtract(Matrix other) {
116      return new Matrix(SubtractOnline(other), Rows, Columns);
117    }
118
119    public Matrix Multiply(Matrix other) {
120      return new Matrix(MultiplyOnline(other), Rows, other.Columns);
121    }
122
123    public Matrix Multiply(double value) {
124      return new Matrix(values.Select(x => x * value), Rows, Columns);
125    }
126
127    public double VectorLength() {
128      return Math.Sqrt(SquaredVectorLength());
129    }
130
131    public double SquaredVectorLength() {
132      if (Rows != 1) throw new ArgumentException("Length only works on vectors.");
133      return values.Sum(x => x * x);
134    }
135
136    public Matrix OuterProduct(Matrix other) {
137      if (Rows != 1 || other.Rows != 1) throw new ArgumentException("OuterProduct can only be applied to vectors.");
138      return Transpose().Multiply(other);
139    }
140
141    public Matrix Negate() {
142      return new Matrix(values.Select(x => -x), Rows, Columns);
143    }
144
145    public Matrix Apply() {
146      return new Matrix(values.ToArray(), Rows, Columns);
147    }
148
149    public IEnumerator<double> GetEnumerator() { return values.GetEnumerator(); }
150    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
151
152
153    private IEnumerable<double> AddOnline(Matrix other) {
154      if (Rows != other.Rows || Columns != other.Columns) throw new ArgumentException("Number of rows and columns are not equal.");
155      var meIter = values.GetEnumerator();
156      var otherIter = other.GetEnumerator();
157      if (!meIter.MoveNext()) yield break;
158      if (!otherIter.MoveNext()) yield break;
159      for (int i = 0; i < Rows * Columns; i++) {
160        yield return meIter.Current + otherIter.Current;
161        meIter.MoveNext();
162        otherIter.MoveNext();
163      }
164    }
165
166    private IEnumerable<double> SubtractOnline(Matrix other) {
167      if (Rows != other.Rows || Columns != other.Columns) throw new ArgumentException("Number of rows and columns are not equal.");
168      var meIter = values.GetEnumerator();
169      var otherIter = other.GetEnumerator();
170      if (!meIter.MoveNext()) yield break;
171      if (!otherIter.MoveNext()) yield break;
172      for (int i = 0; i < Rows * Columns; i++) {
173        yield return meIter.Current - otherIter.Current;
174        meIter.MoveNext();
175        otherIter.MoveNext();
176      }
177    }
178
179    private IEnumerable<double> MultiplyOnline(Matrix other) {
180      if (Columns != other.Rows) throw new ArgumentException("Number of rows and columns are not equal.");
181      var meIter = values.GetEnumerator();
182      var otherByColumn = other.Transpose();
183      var otherIter = otherByColumn.GetEnumerator();
184      if (!meIter.MoveNext()) yield break;
185      if (!otherIter.MoveNext()) yield break;
186      for (int r = 0; r < Rows; r++) {
187        var row = new double[Columns];
188        for (int x = 0; x < Columns; x++) {
189          row[x] = meIter.Current;
190          meIter.MoveNext();
191        }
192        for (int c = 0; c < other.Columns; c++) {
193          var sum = 0.0;
194          for (int y = 0; y < other.Rows; y++) {
195            sum += row[y] * otherIter.Current;
196            otherIter.MoveNext();
197          }
198          yield return sum;
199        }
200        otherIter = otherByColumn.GetEnumerator();
201        otherIter.MoveNext();
202      }
203    }
204
205    private IEnumerable<double> GetOnlineValues(double[,] matrix) {
206      for (int i = 0; i < matrix.GetLength(0); i++)
207        for (int j = 0; j < matrix.GetLength(1); j++) {
208          yield return matrix[i, j];
209        }
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.