Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Common/3.3/MatrixExtensions.cs @ 15632

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

#2640: Updated year of copyrights in license headers

File size: 3.4 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.Diagnostics.Contracts;
23
24namespace HeuristicLab.Common {
25  public static class MatrixExtensions {
26    /// <summary>
27    /// Returns a transposed matrix of the given <paramref name="matrix"/>.
28    /// </summary>
29    /// <typeparam name="T">The type of the matrix</typeparam>
30    /// <param name="matrix">The matrix that should be transposed.</param>
31    /// <returns>The transposed matrix.</returns>
32    public static T[,] Transpose<T>(this T[,] matrix) {
33      var result = new T[matrix.GetLength(1), matrix.GetLength(0)];
34      for (int i = 0; i < matrix.GetLength(0); i++)
35        for (int j = 0; j < matrix.GetLength(1); j++)
36          result[j, i] = matrix[i, j];
37      return result;
38    }
39
40    /// <summary>
41    /// Concatenates matrices vertically.
42    /// A      B
43    /// 1 2    9 8
44    /// 3 4    7 6
45    ///
46    /// VertCat(A, B)
47    /// 1 2
48    /// 3 4
49    /// 9 8
50    /// 7 6
51    /// </summary>
52    /// <typeparam name="T"></typeparam>
53    /// <param name="a"></param>
54    /// <param name="b"></param>
55    /// <returns>A new matrix with the number of rows = a.GetLength(0) + b.GetLength(0)</returns>
56    public static T[,] VertCat<T>(this T[,] a, T[,] b) {
57      Contract.Assert(a.GetLength(1) == b.GetLength(1));
58      var aLen = a.GetLength(0);
59      var bLen = b.GetLength(0);
60      var result = new T[aLen + bLen, a.GetLength(1)];
61      for (int i = 0; i < aLen; i++)
62        for (int j = 0; j < a.GetLength(1); j++)
63          result[i, j] = a[i, j];
64      for (int i = 0; i < bLen; i++)
65        for (int j = 0; j < b.GetLength(1); j++)
66          result[i + aLen, j] = b[i, j];
67
68      return result;
69    }
70
71    /// <summary>
72    /// Concatenates matrices horizontally.
73    /// A      B
74    /// 1 2    9 8
75    /// 3 4    7 6
76    ///
77    /// HorzCat(A, B)
78    /// 1 2 9 8
79    /// 3 4 7 6
80    /// </summary>
81    /// <typeparam name="T"></typeparam>
82    /// <param name="a"></param>
83    /// <param name="b"></param>
84    /// <returns>A new matrix with the number of columns = a.GetLength(1) + b.GetLength(1)</returns>
85    public static T[,] HorzCat<T>(this T[,] a, T[,] b) {
86      Contract.Assert(a.GetLength(0) == b.GetLength(0));
87      var aLen = a.GetLength(1);
88      var bLen = b.GetLength(1);
89      var result = new T[a.GetLength(0), aLen + bLen];
90      for (int i = 0; i < a.GetLength(0); i++)
91        for (int j = 0; j < aLen; j++)
92          result[i, j] = a[i, j];
93      for (int i = 0; i < a.GetLength(0); i++)
94        for (int j = 0; j < bLen; j++)
95          result[i, j + aLen] = b[i, j];
96      return result;
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.