Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/05/16 14:25:28 (8 years ago)
Author:
gkronber
Message:

#2650: work in progress..

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Common/3.3/MatrixExtensions.cs

    r14185 r14237  
    2020#endregion
    2121
     22using System.Diagnostics.Contracts;
     23
    2224namespace HeuristicLab.Common {
    2325  public static class MatrixExtensions {
     
    3537      return result;
    3638    }
     39
     40    /// <summary>
     41    /// Concatenates matrices horizontally.
     42    /// A      B
     43    /// 1 2    9 8
     44    /// 3 4    7 6
     45    ///
     46    /// HorzCat(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[,] HorzCat<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    /// <summary>
     71    /// Concatenates matrices vertically.
     72    /// A      B
     73    /// 1 2    9 8
     74    /// 3 4    7 6
     75    ///
     76    /// VertCat(A, B)
     77    /// 1 2 9 8
     78    /// 3 4 7 6
     79    /// </summary>
     80    /// <typeparam name="T"></typeparam>
     81    /// <param name="a"></param>
     82    /// <param name="b"></param>
     83    /// <returns>A new matrix with the number of columns = a.GetLength(1) + b.GetLength(1)</returns>
     84    public static T[,] VertCat<T>(this T[,] a, T[,] b) {
     85      Contract.Assert(a.GetLength(0) == b.GetLength(0));
     86      var aLen = a.GetLength(1);
     87      var bLen = b.GetLength(1);
     88      var result = new T[a.GetLength(0), aLen + bLen];
     89      for (int i = 0; i < a.GetLength(0); i++)
     90        for (int j = 0; j < aLen; j++)
     91          result[i, j] = a[i, j];
     92      for (int i = 0; i < a.GetLength(0); i++)
     93        for (int j = 0; j < bLen; j++)
     94          result[i, j + aLen] = b[i, j];
     95      return result;
     96    }
    3797  }
    3898}
Note: See TracChangeset for help on using the changeset viewer.