Exemplo n.º 1
0
        public void AddAxisTest()
        {
            int[,] m = Vector.Range(0, 15).Reshape(3, 5);

            var actual = m.Add(5);

            Assert.IsTrue(actual.IsEqual(new int[, ]
            {
                { 5, 6, 7, 8, 9 },
                { 10, 11, 12, 13, 14 },
                { 15, 16, 17, 18, 19 },
            }));

            actual = m.Add(new[] { 10, 20, 30 }, dimension: 1);
            Assert.IsTrue(actual.IsEqual(new int[, ]
            {
                { 10, 11, 12, 13, 14 },
                { 25, 26, 27, 28, 29 },
                { 40, 41, 42, 43, 44 },
            }));

            actual = m.Add(new[] { 10, 20, 30, 40, 50 }, dimension: 0);
            Assert.IsTrue(actual.IsEqual(new int[, ]
            {
                { 10, 21, 32, 43, 54 },
                { 15, 26, 37, 48, 59 },
                { 20, 31, 42, 53, 64 },
            }));
        }
Exemplo n.º 2
0
        protected override string doExecute(IAlgorithmInput input)
        {
            // read input
            int[,] A = readInput(input);
            int[,] A_seq;
            if (input.ExecuteCompairAlgorithm)
            {
                A_seq = A.NaiveMultiplication(A);
            }
            else
            {
                A_seq = A.Multiply(A);
            }

            int[,] ResM = A.Add(A_seq);
            List <int> result = new List <int>();

            for (int i = 0; i < A.GetLength(0); i++)
            {
                bool hasX = true;
                for (int j = 0; j < A.GetLength(1); j++)
                {
                    if (i != j && ResM[i, j] == 0)
                    {
                        hasX = false;
                    }
                }
                if (hasX)
                {
                    result.Add(i);
                }
            }
            return(string.Join(",", result.ToArray()));
        }
Exemplo n.º 3
0
        public void addPlayerField(int x, int y, short player)
        {
            int       fieldsCnt;
            ArrayList fields = null;

            //get player fields
            if (player == GameVar.PLAYER_WHITE)
            {
                fields = fieldsWhite;
            }
            else if (player == GameVar.PLAYER_BLACK)
            {
                fields = fieldsBlack;
            }
            else
            {
                throw new Exception("Player not recognized");
            }

            fieldsCnt = fields.Count;

            if (fieldsCnt == piecesPerPlayer)     //adding more fields than player's max fields count
            {
                throw new Exception("filed " + x + " " + y + " for player " + player + " out of max player fields count");
            }
            fields.Add(new int[] { x, y });
        }
Exemplo n.º 4
0
        public static int[,] Multiply(this int[,] a, int[,] b)
        {
            // check if the task canceled from UI
            //if (pingpongalgorithmnew.current_token.iscancellationrequested)
            //{
            //    throw new taskcanceledexception("task canceled");
            //}
            //if (a.GetLength(0) == 2)
            //{
            //    int[,] data = new int[2, 2];
            //    data[0, 0] = a[0, 0] * b[0, 0] + a[0, 1] * b[1, 0];
            //    data[0, 1] = a[0, 0] * b[0, 1] + a[0, 1] * b[1, 1];
            //    data[1, 0] = a[1, 0] * b[0, 0] + a[1, 1] * b[1, 0];
            //    data[1, 1] = a[1, 0] * b[0, 1] + a[1, 1] * b[1, 1];
            //    return data;
            //}
            if (a.GetLength(0) < 64)
            {
                return(a.NaiveMultiplication(b));
            }

            (int[,] A11, int[,] A12, int[,] A21, int[,] A22) = a.divide();
            (int[,] B11, int[,] B12, int[,] B21, int[,] B22) = b.divide();
            //M1 = multiplication((A11 + A22), (B11 + B22))
            int[,] M1 = (A11.Add(A22)).Multiply(B11.Add(B22));
            //M2 = multiplication((A21 + A22), B11)
            int[,] M2 = (A21.Add(A22)).Multiply(B11);
            //M3 = multiplication(A11, (B12 - B22))
            int[,] M3 = A11.Multiply(B12.Subtrac(B22));
            //M4 = multiplication(A22, (B21 - B11))
            int[,] M4 = A22.Multiply(B21.Subtrac(B11));
            //M5 = multiplication((A11 + A12), B22)
            int[,] M5 = (A11.Add(A12)).Multiply(B22);
            //M6 = multiplication((A21 - A11), (B11 + B12))
            int[,] M6 = (A21.Subtrac(A11)).Multiply(B11.Add(B12));
            //M7 = multiplication((A12 - A22), (B21 + B22))
            int[,] M7 = (A12.Subtrac(A22)).Multiply(B21.Add(B22));

            //C11 = M1 + M4 - M5 + M7
            int[,] C11 = M1.Add(M4).Subtrac(M5).Add(M7);
            //C12 = M3 + M5
            int[,] C12 = M3.Add(M5);
            //C21 = M2 + M4
            int[,] C21 = M2.Add(M4);
            //C22 = M1 - M2 + M3 + M6
            int[,] C22 = M1.Subtrac(M2).Add(M3).Add(M6);
            return(combine(C11, C12, C21, C22, a.GetLength(0)));
        }