/// <summary>
        /// Normalize the specified row in the weight matrix.
        /// </summary>
        /// <param name="matrix">The weight matrix.</param>
        /// <param name="row">The row to normalize.</param>
        protected void NormalizeWeight(Matrix.Matrix matrix, int row)
        {
            double len = MatrixMath.vectorLength(matrix.GetRow(row));

            len = Math.Max(len, VERYSMALL);

            len = 1.0 / len;
            for (int i = 0; i < this.inputNeuronCount; i++)
            {
                matrix[row, i] = matrix[row, i] * len;
            }
            matrix[row, this.inputNeuronCount] = 0;
        }
示例#2
0
        /// <summary>
        /// Processes the spectra in matrix xMatrix for prediction.
        /// </summary>
        /// <param name="xMatrix">The matrix of spectra. Each spectrum is a row of the matrix.</param>
        /// <param name="xMean">Not used.</param>
        /// <param name="xScale">Not used.</param>
        /// <param name="regionstart">Starting index of the region to process.</param>
        /// <param name="regionend">End index of the region to process.</param>
        public void ProcessForPrediction(IMatrix <double> xMatrix, IReadOnlyList <double> xMean, IReadOnlyList <double> xScale, int regionstart, int regionend)
        {
            int regionlength = regionend - regionstart;

            var helpervector = VectorMath.ToVector(new double[regionlength]);

            for (int n = 0; n < xMatrix.RowCount; n++)
            {
                var vector = MatrixMath.RowToVector(xMatrix, n, regionstart, regionlength);
                _filter.Apply(vector, helpervector);
                VectorMath.Copy(helpervector, vector);
            }
        }
示例#3
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);
        }
示例#4
0
    public static void Main()
    {
        Console.WriteLine("\n==================== TEST 1 ======================\n");
        double[,] matrix1 =
        {
            { 1, 2 },
            { 3, 4 }
        };
        Console.WriteLine($"Matrix1:\nRows = {matrix1.GetLength(0)}\nColumns = {matrix1.GetLength(1)}");
        char   direction = 'x';
        double factor    = 2;

        Console.WriteLine($"Factor = {factor}\nDirection = {direction}");

        printMatrix(MatrixMath.Shear2D(matrix1, direction, factor));

        Console.WriteLine("\n==================== TEST 2 ======================\n");
        double[,] matrix3 = new double[, ] {
            { 1, 2 },
            { 3, 4 }
            //{5, 2}
        };

        Console.WriteLine($"Matrix3:\nRows = {matrix3.GetLength(0)}\nColumns = {matrix3.GetLength(1)}");
        direction = 'y';
        Console.WriteLine($"Factor = {factor}\nDirection = {direction}");
        printMatrix(MatrixMath.Shear2D(matrix3, direction, factor));

        Console.WriteLine("\n==================== TEST 3 ======================\n");
        double[,] matrix4 = new double[, ] {
            { 6, 16 },
            { 5, 2 },
            { -3, -7 }
        };

        Console.WriteLine($"Matrix4:\nRows = {matrix4.GetLength(0)}\nColumns = {matrix4.GetLength(1)}");
        direction = 'x';
        Console.WriteLine($"Factor = {factor}\nDirection = {direction}");
        printMatrix(MatrixMath.Shear2D(matrix4, direction, factor));

        Console.WriteLine("\n==================== TEST 4 ======================\n");
        double[,] matrix5 = new double[, ] {
            { 6, 16 },
            { -3, -7 }
        };

        Console.WriteLine($"Matrix5:\nRows = {matrix5.GetLength(0)}\nColumns = {matrix5.GetLength(1)}");
        direction = 'a';
        Console.WriteLine($"Factor = {factor}\nDirection = {direction}");
        printMatrix(MatrixMath.Shear2D(matrix5, direction, factor));
    }
示例#5
0
 static void Main(string[] args)
 {
     double[,] matrix1 = { { 2, 3 }, { -1, 0 } };
     double[,] matrix2 = { { 1, 7 }, { -8, -2 } };
     double[,] result  = MatrixMath.Multiply(matrix1, matrix2);
     for (int i = 0; i < 2; i++)
     {
         for (int j = 0; j < 2; j++)
         {
             Console.Write(result[i, j] + " ");
         }
         Console.WriteLine();
     }
 }
示例#6
0
        private void WritePredictionScores()
        {
            _writer.WriteStartElement("PredictionScores");

            var predictionScores = _memento.Analysis.CalculatePredictionScores(
                _table,
                _numberOfFactors);

            for (int i = 0; i < _numberOfY; i++)
            {
                WriteVector("Score" + i.ToString(), MatrixMath.ColumnToROVector(predictionScores, i), _numberOfX);
            }
            _writer.WriteEndElement();
        }
示例#7
0
        public void Rank_6x4_5_Ok()
        {
            float[,] matrix = new float[6, 4] {
                { 689, 5, 0.5F, -25 },
                { 0, -7, 7, -1 },
                { 33, -66, 1, -3 },
                { -8.3F, -0.25F, -0.3F, 31 },
                { 0.36F, 12, -2, 12.36F },
                { -86, -9.6F, 7.25F, 45 }
            };
            ushort rank = MatrixMath.Rank(matrix);

            Assert.IsTrue(rank == 4, rank.ToString());
        }
示例#8
0
        public void Determinant_5x5_Ok()
        {
            float[,] m = new float[5, 5] {
                { 1, 2, 3, 4, 5 },
                { 10, 20, 30, 40, 50 },
                { 100, 200, 300, 400, 500 },
                { 1000, 2000, 3000, 4000, 5000 },
                { 10000, 20000, 30000, 40000, 50000 }
            };
            float exp = 0;
            float res = MatrixMath.Determinant(m);

            Assert.IsTrue(res == exp);
        }
示例#9
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("");
     }
 }
示例#10
0
        public void Rank_6x5_5_Ok()
        {
            float[,] matrix = new float[6, 5] {
                { 78, 0.36F, -73, 8, 17 },
                { 0, 48, 0.33F, -0.33F, 21 },
                { -88, 7.38F, 0.1F, -99, -0.58F },
                { 0, 19, 25, -0.21F, 62 },
                { 56, -5, -6.33F, 0.38F, 2 },
                { 0, 2.36F, 33, 4.3F, -9.4F }
            };
            ushort rank = MatrixMath.Rank(matrix);

            Assert.IsTrue(rank == 5, rank.ToString());
        }
示例#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();
     }
 }
示例#12
0
        /// <inheritdoc/>
        public double Probability(IMLDataPair o)
        {
            // double[] v = o.InputArray;
            //  Matrix vmm = Matrix.CreateColumnMatrix(EngineArray.Subtract(v,
            Matrix vmm = Matrix.CreateColumnMatrix(EngineArray.Subtract(o.Input,
                                                                        _mean));
            Matrix t      = MatrixMath.Multiply(_covarianceInv, vmm);
            double expArg = MatrixMath.Multiply(MatrixMath.Transpose(vmm), t)
                            [0, 0] * -0.5;

            return(Math.Exp(expArg)
                   / (Math.Pow(2.0 * Math.PI, _dimension / 2.0) * Math.Pow(
                          _covarianceDet, 0.5)));
        }
示例#13
0
        public void ShouldRegress()
        {
            var X = new Matrix(new double[, ]
            {
                { 13.0, 12.0, 0.0 },
                { 15.0, 19.0, 0.0 },
                { 12.0, 23.0, 0.0 },
                { 13.0, 30.0, 1.0 },
                { 13.0, 22.0, 1.0 },
                { 16.0, 11.0, 0.0 },
                { 15.0, 13.0, 1.0 },
                { 16.0, 28.0, 1.0 },
                { 15.0, 10.0, 1.0 },
                { 16.0, 11.0, 1.0 }
            });

            var Y = new Vector(new double[]
            {
                34.12,
                40.94,
                33.58,
                38.95,
                35.42,
                32.12,
                28.57,
                40.47,
                32.06,
                30.55
            });

            Debug.WriteLine($"Data:\n{X.ConcatColumns(Y):F2}");

            var Xdesign = new Vector(X.Rows).Fill(1).ConcatColumns(X);

            Debug.WriteLine($"Design:\n{Xdesign:F2}");

            var Xt   = Xdesign.Transpose();
            var coef = (Xt * Xdesign).Inverse() * Xt * Y;

            //var coef = LU.Solve(Xdesign, Y);

            var r2 = X.ConcatColumns(Y).RSquared(coef);

            Debug.Print($"R2={r2}");

            var y = MatrixMath.Predict(new double[] { 14, 12, 0 }, coef);

            Debug.Print($"Prediction is {y}");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RealFourierTransformation2D"/> class.
        /// </summary>
        /// <param name="sourceMatrix">The source matrix. This member is mandatory. Before you call <see cref="Execute"/>, you can set the other properties of this class as you like.</param>
        /// <exception cref="System.ArgumentNullException">SourceMatrix must not be null</exception>
        public RealFourierTransformation2D(IROMatrix <double> sourceMatrix)
        {
            if (null == sourceMatrix)
            {
                throw new ArgumentNullException("SourceMatrix must not be null");
            }

            if (sourceMatrix.RowCount < 2 || sourceMatrix.ColumnCount < 2)
            {
                throw new ArgumentException("SourceMatrix must at least have the dimensions 2x2");
            }

            _realMatrix = new DoubleMatrixInArray1DRowMajorRepresentation(sourceMatrix.RowCount, sourceMatrix.ColumnCount);
            MatrixMath.Copy(sourceMatrix, _realMatrix);
        }
示例#15
0
    static void Main(string[] args)
    {
        var a = new double[, ]
        {
            { 1, 2 },
            { 3, 4 },
        };

        var s = MatrixMath.Shear2D(a, 'y', 2);

        Console.WriteLine("success?: |{0}|", s[0, 0]);

        Console.WriteLine("0: |{0}|{1}|", s[0, 0], s[0, 1]);
        Console.WriteLine("1: |{0}|{1}|", s[1, 0], s[1, 1]);
    }
示例#16
0
        static void Main(string[] args)
        {
            var matrix = new double[2, 2] {
                { 1, 2 }, { 3, 4 }
            };
            // var matrix = new double[2,2]
            // {
            //     {2, 2},
            //     {9, 4}
            // };
            var s = MatrixMath.Rotate2D(matrix, -1.57);

            Console.WriteLine("0: |{0}|{1}|", s[0, 0], s[0, 1]);
            Console.WriteLine("1: |{0}|{1}|", s[1, 0], s[1, 1]);
        }
示例#17
0
        public void TestGauss2()
        {
            decimal[] data  = MatrixMath.CalculateGauss(matrix1);
            decimal   delta = 0.01m;

            Assert.AreEqual(validData1.Length, data.Length);

            for (var i = 0; i < validData1.Length; i++)
            {
                if (MatrixMath.Abs(data[i] - validData1[i]) > delta)
                {
                    Assert.Fail(MatrixMath.Abs(data[i] - validData1[i]).ToString());
                }
            }
        }
示例#18
0
        public void TestZeidel1()
        {
            decimal[] data  = MatrixMath.CalculateZeidel(matrix2, 0.00001m);
            decimal   delta = 0.0001m;

            Assert.AreEqual(validData2.Length, data.Length);

            for (var i = 0; i < validData2.Length; i++)
            {
                if (MatrixMath.Abs(data[i] - validData2[i]) > delta)
                {
                    Assert.Fail(MatrixMath.Abs(data[i] - validData2[i]).ToString());
                }
            }
        }
示例#19
0
    static void Main(string[] args)
    {
        double[,] matrix1 = { { 1, 2 }, { 3, 4 } };
        double angle = -1.57;

        Console.Write($"{MatrixToString(matrix1)}\tRotate\n{angle} radians\n");
        Console.WriteLine($"\t=\n{MatrixToString(MatrixMath.Rotate2D(matrix1, angle))}");
        Console.WriteLine("------------\n");

        double[,] matrix4 = { { 2, 1, 3 }, { -1, 5, 8 } };

        Console.Write($"{MatrixToString(matrix4)}\tRotate\n{angle} radians\n");
        Console.WriteLine($"\t=\n{MatrixToString(MatrixMath.Rotate2D(matrix4, angle))}");
        Console.WriteLine("------------\n");
    }
示例#20
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();
        }
    }
示例#21
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();
        }
示例#22
0
        /// <summary>
        ///   Calculates the regularization matrix.
        /// </summary>
        private void calculateRegularizationMatrix2D()
        {
            _regularizationMatrix = new double[4, 4];
            double a = 1.0, b = 0.0, c = 0.0, d = 1.0, tauX = 0.0, tauY = 0.0;
            double length = 1;


            if (L.nodes.Count >= 1)
            {
                tauX = -L.nodes[0].X;
                tauY = -L.nodes[0].Y;
            }
            if (L.nodes.Count >= 2)
            {
                var theta = -Math.Atan2((L.nodes[1].Y - L.nodes[0].Y), (L.nodes[1].X - L.nodes[0].X));
                if (MatrixMath.sameCloseZero(Math.Abs(theta), Math.PI / 2)) // theta is 90-degrees
                {
                    a      = d = 0.0;
                    b      = (theta > 0) ? -1 : 1;
                    c      = -b;
                    length = Math.Abs(L.nodes[1].Y - L.nodes[0].Y);
                }
                else if (MatrixMath.sameCloseZero(theta))//theta is 0-degrees
                {
                    a      = d = 1;
                    b      = c = 0;
                    length = Math.Abs(L.nodes[1].X - L.nodes[0].X);
                }
                else
                {
                    a      = d = Math.Cos(theta);
                    length = (L.nodes[1].X - L.nodes[0].X) / a;
                    b      = -Math.Sin(theta);
                    c      = -b;
                }
            }
            _regularizationMatrix[0, 0] = a / length;
            _regularizationMatrix[0, 1] = b / length;
            _regularizationMatrix[0, 2] = (a * tauX + b * tauY) / length;
            _regularizationMatrix[1, 0] = c / length;
            _regularizationMatrix[1, 1] = d / length;
            _regularizationMatrix[1, 2] = (c * tauX + d * tauY) / length;
            _regularizationMatrix[2, 2] = 1.0;
            _regularizationMatrix[3, 0] = 0.0;
            _regularizationMatrix[3, 1] = 0.0;
            _regularizationMatrix[3, 2] = 0.0;
            _regularizationMatrix[3, 3] = 1.0;
        }
示例#23
0
    public Matrix feedforward(Matrix inputs)
    {
        this.input = inputs;
        Matrix hidden    = MatrixMath.Multiplication(weightH1, inputs);
        Matrix hOut      = MatrixMath.Addition(hidden, bias1);
        Matrix hiddenOut = activationFunction(hidden);
        Matrix output    = MatrixMath.Multiplication(weightH2, hiddenOut);
        Matrix oOut      = MatrixMath.Addition(output, bias2);
        Matrix outputOut = activationFunction(output);

        learningOut = outputOut;
        hiddenLearn = hidden;
        outputOut.Print();
        Debug.Log("");
        return(outputOut);
    }
示例#24
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]);
    }
示例#25
0
        public static void CalculateXLeverageFromPreprocessed(
            IROMatrix <double> xScores,
            int numberOfFactors,
            IMatrix <double> leverage)
        {
            var subscores = new MatrixMath.LeftSpineJaggedArrayMatrix <double>(xScores.RowCount, numberOfFactors);

            MatrixMath.Submatrix(xScores, subscores);

            var decompose = new MatrixMath.SingularValueDecomposition(subscores);

            for (int i = 0; i < xScores.RowCount; i++)
            {
                leverage[i, 0] = decompose.HatDiagonal[i];
            }
        }
示例#26
0
        /// <summary>
        /// Creates an analyis from preprocessed spectra and preprocessed concentrations.
        /// </summary>
        /// <param name="matrixX">The spectral matrix (each spectrum is a row in the matrix). They must at least be centered.</param>
        /// <param name="matrixY">The matrix of concentrations (each experiment is a row in the matrix). They must at least be centered.</param>
        /// <param name="maxFactors">Maximum number of factors for analysis.</param>
        /// <returns>A regression object, which holds all the loads and weights neccessary for further calculations.</returns>
        protected override void AnalyzeFromPreprocessedWithoutReset(IROMatrix <double> matrixX, IROMatrix <double> matrixY, int maxFactors)
        {
            int numFactors = Math.Min(matrixX.ColumnCount, maxFactors);

            ExecuteAnalysis(matrixX, matrixY, ref numFactors, out var xLoads, out var xScores, out var V);

            var yLoads = new MatrixMath.LeftSpineJaggedArrayMatrix <double>(matrixY.RowCount, matrixY.ColumnCount);

            MatrixMath.Copy(matrixY, yLoads);

            _calib.NumberOfFactors = numFactors;
            _calib.XLoads          = xLoads;
            _calib.YLoads          = yLoads;
            _calib.XScores         = xScores;
            _calib.CrossProduct    = V;
        }
示例#27
0
        public void PseudoInverse2x2Test()
        {
            DoubleMatrix ma = new DoubleMatrix(2, 2);

            ma[0, 0] = 2;
            ma[0, 1] = 1;
            ma[1, 0] = 2;
            ma[1, 1] = 1;

            IMatrix mb = MatrixMath.PseudoInverse(ma);

            Assert.AreEqual(0.2, mb[0, 0], DoubleConstants.DBL_EPSILON);
            Assert.AreEqual(0.2, mb[0, 1], DoubleConstants.DBL_EPSILON);
            Assert.AreEqual(0.1, mb[1, 0], DoubleConstants.DBL_EPSILON);
            Assert.AreEqual(0.1, mb[1, 1], DoubleConstants.DBL_EPSILON);
        }
示例#28
0
        /// <summary>
        ///   Updates the position of a node.
        /// </summary>
        /// <param name = "update">The node to update.</param>
        /// <param name = "T">The Transformation matrix, T.</param>
        /// <param name = "given">The given rule node.</param>
        private static void TransformPositionOfNode(node update, double[,] T, node given)
        {
            var pt = new[] { given.X, given.Y, given.Z, 1 };

            pt = T.multiply(pt, 4);
            var newT = MatrixMath.Identity(4);

            newT[0, 3] = update.X = pt[0] / pt[3];
            newT[1, 3] = update.Y = pt[1] / pt[3];
            newT[2, 3] = update.Z = pt[2] / pt[3];

            if (update.DisplayShape != null)
            {
                update.DisplayShape.TransformMatrix = newT;
            }
        }
示例#29
0
 protected override void OnMouseWheel(MouseEventArgs e)
 {
     base.OnMouseWheel(e);
     if (e.Delta != 0)
     {
         float scale;
         if (e.Delta > 0)
         {
             scale = (0.01f * e.Delta);
         }
         else
         {
             scale = -100f / e.Delta;
         }
         worldMatrix *= MatrixMath.MatrixScale(scale, scale, scale);
     }
 }
示例#30
0
        public static void CalculateXLeverageFromPreprocessed(
            IROMatrix xScores,
            int numberOfFactors,
            IMatrix leverage)
        {
            IMatrix subscores = new MatrixMath.BEMatrix(xScores.Rows, numberOfFactors);

            MatrixMath.Submatrix(xScores, subscores);

            MatrixMath.SingularValueDecomposition decompose = new MatrixMath.SingularValueDecomposition(subscores);


            for (int i = 0; i < xScores.Rows; i++)
            {
                leverage[i, 0] = decompose.HatDiagonal[i];
            }
        }