示例#1
0
        static public matp mul(matp m1, matp m2)
        {
            if (m1.n != m2.n)
            {
                Console.WriteLine("Wrong mul!");
                return(new matp(1));
            }

            int  n   = m1.n;
            matp rez = new matp(n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    for (int k = 0; k < n; k++)
                    {
                        rez.m[i, j] += (m1.m[i, k] * m2.m[k, j]);
                    }
                    if (Math.Abs(rez.m[i, j]) < eps)
                    {
                        rez.m[i, j] = 0;
                    }
                }
            }
            return(rez);
        }
示例#2
0
        static public double[] mulsc(matp m, double[] x)
        {
            if (m.n != x.Length)
            {
                MessageBox.Show("Invalid length !");
            }

            double[] rez  = new double[m.n];
            double   sumt = 0;

            for (int i = 0; i < m.n; i++)
            {
                for (int j = 0; j < m.n; j++)
                {
                    sumt += (m.m[i, j] * x[j]);
                }
                rez[i] = sumt;
                sumt   = 0;

                if (Math.Abs(rez[i]) < eps)
                {
                    rez[i] = 0;
                }
            }

            return(rez);
        }
示例#3
0
        private double[] calcB(matp a)
        {
            double[] b = new double[a.n];

            for (int i = 1; i <= a.n; i++)
                for (int j = 1; j <= a.n; j++)
                    b[i - 1] += j * a.gm(i, j);

            return b;
        }
示例#4
0
        private matp matRot(int n, int p, int q, double c, double s)
        {
            matp r = new matp(n);

            r.initmat();
            r.sm(p, p, c);
            r.sm(q, q, c);
            r.sm(q, p, -s);
            r.sm(p, q, s);

            return(r);
        }
示例#5
0
        private matp matRot(int n,int p, int q, double c, double s)
        {
            matp r = new matp(n);

            r.initmat();
            r.sm(p, p, c);
            r.sm(q, q, c);
            r.sm(q, p, -s);
            r.sm(p, q, s);

            return r;
        }
示例#6
0
        private double[] calcB(matp a)
        {
            double[] b = new double[a.n];

            for (int i = 1; i <= a.n; i++)
            {
                for (int j = 1; j <= a.n; j++)
                {
                    b[i - 1] += j * a.gm(i, j);
                }
            }

            return(b);
        }
示例#7
0
        public matp transpose()
        {
            matp t = new matp(n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    t.m[j, i] = m[i, j];
                }
            }

            return(t);
        }
示例#8
0
        private void button4_Click(object sender, EventArgs e)
        {
            Ainv = new matp(n);
            double[] baux;

            for (int j = 1; j <= n; j++)
            {
                baux = getbcol(j);
                setAinvcol(j, getSolTsup(R, baux));
            }

            show("Ainv givens calculated !");
            tbAinv.Clear();
            tbAinv.AppendText(Ainv.ToString());
        }
示例#9
0
        private double[] getSolTsup(matp Tsup, double[] b)
        {
            if (Tsup.n != b.Length)
                MessageBox.Show("Invalid Length !");
            double[] x = new double[Tsup.n];
            double sum = 0;

            x[n - 1] = b[n - 1] / Tsup.m[n - 1, n - 1];
            for (int i = n - 2; i >= 0; i--)
            {
                for (int j = i; j < n; j++)
                    sum += (Tsup.m[i, j] * x[j]);
                x[i] = (b[i] - sum) / Tsup.m[i, i];
                sum = 0;
            }
            return x;
        }
示例#10
0
        static public matp remake(matp[] ms)
        {
            int  n  = ms[0].n * 2;
            matp mr = new matp(n);

            for (int i = 0; i < n / 2; i++)
            {
                for (int j = 0; j < n / 2; j++)
                {
                    mr.m[i, j]                 = ms[0].m[i, j];
                    mr.m[i, j + n / 2]         = ms[1].m[i, j];
                    mr.m[i + n / 2, j]         = ms[2].m[i, j];
                    mr.m[i + n / 2, j + n / 2] = ms[3].m[i, j];
                }
            }
            return(mr);
        }
示例#11
0
        static public matp sub(matp m1, matp m2)
        {
            if (m1.n != m2.n)
            {
                Console.WriteLine("Wrong sub!");
                return(new matp(1));
            }
            int  n   = m1.n;
            matp rez = new matp(n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    rez.m[i, j] = m1.m[i, j] - m2.m[i, j];
                }
            }
            return(rez);
        }
示例#12
0
        public matp[] section()
        {
            matp[] blocs = new matp[4];
            for (int i = 0; i < 4; i++)
            {
                blocs[i] = new matp(this.n / 2);
            }

            for (int i = 0; i < n / 2; i++)
            {
                for (int j = 0; j < n / 2; j++)
                {
                    blocs[0].m[i, j] = this.m[i, j];
                    blocs[1].m[i, j] = this.m[i, j + n / 2];
                    blocs[2].m[i, j] = this.m[i + n / 2, j];
                    blocs[3].m[i, j] = this.m[i + n / 2, j + n / 2];
                }
            }

            return(blocs);
        }
示例#13
0
        private double[] getSolTsup(matp Tsup, double[] b)
        {
            if (Tsup.n != b.Length)
            {
                MessageBox.Show("Invalid Length !");
            }
            double[] x   = new double[Tsup.n];
            double   sum = 0;

            x[n - 1] = b[n - 1] / Tsup.m[n - 1, n - 1];
            for (int i = n - 2; i >= 0; i--)
            {
                for (int j = i; j < n; j++)
                {
                    sum += (Tsup.m[i, j] * x[j]);
                }
                x[i] = (b[i] - sum) / Tsup.m[i, i];
                sum  = 0;
            }
            return(x);
        }
示例#14
0
        public matp transpose()
        {
            matp t = new matp(n);

            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    t.m[j, i] = m[i, j];

            return t;
        }
示例#15
0
        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;

            eps             = Math.Pow(10, -Convert.ToInt32(tbeps.Text));
            opr.eps         = eps;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;

            A = new matp(tbA.Text);
            n = A.n;
            show("A was processed !");

            B     = calcB(A);
            Binit = B;
            tbB.Clear();
            for (int i = 0; i < n; i++)
            {
                tbB.AppendText(B[i].ToString() + Environment.NewLine);
            }
            show("B calculated (init) !");

            Qt = new matp(n);
            Qt.initmat();
            show("Q initiated !");

            double r0 = 0;
            double c, s;

            R = A;
            for (int r = 1; r < n; r++)
            {
                for (int i = r + 1; i <= n; i++)
                {
                    r0 = Math.Sqrt(R.gm(r, r) * R.gm(r, r) + R.gm(i, r) * R.gm(i, r));
                    if (Math.Abs(r0) < eps)
                    {
                        c = 1;
                        s = 0;
                    }
                    else
                    {
                        c = R.gm(r, r) / r0;
                        s = R.gm(i, r) / r0;
                    }

                    matp Ri = matRot(n, r, i, c, s);
                    R  = opr.mul(Ri, R);
                    B  = opr.mulsc(Ri, B);
                    Qt = opr.mul(Ri, Qt);
                }
            }

            show("R,B,Q calculated !");

            if (R.checksing() == false)
            {
                MessageBox.Show("Matrix singular !");
            }
            else
            {
                button3.Enabled = true;
                button4.Enabled = true;
            }

            tbR.Clear();
            tbR.AppendText(R.ToString());

            tbQ.Clear();
            tbQ.AppendText(Qt.transpose().ToString());

            tbBb.Clear();
            for (int i = 0; i < n; i++)
            {
                tbBb.AppendText(B[i].ToString() + Environment.NewLine);
            }
            show("B calculated (re) !");

            tgiv = DateTime.Now - dt;
        }
示例#16
0
        static public matp remake(matp[] ms)
        {
            int n = ms[0].n * 2;
            matp mr = new matp(n);

            for (int i = 0; i < n / 2; i++)
                for (int j = 0; j < n / 2; j++)
                {
                    mr.m[i, j] = ms[0].m[i, j];
                    mr.m[i, j + n / 2] = ms[1].m[i, j];
                    mr.m[i + n / 2, j] = ms[2].m[i, j];
                    mr.m[i + n / 2, j + n / 2] = ms[3].m[i, j];
                }
            return mr;
        }
示例#17
0
        static public matp sub(matp m1, matp m2)
        {
            if (m1.n != m2.n)
            {
                Console.WriteLine("Wrong sub!");
                return new matp(1);
            }
            int n = m1.n;
            matp rez = new matp(n);

            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    rez.m[i, j] = m1.m[i, j] - m2.m[i, j];
            return rez;
        }
示例#18
0
        static public double[] mulsc(matp m, double[] x)
        {
            if (m.n != x.Length)
                MessageBox.Show("Invalid length !");

            double[] rez = new double[m.n];
            double sumt = 0;

            for (int i = 0; i < m.n; i++)
            {
                for (int j = 0; j < m.n; j++)
                    sumt += (m.m[i, j] * x[j]);
                rez[i] = sumt;
                sumt = 0;

                if (Math.Abs(rez[i]) < eps)
                    rez[i] = 0;
            }

            return rez;
        }
示例#19
0
        static public matp mul(matp m1, matp m2)
        {
            if (m1.n != m2.n)
            {
                Console.WriteLine("Wrong mul!");
                return new matp(1);
            }

            int n = m1.n;
            matp rez = new matp(n);

            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                {
                    for (int k = 0; k < n; k++)
                        rez.m[i, j] += (m1.m[i, k] * m2.m[k, j]);
                    if (Math.Abs(rez.m[i, j]) < eps)
                        rez.m[i, j] = 0;
                }
            return rez;
        }
示例#20
0
        private void button4_Click(object sender, EventArgs e)
        {
            Ainv = new matp(n);
            double[] baux;

            for (int j = 1; j <= n; j++)
            {
                baux = getbcol(j);
                setAinvcol(j, getSolTsup(R, baux));
            }

            show("Ainv givens calculated !");
            tbAinv.Clear();
            tbAinv.AppendText(Ainv.ToString());
        }
示例#21
0
        public matp[] section()
        {
            matp[] blocs = new matp[4];
            for (int i = 0; i < 4; i++)
                blocs[i] = new matp(this.n / 2);

            for (int i = 0; i < n / 2; i++)
                for (int j = 0; j < n / 2; j++)
                {
                    blocs[0].m[i, j] = this.m[i, j];
                    blocs[1].m[i, j] = this.m[i, j + n / 2];
                    blocs[2].m[i, j] = this.m[i + n / 2, j];
                    blocs[3].m[i, j] = this.m[i + n / 2, j + n / 2];
                }

            return blocs;
        }
示例#22
0
        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;

            eps = Math.Pow(10, -Convert.ToInt32(tbeps.Text));
            opr.eps = eps;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;

            A = new matp(tbA.Text);
            n=A.n;
            show("A was processed !");

            B = calcB(A);
            Binit = B;
            tbB.Clear();
            for (int i = 0; i < n; i++)
                tbB.AppendText(B[i].ToString() + Environment.NewLine);
            show("B calculated (init) !");

            Qt = new matp(n);
            Qt.initmat();
            show("Q initiated !");

            double r0 = 0;
            double c,s;

            R = A;
            for (int r = 1; r < n; r++)
                for (int i = r + 1; i <= n; i++)
                {
                    r0 = Math.Sqrt(R.gm(r, r) * R.gm(r, r) + R.gm(i, r) * R.gm(i, r));
                    if (Math.Abs(r0) < eps)
                    {
                        c = 1;
                        s = 0;
                    }
                    else
                    {
                        c = R.gm(r, r) / r0;
                        s = R.gm(i, r) / r0;
                    }

                    matp Ri = matRot(n, r, i, c, s);
                    R = opr.mul(Ri, R);
                    B = opr.mulsc(Ri, B);
                    Qt = opr.mul(Ri, Qt);
                }

            show("R,B,Q calculated !");

            if (R.checksing() == false)
                MessageBox.Show("Matrix singular !");
            else
            {
                button3.Enabled = true;
                button4.Enabled = true;
            }

            tbR.Clear();
            tbR.AppendText(R.ToString());

            tbQ.Clear();
            tbQ.AppendText(Qt.transpose().ToString());

            tbBb.Clear();
            for (int i = 0; i < n; i++)
                tbBb.AppendText(B[i].ToString() + Environment.NewLine);
            show("B calculated (re) !");

            tgiv = DateTime.Now - dt;

        }