Exemplo n.º 1
0
    static void Main(string[] args)
    {
        double[,] vector = {{14, -3, 0}, {-11, -5, 3}, {2, -9, 13}};
        double[,] vector1 = {{6, 16, 21}, {5, 2, 0}, {1, 3, 7}};
        double[,] vector2 = {{8, -11}, {1, 2}};
        double[,] vector3 = {{-4, 9}, {1, 2}};
        double[,] res;
        double[,] res1;

        res = MatrixMath.Add(vector, vector1);
        res1 = MatrixMath.Add(vector2, vector3);

        for (int x = 0; x < res.GetLength(0); x++)
        {
            for (int y = 0; y < res.GetLength(1); y++)
            {
            Console.Write(res[x , y] + " ");
            }
            Console.WriteLine(',');
        }
        Console.WriteLine();
        for (int x = 0; x < res1.GetLength(0); x++)
        {
            for (int y = 0; y < res1.GetLength(1); y++)
            {
            Console.Write(res1[x, y] + " ");
            }
            Console.WriteLine(',');
        }
    }
Exemplo n.º 2
0
        /// <summary>
        /// Train the neural network for the specified pattern. The neural network
        /// can be trained for more than one pattern. To do this simply call the
        /// train method more than once.
        /// </summary>
        /// <param name="pattern">The pattern to train on.</param>
        public void Train(bool[] pattern)
        {
            if (pattern.Length != this.weightMatrix.Rows)
            {
                throw new NeuralNetworkError("Can't train a pattern of size "
                                             + pattern.Length + " on a hopfield network of size "
                                             + this.weightMatrix.Rows);
            }

            // Create a row matrix from the input, convert boolean to bipolar
            Matrix.Matrix m2 = Matrix.Matrix.CreateRowMatrix(BiPolarUtil
                                                             .Bipolar2double(pattern));
            // Transpose the matrix and multiply by the original input matrix
            Matrix.Matrix m1 = MatrixMath.Transpose(m2);
            Matrix.Matrix m3 = MatrixMath.Multiply(m1, m2);

            // matrix 3 should be square by now, so create an identity
            // matrix of the same size.
            Matrix.Matrix identity = MatrixMath.Identity(m3.Rows);

            // subtract the identity matrix
            Matrix.Matrix m4 = MatrixMath.Subtract(m3, identity);

            // now add the calculated matrix, for this pattern, to the
            // existing weight matrix.
            this.weightMatrix = MatrixMath.Add(this.weightMatrix, m4);
        }
 public static void Main()
 {
     Console.WriteLine("\n==================== TEST 1 ======================\n");
     double[,] matrix1 = new double[, ] {
         { 14, -3, 0 },
         { -11, -5, 3 },
         { 2, -9, 13 }
     };
     double[,] matrix2 = new double[, ] {
         { 6, 16, 21 },
         { 5, 2, 0 },
         { 1, 3, 7 }
     };
     printMatrix(MatrixMath.Add(matrix1, matrix2));
     Console.WriteLine("\n==================== TEST 2 ======================\n");
     double[,] matrix3 = new double[, ] {
         { 14, -3, 0 },
         { -11, -5, 3 },
     };
     double[,] matrix4 = new double[, ] {
         { 6, 16, 21 },
         { 5, 2, 0 },
         { 1, 3, 7 }
     };
     printMatrix(MatrixMath.Add(matrix3, matrix4));
 }
Exemplo n.º 4
0
    static void Main(string[] args)
    {
        double[,] matrix1 = { { 1, 3 }, { 1, 5 } };
        double[,] matrix2 = { { 2, 1 }, { 1, 2 } };

        Console.Write($"{MatrixToString(matrix1)}\t+\n{MatrixToString(matrix2)}");
        Console.WriteLine($"\t=\n{MatrixToString(MatrixMath.Add(matrix1, matrix2))}");
        Console.WriteLine("------------\n");

        double[,] matrix3 = { { 14, -3, 0 }, { -11, -5, 3 }, { 2, -9, 13 } };
        double[,] matrix4 = { { 6, 16, 21 }, { 5, 2, 0 }, { 1, 3, 7 } };

        Console.Write($"{MatrixToString(matrix3)}\t+\n{MatrixToString(matrix4)}");
        Console.WriteLine($"\t=\n{MatrixToString(MatrixMath.Add(matrix3, matrix4))}");
        Console.WriteLine("------------\n");

        Console.Write($"{MatrixToString(matrix2)}\t+\n{MatrixToString(matrix4)}");
        Console.WriteLine($"\t=\n{MatrixToString(MatrixMath.Add(matrix2, matrix4))}");
        Console.WriteLine("------------\n");

        double[,] matrix5 = { { 1 } };
        double[,] matrix6 = { { 2 } };

        Console.Write($"{MatrixToString(matrix5)}\t+\n{MatrixToString(matrix6)}");
        Console.WriteLine($"\t=\n{MatrixToString(MatrixMath.Add(matrix5, matrix6))}");
        Console.WriteLine("------------\n");

        double[,] matrix7 = { { 1, 3, 1, 2 }, { 1, 3, 1, 2 }, { 1, 3, 1, 2 }, { 1, 3, 1, 2 } };
        double[,] matrix8 = { { 1, 3, 1, 2 }, { 1, 3, 1, 2 }, { 1, 3, 1, 2 }, { 1, 3, 1, 2 } };

        Console.Write($"{MatrixToString(matrix7)}\t+\n{MatrixToString(matrix8)}");
        Console.WriteLine($"\t=\n{MatrixToString(MatrixMath.Add(matrix7, matrix8))}");
        Console.WriteLine("------------\n");
    }
Exemplo n.º 5
0
        public static void Predict(
            IROMatrix <double> XU,             // unknown spectrum or spectra,  horizontal oriented
            IROMatrix <double> xLoads,         // x-loads matrix
            IROMatrix <double> yLoads,         // y-loads matrix
            IROMatrix <double> W,              // weighting matrix
            IROMatrix <double> V,              // Cross product vector
            int numFactors,                    // number of factors to use for prediction
            IMatrix <double> predictedY,       // Matrix of predicted y-values, must be same number of rows as spectra
            IMatrix <double> spectralResiduals // Matrix of spectral residuals, n rows x 1 column, can be zero
            )
        {
            // now predicting a "unkown" spectra
            var si = new MatrixMath.ScalarAsMatrix <double>(0);
            var Cu = new MatrixMath.MatrixWithOneRow <double>(yLoads.ColumnCount);

            var wi    = new MatrixMath.MatrixWithOneRow <double>(XU.ColumnCount);
            var cuadd = new MatrixMath.MatrixWithOneRow <double>(yLoads.ColumnCount);

            // xu holds a single spectrum extracted out of XU
            var xu = new MatrixMath.MatrixWithOneRow <double>(XU.ColumnCount);

            // xl holds temporarily a row of the xLoads matrix+
            var xl = new MatrixMath.MatrixWithOneRow <double>(xLoads.ColumnCount);

            int maxFactors = Math.Min(yLoads.RowCount, numFactors);

            for (int nSpectrum = 0; nSpectrum < XU.RowCount; nSpectrum++)
            {
                MatrixMath.Submatrix(XU, xu, nSpectrum, 0); // extract one spectrum to predict
                MatrixMath.ZeroMatrix(Cu);                  // Set Cu=0
                for (int i = 0; i < maxFactors; i++)
                {
                    //1. Calculate the unknown spectral score for a weighting vector
                    MatrixMath.Submatrix(W, wi, i, 0);
                    MatrixMath.MultiplySecondTransposed(wi, xu, si);
                    // take the y loading vector
                    MatrixMath.Submatrix(yLoads, cuadd, i, 0);
                    // and multiply it with the cross product and the score
                    MatrixMath.MultiplyScalar(cuadd, si * V[0, i], cuadd);
                    // Add it to the predicted y-values
                    MatrixMath.Add(Cu, cuadd, Cu);
                    // remove the spectral contribution of the factor from the spectrum
                    // TODO this is quite ineffective: in every loop we extract the xl vector, we have to find a shortcut for this!
                    MatrixMath.Submatrix(xLoads, xl, i, 0);
                    MatrixMath.SubtractProductFromSelf(xl, (double)si, xu);
                }
                // xu now contains the spectral residual,
                // Cu now contains the predicted y values
                if (null != predictedY)
                {
                    MatrixMath.SetRow(Cu, 0, predictedY, nSpectrum);
                }

                if (null != spectralResiduals)
                {
                    spectralResiduals[nSpectrum, 0] = MatrixMath.SumOfSquares(xu);
                }
            } // for each spectrum in XU
        }     // end partial-least-squares-predict
Exemplo n.º 6
0
    public static void Main(string[] args)
    {
        double[,] matrix1 = { { 14, -3, 0 }, { -11, -5, 3 }, { 2, -9, 13 } };
        double[,] matrix2 = { { 6, 16, 21 }, { 5, 2, 0 }, { 1, 3, 7 } };

        double[,] result = MatrixMath.Add(matrix1, matrix2);

        Console.WriteLine("{0}", result[0, 0]);
    }
Exemplo n.º 7
0
 static void Main(string[] args)
 {
     double[,] product = MatrixMath.Add(new double[, ] {
         { 3, 4 }, { 3, 4 }
     }, new double[, ] {
         { 3, 4 }, { 3, 4 }
     });
     Console.WriteLine("|{0}|{1}|\n", product[0, 0], product[0, 1]);
     Console.WriteLine("|{0}|{1}|\n", product[1, 0], product[1, 1]);
 }
Exemplo n.º 8
0
        public void Add_3x4_DifferentDimensionException()
        {
            float[,] m1 = new float[3, 3] {
                { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }
            };
            float[,] m2 = new float[3, 4] {
                { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 7, 8, 9, 0 }
            };

            MatrixMath.Add(m1, m2);
        }
Exemplo n.º 9
0
        public void Train(bool[] pattern)
        {
            Matrix m2 = Matrix.CreateRowMatrix(BiPolarUtil.Bipolar2Double(pattern));

            Matrix m1 = MatrixMath.Transpose(m2);
            Matrix m3 = MatrixMath.Multiply(m1, m2);

            Matrix identity = MatrixMath.CreateIdentityMatrix(m3.Rows);

            Matrix m4 = MatrixMath.Subtract(m3, identity);

            _weightMatrix = MatrixMath.Add(_weightMatrix, m4);
        }
Exemplo n.º 10
0
 static void Main(string[] args)
 {
     double[,] matrix1 = { { 14, -3, 0 }, { -11, -5, 3 }, { 2, -9, 13 } };
     double[,] matrix2 = { { 6, 16, 21 }, { 5, 2, 0 }, { 1, 3, 7 } };
     double[,] result  = MatrixMath.Add(matrix1, matrix2);
     for (int i = 0; i < result.GetLength(0); i++)
     {
         for (int j = 0; j < result.GetLength(1); j++)
         {
             Console.Write(result[i, j] + " ");
         }
         Console.WriteLine("");
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// Learn from the last error calculation.
 /// </summary>
 /// <param name="learnRate">The learning rate.</param>
 /// <param name="momentum">The momentum.</param>
 public void Learn(double learnRate, double momentum)
 {
     // process the matrix
     if (this.layer.HasMatrix())
     {
         Matrix.Matrix m1 = MatrixMath.Multiply(this.accMatrixDelta,
                                                learnRate);
         Matrix.Matrix m2 = MatrixMath.Multiply(this.matrixDelta, momentum);
         this.matrixDelta       = MatrixMath.Add(m1, m2);
         this.layer.LayerMatrix = (MatrixMath.Add(this.layer.LayerMatrix,
                                                  this.matrixDelta));
         this.accMatrixDelta.Clear();
     }
 }
Exemplo n.º 12
0
    static void Main(string[] args)
    {
        double[,] matrix1    = { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
        double[,] matrix2    = { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
        double[,] new_matrix = MatrixMath.Add(matrix1, matrix2);

        for (int i = 0; i < new_matrix.GetLength(0); i++)
        {
            for (int j = 0; j < new_matrix.GetLength(1); j++)
            {
                Console.Write("{0} ", new_matrix[i, j]);
            }
            Console.WriteLine();
        }
    }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            Matrix matrix1 = new Matrix(new double[, ] {
                { 1, 4, 6 }, { -2, 3, 5 }, { 1, 0, 4 }
            });
            Matrix matrix2 = new Matrix(new double[, ] {
                { 5, 6, 0 }, { 7, 8, 0 }, { 0, 0, 1 }
            });
            int scalar = 2;

            Console.WriteLine("Matrix 1:");
            matrix1.Print();
            Console.WriteLine("");

            Console.WriteLine("Matrix 2:");
            matrix2.Print();
            Console.WriteLine("");

            Console.WriteLine("Matrix 1 + Matrix 2:");
            MatrixMath.Add(matrix1, matrix2).Print();
            Console.WriteLine("");

            Console.WriteLine("Matrix 1 - Matrix 2:");
            MatrixMath.Subtract(matrix1, matrix2).Print();
            Console.WriteLine("");

            Console.WriteLine("Matrix 1 * " + scalar);
            MatrixMath.Add(matrix1, matrix2).Print();
            Console.WriteLine("");

            Console.WriteLine("Matrix 1 * Matrix 2:");
            MatrixMath.Multiply(matrix1, matrix2).Print();
            Console.WriteLine("");

            Console.WriteLine("Matrix 1 Determinant:");
            Console.WriteLine(MatrixMath.GetDeterminant(matrix1));
            Console.WriteLine("");

            Console.WriteLine("Matrix 1 Transposed:");
            MatrixMath.Transpose(matrix1).Print();
            Console.WriteLine("");

            Console.WriteLine("Matrix 1 Inverted:");
            MatrixMath.Invert(matrix1).Print();
            Console.WriteLine("");

            Console.ReadLine();
        }
Exemplo n.º 14
0
    static void Main(string[] args)
    {
        var a = new double[, ] {
            { 14, -3, 0 }, { -11, -5, 3 }, { 2, -9, 13 }
        };

        var b = new double[, ] {
            { 6, 16, 21 }, { 5, 2, 0 }, { 1, 3, 7 }
        };

        var s = MatrixMath.Add(a, b);

        Console.WriteLine("|{0}|{1}|{2}|\n", s[0, 0], s[0, 1], s[0, 2]);
        Console.WriteLine("|{0}|{1}|{2}|\n", s[1, 0], s[1, 1], s[1, 2]);
        Console.WriteLine("|{0}|{1}|{2}|\n", s[2, 0], s[2, 1], s[2, 2]);
    }
Exemplo n.º 15
0
        public void Add_3x3_Ok()
        {
            float[,] m1 = new float[3, 3] {
                { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }
            };
            float[,] m2 = new float[3, 3] {
                { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }
            };

            float[,] exp = new float[3, 3] {
                { 2, 4, 6 }, { 8, 10, 12 }, { 14, 16, 18 }
            };
            float[,] res = MatrixMath.Add(m1, m2);

            for (ushort x = 0; x < 3; x++)
            {
                for (ushort y = 0; y < 3; y++)
                {
                    Assert.IsTrue(exp[x, y] == res[x, y]);
                }
            }
        }
Exemplo n.º 16
0
    static void Main(string[] args)
    {
        double[,] matrix  = { { 1, 2 }, { 3, 4 } };
        double[,] matrix1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        double[,] matrix2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

        Console.WriteLine("matrix 2D 0 {0}", matrix.GetLength(0));
        Console.WriteLine("matrix 3D 0 {0}", matrix2.GetLength(0));
        Console.WriteLine("matrix 2D 1 {0}", matrix.GetLength(1));
        Console.WriteLine("matrix 3D 1 {0}", matrix2.GetLength(1));

        double[,] total = MatrixMath.Add(matrix1, matrix2);
        int dim1 = matrix1.GetLength(0);

        for (int i = 0; i < dim1; i++)
        {
            for (int j = 0; j < dim1; j++)
            {
                Console.WriteLine(total[i, j]);
            }
        }
    }