Join 136,912 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,735 people online right now. Registration is fast and FREE... Join Now!
Hope you are enjoying the day =). It's another wonderful day at work coding.
I just had a question to pose to the members here. I think I have a possible solution, but I am wondering if there may be more eloquent solutions out there (there typically are).
I have a list of parameters. Each parameter can be a scalar, or it can be a matrix of values from 2x2 all the way up to 15x21. I was thinking about how to store each of these parameters (I am displaying them on a grid) and had a solution, but I don't know if its confusing and if there is a better way:
CODE
//Note that parameterDimensions[] is an array that has the number of ROWS in [0] and number of COLUMNS in [1]
double[,] formattedParameterArray = new double[parameterDimensions[0],parameterDimensions[1]]; for (int c = 0; c < parameterDimensions[1]; c++) {
for (int r = 0; r < parameterDimensions[0]; r++) { formattedParameterArray[r, c] = parameterValues[r + c];//TEST }
}
Thoughts? Improvements? Better structures? I think this is a fairly expensive method to use, but don't really have anything better that is still readable.
This post has been edited by killnine: 2 Apr, 2008 - 10:27 AM
public FormattedParameter(int rows, int cols) { this.rows = rows; this.cols = cols; this.array = new double[this.Size]; }
public bool IsScalar { get { return array == null; } } public int Size { get { return (IsScalar) ? 1 : rows * cols; } } public int Rows { get { return rows; } } public int Cols { get { return cols; } } private int GetIndex(int row, int col) { if (IsScalar) { throw new ArgumentOutOfRangeException(); } int index = (row * rows) + col; if (index >= Size) { throw new ArgumentOutOfRangeException(); } return index; } public double SingleValue { get { if (!IsScalar) { throw new ArgumentOutOfRangeException(); } return this.singleValue; } } public double this[int row, int col] { get { return this.array[GetIndex(row, col)]; } set { this.array[GetIndex(row, col)] = value; } } }
FormattedParameter param = new FormattedParameter(parameterDimensions[0], parameterDimensions[1]); for (int c = 0; c < param.Cols; c++) { for (int r = 0; r < param.Rows; r++) { param[r, c] = parameterValues[r + c];//TEST } }
Well to simplify everything you could consider creating a parameter object class which would take a matrix or scalar as a value. The beauty of this setup would be that it would create an object in which case you could create an array of objects, sort them, move them around as a collection, iterate through the parameters and apply interfaces to them.
It would also get out of the situation where you would have to start thinking about all these array indexes and dimensions etc. You could just have the class take in matrices and think of scalars as a matrix of dimensions 1 x 1.
A slight alteration of the idea is storying the matrices in a structure like arraylist. Then when you get at the values cast them back out as Double[,], use your dimension subscripts on it to get the values. It could be a little messy but it would work as well.
Lastly if you want to go with the array subscript route you can try an array of 2d arrays or a jagged array of multidimensional arrays. Whoa, what a mouthful. Example below...
csharp
// Our matrix example of doubles Double[,] matrix = new Double[1,1]; matrix[0, 0] = 3.5;
// Our jagged array of 100 2d matrices Double [][,] lstMartices = new Double[100][,];
// Store the matrix in the matrices list lstMartices[0] = matrix;
// Return the matrix, access element 0,0 and show it in a messagebox MessageBox.Show(lstMartices[0][0, 0].ToString());
Crazy stuff I tell you but those are some options you could go with. Whatever you wish to do will be based on your overall design.
Enjoy!
"At DIC we be jagged array loving code ninjas, because if it is jagged it means it can cut you!"
Also...no matter what only a Array of size 1 is created... I believe this is because in the constructor for a matrix, this.Size is called before array is actually created so it always returns a scalar:
CODE
this.array = new double[rows * cols /*this.Size*/]
Thanks for the prototype code, though, it really helps. I also learned what a ternary operator is!
I think I may go with this new type for simplicity of modification later.
This may lead me to go back and change a few other items in code to behave similarly.
I feel that in the year that I have been programming C#, I first learned how to create solutions to problems. Now I am learning how to make more EFFICIENT solutions to problems.
Thanks again.
This post has been edited by killnine: 3 Apr, 2008 - 07:52 AM
You are correct! Sorry about that, some days right is left...
QUOTE(killnine @ 3 Apr, 2008 - 10:33 AM)
Also...no matter what only a Array of size 1 is created... I believe this is because in the constructor for a matrix, this.Size is called before array is actually created so it always returns a scalar:
CODE
this.array = new double[rows * cols /*this.Size*/]
Pity, I hate redundant code. If you have to do figure it out in the constructor, I'd store it. e.g.
csharp
class FormattedParameter { private double singleValue; private int rows, cols, size; private double[] array = null;
public FormattedParameter(int rows, int cols) { this.rows = rows; this.cols = cols; this.size = rows * cols this.array = new double[this.size]; }
public int Size { get { return this.size; } } //...
QUOTE(killnine @ 3 Apr, 2008 - 10:33 AM)
I feel that in the year that I have been programming C#, I first learned how to create solutions to problems. Now I am learning how to make more EFFICIENT solutions to problems.
No worries, happy to help.
You always learn stuff, that's the fun. You'll look at code you did a few years ago and yell at yourself, but it's nice to know you can do better now. There's always something to discover, like bloody late binding properties in the constructor.