Ignore:
Timestamp:
04/04/17 17:52:44 (6 months ago)
Author:
gkronber
Message:

#2650: merged the factors branch into trunk

Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Common/3.3/MatrixExtensions.cs

    r14185 r14826  
    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 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    }
    3798  }
    3899}
Note: See TracChangeset for help on using the changeset viewer.