当前位置: 首页>>代码示例>>C#>>正文


C# Matrix.Multiply方法代码示例

本文整理汇总了C#中System.Matrix.Multiply方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.Multiply方法的具体用法?C# Matrix.Multiply怎么用?C# Matrix.Multiply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Matrix的用法示例。


在下文中一共展示了Matrix.Multiply方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestMultiply

		[Test()] public void TestMultiply()
		{
			var matrix = new Matrix<float>(5, 5);
			float[] row = { 1, 2, 3, 4, 5 };
			for (int i = 0; i < 5; i++)
				matrix.SetRow(i, row);
			matrix.Multiply(2.5f);
			float[] testrow = { 2.5f, 5f, 7.5f, 10f, 12.5f };
			Assert.AreEqual(testrow, matrix.GetRow(3));
		}
开发者ID:WisonHuang,项目名称:MyMediaLite,代码行数:10,代码来源:MatrixExtensionsTest.cs

示例2: ExceptionInterfaceIncompatibleMatrices

        public void ExceptionInterfaceIncompatibleMatrices()
        {
            IMathematicalMatrix matrix1 = new Matrix(2, 3);
            matrix1[0, 0] = 1;
            matrix1[0, 1] = 2;
            matrix1[0, 2] = -4;
            matrix1[1, 0] = 0;
            matrix1[1, 1] = 3;
            matrix1[1, 2] = -1;

            IMathematicalMatrix matrix2 = MatrixTest.GetTestMatrix();

            matrix1.Multiply(matrix2);
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:14,代码来源:Multiply.cs

示例3: MultiplyRaiseException

        public void MultiplyRaiseException()
        {
            Matrix matrix1 = new Matrix(new double[][] { new double[] { 1.0, 2.0 }, new double[] { 3.0, 4.0 } });
            Matrix matrix2 = new Matrix(new double[][] { new double[] { 5.0, 6.0 }, new double[] { 7.0, 8.0 }, new double[] { 9.0, 10.0 } });

            try
            {
                matrix1.Multiply(matrix2);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                Assert.AreEqual("Matrices cannot be multiplied", ex.Message);
            }
        }
开发者ID:ajlopez,项目名称:MathelSharp,代码行数:16,代码来源:MatrixTests.cs

示例4: MultiplyByNumber

        public void MultiplyByNumber()
        {
            Matrix matrix1 = new Matrix(new double[][] { new double[] { 1.0, 2.0 }, new double[] { 3.0, 4.0 } });
            Matrix matrix2 = new Matrix(new double[][] { new double[] { 1.0 * 3.0, 2.0 * 3.0 }, new double[] { 3.0 * 3.0, 4.0 * 3.0 } });

            Matrix matrix = matrix1.Multiply(3.0);

            Assert.AreEqual(matrix2, matrix);
        }
开发者ID:ajlopez,项目名称:MathelSharp,代码行数:9,代码来源:MatrixTests.cs

示例5: Multiply

        public void Multiply()
        {
            Matrix matrix1 = new Matrix(new double[][] { new double[] { 1.0, 2.0 }, new double[] { 3.0, 4.0 } });
            Matrix matrix2 = new Matrix(new double[][] { new double[] { 5.0, 6.0 }, new double[] { 7.0, 8.0 } });

            Matrix matrix = matrix1.Multiply(matrix2);

            Assert.AreEqual(4, matrix.Size);

            var elements = matrix.Elements;

            Assert.AreEqual((1.0 * 5.0) + (2.0 * 7.0), elements[0][0]);
            Assert.AreEqual((1.0 * 6.0) + (2.0 * 8.0), elements[0][1]);
            Assert.AreEqual((3.0 * 5.0) + (4.0 * 7.0), elements[1][0]);
            Assert.AreEqual((3.0 * 6.0) + (4.0 * 8.0), elements[1][1]);
        }
开发者ID:ajlopez,项目名称:MathelSharp,代码行数:16,代码来源:MatrixTests.cs

示例6: Gradient

    private static double Gradient(RealVector A, RealVector grad, double[,] data, double[] classes, int dimensions, int neighborSamples, double regularization) {
      var instances = data.GetLength(0);
      var attributes = data.GetLength(1);

      var AMatrix = new Matrix(A, A.Length / dimensions, dimensions);

      alglib.sparsematrix probabilities;
      alglib.sparsecreate(instances, instances, out probabilities);
      var transformedDistances = new Dictionary<int, double>(instances);
      for (int i = 0; i < instances; i++) {
        var iVector = new Matrix(GetRow(data, i), data.GetLength(1));
        for (int k = 0; k < instances; k++) {
          if (k == i) {
            transformedDistances.Remove(k);
            continue;
          }
          var kVector = new Matrix(GetRow(data, k));
          transformedDistances[k] = Math.Exp(-iVector.Multiply(AMatrix).Subtract(kVector.Multiply(AMatrix)).SumOfSquares());
        }
        var normalization = transformedDistances.Sum(x => x.Value);
        if (normalization <= 0) continue;
        foreach (var s in transformedDistances.Where(x => x.Value > 0).OrderByDescending(x => x.Value).Take(neighborSamples)) {
          alglib.sparseset(probabilities, i, s.Key, s.Value / normalization);
        }
      }
      alglib.sparseconverttocrs(probabilities); // needed to enumerate in order (top-down and left-right)

      int t0 = 0, t1 = 0, r, c;
      double val;
      var pi = new double[instances];
      while (alglib.sparseenumerate(probabilities, ref t0, ref t1, out r, out c, out val)) {
        if (classes[r].IsAlmost(classes[c])) {
          pi[r] += val;
        }
      }

      var innerSum = new double[attributes, attributes];
      while (alglib.sparseenumerate(probabilities, ref t0, ref t1, out r, out c, out val)) {
        var vector = new Matrix(GetRow(data, r)).Subtract(new Matrix(GetRow(data, c)));
        vector.OuterProduct(vector).Multiply(val * pi[r]).AddTo(innerSum);

        if (classes[r].IsAlmost(classes[c])) {
          vector.OuterProduct(vector).Multiply(-val).AddTo(innerSum);
        }
      }

      var func = -pi.Sum() + regularization * AMatrix.SumOfSquares();

      r = 0;
      var newGrad = AMatrix.Multiply(-2.0).Transpose().Multiply(new Matrix(innerSum)).Transpose();
      foreach (var g in newGrad) {
        grad[r] = g + regularization * 2 * A[r];
        r++;
      }

      return func;
    }
开发者ID:thunder176,项目名称:HeuristicLab,代码行数:57,代码来源:NcaGradientCalculator.cs

示例7: Solve

        /// <summary>
        /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the
        /// solution vector and x is the unknown vector.
        /// </summary>
        /// <param name="matrix">The coefficient matrix, <c>A</c>.</param>
        /// <param name="input">The solution vector, <c>b</c></param>
        /// <param name="result">The result vector, <c>x</c></param>
        public void Solve(Matrix matrix, Vector input, Vector result)
        {
            // If we were stopped before, we are no longer
            // We're doing this at the start of the method to ensure
            // that we can use these fields immediately.
            _hasBeenStopped = false;

            // Error checks
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if (matrix.RowCount != matrix.ColumnCount)
            {
                throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
            }

            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            if (result.Count != input.Count)
            {
                throw new ArgumentException(Resources.ArgumentVectorsSameLength);
            }

            if (input.Count != matrix.RowCount)
            {
                throw Matrix.DimensionsDontMatch<ArgumentException>(input, matrix);
            }

            // Initialize the solver fields
            // Set the convergence monitor
            if (_iterator == null)
            {
                _iterator = Iterator.CreateDefault();
            }

            if (_preconditioner == null)
            {
                _preconditioner = new UnitPreconditioner();
            }

            _preconditioner.Initialize(matrix);

            var d = new DenseVector(input.Count);
            var r = new DenseVector(input);

            var uodd = new DenseVector(input.Count);
            var ueven = new DenseVector(input.Count);

            var v = new DenseVector(input.Count);
            var pseudoResiduals = new DenseVector(input);

            var x = new DenseVector(input.Count);
            var yodd = new DenseVector(input.Count);
            var yeven = new DenseVector(input);

            // Temp vectors
            var temp = new DenseVector(input.Count);
            var temp1 = new DenseVector(input.Count);
            var temp2 = new DenseVector(input.Count);

            // Initialize
            var startNorm = input.Norm(2);

            // Define the scalars
            double alpha = 0;
            double eta = 0;
            double theta = 0;

            var tau = startNorm;
            var rho = tau * tau;

            // Calculate the initial values for v
            // M temp = yEven
            _preconditioner.Approximate(yeven, temp);

            // v = A temp
            matrix.Multiply(temp, v);

            // Set uOdd
            v.CopyTo(ueven);

            // Start the iteration
            var iterationNumber = 0;
//.........这里部分代码省略.........
开发者ID:nrolland,项目名称:mathnet-numerics,代码行数:101,代码来源:TFQMR.cs

示例8: Multiply

        /// <summary>
        /// return a * b
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Matrix Multiply(Matrix a, Matrix b) {
            a.ShouldNotBeNull("a");
            b.ShouldNotBeNull("b");
            Guard.Assert(a.Cols == b.Rows, "Matrix dimension is not match. a.Cols equals b.Rows for Multipling.");

            var result = new Matrix(a.Rows, b.Cols);
            result.Multiply(a, b);

            return result;
        }
开发者ID:debop,项目名称:NFramework,代码行数:16,代码来源:MatrixTool.cs

示例9: Solve


//.........这里部分代码省略.........
            Vector nu = new DenseVector(residuals.Count);
            Vector vecS = new DenseVector(residuals.Count);
            Vector vecSdash = new DenseVector(residuals.Count);
            Vector temp = new DenseVector(residuals.Count);
            Vector temp2 = new DenseVector(residuals.Count);

            // create some temporary double variables that are needed
            // to hold values in between iterations
            Complex currentRho = 0;
            Complex alpha = 0;
            Complex omega = 0;

            var iterationNumber = 0;
            while (ShouldContinue(iterationNumber, result, input, residuals))
            {
                // rho_(i-1) = r~^T r_(i-1) // dotproduct r~ and r_(i-1)
                var oldRho = currentRho;
                currentRho = tempResiduals.DotProduct(residuals);

                // if (rho_(i-1) == 0) // METHOD FAILS
                // If rho is only 1 ULP from zero then we fail.
                if (currentRho.Real.AlmostEqual(0, 1) && currentRho.Imaginary.AlmostEqual(0, 1))
                {
                    // Rho-type breakdown
                    throw new Exception("Iterative solver experience a numerical break down");
                }

                if (iterationNumber != 0)
                {
                    // beta_(i-1) = (rho_(i-1)/rho_(i-2))(alpha_(i-1)/omega(i-1))
                    var beta = (currentRho / oldRho) * (alpha / omega);

                    // p_i = r_(i-1) + beta_(i-1)(p_(i-1) - omega_(i-1) * nu_(i-1))
                    nu.Multiply(-omega, temp);
                    vecP.Add(temp, temp2);
                    temp2.CopyTo(vecP);

                    vecP.Multiply(beta, vecP);
                    vecP.Add(residuals, temp2);
                    temp2.CopyTo(vecP);
                }
                else
                {
                    // p_i = r_(i-1)
                    residuals.CopyTo(vecP);
                }

                // SOLVE Mp~ = p_i // M = preconditioner
                _preconditioner.Approximate(vecP, vecPdash);
                
                // nu_i = Ap~
                matrix.Multiply(vecPdash, nu);

                // alpha_i = rho_(i-1)/ (r~^T nu_i) = rho / dotproduct(r~ and nu_i)
                alpha = currentRho * 1 / tempResiduals.DotProduct(nu);

                // s = r_(i-1) - alpha_i nu_i
                nu.Multiply(-alpha, temp);
                residuals.Add(temp, vecS);

                // Check if we're converged. If so then stop. Otherwise continue;
                // Calculate the temporary result. 
                // Be careful not to change any of the temp vectors, except for
                // temp. Others will be used in the calculation later on.
                // x_i = x_(i-1) + alpha_i * p^_i + s^_i
                vecPdash.Multiply(alpha, temp);
开发者ID:EricGT,项目名称:mathnet-numerics,代码行数:67,代码来源:BiCgStab.cs

示例10: IterateBatch


//.........这里部分代码省略.........
            for (int index = 0; index < ratings.Count; index++)
            {
                int user_id = ratings.Users[index];
                int item_id = ratings.Items[index];

                // prediction
                float score = global_bias + user_bias[user_id] + item_bias[item_id];
                score += DataType.MatrixExtensions.RowScalarProduct(user_factors, user_id, item_factors, item_id);
                double sig_score = 1 / (1 + Math.Exp(-score));

                float prediction = (float) (MinRating + sig_score * rating_range_size);
                float error      = prediction - ratings[index];

                float gradient_common = compute_gradient_common(sig_score, error);

                user_bias_gradient[user_id] += gradient_common;
                item_bias_gradient[item_id] += gradient_common;

                for (int f = 0; f < NumFactors; f++)
                {
                    float u_f = user_factors[user_id, f];
                    float i_f = item_factors[item_id, f];

                    user_factors_gradient.Inc(user_id, f, gradient_common * i_f);
                    item_factors_gradient.Inc(item_id, f, gradient_common * u_f);
                }
            }

            // I.2 L2 regularization
            //        biases
            for (int u = 0; u < user_bias_gradient.Length; u++)
                user_bias_gradient[u] += user_bias[u] * RegU * BiasReg;
            for (int i = 0; i < item_bias_gradient.Length; i++)
                item_bias_gradient[i] += item_bias[i] * RegI * BiasReg;
            //        latent factors
            for (int u = 0; u < user_factors_gradient.dim1; u++)
                for (int f = 0; f < user_factors_gradient.dim2; f++)
                    user_factors_gradient.Inc(u, f, user_factors[u, f] * RegU);

            for (int i = 0; i < item_factors_gradient.dim1; i++)
                for (int f = 0; f < item_factors_gradient.dim2; f++)
                    item_factors_gradient.Inc(i, f, item_factors[i, f] * RegI);

            // I.3 social network regularization -- see eq. (13) in the paper
            if (SocialRegularization != 0)
                for (int u = 0; u < user_factors_gradient.dim1; u++)
                {
                    var sum_connections        = new float[NumFactors];
                    float bias_sum_connections = 0;
                    int num_connections        = user_connections[u].Count;
                    foreach (int v in user_connections[u])
                    {
                        bias_sum_connections += user_bias[v];
                        for (int f = 0; f < sum_connections.Length; f++)
                            sum_connections[f] += user_factors[v, f];
                    }
                    if (num_connections != 0)
                    {
                        user_bias_gradient[u] += social_regularization * (user_bias[u] - bias_sum_connections / num_connections);
                        for (int f = 0; f < user_factors_gradient.dim2; f++)
                            user_factors_gradient.Inc(u, f, social_regularization * (user_factors[u, f] - sum_connections[f] / num_connections));
                    }

                    foreach (int v in user_reverse_connections[u])
                    {
                        float trust_v = (float) 1 / user_connections[v].Count;
                        float neg_trust_times_reg = -social_regularization * trust_v;

                        float bias_diff = 0;
                        var factor_diffs = new float[NumFactors];
                        foreach (int w in user_connections[v])
                        {
                            bias_diff -= user_bias[w];
                            for (int f = 0; f < factor_diffs.Length; f++)
                                factor_diffs[f] -= user_factors[w, f];
                        }

                        bias_diff *= trust_v; // normalize
                        bias_diff += user_bias[v];
                        user_bias_gradient[u] += neg_trust_times_reg * bias_diff;

                        for (int f = 0; f < factor_diffs.Length; f++)
                        {
                            factor_diffs[f] *= trust_v; // normalize
                            factor_diffs[f] += user_factors[v, f];
                            user_factors_gradient.Inc(u, f, neg_trust_times_reg * factor_diffs[f]);
                        }
                    }
                }

            // II. apply gradient descent step
            for (int user_id = 0; user_id < user_factors_gradient.dim1; user_id++)
                user_bias[user_id] -= user_bias_gradient[user_id] * LearnRate * BiasLearnRate;
            for (int item_id = 0; item_id < item_factors_gradient.dim1; item_id++)
                item_bias[item_id] -= item_bias_gradient[item_id] * LearnRate * BiasLearnRate;
            user_factors_gradient.Multiply(-LearnRate);
            user_factors.Inc(user_factors_gradient);
            item_factors_gradient.Multiply(-LearnRate);
            item_factors.Inc(item_factors_gradient);
        }
开发者ID:Spirit-Dongdong,项目名称:MyMediaLite,代码行数:101,代码来源:SocialMF.cs

示例11: Matrix4fSetRotationFromMatrix3f

 private void Matrix4fSetRotationFromMatrix3f(ref Matrix transform, Matrix matrix)
 {
     float scale = transform.TempSVD();
     transform.FromOtherMatrix(matrix, 3, 3);
     transform.Multiply(scale, 3, 3);
 }
开发者ID:hhool,项目名称:sharpgl,代码行数:6,代码来源:ArcBall.cs

示例12: TestMultiply

 public void TestMultiply()
 {
     var matrix = new Matrix<double>(5, 5);
     double[] row = { 1, 2, 3, 4, 5 };
     for (int i = 0; i < 5; i++)
         matrix.SetRow(i, row);
     matrix.Multiply(2.5);
     double[] testrow = { 2.5, 5, 7.5, 10, 12.5 };
     Assert.AreEqual(testrow, matrix.GetRow(3));
 }
开发者ID:kinyue,项目名称:MyMediaLite,代码行数:10,代码来源:MatrixExtensionsTest.cs

示例13: weightUpdate

        /// <summary>
        /// After the forward and backward pass, the weights must be updated. This is done in this function.
        /// </summary>
        /// <param name="pat">input pattern</param>
        /// <param name="dw">delta w, from last updates. Used to update weights1 after recalculation.</param>
        /// <param name="dv">delta v, from last updates. Used to update weights2 after recalculation.</param>
        private void weightUpdate(Matrix<float> pat, Matrix<float> dw, Matrix<float> dv, Matrix<float> targets)
        {
            /*
                % weight update, MATLAB code
                dw = (dw .* alpha) - (delta_h * pat') .* (1-alpha);
                dv = (dv .* alpha) - (delta_o * hout') .* (1-alpha);
                w = w + dw .* eta .* (1 + rand(1,1)/1000)';
                v = v + dv .* eta .* (1 + rand(1,1)/1000)';

                error(epoch) = sum(sum(abs(sign(out) - targets)./2));
             */
            float alpha = 0.9f;
            float eta = 0.1f;

            dw = (dw.Multiply(alpha)).Subtract((net_deltah.Multiply(pat.Transpose())).Multiply(1 - alpha));
            dv = (dv.Multiply(alpha)).Subtract((net_deltao.Multiply(net_hout.Transpose())).Multiply(1 - alpha));

            weights1 += dw.Multiply(eta).Multiply(1 + DataManipulation.rand(1, 1)[0, 0] / 1000f);
            weights2 += dv.Multiply(eta).Multiply(1 + DataManipulation.rand(1, 1)[0, 0] / 1000f);

            Matrix<float> e1 = (MatrixSign(net_out) - targets).Multiply(0.5f).Multiply(new DenseMatrix(net_out.ColumnCount, 1, 1.0f)).Transpose().Multiply(new DenseMatrix(net_out.RowCount, 1, 1.0f));
            double e = e1[0, 0];
            e = e;
        }
开发者ID:felix11,项目名称:MSP-Workshops,代码行数:30,代码来源:MultilayerNetwork.cs

示例14: Solve


//.........这里部分代码省略.........
            float beta = 0;
            float sigma;

            // Define the temporary vectors
            // rDash_0 = r_0
            Vector rdash = new DenseVector(residuals);

            // t_-1 = 0
            Vector t = new DenseVector(residuals.Count);
            Vector t0 = new DenseVector(residuals.Count);

            // w_-1 = 0
            Vector w = new DenseVector(residuals.Count);

            // Define the remaining temporary vectors
            Vector c = new DenseVector(residuals.Count);
            Vector p = new DenseVector(residuals.Count);
            Vector s = new DenseVector(residuals.Count);
            Vector u = new DenseVector(residuals.Count);
            Vector y = new DenseVector(residuals.Count);
            Vector z = new DenseVector(residuals.Count);

            Vector temp = new DenseVector(residuals.Count);
            Vector temp2 = new DenseVector(residuals.Count);
            Vector temp3 = new DenseVector(residuals.Count);

            // for (k = 0, 1, .... )
            var iterationNumber = 0;
            while (ShouldContinue(iterationNumber, xtemp, input, residuals))
            {
                // p_k = r_k + beta_(k-1) * (p_(k-1) - u_(k-1))
                p.Subtract(u, temp);

                temp.Multiply(beta, temp2);
                residuals.Add(temp2, p);

                // Solve M b_k = p_k
                _preconditioner.Approximate(p, temp);

                // s_k = A b_k
                matrix.Multiply(temp, s);

                // alpha_k = (r*_0 * r_k) / (r*_0 * s_k)
                var alpha = rdash.DotProduct(residuals) / rdash.DotProduct(s);

                // y_k = t_(k-1) - r_k - alpha_k * w_(k-1) + alpha_k s_k
                s.Subtract(w, temp);
                t.Subtract(residuals, y);

                temp.Multiply(alpha, temp2);
                y.Add(temp2, temp3);
                temp3.CopyTo(y);

                // Store the old value of t in t0
                t.CopyTo(t0);

                // t_k = r_k - alpha_k s_k
                s.Multiply(-alpha, temp2);
                residuals.Add(temp2, t);

                // Solve M d_k = t_k
                _preconditioner.Approximate(t, temp);

                // c_k = A d_k
                matrix.Multiply(temp, c);
                var cdot = c.DotProduct(c);
开发者ID:nyurik,项目名称:mathnet-numerics,代码行数:67,代码来源:GpBiCg.cs

示例15: CalculateTrueResidual

        /// <summary>
        /// Calculates the <c>true</c> residual of the matrix equation Ax = b according to: residual = b - Ax
        /// </summary>
        /// <param name="matrix">Instance of the <see cref="Matrix"/> A.</param>
        /// <param name="residual">Residual values in <see cref="Vector"/>.</param>
        /// <param name="x">Instance of the <see cref="Vector"/> x.</param>
        /// <param name="b">Instance of the <see cref="Vector"/> b.</param>
        private static void CalculateTrueResidual(Matrix matrix, Vector residual, Vector x, Vector b)
        {
            // -Ax = residual
            matrix.Multiply(x, residual);
            residual.Multiply(-1, residual);

            // residual + b
            residual.Add(b, residual);
        }
开发者ID:nyurik,项目名称:mathnet-numerics,代码行数:16,代码来源:GpBiCg.cs


注:本文中的System.Matrix.Multiply方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。