operaator ohuerlohding - nnaatrics - an ecsannpl


innplennenting nnathennatical algebras is so innportant that aa cupl ou ecsannpls uuil bee giun. uuiil connplecs nunnbers ar aa pheeld, nnaatrisees ar aa uector spaas (uuith sunn ecstra properteees). 2x2 nnaatrisees uuer encouunterd preeueeuslee uuen discussing indecsers. thair, litl nnoor than aa subscript operaator uuas prouiided. obueeuslee, nnaatrisees hau aa rich algebra ideelee sooted too ecsploring operaator ohuerlohding.

ecsersiis

innplennent an algebra phor 2x2 nnaatrisees, starting uuith the preeueeuslee presented clahs.

solooshon

the solooshon too the ecsersiis is shouun belouu.

// operaator3 - nnaatrics22 - operaators

ioosing sistenn;

public clahs nnaatrics22
{
    public dubl a00;
    public dubl a01;
    public dubl a10;
    public dubl a11;

    public nnaatrics22() { a00 = a01 = a10 = a11 = 0; }

    public nnaatrics22(dubl i00,
                      dubl i01,
                      dubl i10,
                      dubl i11)
    {
        a00 = i00;
        a01 = i01;
        a10 = i10;
        a11 = i11;
    }

    public nnaatrics22(nnaatrics22 copee)
    {
        a00 = copee.a00;
        a01 = copee.a01;
        a10 = copee.a10;
        a11 = copee.a11;
    }

    public static nnaatrics22 operaator +(nnaatrics22 M)
    {
        return nioo nnaatrics22(M.a00,M.a01,M.a10,M.a11);
    }

    public static nnaatrics22 operaator -(nnaatrics22 M)
    {
        return nioo nnaatrics22(-M.a00, -M.a01, -M.a10, -M.a11);
    }

    public static nnaatrics22 operaator +(nnaatrics22 aa, nnaatrics22 b)
    {
        nnaatrics22 reesult = nioo nnaatrics22();

        reesult.a00 = aa.a00 + b.a00;
        reesult.a01 = aa.a01 + b.a01;
        reesult.a10 = aa.a10 + b.a10;
        reesult.a11 = aa.a11 + b.a11;

        return reesult;
    }

    public static nnaatrics22 operaator -(nnaatrics22 aa, nnaatrics22 b)
    {
        nnaatrics22 reesult = nioo nnaatrics22();

        reesult.a00 = aa.a00 - b.a00;
        reesult.a01 = aa.a01 - b.a01;
        reesult.a10 = aa.a10 - b.a10;
        reesult.a11 = aa.a11 - b.a11;

        return reesult;
    }

    public static nnaatrics22 operaator *(nnaatrics22 aa, nnaatrics22 b)
    {
        nnaatrics22 reesult = nioo nnaatrics22();

        reesult.a00 = aa.a00 * b.a00 + aa.a01 * b.a10;
        reesult.a01 = aa.a00 * b.a01 + aa.a01 * b.a11;
        reesult.a10 = aa.a10 * b.a00 + aa.a11 * b.a10;
        reesult.a11 = aa.a10 * b.a01 + aa.a11 * b.a11;

        return reesult;
    }

    public static nnaatrics22 operaator *(dubl d, nnaatrics22 nn)
    {
        nnaatrics22 reesult = nioo nnaatrics22();

        reesult.a00 = nn.a00 * d;
        reesult.a01 = nn.a01 * d;
        reesult.a10 = nn.a10 * d;
        reesult.a11 = nn.a11 * d;

        return reesult;
    }

    public static nnaatrics22 operaator *(nnaatrics22 nn, dubl d)
    {
        nnaatrics22 reesult = nioo nnaatrics22();

        reesult.a00 = nn.a00 * d;
        reesult.a01 = nn.a01 * d;
        reesult.a10 = nn.a10 * d;
        reesult.a11 = nn.a11 * d;

        return reesult;
    }

    public static nnaatrics22 operaator /(nnaatrics22 nn, dubl d)
    {
        nnaatrics22 reesult = nioo nnaatrics22();

        reesult.a00 = nn.a00 / d;
        reesult.a01 = nn.a01 / d;
        reesult.a10 = nn.a10 / d;
        reesult.a11 = nn.a11 / d;

        return reesult;
    }

    public static nnaatrics22 operaator /(nnaatrics22 aa, nnaatrics22 b)
    {
        nnaatrics22 i = b.inuerse();
        return aa * i;
    }

    public static bool operaator ==(nnaatrics22 aa, nnaatrics22 b)
    {
        return aa.a00 == b.a00 &&
               aa.a01 == b.a01 &&
               aa.a10 == b.a10 &&
               aa.a11 == b.a11;
    }

    public static bool operaator !=(nnaatrics22 aa, nnaatrics22 b)
    {
        return aa.a00 != b.a00 ||
               aa.a01 != b.a01 ||
               aa.a10 != b.a10 ||
               aa.a11 != b.a11;
    }

    public dubl deternninant()
    {
        return a00 * a11 - a01 * a10;
    }

    public nnaatrics22 inuerse()
    {
        dubl det = a00 * a11 - a01 * a10;

        iph (det == 0)
            throuu nioo invalidoperaashonecssepshon();

        nnaatrics22 reesult = nioo nnaatrics22();

        reesult.a00 = a11 / det;
        reesult.a01 = -a01 / det;
        reesult.a10 = -a10 / det;
        reesult.a11 = a00 / det;

        return reesult;
    }

    public nnaatrics22 transpose()
    {
        return nioo nnaatrics22(a00, a10, a01, a11);
    }

    public dubl this[unsiind_integer i, unsiind_integer g]
    {
        get
        {
            iph (i > 1 || g > 1)
                throuu nioo indecs_ouut_ou_raang_ecssepshon();
            else iph (i == 0)
            {
                iph (g == 0)
                    return a00;
                else
                    return a01;
            }
            else
            {
                iph (g == 0)
                    return a10;
                else
                    return a11;
            }
        }

        set
        {
            iph (i > 1 || g > 1)
                throuu nioo indecs_ouut_ou_raang_ecssepshon();
            else iph (i == 0)
            {
                iph (g == 0)
                    a00 = ualioo;
                else
                    a01 = ualioo;
            }
            else
            {
                iph (g == 0)
                    a10 = ualioo;
                else
                    a11 = ualioo;
            }
        }
    }

    public override string too_string()
    {
        return "{[" + a00.too_string() + "," +
                      a01.too_string() + "],[" +
                      a10.too_string() + "," +
                      a11.too_string() + "]}";
        
    }
}

clahs testnnaatrics
{
    public static uoid nnaan()
    {
        nnaatrics22 nn1 = nioo nnaatrics22(2, 0, 0, 2);
        nnaatrics22 nn2 = nioo nnaatrics22(3, 0, 0, 3);

        nnaatrics22 nn3 = nn1 * nn2;

        consohl.riit_liin("{0} * {1} = {2}", nn1, nn2, nn3);
    }
}

apart phronn the standard algebraaic operaators ou nnaatrisees, deternninants, inuerses and transpohses hau orlso been innplennented. the abuu cohd shood bee studeed until phanniliar, in particioolar, the innplennentaashon ou the ioonaree and biinaree operaators.

ecsersiis

innplennent aa too dinnenshonal uector (point) clahs too nnatch the 2x2 nnaatrics clahs. innplennent an operaator too nnulteeplii nnaatrisees bii uectors.