public float GetAverage(FloatMatrix m)
        {
            float d = 0;

            MatrixLoop((i, j) => { d += m.GetValue(i, j); }, m.x, m.y);
            return(d / (m.x * m.y));
        }
 //Operations
 //Transpose
 public static FloatMatrix Transpose(FloatMatrix m)
 {
     float[,] mT = new float[m.y, m.x];
     MatrixLoop((i, j) => {
         mT[j, i] = m.GetValue(i, j);
     }, m.x, m.y);
     return(mT);
 }
 public static FloatMatrix Pow(FloatMatrix m2, float m1)
 {
     float[,] output = new float[m2.x, m2.y];
     MatrixLoop((i, j) => {
         output[i, j] = (float)Math.Pow(m2.GetValue(i, j), m1);
     }, m2.x, m2.y);
     return(output);
 }
        public static FloatMatrix MatfloatMult(FloatMatrix m2, float m1)
        {
            float[,] a = m2;
            float[,] b = new float[m2.x, m2.y];

            MatrixLoop((i, j) => {
                b[i, j] = a[i, j] * m1;
            }, b.GetLength(0), b.GetLength(1));

            return(b);
        }
        //Flat
        public FloatMatrix GetFlat(FloatMatrix m)
        {
            float[,] output = new float[m.x * m.y, 1];

            MatrixLoop((i, j) =>
            {
                output[m.x * i + j, 0] = m.GetValue(i, j);
            }, m.x, m.y);

            return(output);
        }
 public static FloatMatrix DeltaMult(FloatMatrix m1, FloatMatrix m2)
 {
     if (m1.x != m2.x || m1.y != m2.y)
     {
         throw new ArgumentException("Matrix must have the same dimensions");
     }
     float[,] output = new float[m1.x, m2.y];
     MatrixLoop((i, j) =>
     {
         output[i, j] = m1.GetValue(i, j) * m2.GetValue(i, j);
     }, m1.x, m2.y);
     return(output);
 }
        public float GetMax(FloatMatrix m)
        {
            float max = float.MinValue;

            MatrixLoop((i, j) =>
            {
                if (m.GetValue(i, j) > max)
                {
                    max = m.GetValue(i, j);
                }
            }, m.x, m.y);
            return(max);
        }
Exemplo n.º 8
0
 public static string[][] MatrixToCsv(FloatMatrix m)
 {
     float[,] _m = m;
     string[][] output = new string[m.x][];
     for (int i = 0; i < m.x; i++)
     {
         output[i] = new string[m.y];
         for (int j = 0; j < m.y; j++)
         {
             output[i][j] = _m[i, j].ToString();
         }
     }
     return(output);
 }
        //Sumatory
        public static FloatMatrix Sumatory(FloatMatrix m, int dimension = -1)
        {
            float[,] output;
            if (dimension == -1)
            {
                output = new float[1, 1];
            }
            else if (dimension == 0)
            {
                output = new float[m.x, 1];
            }
            else if (dimension == 1)
            {
                output = new float[1, m.y];
            }
            else
            {
                throw new ArgumentException("The dimension must be -1, 0 or 1");
            }

            if (dimension == -1)
            {
                MatrixLoop((i, j) =>
                {
                    output[0, 0] += m.GetValue(i, j);
                }, m.x, m.y);
            }
            else if (dimension == 0)
            {
                MatrixLoop((i, j) =>
                {
                    output[i, 0] += m.GetValue(i, j);
                }, m.x, m.y);
            }
            else if (dimension == 1)
            {
                MatrixLoop((i, j) =>
                {
                    output[0, j] += m.GetValue(i, j);
                }, m.x, m.y);
            }
            return(output);
        }
Exemplo n.º 10
0
        public static bool SaveMatrix(FloatMatrix m, string directory)
        {
            FileStream fs = new FileStream(directory, FileMode.Create);

            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, m.Matrix);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to serializate: " + e.Message);
                return(false);
            }
            finally
            {
                fs.Close();
            }
            return(true);
        }
Exemplo n.º 11
0
        public static bool LoadMatrix(out FloatMatrix m, string directory)
        {
            FileStream fs = new FileStream(directory, FileMode.Open);

            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                m = (float[, ])bf.Deserialize(fs);
            }
            catch (SerializationException e)
            {
                Console.WriteLine("Failed to deserializate: " + e.Message);
                m = null;
                return(false);
            }
            finally
            {
                fs.Close();
            }
            return(true);
        }
        public static FloatMatrix MatSum(FloatMatrix m1, FloatMatrix m2, bool neg = false)
        {
            if (m1.x != m2.x || m1.y != m2.y)
            {
                throw new ArgumentException("Matrix must have the same dimensions");
            }

            float[,] a = m1;
            float[,] b = m2;
            float[,] c = new float[m1.x, m2.y];
            MatrixLoop((i, j) => {
                if (!neg)
                {
                    c[i, j] = a[i, j] + b[i, j];
                }
                else
                {
                    c[i, j] = a[i, j] - b[i, j];
                }
            }, c.GetLength(0), c.GetLength(1));
            return(c);
        }
        public static FloatMatrix MatMult(FloatMatrix m1, FloatMatrix m2)
        {
            if (m1.y != m2.x)
            {
                throw new ArgumentException("Matrix must have compatible dimensions");
            }
            int n = m1.x;
            int m = m1.y;
            int p = m2.y;

            float[,] a = m1;
            float[,] b = m2;
            float[,] c = new float[n, p];
            MatrixLoop((i, j) => {
                float sum = 0;
                for (int k = 0; k < m; k++)
                {
                    sum += a[i, k] * b[k, j];
                }
                c[i, j] = sum;
            }, n, p);
            return(c);
        }
Exemplo n.º 14
0
        public static bool LoadMatrix(out FloatMatrix[] m, string directory)
        {
            try
            {
                FileStream fs = new FileStream(directory, FileMode.Open);
                try
                {
                    BinaryFormatter bf = new BinaryFormatter();

                    float[][,] _m = (float[][, ])bf.Deserialize(fs);
                    m             = new FloatMatrix[_m.Length];

                    for (int i = 0; i < _m.Length; i++)
                    {
                        m[i] = _m[i];
                    }
                }
                catch (SerializationException e)
                {
                    Console.WriteLine("Failed to deserializate: " + e.Message);
                    m = null;
                    return(false);
                }
                finally
                {
                    fs.Close();
                }
            }
            catch (Exception)
            {
                m = null;
                return(false);
            }

            return(true);
        }
        public FloatMatrix AddColumn(FloatMatrix m2)
        {
            if (_matrix == null)
            {
                throw new ArgumentException("Matrix can not be null");
            }
            if (m2.y != 1 || m2.x != x)
            {
                throw new ArgumentException("Invalid dimensions");
            }

            float[,] newMatrix = new float[x, y + 1];
            float[,] m         = Matrix;

            for (int i = 0; i < x; i++)
            {
                newMatrix[i, 0] = m2.GetValue(i, 0);
            }
            MatrixLoop((i, j) =>
            {
                newMatrix[i, j + 1] = m[i, j];
            }, x, y);
            return(newMatrix);
        }
        public FloatMatrix AddRow(FloatMatrix m2)
        {
            if (_matrix == null)
            {
                throw new ArgumentException("Matrix can not be null");
            }
            if (m2.x != 1 || m2.y != y)
            {
                throw new ArgumentException("Invalid dimensions");
            }

            float[,] newMatrix = new float[x + 1, y];
            float[,] m         = Matrix;

            for (int j = 0; j < y; j++)
            {
                newMatrix[0, j] = m2.GetValue(0, j);
            }
            MatrixLoop((i, j) =>
            {
                newMatrix[i + 1, j] = m[i, j];
            }, x, y);
            return(newMatrix);
        }
 //ABS
 public FloatMatrix GetAbs(FloatMatrix m)
 {
     float[,] d = m;
     MatrixLoop((i, j) => { d[i, j] = Math.Abs(m.GetValue(i, j)); }, m.x, m.y);
     return(d);
 }
 public static FloatMatrix Dot(FloatMatrix m1, FloatMatrix m2)
 {
     return(m1 * m2.T);
 }
 //DOT PRODUCT
 public FloatMatrix Dot(FloatMatrix m2)
 {
     return(Dot(this, m2));
 }