MacRumors Forums Could a multidimensional array be viewed as a matrix?

 Sep 8, 2010, 06:16 AM #1 MorphingDragon Banned   Join Date: Mar 2009 Location: The World Inbetween Could a multidimensional array be viewed as a matrix? Could a multidimensional array be viewed as a matrix of numbers? I've gotten to a point where I really need to use 2D arrays now, but I'm having trouble visualizing them and making the mathematical connection which is making debugging a pain. Say A is a 3x3 matrix and "arr" is a 2D array. A= [1 2 3] [4 5 6] [7 8 9] So would: arr[0][0] = 1? arr[0][1] = 2? arr[2][1] = 8? arr[1][1] = 5? (A)33 = arr[2][2]? etc. Slightly OT: Is it possible to get multidimensional NSArray without nesting an array within an array? 0
Sep 8, 2010, 06:18 AM   #2
Hawkeye411
macrumors 68000

Join Date: Jun 2007
Quote:
 Originally Posted by MorphingDragon Could a multidimensional array be viewed as a matrix of numbers? I've gotten to a point where I really need to use 2D arrays now, but I'm having trouble visualizing them and making the mathematical connection which is making debugging a pain. Say A is a 3x3 matrix and "arr" is a 2D array. A= [1 2 3] [4 5 6] [7 8 9] So would: arr[0][0] = 1? arr[0][1] = 2? arr[2][1] = 8? arr[1][1] = 5? (A)33 = arr[2][2]? etc.
Doesn't arr[2][2] = 8?
0
Sep 8, 2010, 06:48 AM   #3
robbieduncan
Moderator

Join Date: Jul 2002
Location: London
Quote:
 Originally Posted by MorphingDragon Is it possible to get multidimensional NSArray without nesting an array within an array?
No
0
Sep 8, 2010, 07:26 AM   #4
MorphingDragon
Banned

Join Date: Mar 2009
Location: The World Inbetween
Quote:
 Originally Posted by Hawkeye411 Doesn't arr[2][2] = 8?
Arrays start counting at 0.

arr[2] is the 3rd member.
0
 Sep 8, 2010, 07:42 AM #5 AlmostThere macrumors 6502a   Join Date: Feb 2005 Location: The royal throne of kings, the sceptred isle, the other Eden, the fortress built If I understand the question correctly, yes. But you probably want to read this page http://en.wikipedia.org/wiki/Row-major_order and bear in mind the domain you are working in (for example, much linear algebra code is written in Fortran and different C bindings may or may not perform the appropriate transformation ... it has been a while. I think veclib does but other implementations don't). Also, matrix notation generally starts counting at 1. Lots of tiny gotchas to be aware of. __________________ Join the army of the dead: http://www.urbandead.com 0
Sep 8, 2010, 09:58 AM   #6
MorphingDragon
Banned

Join Date: Mar 2009
Location: The World Inbetween
Quote:
 Originally Posted by AlmostThere Also, matrix notation generally starts counting at 1.
I know, but array notation starts at 0.

That wiki page was interesting though.

Last edited by MorphingDragon; Sep 8, 2010 at 10:05 AM.
0
Sep 8, 2010, 10:03 AM   #7
lee1210
macrumors 68040

Join Date: Jan 2005
Location: Dallas, TX
Quote:
 Originally Posted by MorphingDragon I know, but array notation starts at 0.
Just use fortran, it starts at 1. Unless you want it to start somewhere else...

-Lee
0
Sep 8, 2010, 10:14 AM   #8
balamw
Moderator

Join Date: Aug 2005
Location: New England
Quote:
 Originally Posted by lee1210 Just use fortran, it starts at 1.
MATLAB too.

MorphingDragon, do you intend to do matrix operations/linear algebra on your 2D arrays? Or are they containers for some other kind of data?

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
0
Sep 8, 2010, 10:38 AM   #9
MorphingDragon
Banned

Join Date: Mar 2009
Location: The World Inbetween
Quote:
 Originally Posted by balamw MorphingDragon, do you intend to do matrix operations/linear algebra on your 2D arrays? Or are they containers for some other kind of data?
Yes, this is another part of my arbitrary University assignment to make classes/methods for Vectors and Matrices.

Requirements:
Vectors -
Dot/Cross product
Find area/volume of parallelogram/ppped formed.

Matrices -
Find Inverse/Discriminant

There's "extra credit" (Which turns out the lecturer buys you a chocolate bar ¬_¬) for making the methods and classes scale to different sizes, so I've been trying to make them scale by using arrays. I have the classes working with fixed sized vectors and arrays.

I'm going to go out on a limb and say that the University is making sure we have the maths knowledge for computer graphics, physics processing for papers next year etc.

Last edited by MorphingDragon; Sep 8, 2010 at 10:48 AM.
0
Sep 8, 2010, 11:39 AM   #10
Sydde
macrumors 68000

Join Date: Aug 2009
Quote:
 Originally Posted by MorphingDragon Slightly OT: Is it possible to get multidimensional NSArray without nesting an array within an array?
It is possible if you calculate your indices manually, as in ( yIndex * arrayWidth ) + xIndex; this would work for any array depth, just as it would for any depth of a C array, but it would probably fail spectacularly if the NSArray was not absolutely full. One possible alternative would be a NSPointerArray, which can be presized and can contain null elements - but at that point, you might as well use a C array unless you have encoding concerns (pointer size/endian-ness).
__________________
You got to be a spirit. You can't be no ghost.
0
Sep 8, 2010, 01:15 PM   #11
chown33
macrumors 603

Join Date: Aug 2009
Quote:
 Originally Posted by MorphingDragon Say A is a 3x3 matrix and "arr" is a 2D array. A= [1 2 3] [4 5 6] [7 8 9] So would: arr[0][0] = 1? arr[0][1] = 2? arr[2][1] = 8? arr[1][1] = 5? (A)33 = arr[2][2]?
Given what you've posted, then arr is uninitialized. So if your questions use '=' as the symbol meaning "compares equal to", then the answer is "Maybe", because arr's initial contents are undefined.

If your questions use '=' as the assignment symbol, then they are assignment statements, not questions.

And I have no idea how to interpret the expression (A)33 on the left-hand side of the last statement/question.

Also remember that NSArray can only hold objects. It can't hold fundamental types like int, long, or double. To do that, you must wrap the value in an object type, like NSNumber. This adds complexity, and may incur a significant performance cost, depending on what you're doing with the arrays.
0
Sep 8, 2010, 07:49 PM   #12
MorphingDragon
Banned

Join Date: Mar 2009
Location: The World Inbetween
Quote:
 Originally Posted by chown33 Given what you've posted, then arr is uninitialized. So if your questions use '=' as the symbol meaning "compares equal to", then the answer is "Maybe", because arr's initial contents are undefined. If your questions use '=' as the assignment symbol, then they are assignment statements, not questions. And I have no idea how to interpret the expression (A)33 on the left-hand side of the last statement/question. Also remember that NSArray can only hold objects. It can't hold fundamental types like int, long, or double. To do that, you must wrap the value in an object type, like NSNumber. This adds complexity, and may incur a significant performance cost, depending on what you're doing with the arrays.
Its not a programming question chown, its just pseudo code and mathematical notation to ask a theory question. If you don't understand mathematical notation like (A)33 then don't worry. (Which is FYI, 3rd row and 3rd column of Matrix A)

Quote:
 Originally Posted by Sydde It is possible if you calculate your indices manually, as in ( yIndex * arrayWidth ) + xIndex; this would work for any array depth, just as it would for any depth of a C array, but it would probably fail spectacularly if the NSArray was not absolutely full. One possible alternative would be a NSPointerArray, which can be presized and can contain null elements - but at that point, you might as well use a C array unless you have encoding concerns (pointer size/endian-ness).
Can C arrays hold pointers? Could you just give the pointer itself to an object in memory? (I'm not near a computer with GCC or clang atm)

id aPointer;
arr[0] = &aPointer; ?

Last edited by MorphingDragon; Sep 8, 2010 at 08:02 PM.
0
Sep 8, 2010, 08:28 PM   #13
chown33
macrumors 603

Join Date: Aug 2009
Quote:
 Originally Posted by MorphingDragon Its not a programming question chown, its just pseudo code and mathematical notation to ask a theory question. If you don't understand mathematical notation like (A)33 then don't worry. (Which is FYI, 3rd row and 3rd column of Matrix A)
I've seen several notations for matrix elements, such as M(i,j), M[i,j], but never (M)ij. I can't help wondering what happens when rows or columns exceeds 9.

Quote:
 Can C arrays hold pointers? Could you just give the pointer itself to an object in memory? (I'm not near a computer with GCC or clang atm) id aPointer; arr[0] = &aPointer; ?
Looks like programming questions to me.

Yes. Yes. Yes. (Assuming appropriate type declarations for the variables, of course.)

Last edited by chown33; Sep 8, 2010 at 08:35 PM.
0
Sep 8, 2010, 08:36 PM   #14
MorphingDragon
Banned

Join Date: Mar 2009
Location: The World Inbetween
Quote:
 Originally Posted by chown33 I've seen several notations for matrix elements, such as M(i,j), M[i,j], but never (M)ij. I can't help wondering what happens when rows or columns exceeds 9.
I come from a commonwealth country, it'll most likely be british notation.

Quote:
 Originally Posted by chown33 Looks like a programming question to me.
I asked two questions one main one and one sort of OT one.
0
 Sep 8, 2010, 08:37 PM #15 lee1210 macrumors 68040     Join Date: Jan 2005 Location: Dallas, TX http://forums.macrumors.com/showthread.php?t=844474 Pointers to pointers is a pretty common way to implement dynamically allocated multidimensional arrays. Row 11 column 1 and row 1 column 11 will be a bitch to tell apart with that notation. I just had to fix some code that didn't use a delimiter, it is not fun. -Lee 0
Sep 8, 2010, 11:12 PM   #16
MorphingDragon
Banned

Join Date: Mar 2009
Location: The World Inbetween
Quote:
 Originally Posted by lee1210 http://forums.macrumors.com/showthread.php?t=844474 Pointers to pointers is a pretty common way to implement dynamically allocated multidimensional arrays.
Thats... complex.

Though this shouldn't be as complex as sorting strings in a multiarray.

I'll try study it over the weekend and extract any useful bits.

Quote:
 Originally Posted by lee1210 Row 11 column 1 and row 1 column 11 will be a bitch to tell apart with that notation. I just had to fix some code that didn't use a delimiter, it is not fun.

I don't think it will be that difficult if I treat the notation like normal Matrix notation.

A[rows][columns]

Last edited by MorphingDragon; Sep 8, 2010 at 11:43 PM.
0
 Sep 9, 2010, 03:08 AM #17 autorelease macrumors regular   Join Date: Oct 2008 Location: Achewood, CA It seems like this is being made much more complicated than it has to be. You don't need NSArrays, you don't need pointers to pointers. Just a dynamically allocated, one-dimensional C array (created with malloc), and a width and height parameter. (number of columns, number of rows) Like Sydde said, it's easy to convert multidimensional coordinates to a linear index: Code: `[element at row N, column M = elements[numColumns*N + M]` You don't get to use nice multidimensional array syntax, so just write accessor/modifier methods: Code: ```[myMatrix elementAtRow:n column:m]; [myMatrix setElementAtRow:n column:m to:x];``` As an aside, when you use an array of pointers to arrays, you get what's called a two-dimensional "ragged array"; one where all rows don't have to have the same number of columns. This is overkill for a simple matrix class though. __________________ *** -[NSAutoreleasePool autorelease]: Cannot autorelease an autorelease pool 0
 Sep 9, 2010, 05:02 AM #18 Sander macrumors 6502   Join Date: Apr 2008 Is this an Objective-C assignment? (If so, interesting that this is being taught at an university.) __________________ Computer Programming: An Introduction for the Scientifically Inclined So how much does an iPhone developer make? My iPhone games: Sjoelen, Mazer (free) 0
Sep 9, 2010, 05:04 AM   #19
MorphingDragon
Banned

Join Date: Mar 2009
Location: The World Inbetween
Quote:
 Originally Posted by Sander Is this an Objective-C assignment? (If so, interesting that this is being taught at an university.)
This paper is taught in C#, but we can submit the assignments in any language.

The university teaches us Java and C# (and if you're doing software engineering assembly for x86) explicitly, we're expected to learn any other languages by ourselves and in most papers we get to choose what language we use.

Last edited by MorphingDragon; Sep 9, 2010 at 05:43 AM.
0
Sep 9, 2010, 12:24 PM   #21
MorphingDragon
Banned

Join Date: Mar 2009
Location: The World Inbetween
Quote:
 Originally Posted by SidBala Create a common class for both vectors and matrices called VecMat or something else. Vectors are just matrices with the length in one dimension = 1.
Yes but separate classes make the distinction between them clear, and it help avoids silly things like -

aMatrixAsAVector.dotProduct(aMatrix);

Quote:
 Originally Posted by SidBala There, I have just outlined your project. Good luck!
I've already done the compulsory part of the assignment, changing the classes to scale is the extra credit.

---

This is just my preference but I do my assignments in C and Obj-C/C++ (Depending on the assignment if it needs OOP). C# is a nice language (I prefer it over Java), but all that syntax sugar is why I don't use it for Uni projects. I want to learn how the actual logic behind it works, spending the time to get it work in something like C will be more beneficial than getting it to work in a sugar coated language like C#.

Last edited by MorphingDragon; Sep 9, 2010 at 12:37 PM.
0
Sep 9, 2010, 02:12 PM   #22
SidBala
macrumors 6502a

Join Date: Jun 2010
Quote:
 Originally Posted by MorphingDragon Yes but separate classes make the distinction between them clear, and it help avoids silly things like - aMatrixAsAVector.dotProduct(aMatrix);
I would STRONGLY disagree. Defining seperate classes just to "make the distinction between them clear" is a very bad reason to do so.

The fundamental reason why you want them to be same class is because they are the same. You can share the operators among them. Remember that you can multiply vectors to matrices, matrices to vectors and matrices to matrices and vectors to vectors.

You will need to define 4 operators to accomplish these. But with common class, you can just define one overloaded operator for every operation. It makes the code look a lot cleaner and it will allow you to scale it very easily.

This approach is very common in Matlab and many other math suites. They don't differentiate between vectors and matrices. Vectors are just matrices with one row/column.

The error case you mentioned,

aVectorAsAMatrix DOT aMatrix

is no different from:

Both the above operations fail because of their non matching sizes. So differentiating the classes wouldn't help since you would still have to error out with the sizes of two vectors are different.

The two operations would exception out in the same way:

if(LeftOperand.isVector && rightOperand.isVector && leftOperand.Size == rightOperand.Size)
---transpose the leftOperand into a row vector and multiply with the rightOperand as a column vector.
ELSE
---throw exception

.isVector does nothing but check if one of the dimensions is = 1 and .Size is a struct that holds x and y dimensions.

This works the right way for vectors and matrices.

Hence there is no reason to unnecessarily duplicate the classes.

Quote:
 Originally Posted by MorphingDragon This is just my preference but I do my assignments in C and Obj-C/C++ (Depending on the assignment if it needs OOP). C# is a nice language (I prefer it over Java), but all that syntax sugar is why I don't use it for Uni projects. I want to learn how the actual logic behind it works, spending the time to get it work in something like C will be more beneficial than getting it to work in a sugar coated language like C#.
Whatever language you choose, the basic concepts are all the same. The more complex concepts however, are not available in C. For OOP you would need to go C++ as is not available in C. C# offers even more with reflection etc.
0
Sep 9, 2010, 03:13 PM   #23
MorphingDragon
Banned

Join Date: Mar 2009
Location: The World Inbetween
Quote:
 Originally Posted by SidBala Whatever language you choose, the basic concepts are all the same.

Oh and FTR, you can do OO (and thus reflection) with C.
http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

Last edited by MorphingDragon; Sep 9, 2010 at 03:23 PM.
0
Sep 9, 2010, 04:29 PM   #24
SidBala
macrumors 6502a

Join Date: Jun 2010
Quote:
 Originally Posted by MorphingDragon I wasn't talking about concepts, I was talking about the underlying logic of programs that syntax sugar hide. Please read. Oh and FTR, you can do OO (and thus reflection) with C. http://www.planetpdf.com/codecuts/pdfs/ooc.pdf
Your "FTR" statement is very common knowledge. You can implement everything directly in opcodes too. There is a reason why people don't do that. If you have to ask on a forum about how to use dynamic arrays, you are obviously far from understanding implementation details behind it.

Quote:
No I am not going to "read". You asked for help. I gave it. Now if you want to be thankless about it, then fine. But don't tell me to "read". It is not like it's my job.

Got it?
0

 MacRumors Forums