Beispiel #1
0
 public abstract void VisitMatrixElement(MatrixVariableDeclaration element);
Beispiel #2
0
 public override void VisitMatrixElement(MatrixVariableDeclaration element)
 {
     Console.Write("Matrix<" + element.getType()+">[");
     VisitElement(element.getRow());
     Console.Write("][");
     VisitElement(element.getColumn());
     Console.Write("] ");
     VisitElement(element.getVar());
     Console.Write(" = [");
     List<Element> elem = element.getList();
     for (int i = 0; i < elem.Count; i++)
     {
         VisitElement(elem[i]);
         if (elem.Count > 1 && i!= (elem.Count-1))
         {
             Console.Write(",");
         }
     }
     Console.Write("];\n");
 }
Beispiel #3
0
        public override void VisitMatrixElement(MatrixVariableDeclaration element)
        {
            string variable_name = element.getVar().getText();
            string type = element.getType();
            if (mVariableMap.Count == 0)
                mVariableMap.Add(variable_name, element);
            else
            {
                if (mVariableMap.Contains(variable_name))
                {
                    Console.Write(" \nSemantic Error.. ");
                    sendres(112, "\nSemantic Error...\n");
                    Console.Write("\n The matrix name you entered is already existing.. try again..");
                    sendres(112, "\n The matrix name you entered is already existing.. try again..");
                    return;
                }
                else
                {
                    mVariableMap.Add(variable_name, element);
                }
            }
            /*int row = int.Parse(element.getRow().getText());
            int col = int.Parse(element.getColumn().getText());
            Console.Write("\n Matrix name : ");
            Console.Write(variable_name);
            Console.Write("\nMatrix Type : ");
            Console.Write(type); Console.Write("\n");
            Console.Write(" Rows : "); Console.Write(row); Console.Write("\n");
            Console.Write(" Columns : "); Console.Write(col);
            string mat_type = element.getType();
            if (mat_type == "int")
            {
                int[,] elements = element.getintValue();
                Console.Write("\nMatrix Elements are : \n");
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        Console.Write(elements[i, j]);
                        Console.Write("\t");
                    }
                    Console.Write("\n");
                }
            }
            else if (mat_type == "double")
            {
                double[,] elemenets = element.getdoubleValue();
                if (elemenets != null)
                {
                    Console.Write("\n Matrix Elements are : \n");
                    for (int i = 0; i < row; i++)
                    {
                        for (int j = 0; j < col; j++)
                        {
                            Console.Write(elemenets[i, j]);
                            Console.Write("\t");
                        }
                        Console.Write("\n");
                    }
                }
            }
            Console.Write("\n");*/

            //throw new NotImplementedException();
        }
Beispiel #4
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();
        }
Beispiel #5
0
        public override void VisitMatrixTransposeElement(MatrixTranspose element)
        {
            if (element.getvariable() != null)
            {
                VisitElement(element.getvariable());
                Object obj_rhs = getTopOfStack_Matrix();
                //Object result = new Object();
                MatrixVariableDeclaration mat = (MatrixVariableDeclaration)(obj_rhs);
                MatrixVariableDeclaration transpose = new MatrixVariableDeclaration();
                transpose.setType(mat.getType());
                int rhs_type = GetTypeOfElement((Element)obj_rhs);
                if (rhs_type == 3)
                {
                    if (mat.getType() == "int")
                    {
                        int[,] mat_elem = mat.getintValue();

                        int row = int.Parse(((IntegerElement)(mat.getRow())).getText());
                        int col = int.Parse(((IntegerElement)(mat.getColumn())).getText());
                        int[,] trans_elem = new int[col, row];
                        transpose.setRow(mat.getColumn());
                        transpose.setColumn(mat.getRow());
                        for (int i = 0; i < row; i++)
                        {
                            for (int j = 0; j < col; j++)
                                trans_elem[j, i] = mat_elem[i, j];
                        }
                        Console.Write("\n");
                        transpose.setIntMatrix(trans_elem);
                        int[,] trans = transpose.getintValue();
                        int trans_row = int.Parse(((IntegerElement)(transpose.getRow())).getText());
                        int trans_col = int.Parse(((IntegerElement)(transpose.getColumn())).getText());
                        Console.Write("Transpose of the given matrix is : \n\n");
                        //result("Transpose of the matrix\n");

                        for (int i = 0; i < trans_row; i++)
                        {
                            for (int j = 0; j < trans_col; j++)
                            {
                                Console.Write("\t" + trans[i, j]);
                                //      result("\t" + trans[i, j].ToString());
                            }
                            Console.Write("\n");
                            //result("\n");

                        }
                    }
                    else if (mat.getType() == "double")
                    {
                        double[,] mat_elem = mat.getdoubleValue();

                        int row = int.Parse(((IntegerElement)(mat.getRow())).getText());
                        int col = int.Parse(((IntegerElement)(mat.getColumn())).getText());
                        double[,] trans_elem = new double[col, row];
                        transpose.setRow(mat.getColumn());
                        transpose.setColumn(mat.getRow());
                        for (int i = 0; i < row; i++)
                        {
                            for (int j = 0; j < col; j++)
                                trans_elem[j, i] = mat_elem[i, j];
                        }
                        Console.Write("\n");
                        //result("\n");

                        transpose.setDoubleMatrix(trans_elem);
                        double[,] trans = transpose.getdoubleValue();
                        int trans_row = int.Parse(((IntegerElement)(transpose.getRow())).getText());
                        int trans_col = int.Parse(((IntegerElement)(transpose.getColumn())).getText());
                        //result( "Transpose of the given matrix\n");
                        Console.Write("Transpose of the given matrix is : \n\n");
                        for (int i = 0; i < trans_row; i++)
                        {
                            for (int j = 0; j < trans_col; j++)
                            {
                                Console.Write("\t" + trans[i, j]);
                                //      result( "\t" + trans[i, j].ToString());

                            }
                            Console.Write("\n");
                            //result("\n");
                        }
                    }
                    Object result = (Object)(transpose);
                    mat_stack.Push(result);

                }
            }
            else
            {
                Console.Write("Matrix needs to be decalred first.. try again.. ");
                sendres(112, "Matrix needs to be declared first.. try again..\n");
            }
        }
Beispiel #6
0
        public override void VisitAdditionOperationElement(AdditiveElement element)
        {

            if (element.getRhs() == null)
            {
                VisitElement(element.getLhs());
            }
            else
            {
                if (inParallelFor == 1)
                {
                    ParallelAddition(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)
                        {
                            MatrixVariableDeclaration stk_rhs = (MatrixVariableDeclaration)(obj_rhs);
                            MatrixVariableDeclaration stk_lhs = (MatrixVariableDeclaration)(obj_lhs);
                            MatrixVariableDeclaration final = new MatrixVariableDeclaration();
                            if (stk_lhs != null && stk_rhs != null)
                            {
                                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_row == rhs_row && lhs_col == rhs_col)
                                    {
                                        final.setRow(lRow);
                                        final.setColumn(lCol);
                                        final.setType(stk_lhs.getType());

                                        Console.Write("Addition..\n");
                                        Console.Write(element.getLhs().GetType());
                                        string mat_type = stk_lhs.getType();
                                        if (mat_type == "int")
                                        {
                                            int[,] rhs_elements = stk_rhs.getintValue();
                                            int[,] lhs_elements = stk_lhs.getintValue();
                                            int[,] output = new int[lhs_row, lhs_col];
                                            for (int i = 0; i < lhs_row; i++)
                                            {
                                                for (int j = 0; j < lhs_col; j++)
                                                {
                                                    output[i, j] = lhs_elements[i, j] + rhs_elements[i, j];
                                                    Console.Write(output[i, j]);
                                                    Console.Write("\t");
                                                }
                                                Console.Write("\n");
                                            }
                                            bool mat_set = final.setIntMatrix(output);
                                        }
                                        else if (mat_type == "double")
                                        {
                                            double[,] rhs_elements = stk_rhs.getdoubleValue();
                                            double[,] lhs_elements = stk_lhs.getdoubleValue();
                                            // int[,] result = new int[lhs_row, lhs_col];
                                            double[,] output = new double[lhs_row, lhs_col];
                                            for (int i = 0; i < lhs_row; i++)
                                            {
                                                for (int j = 0; j < lhs_col; j++)
                                                {
                                                    output[i, j] = lhs_elements[i, j] + rhs_elements[i, j];
                                                    Console.Write(output[i, j]);
                                                    Console.Write("\t");
                                                }
                                                Console.Write("\n");
                                            }
                                            bool mat_set = final.setDoubleMatrix(output);
                                        }
                                        Object result = (Object)(final);
                                        mat_stack.Push(result);
                                    }
                                    // addition
                                }
                                else
                                {
                                    Console.Write("Matrix dimensions does not match.. try again.. \n");
                                }
                            }
                        }
                        else if (lhs_type == 2 && rhs_type == 2)
                        {
                            //Double
                            PerformDoubleAddition(obj_rhs, obj_lhs);

                        }
                        else if (lhs_type == 1 || lhs_type==5 || rhs_type==1 || rhs_type==5 )
                        {
                            //Int
                            PerformIntAddition(obj_rhs, obj_lhs);
                        }
                        else
                        {
                            Console.Write("Scalar and matrix addition not possible\n");
                            sendres(112, "Scalar and matrix addition not possible\n");
                        }
                    }
                    else
                    {
                        Console.Write("Matrix types are different.. try again.. ");
                        sendres(112, "Matrix types are different.. try again.. ");
                    }
                }
            }
        }
Beispiel #7
0
        private void CreateData()
        {
            int curElement = 0;
            
            StringReader readStr = new StringReader(matStr.ToString());
            XmlTextReader xf = new XmlTextReader(readStr);
            MatrixVariableDeclaration mat=null;
            VectorVariableDeclaration vec = null;
            string matElem="";
            int currRow = 0;
            int index = 0;
            string type="";
            int totalRows = 0;
            int totalCols = 0;
            int currCol = -1;
            int[,] elems = new int[10, 10]; ;
            while (xf.Read())
            {
                switch (xf.NodeType)
                {
                    case XmlNodeType.Element:
                        {
                            if (xf.Name == "Matrix")
                            {
                                curElement = 1;
                                mat = new MatrixVariableDeclaration();
                                currRow = 0; currCol = -1;
                            }
                            if (xf.Name == "Vector")
                            {
                                curElement = 2;
                                vec = new VectorVariableDeclaration();
                            }
                            if (curElement == 2)
                            {
                                if (xf.Name == "name")
                                    matElem = "name";
                               
                            }
                            if (curElement == 1)
                            {
                                if (xf.Name == "name")
                                    matElem = "name";
                                if (xf.Name == "row")
                                    matElem = "row";
                                if (xf.Name == "col")
                                    matElem = "column";
                                if (xf.Name == "type")
                                    matElem = "type";
                                if (xf.Name == "Elements")
                                {
                                    elems = new int[totalRows, totalCols];
                                }
                                if (xf.Name == "Element")
                                {
                                    matElem = "elem";
                                    if (currCol == totalCols - 1 && currRow < totalRows - 1)
                                    {
                                        currRow += 1;
                                        currCol = 0;
                                    }
                                    else currCol += 1;
                                }
                            }

                            break;
                        }
                    case XmlNodeType.Text:
                        {
                            if (curElement == 1)
                            {
                                if (matElem == "row")
                                {
                                    IntegerElement row = new IntegerElement();
                                    totalRows = int.Parse(xf.Value.ToString());
                                    row.setText(xf.Value);
                                    mat.setRow(row);
                                }
                                if (matElem == "column")
                                {
                                    IntegerElement col = new IntegerElement();
                                    totalCols = int.Parse(xf.Value.ToString());
                                    col.setText(xf.Value);
                                    mat.setColumn(col);
                                }
                                if (matElem == "name")
                                {
                                    VariableElement val = new VariableElement();
                                    val.setText(xf.Value);
                                    mat.setVar(val);
                                }
                                if (matElem == "type")
                                    {
                                        type = xf.Value;
                                        mat.setType(type);
                                    }
                                if (matElem == "elem")
                                {
                                    if (type == "int")
                                    {
                                        elems[currRow, currCol] = int.Parse(xf.Value.ToString());
                                    }
                                    else if (type == "double")
                                        mat.setdoubleValueat(currRow, currCol, double.Parse(xf.Value.ToString()));
                                }
                            }
                            break;
                        }
                    case XmlNodeType.EndElement:
                        {
                            if (xf.Name == "Elements" && curElement==1)
                            {
                                mat.setIntMatrix(elems);
                                parallelMap.Add(mat.getVar().getText(), mat);
                            }
                            Console.Write("End:" + xf.Name);
                            break;
                        }
                    default:
                        {
                            Console.Write("\n");
                            break;
                        }
                }
            }
        }
Beispiel #8
0
        public override void VisitMatrixElement(MatrixVariableDeclaration element)
        {
            //string variable_name = element.getVar().getText();
            string type = element.getType();
            int row = int.Parse(element.getRow().getText());
            int col = int.Parse(element.getColumn().getText());
            Console.Write("\nMatrix Type : ");
            Console.Write(type); Console.Write("\n");
            Console.Write(" Rows : "); Console.Write(row); Console.Write("\n");
            Console.Write(" Columns : "); Console.Write(col);
            interp.sendres(114,"\nMatrix Type : " + type + "\n" + "Rows:" + row.ToString() + "\n" + "Columns:" + col.ToString());
            string mat_type = element.getType();
            if (mat_type == "int")
            {
                int[,] elements = element.getintValue();
                Console.Write("\nMatrix Elements are : \n");
                interp.sendres(114, "\nMatrix Elements are : \n");
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        Console.Write(elements[i, j]);
                        Console.Write("\t");
                        interp.sendres(114, elements[i,j].ToString() + "\t");
                    }
                    Console.Write("\n");
                    interp.sendres(114, "\n");
                }
            }
            else if (mat_type == "double")
            {
                double[,] elemenets = element.getdoubleValue();
                Console.Write("\n Matrix Elements are : \n");
                interp.sendres(114, "\nMatrix Elements are : \n");
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        Console.Write(elemenets[i, j]);
                        interp.sendres(114, elemenets[i,j].ToString());
                        Console.Write("\t");
                        interp.sendres(114, "\t");
                    }
                    Console.Write("\n");
                    interp.sendres(114, "\n");
                }
            }
            Console.Write("\n");
            interp.sendres(114, "\n");

            //throw new NotImplementedException();
        }
   //-----------<Test Stub>--------
 static void main(string[] args)
 {
     Console.WriteLine("\nTesting the MatrixVariableDeclaration class");
     Console.WriteLine("\n===========================================\n");
     MatrixVariableDeclaration elem_mat = new MatrixVariableDeclaration();
     VariableElement elem_var = new VariableElement();
     IntegerElement elem_int = new IntegerElement();
     //List<Element> elementlist = new List<Element>();
     string var = "matrix";
     string row = "3";
     string column = "2";
     elem_var.setText(var);
     elem_mat.setVar(elem_var);
     elem_int.setText(row);
     elem_mat.setRow(elem_int);
     elem_int.setText(column);
     elem_mat.setColumn(elem_int);
     for (int i = 0; i < (int.Parse(row) * int.Parse(column)); i++)
     {
         elem_int.setText(i.ToString());
         elem_mat.addValue(elem_int);
     }
     string type = "int";
     elem_mat.setType(type);
     elem_mat.setValue();
     int[,] matrix = new int[int.Parse(row), int.Parse(column)];
     matrix = elem_mat.getintValue();
     Console.WriteLine("The name of the Matrix is:{0}", elem_mat.getVar().getText());
     Console.WriteLine("The Row and Column of the Matrix is:{0},{1}", elem_mat.getRow().getText(), elem_mat.getColumn().getText());
     Console.WriteLine("The type of matrix is:{0}", elem_mat.getType());
     for(int i=0;i<int.Parse(row);i++)
     {
         for (int j = 0; j < int.Parse(column); j++)
         {
             Console.WriteLine("The value of matrix is:{0}", matrix[i, j]);
         }
     }
     
 }