Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 8, 2010, 06:16 AM   #1
MorphingDragon
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
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?
MorphingDragon is offline   0 Reply With Quote
Old Sep 8, 2010, 06:18 AM   #2
Hawkeye411
macrumors 68000
 
Hawkeye411's Avatar
 
Join Date: Jun 2007
Location: Canada EH!!!
Quote:
Originally Posted by MorphingDragon View Post
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?
Hawkeye411 is offline   0 Reply With Quote
Old Sep 8, 2010, 06:48 AM   #3
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by MorphingDragon View Post
Is it possible to get multidimensional NSArray without nesting an array within an array?
No
robbieduncan is offline   0 Reply With Quote
Old Sep 8, 2010, 07:26 AM   #4
MorphingDragon
Thread Starter
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by Hawkeye411 View Post
Doesn't arr[2][2] = 8?
Arrays start counting at 0.

arr[2] is the 3rd member.
MorphingDragon is offline   0 Reply With Quote
Old 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
AlmostThere is offline   0 Reply With Quote
Old Sep 8, 2010, 09:58 AM   #6
MorphingDragon
Thread Starter
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by AlmostThere View Post
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.
MorphingDragon is offline   0 Reply With Quote
Old Sep 8, 2010, 10:03 AM   #7
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by MorphingDragon View Post
I know, but array notation starts at 0.
Just use fortran, it starts at 1. Unless you want it to start somewhere else...

-Lee
lee1210 is offline   0 Reply With Quote
Old Sep 8, 2010, 10:14 AM   #8
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Quote:
Originally Posted by lee1210 View Post
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
balamw is offline   0 Reply With Quote
Old Sep 8, 2010, 10:38 AM   #9
MorphingDragon
Thread Starter
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by balamw View Post
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 -
Add/Subtract vectors etc
Dot/Cross product
Find area/volume of parallelogram/ppped formed.

Matrices -
Addition/Subtraction/Multiplication
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.
MorphingDragon is offline   0 Reply With Quote
Old Sep 8, 2010, 11:39 AM   #10
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by MorphingDragon View Post
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.
Sydde is offline   0 Reply With Quote
Old Sep 8, 2010, 01:15 PM   #11
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by MorphingDragon View Post
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.
chown33 is offline   0 Reply With Quote
Old Sep 8, 2010, 07:49 PM   #12
MorphingDragon
Thread Starter
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by chown33 View Post
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 View Post
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.
MorphingDragon is offline   0 Reply With Quote
Old Sep 8, 2010, 08:28 PM   #13
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by MorphingDragon View Post
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.
chown33 is offline   0 Reply With Quote
Old Sep 8, 2010, 08:36 PM   #14
MorphingDragon
Thread Starter
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by chown33 View Post
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 View Post
Looks like a programming question to me.
I asked two questions one main one and one sort of OT one.
MorphingDragon is offline   0 Reply With Quote
Old Sep 8, 2010, 08:37 PM   #15
lee1210
macrumors 68040
 
lee1210's Avatar
 
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
lee1210 is offline   0 Reply With Quote
Old Sep 8, 2010, 11:12 PM   #16
MorphingDragon
Thread Starter
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by lee1210 View Post
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 View Post
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.
MorphingDragon is offline   0 Reply With Quote
Old 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
autorelease is offline   0 Reply With Quote
Old 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.)
Sander is offline   0 Reply With Quote
Old Sep 9, 2010, 05:04 AM   #19
MorphingDragon
Thread Starter
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by Sander View Post
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.
MorphingDragon is offline   0 Reply With Quote
Old Sep 9, 2010, 11:52 AM   #20
SidBala
macrumors 6502a
 
Join Date: Jun 2010
If you can use C# then use C#. Also, if you are not comfortable with pointers, then don't use them. Use dynamic collection classes provided by the std lib in C++ and the Systems.Collections namespace in C#. They are much safer and a lot cleaner. Almost no one ever uses malloc'ed arrays directly anymore. Unless if it was performance code.

And never use multidimensional dynamic arrays unless you really have to. They are really messy and can lead to many problems and buggy code. Use 1D array and transform the 2D coordinates to 1D.

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.

This class should contain a C# ArrayList or equivalent to hold the values. This is a 1-D array. Make the class contain variables height and width.

Setup a constructor for the class so you can do:

VecMat myMat = New VecMat( 3, 2,
54, 23,
65, 32,
49, 93);

VecMat myVec = new VecMat(3,1,
10,
65,
93);

Where 3,2 and 3,1 specify the size and the rest of the parameters are the values that go into the matrix. Use variable length parameters to accept an uncertain number of parameters into your constructor.

Also create a At(x,y) function or overload the [][] operator to get:

myMat.At(1,1) or
myMat[1][1]

To get the values at 1,1 etc

Now overload the addition, multiplication, subtraction operators to do things like:

myVec = (myMat1 + myMat2) * myVec1 - myMat3

Throw exceptions when they cant be multiplied or added.

Remember that by making the vectors and matrices the same class, you can very easily multiply vectors with matrices and matrices with other matrices by just defining one overloaded operator.

Now overload other operators of your choice to define dot products and cross products.

If you want extra credit, use templates in your matrix class. Now you can multipy a matrix of anything. ints, floats, even complex numbers!

If you really want to go overboard with the extra credit, make the number of dimensions scalable too, so you can operate on multidimensional tensors!

There, I have just outlined your project. Good luck!

Last edited by SidBala; Sep 9, 2010 at 12:02 PM.
SidBala is offline   0 Reply With Quote
Old Sep 9, 2010, 12:24 PM   #21
MorphingDragon
Thread Starter
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by SidBala View Post
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 View Post
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.
MorphingDragon is offline   0 Reply With Quote
Old Sep 9, 2010, 02:12 PM   #22
SidBala
macrumors 6502a
 
Join Date: Jun 2010
Quote:
Originally Posted by MorphingDragon View Post
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:

aVectorOfOneSize DOT aVectorOfaDifferentSize

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 View Post
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.
SidBala is offline   0 Reply With Quote
Old Sep 9, 2010, 03:13 PM   #23
MorphingDragon
Thread Starter
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by SidBala View Post
Whatever language you choose, the basic concepts are all the same.
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

Last edited by MorphingDragon; Sep 9, 2010 at 03:23 PM.
MorphingDragon is offline   0 Reply With Quote
Old Sep 9, 2010, 04:29 PM   #24
SidBala
macrumors 6502a
 
Join Date: Jun 2010
Quote:
Originally Posted by MorphingDragon View Post
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:
Originally Posted by MorphingDragon View Post
Please read.
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?
SidBala is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Safari History Remotely Viewed tyranix OS X 2 Aug 9, 2013 10:44 AM
Zinio viewed on mini epi117 iPad Apps 5 Feb 3, 2013 07:13 PM
Mail always opens last viewed message nStyle iOS 6 3 Jan 31, 2013 12:50 PM
Change to how contacts are viewed? palmerc2 iOS 6 1 Sep 24, 2012 01:03 PM
So 3D maps can only be viewed in US jabingla2810 iOS 6 12 Sep 13, 2012 11:49 AM

Forum Jump

All times are GMT -5. The time now is 04:42 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC