Author Topic: DIM Error  (Read 9766 times)

0 Members and 1 Guest are viewing this topic.

Offline selectcoaxial

  • LV2 Member (Next: 40)
  • **
  • Posts: 29
  • Rating: +0/-0
    • View Profile
DIM Error
« 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];
        }
    }

Offline jsj795

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1105
  • Rating: +84/-3
    • View Profile
Re: DIM Error
« Reply #1 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
« Last Edit: April 18, 2012, 10:59:54 am by jsj795 »


Spoiler For funny life mathematics:
1. ROMANCE MATHEMATICS
Smart man + smart woman = romance
Smart man + dumb woman = affair
Dumb man + smart woman = marriage
Dumb man + dumb woman = pregnancy
2. OFFICE ARITHMETIC
Smart boss + smart employee = profit
Smart boss + dumb employee = production
Dumb boss + smart employee = promotion
Dumb boss + dumb employee = overtime
3. SHOPPING MATH
A man will pay $2 for a $1 item he needs.
A woman will pay $1 for a $2 item that she doesn't need.
4. GENERAL EQUATIONS & STATISTICS
A woman worries about the future until she gets a husband.
A man never worries about the future until he gets a wife.
A successful man is one who makes more money than his wife can spend.
A successful woman is one who can find such a man.
5. HAPPINESS
To be happy with a man, you must understand him a lot and love him a little.
To be happy with a woman, you must love her a lot and not try to understand her at all.
6. LONGEVITY
Married men live longer than single men do, but married men are a lot more willing to die.
7. PROPENSITY TO CHANGE
A woman marries a man expecting he will change, but he doesn't.
A man marries a woman expecting that she won't change, and she does.
8. DISCUSSION TECHNIQUE
A woman has the last word in any argument.
Anything a man says after that is the beginning of a new argument.

Girls = Time * Money (Girls are a combination of time and money)
Time = Money (Time is money)
Girls = Money squared (So, girls are money squared)
Money = sqrt(Evil) (Money is also the root of all evil)
Girls = sqrt(Evil) squared (So, girls are the root of all evil squared)
Girls = Evil (Thus, girls are evil)
*Girls=Evil credit goes to Compynerd255*

Offline selectcoaxial

  • LV2 Member (Next: 40)
  • **
  • Posts: 29
  • Rating: +0/-0
    • View Profile
Re: DIM Error
« Reply #2 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.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: DIM Error
« Reply #3 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

Offline jsj795

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1105
  • Rating: +84/-3
    • View Profile
Re: DIM Error
« Reply #4 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]
« Last Edit: April 18, 2012, 01:02:48 pm by jsj795 »


Spoiler For funny life mathematics:
1. ROMANCE MATHEMATICS
Smart man + smart woman = romance
Smart man + dumb woman = affair
Dumb man + smart woman = marriage
Dumb man + dumb woman = pregnancy
2. OFFICE ARITHMETIC
Smart boss + smart employee = profit
Smart boss + dumb employee = production
Dumb boss + smart employee = promotion
Dumb boss + dumb employee = overtime
3. SHOPPING MATH
A man will pay $2 for a $1 item he needs.
A woman will pay $1 for a $2 item that she doesn't need.
4. GENERAL EQUATIONS & STATISTICS
A woman worries about the future until she gets a husband.
A man never worries about the future until he gets a wife.
A successful man is one who makes more money than his wife can spend.
A successful woman is one who can find such a man.
5. HAPPINESS
To be happy with a man, you must understand him a lot and love him a little.
To be happy with a woman, you must love her a lot and not try to understand her at all.
6. LONGEVITY
Married men live longer than single men do, but married men are a lot more willing to die.
7. PROPENSITY TO CHANGE
A woman marries a man expecting he will change, but he doesn't.
A man marries a woman expecting that she won't change, and she does.
8. DISCUSSION TECHNIQUE
A woman has the last word in any argument.
Anything a man says after that is the beginning of a new argument.

Girls = Time * Money (Girls are a combination of time and money)
Time = Money (Time is money)
Girls = Money squared (So, girls are money squared)
Money = sqrt(Evil) (Money is also the root of all evil)
Girls = sqrt(Evil) squared (So, girls are the root of all evil squared)
Girls = Evil (Thus, girls are evil)
*Girls=Evil credit goes to Compynerd255*

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: DIM Error
« Reply #5 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]
« Last Edit: April 18, 2012, 02:34:59 pm by Runer112 »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: DIM Error
« Reply #6 on: April 18, 2012, 02:48:09 pm »
Runer112 you just turned his code into a new language <_< (Runer112-BASIC)

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: DIM Error
« Reply #7 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
« Last Edit: April 19, 2012, 11:51:43 am by Runer112 »

Offline selectcoaxial

  • LV2 Member (Next: 40)
  • **
  • Posts: 29
  • Rating: +0/-0
    • View Profile
Re: DIM Error
« Reply #8 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