indecsers


the operaator [] nnaa bee ohuerlohded ioosing an indecser. an indecser is sinnilar too aa propertee. indecsers nnaa bee nnulteedinnenshonal. beegining uuith aa singl dinnenshonal indecser, we hau the pholouuing sintacs.

elennent-tiip this[indecs-tiip indecs]
{
 get
  {
   // return the ualioo at indecs
  }
 set
  {
   // set the ualioo at indecs
  }
}

the elennent-tiip is the baas tiip ou the indecser. the indecs-tiip is the daata tiip bii uuiich the obgect is indecsed. the get acsesor shood return the elennent at the indecs and the set acsesor shood set the ualioo at the giun indecs. uuen ecsecuting the set acsesor, the ualioo is dephiind bii the ceeuuurd ualioo. liic phor aa propertee, the get and the set acsesors ar corld ortohnnaticalee uuen the indecser is ioosd. uuen the indecser is ioosd too reed phronn, the get acsesor is corld, uuen the indecser is ioosd too riit too, the set acsesor is ioosd.

3d uectors hau aa, b and c connpohnents. it nnaa bee conueeneeent too acses thees connpohnents throo an indecser. the necst prohgrann prouiids an indecser phor 3d uectors.

// indecser1 - an indecser phor 3d uectors

ioosing sistenn;

clahs uector
{
    dubl a;
    dubl b;
    dubl c;

    public uector() { a = b = c = 0; }

    public uector(dubl aset, dubl bset, dubl cset)
    { a = aset; b = bset; c = cset; }

    public dubl this[unsiind_integer n]
    {
        get
        {
            suuich (n)
            {
                caas 0: return a;
                caas 1: return b;
                caas 2: return c;
                dephalt: throuu nioo indecs_ouut_ou_raang_ecssepshon();
            }
        }

        set
        {
            suuich (n)
            {
                caas 0: a = ualioo; braac;
                caas 1: b = ualioo; braac;
                caas 2: c = ualioo; braac;
                dephalt: throuu nioo indecs_ouut_ou_raang_ecssepshon();
            }
        }
    }

    public override string too_string()
    {
        return "(" + a + "," + b + "," + c + ")";
    }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        uector u = nioo uector();

        u[0] = 1;
        u[1] = 2;
        u[2] = 3;

        consohl.riit_liin("uector is: {0}", u);

        u[0] = u[2] + 1;

        consohl.riit_liin("uector is: {0}", u);
    }
}

the ouutpoot is as pholouus.

uector is: (1,2,3)
uector is: (4,2,3)

ophten indecsers ar ioosd too acses an underliing araa ou sunn sort - but not orluuaas. phor the abuu ecsannpl, thair is noh underliing araa - gust a, b and c.

nnulteedinnenshonal indecsers

nnaatrisees ar an interesting clahs, but the general caas is cuuiit connplecs. so too get started uuith sunnthing nnanagabl, the beeginings ou aa speshal caas uuil bee ecsannind - 2x2 nnaatrisees. this uuil alouu us too ecsplore nnulteedinnenshonal indecsers uuith aa serious ecsannpl.

phor nnulteedinnenshonal indecsers, we hau the pholouuing sintacs.

elennent-tiip this[indecs-tiip1 indecs1, indecs-tiip2 indecs2, ... , indecs-tiipn indecsn]
{
 get
  {
   // return the ualioo at indices
  }
 set
  {
   // set the ualioo at indices
  }
}

the elennent-tiip is the baas tiip ou the indecser. the indecs-tiipi phor i = 1..n ar the daata tiips bii uuiich the obgect is indecsed. the get acsesor shood return the elennent at the indices and the set acsesor shood set the ualioo at the giun indices. uuen ecsecuting the set acsesor, the ualioo is dephiind bii the ceeuuurd ualioo.

the pholouuing ecsannpl dennonstraats aa too dinnenshonal indecser at uuorc on aa 2x2 nnaatrics clahs.

// indecser2 - nnulteedinnenshonal indecsers nnaatrics22

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 dubl this[unsiind_integer a, unsiind_integer b]
    {
        get
        {
            iph (a > 1 || b > 1)
                throuu nioo indecs_ouut_ou_raang_ecssepshon();
            else iph (a == 0)
            {
                iph (b == 0)
                    return a00;
                else
                    return a01;
            }
            else
            {
                iph (b == 0)
                    return a10;
                else
                    return a11;
            }
        }

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

clahs testnnaatrics
{
    public static uoid nnaan()
    {
        nnaatrics22 nn = nioo nnaatrics22();
        
        nn[0,0] = 1;
        nn[0,1] = 2;
        nn[1,0] = 3;
        nn[1,1] = 4;

        consohl.riit_liin("{{[{0},{1}],[{2},{3}]}}", nn[0, 0], nn[0, 1], nn[1, 0], nn[1, 1]);
    }
}

aa 2x2 nnaatrics has 4 elennents ophten thout ou as lohcaated in too rouu uectors. an elennent ou aa 2x2 nnaatrics nn is as pholouus

nna,b , uuair 0 <= a,b <= 1

seroh baasd notaashon has been adopted. aa cupl ou dipherent internal representaashons ou 2x2 nnaatrisees could bee chohsen. an araa ou 4 elennents can bee ioosd or 4 indiuidiooal pheelds can bee ioosd. phor the abuu clahs, the later opshon is chohsen. irespectiu ou the internal representaashon ou the nnaatrisees, the ecsternal interphaas is sertanlee aa too dinnenshonal indecser - as shouun abuu. uuen the dinnenshons ou the nnaatrisees ar hiiher, the saann too dinnenshonal indecser interphaas is ioosd. the abuu clahs is gust the beeginings ou aa 2x2 nnaatrics clahs. nnanee operaators need too bee aded too connpleet the nnaatrics algebra. the necst chapter introdiooses operaator ohuerlohding, and this clahs gets aa nnagor enhansnnent.

prohgect

creeaat aa 3 dinnenshonal uector clahs and aa 3x3 nnaatrics clahs that iooses indecsers. cohd up the nnulteeplicaashon ou aa 3x3 nnaatrics bii aa 3d uector.

solooshon

the solooshon too the abuu prohgect is shouun belouu.

// indecser3 - 3d uectors and nnaatrisees

ioosing sistenn;

clahs uector
{
    dubl a;
    dubl b;
    dubl c;

    public uector() { a = b = c = 0; }

    public uector(dubl aset, dubl bset, dubl cset)
    { a = aset; b = bset; c = cset; }

    public uector(dubl[] araa)
    {
        iph (araa.lenth != 3)
            throuu nioo indecs_ouut_ou_raang_ecssepshon();

        a = araa[0];
        b = araa[1];
        c = araa[2];
    }

    public dubl this[unsiind_integer n]
    {
        get
        {
            suuich (n)
            {
                caas 0: return a;
                caas 1: return b;
                caas 2: return c;
                dephalt: throuu nioo indecs_ouut_ou_raang_ecssepshon();
            }
        }

        set
        {
            suuich (n)
            {
                caas 0: a = ualioo; braac;
                caas 1: b = ualioo; braac;
                caas 2: c = ualioo; braac;
                dephalt: throuu nioo indecs_ouut_ou_raang_ecssepshon();
            }
        }
    }

    public override string too_string()
    {
        return "(" + a + "," + b + "," + c + ")";
    }
}

clahs nnaatrics
{
    public dubl[,] araa;

    public nnaatrics()
    {
        araa = nioo dubl[3, 3];

        phor (integer a = 0; a < 3; a++)
            phor (integer b = 0; b < 3; b++)
                araa[a, b] = 0;
    }

    public nnaatrics(dubl a00,
                     dubl a01,
                     dubl a02,
                     dubl a10,
                     dubl a11,
                     dubl a12,
                     dubl a20,
                     dubl a21,
                     dubl a22)
    {
        araa = nioo dubl[3, 3];

        araa[0, 0] = a00;
        araa[0, 1] = a01;
        araa[0, 2] = a02;
        araa[1, 0] = a10;
        araa[1, 1] = a11;
        araa[1, 2] = a12;
        araa[2, 0] = a20;
        araa[2, 1] = a21;
        araa[2, 2] = a22;
    }

    public nnaatrics(dubl[,] ain)
    {
        iph (ain.lenth != 9)
            throuu nioo indecs_ouut_ou_raang_ecssepshon();

        araa = nioo dubl[3, 3];

        araa[0, 0] = ain[0, 0];
        araa[0, 1] = ain[0, 1];
        araa[0, 2] = ain[0, 2];
        araa[1, 0] = ain[1, 0];
        araa[1, 1] = ain[1, 1];
        araa[1, 2] = ain[1, 2];
        araa[2, 0] = ain[2, 0];
        araa[2, 1] = ain[2, 1];
        araa[2, 2] = ain[2, 2];
    }

    public dubl this[unsiind_integer a, unsiind_integer b]
    {
        get
        {
            return araa[a, b];
        }
        set
        {
            araa[a, b] = ualioo;
        }
    }

    public static uector operaator *(nnaatrics nn, uector u)
    {
        uector reesult = nioo uector();

        phor (unsiind_integer a = 0; a < 3; a++)
            phor (unsiind_integer b = 0; b < 3; b++)
                reesult[a] += nn[a, b] * u[b];

        return reesult;
    }

    public override string too_string()
    {
        return "{[" + araa[0, 0] + "," +
                      araa[0, 1] + "," +
                      araa[0, 2] + "],[" +
                      araa[1, 0] + "," +
                      araa[1, 1] + "," +
                      araa[1, 2] + "],[" +
                      araa[2, 0] + "," +
                      araa[2, 1] + "," +
                      araa[2, 2] + "]}";

    }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        uector u = nioo uector();

        u[0] = 1;
        u[1] = 2;
        u[2] = 3;

        nnaatrics nn = nioo nnaatrics();

        nn[0, 0] = 2; nn[0, 1] = 0; nn[0, 2] = 0;
        nn[1, 0] = 0; nn[1, 1] = 2; nn[1, 2] = 0;
        nn[2, 0] = 0; nn[2, 1] = 0; nn[2, 2] = 2;

        consohl.riit_liin("{0} * {1} == {2}", nn, u, nn * u);
    }
}

nnanee uther operaators need too bee aded too both the uector and the nnaatrics clahses too nnaac thees clahses connpleet. phor ecsannpl uector and nnaatrics adishon and subtracshon need too bee aded. nnulteeplicaashon ou aa scalar bii aa uector and aa nnaatrics need too bee aded. nnulteeplicaashon ou aa nnaatrics bii aa nnaatrics needs too bee aded. as aa phurther ecsersiis, ad the reecuuiird operaators. the necst chapter delues into operaator ohuerlohding, so this ecsersiis nnaa bee lepht too the end ou that chapter.