Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: selectcoaxial on April 18, 2012, 10:11:22 am

Title: DIM Error
Post by: selectcoaxial on April 18, 2012, 10:11:22 am
I'm making a program to find the matrix of minors ([wikipedia]http://en.wikipedia.org/wiki/Minor_(linear_algebra[/wikipedia])), and I keep getting a DIM error at [A](I,J)->(M,N). This is my code:

Code: [Select]
Prompt [A]
dim([A])ListDIM
ListDIM(1)->O
{O-1,O-1}->dim([B])
ListDIM->dim([C])

For(Y,1,O)
For(X,1,O)

1üM
1üN
For(I,1,O)
If I "is not equal to" R
1->N
For(J,1,O)
If J "is not equal to" C
[b][A](I,J)->[B](M,N)[/b]
N+1->N
End
End
M+1->M
End
End

det([B])->[C](X,Y)
End
End

Disp [C]

I don't see why I would get this error. Also, I'm basing this code on http://chi3x10.wordpress.com/2008/05/28/calculate-matrix-inversion-in-c/ inside this code block:
Code: [Select]
for(int j=0;j<order;j++)
    {
        for(int i=0;i<order;i++)
        {
            // get the co-factor (matrix) of A(j,i)
            GetMinor(A,minor,j,i,order);
            Y[i][j] = det*CalcDeterminant(minor,order-1);
            //Not including these 2 lines in the TI-BASIC version
            if( (i+j)%2 == 1)
                Y[i][j] = -Y[i][j];
        }
    }
Title: Re: DIM Error
Post by: jsj795 on April 18, 2012, 10:58:46 am
it's because the dimension of [A] and [ B ] is different, from this:


Code: [Select]

{O-1,O-1}->dim([B])


You're deceasing the dimension by 1 on both x and y, and so the calculator cannot store the value of [A] to [ B ].

I'll try to post a code when i figure out exactly what minor linear algebra thing is :P
Title: Re: DIM Error
Post by: selectcoaxial on April 18, 2012, 11:05:15 am
This explains it a bit more spot on. http://mathworld.wolfram.com/Minor.html
So if the i = 2, and j = 2, in my case, X=Y=2, any elements across X's row and Y's column will be removed. So Minor(2,2) would be the matrix without that row 2 and column 2. So effectively, dimension of B, which will contain the matrix minor would have each dimension one less.
Title: Re: DIM Error
Post by: Xeda112358 on April 18, 2012, 11:25:51 am
I had to change your code a bit (I think you need a "Then" after your Ifs). The issue is that [B] has dimensions n-1 and you are trying to read the nth element in a row (which doesn't exist). I'll try to see if I can figure out a solution.

EDIT: Here is a solution that isn't too elegant, but it works:

Program: 126 bytes of code
Lists: none
Matrices: [A],[B],[C],[D]
Real: A,B,C,D
Code: [Select]
ClrHome
Prompt [A]
[A]→[C]
dim([A]
Ans(1→D
For(A,1,D
[A]T                      ;T=transpose
For(B,A,D-1
rowSwap(Ans,B,B+1
End
AnsT→[B]    ;The Ts here are transpose. [2nd][Matrix][right][2]
For(C,1,D-1
[B]
For(B,C,D-1
rowSwap(Ans,B,B+1
End
Ans→[D]
{D-1,D-1→dim([D]
det([D]→[C](C,A
End
End
ClrHome
[C]
If you don't want to use [D], then you can just zero out the last column and row and then put a 1 in the lower right corner before taking the determinant. I can't quite figure out how to do that without using another matrix, though :[ I can zero it: *row(0,*row(0,AnsT,D)T,D

EDIT2: Runer optimised the transposes by putting them outside of the For( loop, also saving a byte :D
Title: Re: DIM Error
Post by: jsj795 on April 18, 2012, 12:32:12 pm
*IMPORTANT* Mine only gives out a specific minor matrix
Here's mine, and I put in bunch of different GUI and error checks. I could make an interface to make matrix inputting is easier if you want, but easiest way is just putting in all the number into any matrix first in matrix editor, and within the program just put the matrix name when asked.
Idk if I said it right, so for example, you have a matrix, input all the matrix data into matrix [A] with matrix editor, and start program, and when it asks for MATRIX=, just press [2nd][X^-1][1] :)

Program: 368 bytes of code (I think)
Lists:none
Matrices: [A], [ B ] (if you don't care to save the original matrix you can make all [ B ] into [A] and delete the codes which have * before :
Real:A, B, I, J

Code: [Select]
:ClrHome
:Repeat A=B
:Input "MATRIX=", [A]
:dim([A]
:Ans(1->A
:dim([A]
:Ans(2->B
:If A=/=B
:Disp "ERROR.CHECK DIM.
:End
:Disp "MATRIX DIM:", dim([A]
:Repeat I>0 and I<=A
:Input "I ROW=", I
:ipart(I->I
:If I<=0 and I>A
:Disp "IMPOSSIBLE
:End
:Repeat J>0 or J<=A
:Input "J COLUMN=", J
:ipart(J->J
:If J<=0 or J>A
:Disp "IMPOSSIBLE
:End
*:[A]->[B]
:Disp "WORKING..."
:If I =/= A
:rowSwap([B],A,I->[B]
:{A-1,A->dim([B]
:For(B,I,A-2
:rowSwap([B],B,B+1->[B]
:End
:[B]transpose(little T)->[B]
:If J =/= A
:rowSwap([B],A,J->[B]
:{A-1,A-1->dim([B]
:For(B,J,A-2
:rowSwap([B],B,B+1->[B]
:End
:[B]transpose(little T)->[B]
:ClrHome
*:Disp "MATRIX", [A]
*:Pause
:Disp "MINOR MATRIX", [B]
:Pause
:Disp "DETERMINANT", det([B]
Title: Re: DIM Error
Post by: Runer112 on April 18, 2012, 02:06:36 pm
I'm pretty sure I've developed an unbeatable method. The total size of my program's code is only 81 bytes! ;D

Most of the savings comes from the fact that I never actually find the matrix minors. Instead, I simply alter one row of the input matrix such that this matrix has the same determinant as the proper matrix minor, since that's really all we need from the matrix minor.

Without further ado, here it is in all its 81-byte glory. I've also attached the 8xp. :)

Code: [Select]
Prompt [A]
[A]→[B]
min(dim(Ans→A
For(Y,1,A
For(X,1,A
*row(0,[A],Y→[C]
1-4fPart(.5(X+Y→[C](Y,X
det([C]→[B](Y,X
End
End
DelVar [C]
[B]
Title: Re: DIM Error
Post by: DJ Omnimaga on April 18, 2012, 02:48:09 pm
Runer112 you just turned his code into a new language <_< (Runer112-BASIC)
Title: Re: DIM Error
Post by: Runer112 on April 18, 2012, 05:18:36 pm
Alternatively, here's an even smaller method that might qualify as "cheating." selectcoaxial mentioned in his first post that finding the matrix of minors is part of the algorithm to find the inverse of a matrix. But the calculator already has a matrix inverse function. So instead of going forwards in the algorithm from the input matrix, let's go backwards in the algorithm from the inverted matrix! >:D

After trying a few layouts, I've settled on this program, weighing in at a petite 45 44 bytes! :)

Code: [Select]
Prompt [A]
min(dim([A]→A
identity(A
For(B,2,A,2
*row(⁻1,Ans,B
End
Ans([A]⁻¹Ans)ᵀdet([A]


EDIT: My TI-Basic optimizing skills are a little rusty, so I didn't notice that I could optimize a parenthesis off the last line until now. This makes the size of the code 44 bytes. I won't bother to re-upload the 8xp, it's only a small change. :P
Title: Re: DIM Error
Post by: selectcoaxial on April 19, 2012, 02:44:42 am
Wow, you guys are awesome! So much has been written in just one night :) I came up with an approach similar to runner but instead I used 4 nested for loops :P