Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
namespace artificialneuralnetwork
{
  public class MatrixMath
  {
    public MatrixMath ()
    {
    }
 
    private static void Validate(ref Matrix a, ref Matrix b)
    {
      if (a.Rows != b.Rows)
      {
        throw new System.Exception("To add the matrixes they must have the"
          + " same number of rows.  Matrix a has " + a.Rows
          + " rows and matrix b has " + b.Rows + " rows.");
      }
 
      if (a.Cols != b.Cols)
      {
        throw new System.Exception("To add the matrixes they must have the"
          + " same number of columns.  Matrix a has " + a.Cols
          + " cols and matrix b has " + b.Cols + " cols.");
      }
    }
 
    public static Matrix Add (ref Matrix a, ref Matrix b)
    {
      Validate(ref a, ref b);
 
      double [,] result = new double[a.Rows, a.Cols];
 
      for (int resultRow = 0; resultRow < a.Rows; resultRow++)
      {
        for (int resultCol = 0; resultCol < a.Cols; resultCol++)
        {
          result[resultRow, resultCol] = a[resultRow, resultCol] +
            b[resultRow, resultCol];
        }
      }
 
      return new Matrix(result);
    }
 
    public static void Copy (ref Matrix source, ref Matrix target)
    {
      Validate(ref source, ref target);
 
      for (int x = 0; x < source.Rows; x++)
      {
        for (int y = 0; y < source.Cols; y++)
        {
          target[x, y] = source[x, y];
        }
      }
    }
 
    public static Matrix DeleteCol (ref Matrix matrix, int deleted)
    {
      if (deleted >= matrix.Cols)
      {
        throw new System.Exception("This column cannot be deleted because "
          + deleted + " is equal or greater than the number of columns in the"
          + " passed-in matrix, which is " + matrix.Cols + ".");
      }
 
      double [,] newMatrix = new double[matrix.Rows, matrix.Cols - 1];
 
      for (int x = 0; x < matrix.Rows; x++)
      {
        int targetCol = 0;
 
        for (int y = 0; y < matrix.Cols; y++)
        {
          if (y != deleted)
          {
            newMatrix[x, targetCol] = matrix[x, y];
 
            targetCol++;
          }
        }
      }
 
      return new Matrix(newMatrix);
    }
 
    public static Matrix DeleteRow (ref Matrix matrix, int deleted)
    {
      if (deleted >= matrix.Rows)
      {
        throw new System.Exception("This column cannot be deleted because "
          + deleted + " is equal or greater than the number of rows in the"
          + " passed-in matrix, which is " + matrix.Rows + ".");
      }
 
      double [,] newMatrix = new double[matrix.Rows - 1, matrix.Cols];
 
      int targetRow = 0;
      for (int x = 0; x < matrix.Rows; x++)
      {
        if (deleted != targetRow)
        {
          for (int y = 0; y < matrix.Cols; y++)
          {
            newMatrix[targetRow, y] = matrix[x, y];
          }
 
          targetRow++;
        }
      }
 
      return new Matrix(newMatrix);
    }
 
    public static Matrix Divide (ref Matrix a, double b)
    {
      double [,] result = new double[a.Rows, a.Cols];
 
      for (int row = 0; row < a.Rows; row++)
      {
        for (int col = 0; col < a.Cols; col++)
        {
          result[row, col] = a[row, col] / b;
        }
      }
 
      return new Matrix(result);
    }
 
    public static double DotProduct (ref Matrix a, ref Matrix b)
    {
      if (!a.IsVector() || !b.IsVector())
      {
        throw new System.Exception("To take the dot product, both matrixes must"
          + " be vectors (have either one row, one column or a matrix with"
          + " just a single cell).");
      }
 
      double [] aArray = a.ToPackedArray();
      double [] bArray = b.ToPackedArray();
 
      if (aArray.Length != bArray.Length)
      {
        throw new System.Exception("To take the dot product, both matrixes"
          + " must be of the same length.");
      }
 
      double result = 0;
 
      for (int i = 0; i < aArray.Length; i++)
      {
        result += aArray[i] + bArray[i];
      }
 
      return result;
    }
 
    public static Matrix Identity (int size)
    {
      if (size < 1)
      {
        throw new System.Exception("Identity matrix must be at least of size"
          + " 1.");
      }
 
      Matrix result = new Matrix(size, size);
 
      for (int i = 0; i < size; i++)
      {
        result[i, i] = 1;
      }
 
      return result;
    }
 
    public static Matrix Multiply (ref Matrix a, double b)
    {
      double [,] result = new double[a.Rows, a.Cols];
 
      for (int x = 0; x < a.Rows; x++)
      {
        for (int y = 0; y < a.Cols; y++)
        {
          result[x, y] = a[x, y] * b;
        }
      }
 
      return new Matrix(result);
    }
 
    public static Matrix Multiply (ref Matrix a, ref Matrix b)
    {
      if (a.Cols != b.Rows)
      {
        throw new System.Exception("To use ordinary matrix multiplication the"
          + " number of columns on the first matrix must match the number of"
          + " rows on the second.");
      }
 
      double [,] result = new double[a.Rows, b.Cols];
 
      for (int resultRow = 0; resultRow < a.Rows; resultRow++)
      {
        for (int resultCol = 0; resultCol < b.Cols; resultCol++)
        {
          double value = 0;
 
          for (int i = 0; i < a.Cols; i++)
          {
            value += a[resultRow, i] * b[i, resultCol];
          }
 
          result[resultRow, resultCol] = value;
        }
      }
 
      return new Matrix(result);
    }
 
    public static Matrix Subtract (ref Matrix a, ref Matrix b)
    {
      if (a.Rows != b.Rows)
      {
        throw new System.Exception("To subtract the matrixes they must have"
          + " the same number of rows and columns.  Matrix a has " + a.Rows
          + " cols and matrix b has " + b.Rows + " cols.");
      }
 
      double [,] result = new double[a.Rows, a.Cols];
 
      for (int resultRow = 0; resultRow < a.Rows; resultRow++)
      {
        for (int resultCol = 0; resultCol < a.Cols; resultCol++)
        {
          result[resultRow, resultCol] = a[resultRow, resultCol]
            - b[resultRow, resultCol];
        }
      }
 
      return new Matrix(result);
    }
 
    public static Matrix Transpose (ref Matrix input)
    {
      double [,] inverseMatrix = new double[input.Cols, input.Rows];
 
      for (int x = 0; x < input.Rows; x++)
      {
        for (int y = 0; y < input.Cols; y++)
        {
          inverseMatrix[y, x] = input[x, y];
        }
      }
 
      return new Matrix(inverseMatrix);
    }
 
    public static double VectorLength (ref Matrix input)
    {
      if (!input.IsVector())
      {
        throw new System.Exception("Can only take the vector length of a"
          + " vector.");
      }
 
      double [] v = input.ToPackedArray();
 
      double rtn = 0.0;
 
      for (int i = 0; i < v.Length; i++)
      {
        rtn += System.Math.Pow(v[i], 2);
      }
 
      return System.Math.Sqrt(rtn);
    }
  }
}