Пример #1
0
        public void TestTheBasics()
        {
            ISimple class1 = GoInterface <ISimple, SimpleClass> .From(new SimpleClass());

            ISimple iface1 = GoInterface <ISimple, IDifferentName> .From(new SimpleClass());

            ISimple struct1 = GoInterface <ISimple, SimpleStruct> .From(new SimpleStruct(10));

            Assert.AreEqual(25, class1.Mutate(5));
            Assert.AreEqual(25, iface1.Mutate(5));
            Assert.AreEqual(70, struct1.Mutate(7));

            SimpleBase class2 = GoInterface <SimpleBase, SimpleClass> .From(new SimpleClass());

            SimpleBase iface2 = GoInterface <SimpleBase, IDifferentName> .From(new SimpleClass());

            SimpleBase struct2 = GoInterface <SimpleBase, SimpleStruct> .From(new SimpleStruct(10));

            for (int i = 0; i < 10; i++)
            {
                Assert.AreEqual(class1.Mutate(i), class2.Mutate(i));
                Assert.AreEqual(iface1.Mutate(i), iface2.Mutate(i));
                Assert.AreEqual(struct1.Mutate(i), struct2.Mutate(i));
            }

            Assert.AreEqual("forwarded!", struct1.ToString());
            Assert.AreEqual("forwarded!", struct2.ToString());
            Assert.AreEqual(struct1.GetHashCode(), struct2.GetHashCode());
            // Note that struct1.Equals(struct2) returns FALSE because struct2 is
            // not "really" a SimpleStruct. We must "unwrap" the right-hand side:
            Assert.That(struct1.Equals(GoInterface.Unwrap(struct2)));
            Assert.That(struct2.Equals(GoInterface.Unwrap(struct1)));
        }
Пример #2
0
 public virtual void specify0 <T>(SimpleBase <T, DMatrixRMaj> a, params SimpleBase <T, DMatrixRMaj>[] inputs)
     where T : SimpleBase <T, DMatrixRMaj>
 {
     SimpleBase <T, DMatrixRMaj>[] array = new SimpleBase <T, DMatrixRMaj> [inputs.Length + 1];
     Array.Copy(inputs, 0, array, 0, inputs.Length);
     array[inputs.Length] = a;
     specify(inputs);
 }
Пример #3
0
        /**
         * Computes the QR decomposition of the provided matrix.
         *
         * @param A Matrix which is to be decomposed.  Not modified.
         */
        public void decompose(SimpleMatrix <DMatrixRMaj> A)
        {
            this.QR = A.copy() as SimpleMatrix <DMatrixRMaj>;

            int N = Math.Min(A.numCols(), A.numRows());

            gammas = new double[A.numCols()];

            for (int i = 0; i < N; i++)
            {
                // use extract matrix to get the column that is to be zeroed
                SimpleMatrix <DMatrixRMaj> v = QR.extractMatrix(i, SimpleMatrix <DMatrixRMaj> .END, i, i + 1) as SimpleMatrix <DMatrixRMaj>;
                double max = v.elementMaxAbs();

                if (max > 0 && v.getNumElements() > 1)
                {
                    // normalize to reduce overflow issues
                    v = v.divide(max) as SimpleMatrix <DMatrixRMaj>;

                    // compute the magnitude of the vector
                    double tau = v.normF();

                    if (v.get(0) < 0)
                    {
                        tau *= -1.0;
                    }

                    double u_0   = v.get(0) + tau;
                    double gamma = u_0 / tau;

                    v = v.divide(u_0) as SimpleMatrix <DMatrixRMaj>;
                    v.set(0, 1.0);

                    // extract the submatrix of A which is being operated on
                    SimpleBase <DMatrixRMaj> A_small = QR.extractMatrix(i, SimpleMatrix <DMatrixRMaj> .END, i, SimpleMatrix <DMatrixRMaj> .END);

                    // A = (I - &gamma;*u*u<sup>T</sup>)A
                    A_small = A_small.plus(-gamma, v.mult(v.transpose()).mult(A_small));

                    // save the results
                    QR.insertIntoThis(i, i, A_small);
                    QR.insertIntoThis(i + 1, i, v.extractMatrix(1, SimpleMatrix <DMatrixRMaj> .END, 0, 1));

                    // Alternatively, the two lines above can be replaced with in-place equations
                    // READ THE JAVADOC TO UNDERSTAND HOW THIS WORKS!
//                QR.equation("QR(i:,i:) = A","QR",i,"i",A_small,"A");
//                QR.equation("QR((i+1):,i) = v(1:,0)","QR",i,"i",v,"v");

                    // save gamma for recomputing Q later on
                    gammas[i] = gamma;
                }
            }
        }
Пример #4
0
        public SimpleSVD(Matrix mat, bool compact)
        {
            this.mat  = mat;
            this.is64 = mat is DMatrixRMaj;
            if (is64)
            {
                DMatrixRMaj m = (DMatrixRMaj)mat;
                svd = (SingularValueDecomposition <T>)DecompositionFactory_DDRM.svd(m.numRows, m.numCols, true, true, compact);
            }
            else
            {
                FMatrixRMaj m = (FMatrixRMaj)mat;
                svd = (SingularValueDecomposition <T>)DecompositionFactory_FDRM.svd(m.numRows, m.numCols, true, true, compact);
            }

            if (!svd.decompose((T)mat))
            {
                throw new InvalidOperationException("Decomposition failed");
            }
            U = SimpleMatrix <T> .wrap(svd.getU(null, false));

            W = SimpleMatrix <T> .wrap(svd.getW(null));

            V = SimpleMatrix <T> .wrap(svd.getV(null, false));

            // order singular values from largest to smallest
            if (is64)
            {
                var um = U.getMatrix() as DMatrixRMaj;
                var wm = W.getMatrix() as DMatrixRMaj;
                var vm = V.getMatrix() as DMatrixRMaj;
                SingularOps_DDRM.descendingOrder(um, false, wm, vm, false);
                tol = SingularOps_DDRM.singularThreshold((SingularValueDecomposition_F64 <DMatrixRMaj>)svd);
            }
            else
            {
                var um = U.getMatrix() as FMatrixRMaj;
                var wm = W.getMatrix() as FMatrixRMaj;
                var vm = V.getMatrix() as FMatrixRMaj;
                SingularOps_FDRM.descendingOrder(um, false, wm, vm, false);
                tol = SingularOps_FDRM.singularThreshold((SingularValueDecomposition_F32 <FMatrixRMaj>)svd);
            }
        }
Пример #5
0
        public virtual T convert <T, W>(SimpleBase <T, W> matrix) where T : SimpleBase <T, W>
            where W : Matrix
        {
            if (matrix.getType == commonType)
            {
                return((T)matrix);
            }

            if (!matrix.getType.isDense() && commonType.isDense())
            {
                Console.Error.WriteLine("\n***** WARNING *****\n");
                Console.Error.WriteLine("Converting a sparse to dense matrix automatically.");
                Console.Error.WriteLine("Current auto convert code isn't that smart and this might have been available");
            }

            Matrix m = ConvertMatrixType.convert(matrix.mat, commonType);

            if (m == null)
            {
                throw new System.ArgumentException("Conversion from " + matrix.getType + " to " + commonType + " not possible");
            }

            return((T)matrix.wrapMatrix(m));
        }