项目作者: tarabishy2020

项目描述 :
2D Matrix C# class
高级语言: C#
项目地址: git://github.com/tarabishy2020/sharp-matrix.git
创建时间: 2019-08-01T18:40:11Z
项目社区:https://github.com/tarabishy2020/sharp-matrix

开源协议:MIT License

下载


Sharp Matrix

This is a 2D Matrix C# class library.
Used in a lecture for a deep dive into machine learning matrix operations.

Structure is inspired from this repo https://github.com/rwl/ParallelColt and from here as well https://github.com/CodingTrain/Toy-Neural-Network-JS/blob/master/lib/matrix.js

The implementation uses a 1D List of doubles to store the data.

Features

  • Indexer to get and set matrix cells.
    1. Matrix2d m = new Matrix2d(3,3,1); // Create a 3x3 matrix of ones.
    2. Console.Write(m[1, 1]); // 1
    3. m[2,1] = 2;
    4. Console.Write(m.ToString());
    5. // 1 1 1
    6. // 1 1 1
    7. // 1 2 1
  • Indexer to quickly retrieve rows.

    1. Matrix2d m = new Matrix2d(3, 3, new List<double>() { 20, 21, 22, 30, 31, 31, 40, 41, 42 });
    2. m[1].ForEach(Console.WriteLine);
    3. // 30
    4. // 31
    5. // 31
  • Random initialization of values, uniform and Gaussian using the Box-Muller transformation.

    1. Matrix2d m_uniform = new Matrix2d(3, 3).Random();
    2. Console.Write(m_uniform.ToString());
    3. // 0.698004952956925 0.335880612645243 0.362230141350175
    4. // 0.494230345121692 0.665076169495041 0.984987090800417
    5. // 0.727741793136923 0.0698694698837909 0.0824739393230872
    6. Matrix2d m_gaussian = new Matrix2d(3, 3).Randn();
    7. Console.Write(m_gaussian.ToString());
    8. // -0.223498728675324 1.6653655980595 -1.13907259452779
    9. // -0.61656402728777 -0.370450094746215 -0.738776443648561
    10. // -0.27551853829444 2.10379830910225 1.40016473372225
  • Matrix transpose, returns a copy

    1. Matrix2d m = new Matrix2d(3, 2, new List<double>() { 20, 21, 30, 31, 40, 41 });
    2. Console.Write(m.ToString());
    3. // 20 21
    4. // 30 31
    5. // 40 41
    6. var mTranspose = m.Transpose();
    7. Console.Write(mTranspose.ToString());
    8. // 20 30 40
    9. // 21 31 41
    10. m.vDice(); //Transposes this matrix
  • Quick assigning and editing of values

    1. Matrix2d m = new Matrix2d(3, 3);
    2. m.Assign(5.0);
    3. Console.Write(m.ToString());
    4. // 5 5 5
    5. // 5 5 5
    6. // 5 5 5
    7. m.Assign(n => n*n);
    8. // 25 25 25
    9. // 25 25 25
    10. // 25 25 25
  • View row, view column and get shape

    1. Matrix2d m = new Matrix2d(3, 3).Random();
    2. // 0.05988795452746 0.594980841779607 0.715880787333418
    3. // 0.284296918327127 0.220808626721058 0.29949953514128
    4. // 0.767850642450084 0.848819678578908 0.6419840490641
    5. m.ViewRow(2); // List<double>(3) { 0.76785064245008428, 0.8488196785789075, 0.64198404906409978 }
    6. m.ViewColumn(0); // List<double>(3) { 0.05988795452746002, 0.28429691832712706, 0.76785064245008428 }
    7. m.Shape(); // "3 x 3 matrix"
  • Different operators support
    1. Matrix2d m1 = new Matrix2d(1,3, new List<double>() { 3, 4, 2});
    2. Matrix2d m2 = new Matrix2d(3, 3, new List<double>() {13, 9, 7, 8, 7, 4, 6, 4, 0});
    3. m1.Dot(m2); // [83 63 37]
    4. // Pairwise operations with a double or another matrix of the same shape *, /, +, -
    5. m2 * 2.0; // [[26 18 14],[16 14 8],[12 8 0]]
    6. m2 * m2; // [[676 324 196],[256 196 64],[144 64 0]]