0

From the following code:

   /** Array for internal storage of elements.
   @serial internal array storage.
   */
   private double[][] A;

   /** Row and column dimensions.
   @serial row dimension.
   @serial column dimension.
   */
   private int m, n;

public Matrix times (Matrix B) {
      if (B.m != n) {
         throw new IllegalArgumentException("Matrix inner dimensions must agree.");
      }
      Matrix X = new Matrix(m,B.n);
      double[][] C = X.getArray();
      double[] Bcolj = new double[n];
      for (int j = 0; j < B.n; j++) {
         for (int k = 0; k < n; k++) {
            Bcolj[k] = B.A[k][j];
         }
         for (int i = 0; i < m; i++) {
            double[] Arowi = A[i];
            double s = 0;
            for (int k = 0; k < n; k++) {
               s += Arowi[k]*Bcolj[k];
            }
            C[i][j] = s;
         }
      }
      return X;
   }

double[] Arowi = A[i];

What is this line doing? A is a two dimensional Matrix, why is A[i] allowed? and what exactly is it doing? Is that the same thing as A[i][0]?

This notation is confusing.

Also could someone translate that line to .NET, how would you do that with a double[,] Matrix?

I know there is a Matrix.GetLength() which gives you a specified length of a dimension. But comparison to that line of Java code, how would that be translated?

EDIT I believe I fixed it by just replacing that line and passing in my matrix to copy the first row into a new one dimensional matrix and returning it

 public double[] ConvertFirstRowOfMultiArrayToOneArray(double[,] p)
        {
            double[] array = new double[p.Length];
            for (int i = 0; i < p.GetLength(0); i++)
            {
                array[i] = p[0, i];
            }

            return array;
        }
Technocrat
  • 334
  • 1
  • 4
  • 15

3 Answers3

2

What is this line doing?

It's setting Arowi, that's declared as one dimension array, to the array A[i]

Suppose this case:

double[][] A = new double[2][2];
A[0][0] = 1.5;
A[0][1] = 2.5;
A[1][0] = 3.5;
A[1][0] = 4.5;

double[] B = A[0]; // Imagine A[0] as A[i]

/* then value of B is now A[0]
so B is now a unidimensional array containing value:
B[0] == 1.5 == A[0][0]
B[1] == 2.5 == A[0][1] */

A is a two dimensional Matrix, why is A[i] allowed?

Because as you can see, you are setting the unidimensional array value (B||Arowi) as the second dimension that's containing the A[i] array.

Imagine A[i] containing another array in itself (bidimensional). So we are saving this dimension into a new variable. In this case, it would be referencing the memory address, and not the value itself, if I'm not confused, and that if you edit the value in Arowi||B for example, it will change in the original array.

Example:

B[0] == 1.5 == TRUE
B[0] = 1.75
A[0][0] == 1.5 == FALSE
A[0][0] == 1.75 == TRUE

and what exactly is it doing?

I explain it in the example case.

The application is doing the following if I'm not confused:

It saves the result of adding (plus operation, s += Arowi[k]*Bcolj[k]) the values of multiplied A array (current array member/attribute in class (A) or maybe it's the Matrix class itself, I can't see all of the code) by the values of B columns (Matrix object taken by parameter). In each main loop it's going through a new column in B, storing all the values of rows in that column, and then looping the whole A array or class array, to store the results in C and then returning it as a Matrix object. I hope it is understandable and that I'm not confused.

Is that the same thing as A[i][0]?

No

A[i][0] sets the value. A[i] sets an unidimensional array

Sorry for my bad english, I hope you can understand and that the description and ideas are correct.

Edit

Look the example:

static void Main(string[] args)
    {
        double[][] A = new double[2][] { new double[2], new double[2] };
        A[0][0] = 1.5;
        A[0][1] = 2.5;
        A[1][0] = 3.5;
        A[1][1] = 4.5;
        System.Console.WriteLine(A[0][0]);
        // Prints 1,5
        Console.Read();
        double[] B = A[0];
        System.Console.WriteLine(B[0]);
        // Prints 1,5
        System.Console.Read();
        System.Console.Read();

    }

So the line: double[] Arowi = A[i]; would translate to double[] Arowi = A[i];, just the same.

Only renember to create a jagged array double[][] A instead of array like double[,] A, because there seems to not be a way to reference a dimension with not jagged arrays (A[m,n]; being double[] Arowi = A[i]; not possible to be done like in Java). I can't tell why exactly right now. Just tried in my VS2013.

I recommend you to look at MSDN Documentation: http://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx

I also recommend you this thread: Multidimensional Array [][] vs [,]

Community
  • 1
  • 1
Jesus Gonzalez
  • 411
  • 6
  • 17
  • Okay I understand now. It's just an Array of arrays so I can set a single dimensional array to one of the arrays inside of the two-dimensional arrays. However, how would I do the same thing in .NET? how would I do that same line in C#? double[] Arowi = A[ , ]; ? – Technocrat Jan 17 '15 at 23:15
  • Ohhh I see, I think I understand. I fixed it by making another method that does the following (I edited my post) – Technocrat Jan 18 '15 at 00:08
  • You could also do `double ConvertRowOfMultiDimToOneArray(double[,] p, int row)` - so you can convert any row to a unidimensional array, instead of only the first row. And even create another method to convert any column to a unidimensional array. I think that you will know how to do this correctly. – Jesus Gonzalez Jan 18 '15 at 00:15
0

Not sure how it is in C# (.NET), but java has nothing like two (multi) dimensional arrays. It only has array of arrays (of arrays of arrays ...). So it is possible to have non-rectangular array of arrays.

double[] Arowi = A[i]; means declare new variable named Arowi of type array of doubles and set it to A[i] which is i-th (zero-based) member of A.

It is not the same as A[i][0], because this is 0-th member of i-th member of A.

EDIT: I haven't done any code review.

Zereges
  • 5,139
  • 1
  • 25
  • 49
  • I'm still a little confused on your last part where you mentioned it is not the same as A[i][0]. How would you set double[] equal to A[i][]? That part makes no sense – Technocrat Jan 17 '15 at 22:46
0

To understand what

double[] Arowi = A[i];

means, it is easiest to perceive A as an array of array of doubles instead of a matrix. Therefore, if you index A, it will return an array of doubles. Furthermore, if you index A at some index, it will return a double.

Hope this helps!

Phil Gabardo
  • 142
  • 11