Example #1
0
 /// <summary>
 /// Given a vector x, returns the derivative of x, elementwise
 /// </summary>
 /// <param name="x">the input vector x</param>
 /// <returns> elementwise application of the derivative of the relu function =  x > 0 ? 1 : 0.01</returns>
 public MathNet.Numerics.LinearAlgebra.Vector <double> CalculateDerivative(MathNet.Numerics.LinearAlgebra.Vector <double> x)
 {
     return(x.Map(s => s > 0 ? 1 : 0.01));
 }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //Input
            Matrix mL = null;

            DA.GetData(0, ref mL);

            //size of n x n matrix
            int n = mL.RowCount;


            //Number of eigenvalues/vectors to extract
            int eigsCount = n;

            if (Params.Input[1].SourceCount > 0)
            {
                DA.GetData(1, ref eigsCount);
            }

            if (eigsCount > n)
            {
                eigsCount = n;
            }
            else if (eigsCount < 1)
            {
                eigsCount = 1;
            }


            //Run calculation toggle
            bool run = false;

            DA.GetData(2, ref run);


            //Calculate

            //Create lists to store data
            List <double>     eigenValuesOutput       = new List <double>();
            DataTree <double> eigenVectorsOutput      = new DataTree <double>();
            Matrix            eigenVectorMatrixOutput = null;


            if (run && mL != null)
            {
                //Create matrix for MathNet eigendecomposition
                var matrix = MathNet.Numerics.LinearAlgebra.Matrix <double> .Build.Dense(n, n);

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        matrix[i, j] = mL[i, j];
                    }
                }

                //Eigendecomposition
                var evd = matrix.Evd(MathNet.Numerics.LinearAlgebra.Symmetricity.Symmetric);

                MathNet.Numerics.LinearAlgebra.Vector <Complex> eigsValComplex = evd.EigenValues;
                MathNet.Numerics.LinearAlgebra.Vector <Double>  eigsValReal    = eigsValComplex.Map(c => c.Real);
                MathNet.Numerics.LinearAlgebra.Matrix <double>  eigsVec        = evd.EigenVectors;


                //Convert data back to GH types

                //Eigenvalue list
                for (int i = 0; i < eigsCount; i++)
                {
                    eigenValuesOutput.Add(eigsValReal[i]);
                }

                //Eigenvectors
                eigenVectorMatrixOutput = new Matrix(n, eigsCount);
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < eigsCount; j++)
                    {
                        double val = eigsVec[i, j];

                        GH_Path path = new GH_Path(j);
                        eigenVectorsOutput.Add(val, path);

                        eigenVectorMatrixOutput[i, j] = val;
                    }
                }
            }


            //Output
            DA.SetDataList(0, eigenValuesOutput);
            DA.SetDataTree(1, eigenVectorsOutput);
            DA.SetData(2, eigenVectorMatrixOutput);
        }
Example #3
0
 /// <summary>
 /// Given an input vector x, this function computes and returns max(x,0.01x) elementwise
 /// </summary>
 /// <param name="x">the inout vector x</param>
 /// <returns> max(x,0.01x) </returns>
 public MathNet.Numerics.LinearAlgebra.Vector <double> CalculateActivation(MathNet.Numerics.LinearAlgebra.Vector <double> x)
 {
     return(x.Map(s => s > 0 ? s : 0.01 * s));
 }