www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is this an okay representation of a dynamically sized Matrix, to be

reply Enjoys Math <enjoysmath gmail.com> writes:
Code:
----
module matrix;

import std.array;


struct Matrix(E)
{
private:
    E[][];

    this() {
    }

    void deleteRow(int i)
    {
       E = E[0..i] ~ E[i..$];
    }

    void deleteColumn(int j)
    {
       for (int i=0; i < E.length; i++)
       {
          E[i] = E[i][0..j] ~ E[i][j..$];
       }
    }

    void insertRow(int i, E[] row)
    {
       if (E.length != 0)
          assert(E[0].length == row.length);
       E.insertInPlace(i, row);
    }

    void insertColumn(int j, E[] col)
    {
       for (int i=0; i < E.length; i++)
       {
          E[i].insertInPlace(j, col[i]);
       }
    }

    int numRows() { return E.length; }

    int numColumns() {
       if (E.length == 0)
          return 0;
       return E[0].length;
    }

}


Is there a more efficient way of doing this, knowing that I'll be 
inserting columns / rows every time we need to create a new 
hidden state so this matrix will be huge, for a good model.  By 
huge we'll probably use the full capacity of a long[] in D.  I've 
already tried doing this in MQL5 and we exceeded easily the max 
array capacity.
Dec 27 2017
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 28 December 2017 at 01:34:10 UTC, Enjoys Math wrote:
 Code:
 ----
 module matrix;

 import std.array;


 struct Matrix(E)
 {
 private:
    E[][];

    this() {
    }

    void deleteRow(int i)
    {
       E = E[0..i] ~ E[i..$];
    }

    void deleteColumn(int j)
    {
       for (int i=0; i < E.length; i++)
       {
          E[i] = E[i][0..j] ~ E[i][j..$];
       }
    }

    void insertRow(int i, E[] row)
    {
       if (E.length != 0)
          assert(E[0].length == row.length);
       E.insertInPlace(i, row);
    }

    void insertColumn(int j, E[] col)
    {
       for (int i=0; i < E.length; i++)
       {
          E[i].insertInPlace(j, col[i]);
       }
    }

    int numRows() { return E.length; }

    int numColumns() {
       if (E.length == 0)
          return 0;
       return E[0].length;
    }

 }


 Is there a more efficient way of doing this, knowing that I'll 
 be inserting columns / rows every time we need to create a new 
 hidden state so this matrix will be huge, for a good model.  By 
 huge we'll probably use the full capacity of a long[] in D.  
 I've already tried doing this in MQL5 and we exceeded easily 
 the max array capacity.
Full capacity of a long[] (or any array) is the entire addressable memory, which is 4GB for 32bit. You will not exceed the 64bit address space. Do you know the width of the matrix ahead of time? I assume they are all the same given the implementation of numColumns. Have you looked at mir (https://github.com/libmir/mir-algorithm) for ndslice? by deleting rows, can you get away with just zeroing them?
Dec 28 2017