Exemple #1
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            var vec      = GetVector(Matrix);
            var var_name = GetName(Matrix);

            if (vec != null)
            {
                if (Number <= 0)
                {
                    Number = 10;
                }
                if (Number >= 100)
                {
                    Number = 100;
                }
                var vec_name = "Histogram of " + GetName(Matrix);
                cancelProgressHandler.Progress("Package_Tool", 10, "Calculating...");
                var   dou_vec = MatrixOperation.ToDouble(vec);
                var   hist    = new Histogram(dou_vec, Number);
                int   nhist   = hist.BucketCount;
                int[] xx      = new int[nhist];
                int[] yy      = new int[nhist];
                for (int i = 0; i < nhist; i++)
                {
                    xx[i] = (int)((hist[i].LowerBound + hist[i].UpperBound) * 0.5);
                    yy[i] = (int)hist[i].Count;
                }
                WorkspaceView.Plot <int>(xx, yy, vec_name, Models.UI.MySeriesChartType.Column);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #2
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            var vec = GetVector(Matrix);

            if (vec != null)
            {
                var    dou_vec  = MatrixOperation.ToDouble(vec);
                string vec_name = "CDF of " + GetName(Matrix);
                int    nlen     = dou_vec.Length;
                cancelProgressHandler.Progress("Package_Tool", 10, "Calculating...");
                Array.Sort <double>(dou_vec);
                var cdf       = MathNet.Numerics.Statistics.Statistics.EmpiricalCDFFunc(dou_vec);
                var cdf_value = new double[nlen];
                for (int i = 0; i < nlen; i++)
                {
                    cdf_value[i] = cdf(dou_vec[i]);
                }
                WorkspaceView.Plot <double>(dou_vec, cdf_value, vec_name, Models.UI.MySeriesChartType.FastLine);
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public void ShowPreview(MatrixOperation operation)
 {
     foreach (MatrixItemUI item in itemUIs)
     {
         item.ShowPreview(operation);
     }
 }
Exemple #4
0
        /// <summary>
        /// Summation matrix
        /// </summary>
        /// <param name="matrix1"></param>
        /// <param name="matrix2"></param>
        /// <param name="operation"></param>
        /// <returns></returns>
        public static int[,] SummationMatrix(int[,] matrix1, int[,] matrix2, MatrixOperation operation)
        {
            var n1 = matrix1.GetLength(0);
            var m1 = matrix1.GetLength(1);

            var n2 = matrix2.GetLength(0);
            var m2 = matrix2.GetLength(1);

            if (n1 != n2 || m1 != m2)
            {
                throw new Exception("Количество строк и столбцов в матрицах должно совпадать");
            }

            var resMatrix = new int[n1, m1];

            for (int i = 0; i < n1; i++)
            {
                for (int j = 0; j < m1; j++)
                {
                    if (operation == MatrixOperation.Plus)
                    {
                        resMatrix[i, j] = matrix1[i, j] + matrix2[i, j];
                    }
                    else
                    {
                        resMatrix[i, j] = matrix1[i, j] - matrix2[i, j];
                    }
                }
            }
            return(resMatrix);
        }
        /// <summary>
        /// Invert inplace - WARNING, use Solve for higher performance
        /// </summary>
        /// <param name="operation"></param>
        public void Invert(MatrixOperation operation = MatrixOperation.None)
        {
            Debug.Assert(nRows == nCols);
            Debug.Assert(Buffer.pointer != 0);

            CuBlasApi.Invert(_buffer, operation);
        }
        /// <summary>
        /// Solve A * X = B, B is overwritten
        /// </summary>
        /// <param name="rhs"></param>
        /// <param name="lhsOperation"></param>
        public void Solve(ColumnWiseMatrix rhs, MatrixOperation lhsOperation = MatrixOperation.None)
        {
            Debug.Assert(nRows == nCols);
            Debug.Assert(Buffer.pointer != 0);

            CuBlasApi.Solve(_buffer, rhs._buffer, lhsOperation);
        }
        /// <summary>
        /// Same version as above, but gives the possibility of reusing the output buffer
        /// </summary>
        /// <param name="output"></param>
        /// <param name="rhs"></param>
        /// <param name="lhsOperation"></param>
        /// <param name="rhsOperation"></param>
        /// <param name="alpha"></param>
        public void Multiply(ColumnWiseMatrix output, ColumnWiseMatrix rhs, MatrixOperation lhsOperation = MatrixOperation.None, MatrixOperation rhsOperation = MatrixOperation.None, double alpha = 1.0, double beta = 0.0)
        {
            if (lhsOperation == MatrixOperation.None)
            {
                Debug.Assert(rhs.nRows == nCols);
                Debug.Assert(output.nRows == nRows);
                Debug.Assert(output.nCols == rhs.nCols);
            }
            else
            {
                Debug.Assert(rhs.nCols == nCols);
                Debug.Assert(output.nCols == nRows);
                Debug.Assert(output.nRows == rhs.nCols);
            }

            Debug.Assert(memorySpace == rhs.memorySpace);
            Debug.Assert(memorySpace == output.memorySpace);
            Debug.Assert(mathDomain == rhs.mathDomain);
            Debug.Assert(mathDomain == output.mathDomain);
            Debug.Assert(Buffer.pointer != 0);
            Debug.Assert(rhs.Buffer.pointer != 0);
            Debug.Assert(output.Buffer.pointer != 0);

            CuBlasApi.Multiply(output._buffer, _buffer, rhs._buffer, nRows, rhs.nRows, lhsOperation, rhsOperation, alpha, beta);
        }
        public void Sum_OperationPlusIsNotDefinedForType_TrowArgumentException()
        {
            SquareMatrix <object> matr  = new SquareMatrix <object>(2, new object[] { new object() });
            SquareMatrix <object> smatr = new SquareMatrix <object>(2, new object[] { new object() });

            Assert.Throws <ArgumentException>(() => MatrixOperation.Sum <object>(matr, smatr));
        }
Exemple #9
0
        private static MatrixPart Operation(MatrixPart first, MatrixPart second, MatrixOperation operation)
        {
            if (second == null || first == null)
            {
                throw new ArgumentException("Matrix shouldn't be null");
            }
            var mSizeFirst  = MathExtensions.GetMatrixSize(first.Data);
            var mSizeSecond = MathExtensions.GetMatrixSize(second.Data);

            if (mSizeFirst.height != mSizeFirst.width || mSizeFirst.height != mSizeSecond.height || mSizeSecond.height != mSizeSecond.width)
            {
                throw new ArgumentException("Matrix should be square");
            }
            var result = new int[mSizeFirst.height, mSizeFirst.height];

            for (var i = 0; i < mSizeFirst.height; i++)
            {
                for (var j = 0; j < mSizeFirst.height; j++)
                {
                    if (operation == MatrixOperation.Summation)
                    {
                        result[i, j] = first.Data[i, j] + second.Data[i, j];
                    }
                    else
                    {
                        result[i, j] = first.Data[i, j] - second.Data[i, j];
                    }
                }
            }
            return(new MatrixPart(result));
        }
        public void Sum_MatricesHaveDifferentSizes_TrowArgumentException()
        {
            SquareMatrix <int> matr  = new SquareMatrix <int>(3, new int[] { 1 });
            SquareMatrix <int> smatr = new SquareMatrix <int>(2, new int[] { 2 });

            Assert.Throws <ArgumentException>(() => MatrixOperation.Sum <int>(matr, smatr));
        }
        /// <summary>
        /// y = alpha * A * x + beta * y
        /// </summary>
        /// <param name="rhs"></param>
        /// <param name="lhsOperation"></param>
        /// <param name="alpha"></param>
        /// <param name="beta"></param>
        /// <returns></returns>
        public Vector Dot(Vector rhs, MatrixOperation lhsOperation = MatrixOperation.None, double alpha = 1.0, double beta = 0.0)
        {
            Vector ret = new Vector(rhs.Size, memorySpace, rhs.mathDomain);

            Dot(ret, rhs, lhsOperation, alpha, beta);

            return(ret);
        }
Exemple #12
0
        public Vector Dot(Vector rhs, MatrixOperation lhsOperation, double alpha)
        {
            Vector ret = new Vector(rhs.Size, memorySpace, rhs.mathDomain);

            Dot(ret, rhs, lhsOperation, alpha);

            return(ret);
        }
Exemple #13
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="start"></param>
 /// <param name="end"></param>
 /// <param name="step"></param>
 /// <param name="matrixOperation"></param>
 private static void RunBenchmark(int start, int end, int step, MatrixOperation matrixOperation)
 {
     for (var i = start; i <= end; i += step)
     {
         Console.WriteLine("[" + i + "x" + i + "]");
         matrixOperation(i, i);
     }
 }
Exemple #14
0
        public ColumnWiseMatrix Multiply(ColumnWiseMatrix rhs, MatrixOperation lhsOperation = MatrixOperation.None, MatrixOperation rhsOperation = MatrixOperation.None, double alpha = 1.0)
        {
            ColumnWiseMatrix ret = new ColumnWiseMatrix(nRows, rhs.nCols, memorySpace, rhs.mathDomain);

            Multiply(ret, rhs, lhsOperation, rhsOperation, alpha);

            return(ret);
        }
Exemple #15
0
 // Operate on the matrix based on the matrix operation passed in
 public Matrix Operate(MatrixOperation operation)
 {
     return(operation.type switch
     {
         MatrixOperation.Type.Swap => SwapRows(operation.sourceRow, operation.destinationRow),
         MatrixOperation.Type.Scale => ScaleRow(operation.destinationRow, operation.scalar),
         MatrixOperation.Type.Add => AddRowToRow(operation.sourceRow, operation.scalar, operation.destinationRow),
         _ => this,
     });
Exemple #16
0
        public static void AddEqualMatrix(MemoryTile A, MemoryTile B, MatrixOperation aOperation, MatrixOperation bOperation, double alpha, double beta)
        {
            int err = _AddEqualMatrixRaw(A.pointer, B.pointer, A.nRows, A.nCols, A.memorySpace, A.mathDomain, aOperation, bOperation, alpha, beta);

            if (err != 0)
            {
                Exceptions.CuBlasKernelExceptionFactory.ThrowException("_AddEqualMatrixRaw", err);
            }
        }
Exemple #17
0
        public static void Invert(MemoryTile A, MatrixOperation aOperation)
        {
            int err = _InvertRaw(A.pointer, A.nRows, A.nCols, A.memorySpace, A.mathDomain, aOperation);

            if (err != 0)
            {
                Exceptions.CuBlasKernelExceptionFactory.ThrowException("_InvertRaw", err);
            }
        }
Exemple #18
0
        public static void Dot(MemoryBuffer y, MemoryTile A, MemoryBuffer x, MatrixOperation aOperation, double alpha, double beta)
        {
            int err = _DotRaw(y.pointer, A.pointer, x.pointer, A.nRows, A.nCols, A.memorySpace, A.mathDomain, aOperation, alpha, beta);

            if (err != 0)
            {
                Exceptions.CuBlasKernelExceptionFactory.ThrowException("_DotRaw", err);
            }
        }
Exemple #19
0
        public static double Cofactor(IMatrix a, MatrixOperation operationType, int r, int c)
        {
            double minorDeterminant = Determinant(Minor(a, r, c), operationType);

            if (((r + c) & 1) == 1)
            {
                return(-minorDeterminant);
            }
            else
            {
                return(minorDeterminant);
            }
        }
Exemple #20
0
        public static IMatrix Invert(IMatrix a, MatrixOperation operationType)
        {
            switch (operationType)
            {
            case MatrixOperation.Cofactor:
                return(InvertCofactor(a));

            case MatrixOperation.Gauss:
                return(InvertGauss(a));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #21
0
    public void Setup(int rowIndex)
    {
        base.Start();

        // Setup the operation source to request a row add operation
        operationSource.Setup(() => MatrixOperation.RowAdd(rowIndex, -1, Scalar));

        // Update display
        UpdateDisplay();

        // Listen for operation start-finish events on the matrix
        MatrixParent.OnOperationStart.AddListener(OnMatrixOperationStarted);
        MatrixParent.OnOperationFinish.AddListener(OnMatrixOperationFinished);
    }
Exemple #22
0
        /// <summary>
        /// Same version as above, but gives the possibility of reusing the output buffer
        /// </summary>
        /// <param name="output"></param>
        /// <param name="rhs"></param>
        /// <param name="lhsOperation"></param>
        /// <param name="alpha"></param>
        public void Dot(Vector output, Vector rhs, MatrixOperation lhsOperation = MatrixOperation.None, double alpha = 1.0)
        {
            Debug.Assert(rhs.Size == nCols);
            Debug.Assert(output.Size == rhs.Size);
            Debug.Assert(memorySpace == rhs.memorySpace);
            Debug.Assert(memorySpace == output.memorySpace);
            Debug.Assert(mathDomain == rhs.mathDomain);
            Debug.Assert(mathDomain == output.mathDomain);
            Debug.Assert(Buffer.pointer != 0);
            Debug.Assert(rhs.Buffer.pointer != 0);
            Debug.Assert(output.Buffer.pointer != 0);

            CuSparseApi.SparseDot(output.Buffer, _buffer, rhs.Buffer, lhsOperation, alpha);
        }
Exemple #23
0
        /// <summary>
        /// Same version as above, but gives the possibility of reusing the output buffer
        /// </summary>
        /// <param name="output"></param>
        /// <param name="rhs"></param>
        /// <param name="lhsOperation"></param>
        /// <param name="rhsOperation"></param>
        /// <param name="alpha"></param>
        public void Multiply(ColumnWiseMatrix output, ColumnWiseMatrix rhs, MatrixOperation lhsOperation = MatrixOperation.None, MatrixOperation rhsOperation = MatrixOperation.None, double alpha = 1.0)
        {
            Debug.Assert(rhs.nRows == nCols);
            Debug.Assert(output.nRows == nRows);
            Debug.Assert(output.nCols == rhs.nCols);
            Debug.Assert(memorySpace == rhs.memorySpace);
            Debug.Assert(memorySpace == output.memorySpace);
            Debug.Assert(mathDomain == rhs.mathDomain);
            Debug.Assert(mathDomain == output.mathDomain);
            Debug.Assert(Buffer.pointer != 0);
            Debug.Assert(rhs.Buffer.pointer != 0);
            Debug.Assert(output.Buffer.pointer != 0);

            CuSparseApi.SparseMultiply(output.Buffer as MemoryTile, _buffer, rhs.Buffer as MemoryTile, nRows, rhs.nRows, lhsOperation, rhsOperation, alpha);
        }
    public void Setup(int rowIndex)
    {
        Start();

        this.rowIndex = rowIndex;
        label.text    = (rowIndex + 1).ToString();

        // Listen for start and end of an operation
        MatrixParent.OnOperationStart.AddListener(OnMatrixOperationStart);
        MatrixParent.OnOperationFinish.AddListener(OnMatrixOperationFinish);

        // Initialize the lsit of item uis
        itemUIs = new MatrixItemUI[MatrixParent.CurrentMatrix.cols];

        for (int j = 0; j < MatrixParent.CurrentMatrix.cols; j++)
        {
            MatrixItemUI itemUI = Instantiate(itemUIPrefab, rowRectTransform);
            itemUI.Setup(this, j);
            itemUIs[j] = itemUI;
        }

        // Setup the operation source to request a row swap when dragged
        rowOperationSource.Setup(() => MatrixOperation.RowSwap(-1, rowIndex));
        rowAddWidget.Setup(rowIndex);

        // Setup the entry from on pointer enter
        EventTrigger.Entry pointerEnter = new EventTrigger.Entry
        {
            eventID  = EventTriggerType.PointerEnter,
            callback = new EventTrigger.TriggerEvent()
        };
        pointerEnter.callback.AddListener(data => OnPointerEnter());
        mouseDetector.triggers.Add(pointerEnter);

        // Setup the entry from on pointer exit
        EventTrigger.Entry pointerExit = new EventTrigger.Entry
        {
            eventID  = EventTriggerType.PointerExit,
            callback = new EventTrigger.TriggerEvent()
        };
        pointerExit.callback.AddListener(data => OnPointerExit());
        mouseDetector.triggers.Add(pointerExit);

        // Set non layout objects to last sibling
        nonLayoutObjects.SetAsLastSibling();
    }
Exemple #25
0
        public Func <Matrix, Matrix, Matrix> Resolve(MatrixOperation operationEnum)
        {
            switch (operationEnum)
            {
            case MatrixOperation.multiply:
                return((a, b) => a * b);

            case MatrixOperation.add:
                return((a, b) => a + b);

            case MatrixOperation.subtract:
                return((a, b) => a - b);

            default:
                throw new ArgumentOutOfRangeException(nameof(operationEnum), operationEnum, null);
            }
        }
Exemple #26
0
        static void Main(string[] args)
        {
            Launcher launcher = new Launcher();

            MatrixOparat.MatrixOperation matrix = new MatrixOperation();

            launcher.ReadFile();
            launcher.InitMatrix();
            if (launcher.ValidateMatrixMultiplication())
            {
                double[,] res = matrix.Multiplication(launcher.matrixA.GetMatrix(), launcher.matrixB.GetMatrix());
                Matrix.Print(res);
            }


            Console.ReadLine();
        }
Exemple #27
0
        private void Solve(MatrixOperation operation, Matrix <T> m1, Matrix <T> m2, Matrix <T> m3, Matrix <T> result)
        {
            var trials = getTrials(m1.N, _numTrials);

            var firstResult = new Matrix <T>(m1.M, m2.N);

            var tasks = trials
                        .Select(trial => new TaskInfo(m1, m2, trial, firstResult))
                        .Select(taskInfo => new Task <Trials>(() => operation(taskInfo)))
                        .ToList();

            var continuation = tasks.Select((task) =>
                                            task.ContinueWith((t) => operation(new TaskInfo(firstResult, m3, t.Result, result)))).ToList();

            tasks.ForEach((task) => task.Start());

            Task.WaitAll(continuation.ToArray());
        }
Exemple #28
0
        static void Main(string[] args)
        {
            int n      = 10;
            var matrix = new Matrix(n, n);

            matrix.FillRandomly(0, 10);

            MatrixOperation operation = matrix.DiagonalSum;

            Console.WriteLine(operation());
            operation = matrix.AvgSum;
            Console.WriteLine(operation());
            operation = matrix.SumSaddlePoints;
            Console.WriteLine(operation());
            Console.WriteLine(matrix);

            Console.ReadKey();
        }
Exemple #29
0
        public static double Determinant(IMatrix m, MatrixOperation operationType)
        {
            if (m.Rows != m.Columns)
            {
                throw new ArgumentException("Undetermined");
            }
            switch (operationType)
            {
            case MatrixOperation.Cofactor:
                return(DeterminantWithCofactors(m));

            case MatrixOperation.Gauss:
                return(DeterminantWithGaussEliminitation(m));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #30
0
        public DataCube <float> GetTimeSeries(int segIndex, int rchIndex, int varid, DateTime start)
        {
            DataCube <float> ts = null;

            if (DataCube != null)
            {
                var        scaleFactor = ScaleFactor;
                var        index       = GetReachIndex(segIndex, rchIndex);
                var        vector      = DataCube.GetVector(varid, ":", index.ToString());
                DateTime[] dates       = new DateTime[DataCube.Size[1]];
                for (int t = 0; t < DataCube.Size[1]; t++)
                {
                    dates[t] = start.AddDays(t);
                }
                MatrixOperation.Mulitple(vector, (float)scaleFactor);
                ts = new DataCube <float>(vector, dates);
            }
            return(ts);
        }
 public float GetSlope(SectionPathLossCalcParam param, short[] altitudes, int nIndex)
 {
     int fNumber = (int) (param.MergeEdgeMaxDis / (4f * param.CalcResolution));
     if (fNumber < 1)
     {
         fNumber = 1;
     }
     fNumber = Math.Min(this.Trim(fNumber, 5, 10), nIndex);
     float[] a = new float[fNumber * 2];
     float[] y = new float[fNumber];
     int num2 = nIndex;
     for (int i = 0; i < fNumber; i++)
     {
         a[i] = num2 * param.CalcResolution;
         y[i] = altitudes[num2 - 1];
         num2--;
     }
     float[] numArray3 = new float[2];
     float[] numArray4 = new float[3];
     LinearRegression regression = new LinearRegression();
     MatrixOperation operation = new MatrixOperation();
     regression.mvfitgn(a, y, numArray3, fNumber, 2, false);
     return numArray3[1];
 }
Exemple #32
0
 private ReturnValue Recursion(float[] valueMatrix, float[] actualLossMatrix, float[] coeMatrix, int validDataNumber, int coeNumNeedAdjust, bool isAdjustClutter)
 {
     ReturnValue value2 = ReturnValue.PROPADJ_SUCCESS;
     if (coeNumNeedAdjust == 0)
     {
         return ReturnValue.PROPCOR_NOSUCESS;
     }
     if (validDataNumber < 10)
     {
         return ReturnValue.PROPCOR_LACKDATAERROR;
     }
     float[] numArray = new float[validDataNumber];
     LinearRegression regression = new LinearRegression();
     MatrixOperation operation = new MatrixOperation();
     int num = 0;
     try
     {
         num = regression.mvfitgn(valueMatrix, actualLossMatrix, coeMatrix, validDataNumber, coeNumNeedAdjust, isAdjustClutter);
     }
     catch (Exception exception)
     {
         WriteLog.Logger.Error(exception.StackTrace);
     }
     if (num != 0)
     {
         return ReturnValue.PROPCOR_NOTFIT;
     }
     return value2;
 }