Esempio n. 1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            var z = new StringMatrix();

            z.M[0, 0] = "Cz";
            z.M[0, 1] = "Sz";
            z.M[1, 0] = "-Sz";
            z.M[1, 1] = "Cz";

            var x = new StringMatrix();

            x.M[1, 1] = "Cx";
            x.M[1, 2] = "Sx";
            x.M[2, 1] = "-Sx";
            x.M[2, 2] = "Cx";

            var zx = z * x;

            var y = new StringMatrix();

            y.M[0, 0] = "Cy";
            y.M[0, 2] = "-Sy";
            y.M[2, 0] = "Sy";
            y.M[2, 2] = "Cy";

            var zxy = zx * y;

            Console.WriteLine(zxy);
        }
Esempio n. 2
0
        public static StringMatrix operator *(StringMatrix m1, StringMatrix m2)
        {
            var m = new StringMatrix();

            for (var y = 0; y < 4; y++)
            {
                for (var x = 0; x < 4; x++)
                {
                    var str = "";
                    for (var i = 0; i < 4; i++)
                    {
                        var s1 = m1.M[y, i];
                        var s2 = m2.M[i, x];

                        if (s1 == "" || s2 == "")
                        {
                            continue;
                        }
                        if (s1 == "1" && s2 == "1")
                        {
                            str += "1";
                        }
                        else if (s1 == "1" && s2 != "1")
                        {
                            if (s2[0] == '-')
                            {
                                str += s2;
                            }
                            else if (str.Length > 0)
                            {
                                str += "+" + s2;
                            }
                            else
                            {
                                str += s2;
                            }
                        }
                        else if (s1 != "1" && s2 == "1")
                        {
                            if (s1[0] == '-')
                            {
                                str += s1;
                            }
                            else if (str.Length > 0)
                            {
                                str += "+" + s1;
                            }
                            else
                            {
                                str += s1;
                            }
                        }
                        else
                        {
                            str += string.Format("({0}*{1})", s1, s2);
                        }
                    }
                    m.M[y, x] = str;
                }
            }

            return(m);
        }