Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: Radical Pi on March 10, 2010, 03:15:40 pm

Title: Finding the min of a matrix
Post by: Radical Pi on March 10, 2010, 03:15:40 pm
While trying to finish my simple Connect 4 game to detect tied games (aka the board matrix is all nonzero), I realized that min( isn't overloaded to handle matrices, so I'm in need of some ingenuity. Since I realized I only have to care about the min of the top row, my current solution is this:
Matr>list([A]T,L1
min(L1

But I want to know if there's a better way to do this. And I also want to know how to do the general case of finding the minimum of an entire matrix rather than just the top row like I did.

EDIT: Actually my current idea doesn't work either. Now I'm utterly clueless.

EDIT2: lol it's min(L1, not min(Ans. Disregard my last edit.
Title: Re: Finding the min of a matrix
Post by: DJ Omnimaga on March 10, 2010, 03:45:29 pm
mhmm at the moment I can't really figure out a fast way, but it seems like you need to convert your matrix to a list to use Min(. In this case, the only faster solution I can think about is to simply convert the game to use lists instead of matrices, but idk how well it would work and if it's faster/smaller.

Btw some people use lists instead of matrices in their games. For example in a 12x8 tilemap engine, instead of [A](A,B) they would do L1(1+B+12A), but so you can do a lot of stuff with lists, but as you can see, it increses the program size. Also I couldn't find anything on TIBD regarding the usage of Min( with matrices x.x
Title: Re: Finding the min of a matrix
Post by: ztrumpet on March 10, 2010, 05:13:29 pm
Matrices can't be min(ed. :(
It's too bad, as I know I've wanted to do that from time to time.
I think the solution you found is the smallest. :)
I'm not sure, but this may be faster:
[A](1,1
For(A,2,7 //I'm assuming the matrix is 7 columns. :D
min(Ans,[A](1,A
End
I don't know which way is faster.  Good luck getting it to work! ;D
Title: Re: Finding the min of a matrix
Post by: Radical Pi on March 10, 2010, 05:27:48 pm
Well I just decided to use a turn counter in my C4 program, but this is still an interesting problem.

Your pseudo-recursive solution is nice, albeit big, but it can easily be extended to find the min of any matrix in general:
assuming the dimensions of the matrix are {R,C}...

R->dim(L1
For(B,1,R
[A](B,1
For(A,2,C
min(Ans,[A](B,A->L1(B
End
End
min(L1

But I still hate how there doesn't look to be an elegant way to do this :(
Title: Re: Finding the min of a matrix
Post by: ztrumpet on March 10, 2010, 05:32:58 pm
But I still hate how there doesn't look to be an elegant way to do this :(
How about this? (Still assuming R and C are already set)
[A](1,1
For(A,1,R
For(B,1,C
min(Ans,[A](A,B
End
End


I this slightly more elegant?
Title: Re: Finding the min of a matrix
Post by: Radical Pi on March 10, 2010, 05:35:33 pm
That's much more elegant :P
*saves routine for later use*
Title: Re: Finding the min of a matrix
Post by: DJ Omnimaga on March 10, 2010, 11:21:35 pm
keep in mind, though, that in BASIC, the code being elegant doesn't really matter, though. People go for the fastest or smallest possible way regardless of how weird the code will look like. All that count is either optimizing for speed or for size.
Title: Re: Finding the min of a matrix
Post by: Builderboy on March 10, 2010, 11:31:39 pm
I just optimised for speed and size :P

Code: [Select]
e9
For(X,1,C
Matr>list([A],X,L1
min(Ans,min(L1
End

;)
Title: Re: Finding the min of a matrix
Post by: calc84maniac on March 10, 2010, 11:37:51 pm
You could also replace the [A](1,1 line with any value that is guaranteed to be higher than all elements of the matrix
Title: Re: Finding the min of a matrix
Post by: Builderboy on March 10, 2010, 11:42:06 pm
true, so i guess e9 would be the safest and most memory efficient bet ;D
Title: Re: Finding the min of a matrix
Post by: ztrumpet on March 11, 2010, 05:58:47 pm
That's really nice Builderboy!  Great code! ;D