/**
         * Create the function.  Be sure to handle all possible input types and combinations correctly and provide
         * meaningful error messages.  The output matrix should be resized to fit the inputs.
         */
        public static ManagerFunctions.InputN createMultTransA()
        {
            return(new ManagerFunctions.InputN((l, m) =>
            {
                var inputs = l;
                var manager = m;

                if (inputs.Count != 2)
                {
                    throw new InvalidOperationException("Two inputs required");
                }

                Variable varA = inputs[0];
                Variable varB = inputs[1];

                Operation.Info ret = new Operation.Info();

                if (varA is VariableMatrix && varB is VariableMatrix)
                {
                    // The output matrix or scalar variable must be created with the provided manager
                    VariableMatrix output = manager.createMatrix();
                    ret.output = output;
                    ret.op = new Operation("multTransA-mm")
                    {
                        process = () =>
                        {
                            DMatrixRMaj mA = ((VariableMatrix)varA).matrix;
                            DMatrixRMaj mB = ((VariableMatrix)varB).matrix;
                            output.matrix.reshape(mA.numCols, mB.numCols);

                            CommonOps_DDRM.multTransA(mA, mB, output.matrix);
                        }
                    };
                }
                else
                {
                    throw new ArgumentException("Expected both inputs to be a matrix");
                }

                return ret;
            }));
        }
Beispiel #2
0
        /// <summary>
        ///     Solves this <see cref="LinearEquationSystem" /> using the Gauss-Jordan algorithm.
        /// </summary>
        /// <returns>The result of each variable in chronological order.</returns>
        public double[] Solve()
        {
            uint count = (uint)Equations.Count;

            if (count == 0)
            {
                throw new ArgumentException("There must be at least one equation to solve.");
            }

            var coefficientsCount = Equations.Select(item => item.Coefficients).Count();

            if (coefficientsCount == 0 || coefficientsCount != count)
            {
                throw new EquationNotSolvableException("This linear equation system cannot be solved.");
            }

            var leftSide  = new VariableMatrix(count, count);
            var rightSide = new VariableMatrix(count, 1);

            for (uint i = 0; i < count; ++i)
            {
                var      currentEquation = Equations[(int)i];
                double[] coefficients    = currentEquation.Coefficients;
                for (uint c = 0; c < coefficients.Length; ++c)
                {
                    leftSide[i, c] = coefficients[c];
                }
                rightSide[i, 0] = currentEquation.Result;
            }

            var resultMatrix = MatrixUtils.GaussJordan(leftSide, rightSide);

            double[] result = new double[rightSide.RowCount];
            for (uint i = 0; i < resultMatrix.RowCount; ++i)
            {
                result[i] = resultMatrix[i, 0];
            }

            return(result);
        }
Beispiel #3
0
        private void DoMaths()
        {
            MatrixRequest?.Invoke(); //updates matrix
            double t, toX, step;
            IEnumerable <double> points;

            double[,] ode;
            DiffMethod method;

            try
            {
                t      = DrawingInterval.From;
                toX    = DrawingInterval.To;
                step   = IntegrationStep;
                method = CurrentlySelectedMethod;
                points = VariableMatrix.Cast <double>();
                ode    = ConstMatrix;
            }
            catch (Exception e)
            {
                MessageBox.Show(Locale["#ParsingErrMsg"] + e.Message, Locale["#ParsingErr"], MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return;
            }

            var dynFun = new List <DynamicDiffFun>
            {
                (dt, vars) => ode[0, 0] * vars.x + ode[0, 1] * vars.y + ode[0, 2] * vars.z + ode[0, 3] * vars.w + ode[0, 4],
                (dt, vars) => ode[1, 0] * vars.x + ode[1, 1] * vars.y + ode[1, 2] * vars.z + ode[1, 3] * vars.w + ode[1, 4],
                (dt, vars) => ode[2, 0] * vars.x + ode[2, 1] * vars.y + ode[2, 2] * vars.z + ode[2, 3] * vars.w + ode[2, 4],
                (dt, vars) => ode[3, 0] * vars.x + ode[3, 1] * vars.y + ode[3, 2] * vars.z + ode[3, 3] * vars.w + ode[3, 4]
            };

            var coeffFunctions = new List <string> {
                "x", "y", "z", "w"
            };

            //Test case:
            // *Produces beautiful e^ 2x
            //*
            //*
            // var dynFun = new List<DifferentialService.DynamicDiffFun>
            //{
            //     (dt, variables) => -2*variables.x+4*variables.y,
            //     (dt, variables) => 3*variables.x-variables.y
            //};
            // var coeffFunctions = new List<string> { "x", "y", };
            // var points = new double[] { 1, 1 };

            RungeKuttaMethod rungeKuttaMethod = Rk4;

            switch (method)
            {
            case DiffMethod.RK4:
                rungeKuttaMethod = Rk4;
                break;

            case DiffMethod.Ralston:
                rungeKuttaMethod = Ralston;
                break;
            }

            var cmp     = new List <DataPoint>();
            var results = coeffFunctions.Select(coeffFunction => new List <DataPoint>()).ToList();

            while (t < toX)
            {
                //kutta
                var result = rungeKuttaMethod(t, points, step, dynFun, coeffFunctions);

                var i = 0;
                foreach (var d in result)
                {
                    results[i++].Add(new DataPoint(t, d));
                }

                if (t + step > toX)
                {
                    step = toX - t;
                }
                points = result.ToArray();

                //draw compare
                if (CurrentlySelectedFunction != null)
                {
                    cmp.Add(new DataPoint(t, CurrentlySelectedFunction.GetValue(t)));
                }
                t += step;
            }

            //FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN
            //for (int i = 0; i < results.Count; i++)
            //{
            //    results[0][i] = new DataPoint(results[1][i].Y,results[0][i].Y);
            //}
            //FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN FUN

            CoeffFunctionsAvaibleToDraw =
                coeffFunctions.Select((coeff, i) => new Tuple <string, List <DataPoint> >(coeff, results[i])).ToList();
            ComparisionDataPoints      = cmp;
            SelectedCoeffFunctionIndex = 0;
        }