Пример #1
0
    static void Main()
    {
        MultiplicationElement mul = new MultiplicationElement();
        IntegerElement a = new IntegerElement();
        IntegerElement b = new IntegerElement();
        
        a.setText("5");

        b.setText("5");
        mul.setLhs(a);
       mul.setRhs(b);
      Element c ;
      Element d ;
        c = (Element)mul.getLhs();
        d = (Element)mul.getRhs();
    } 
Пример #2
0
 public abstract void VisitMultiplicationElement(MultiplicationElement element);
Пример #3
0
        public override void VisitMultiplicationElement(MultiplicationElement element)
        {
            if (element.getRhs() == null)
            {
                Element var_name = element.getLhs();
                VisitElement(var_name);
            }
            else
            {
                if (inParallelFor == 1)
                {
                    ParallelMul(element);
                    return;
                }
                else
                {
                    VisitElement(element.getLhs());
                    VisitElement(element.getRhs());

                    if (mat_stack.Count >= 2)
                    {
                        Object obj_rhs = getTopOfStack_Matrix();
                        Object obj_lhs = getTopOfStack_Matrix();
                        int rhs_type = GetTypeOfElement((Element)obj_rhs);
                        int lhs_type = GetTypeOfElement((Element)obj_lhs);
                        if (rhs_type == 3 && lhs_type == 3) // check whether both are matrices or not
                        {

                            MatrixVariableDeclaration stk_rhs = (MatrixVariableDeclaration)(obj_rhs);
                            MatrixVariableDeclaration stk_lhs = (MatrixVariableDeclaration)(obj_lhs);
                            MatrixVariableDeclaration final = new MatrixVariableDeclaration();
                            //Object output = new Object();
                            if (stk_lhs.getType() == stk_rhs.getType())
                            {
                                IntegerElement lRow = (IntegerElement)(stk_lhs.getRow());
                                IntegerElement lCol = (IntegerElement)(stk_lhs.getColumn());
                                IntegerElement rRow = (IntegerElement)(stk_rhs.getRow());
                                IntegerElement rCol = (IntegerElement)(stk_rhs.getColumn());
                                int lhs_row = int.Parse(((IntegerElement)(stk_lhs.getRow())).getText());
                                int lhs_col = int.Parse(((IntegerElement)(stk_lhs.getColumn())).getText());
                                int rhs_row = int.Parse(((IntegerElement)(stk_rhs.getRow())).getText());
                                int rhs_col = int.Parse(((IntegerElement)(stk_rhs.getColumn())).getText());
                                if (lhs_col == rhs_row)
                                {
                                    final.setRow(lRow);
                                    final.setColumn(rCol);
                                    final.setType(stk_lhs.getType());
                                    Console.Write("Multiplication..\n");
                                    string mat_type = stk_lhs.getType();
                                    if (mat_type == "int")
                                    {
                                        int[,] lhs_elem = stk_lhs.getintValue();
                                        int[,] rhs_elem = stk_rhs.getintValue();
                                        int[,] result = new int[lhs_row, rhs_col];
                                        for (int i = 0; i < lhs_row; i++)
                                        {
                                            for (int j = 0; j < rhs_col; j++)
                                            {
                                                for (int k = 0; k < lhs_col; k++)
                                                {
                                                    int res = lhs_elem[i, k] * rhs_elem[k, j];
                                                    result[i, j] = result[i, j] + res;
                                                }
                                            }
                                        }
                                        bool mat_set = final.setIntMatrix(result);
                                        int[,] output = final.getintValue();
                                        int row = int.Parse(((IntegerElement)(final.getRow())).getText());
                                        int col = int.Parse(((IntegerElement)(final.getColumn())).getText());
                                        for (int i = 0; i < row; i++)
                                        {
                                            for (int j = 0; j < col; j++)
                                            {
                                                Console.Write("\t" + output[i, j]);
                                            }
                                            Console.Write("\n");
                                        }
                                        Object final_output = (Object)(final);
                                        mat_stack.Push(final_output);
                                    }
                                    else if (mat_type == "double")
                                    {
                                        double[,] lhs_elem = stk_lhs.getdoubleValue();
                                        double[,] rhs_elem = stk_rhs.getdoubleValue();
                                        double[,] result = new double[lhs_row, rhs_col];
                                        for (int i = 0; i < lhs_row; i++)
                                        {
                                            for (int j = 0; j < rhs_col; j++)
                                            {
                                                for (int k = 0; k < lhs_col; k++)
                                                {
                                                    double res = lhs_elem[i, k] * rhs_elem[k, j];
                                                    result[i, j] = result[i, j] + res;
                                                }
                                            }
                                        }
                                        bool mat_set = final.setDoubleMatrix(result);
                                        double[,] output = final.getdoubleValue();
                                        int row = int.Parse(((IntegerElement)(final.getRow())).getText());
                                        int col = int.Parse(((IntegerElement)(final.getColumn())).getText());
                                        for (int i = 0; i < row; i++)
                                        {
                                            for (int j = 0; j < col; j++)
                                            {
                                                Console.Write("\t" + output[i, j]);
                                            }
                                            Console.Write("\n");
                                        }
                                        Object final_output = (Object)(final);
                                        mat_stack.Push(final_output);
                                    }

                                }
                                else
                                {
                                    Console.Write("Matrix dimensions does not match for multiplication.. try again.. \n");
                                    sendres(112, "Matrix dimensions does not match for multiplication.. try again.. \n");
                                }
                            }
                            else
                            {
                                Console.Write("Matrix types are different.. try again.. ");
                                sendres(112, "Matrix types are different.. try again..\n");
                            }
                        }
                        else if (lhs_type == (int)datatypes.DoubleElement && rhs_type == (int)datatypes.DoubleElement)
                            PerformDoubleMultiplication(obj_rhs, obj_lhs);
                        else if (lhs_type == (int)datatypes.IntElement && rhs_type == (int)datatypes.IntElement)
                            PerformIntMultiplication(obj_rhs, obj_lhs);
                        else
                        {
                            Console.Write("Scalar and Matrix cannot be multiplied.. \n");
                            sendres(112, "Scalar and Matrix cannot be multiplied.. ..\n");
                        }
                    }

                }
            }
            //throw new NotImplementedException();
        }
Пример #4
0
        private void ParallelMul(MultiplicationElement element)
        {
            VisitElement(element.getLhs());
            parallelString.Append("*");
            VisitElement(element.getRhs());

        }
Пример #5
0
 public override void VisitMultiplicationElement(MultiplicationElement element)
 {
 }