Array concatenation in C# - c#

How do I smartly initialize an Array with two (or more) other arrays in C#?
double[] d1 = new double[5];
double[] d2 = new double[3];
double[] dTotal = new double[8]; // I need this to be {d1 then d2}
Another question: How do I concatenate C# arrays efficiently?

You could use CopyTo:
double[] d1 = new double[5];
double[] d2 = new double[3];
double[] dTotal = new double[d1.Length + d2.Length];
d1.CopyTo(dTotal, 0);
d2.CopyTo(dTotal, d1.Length);

var dTotal = d1.Concat(d2).ToArray();
You could probably make it 'better' by creating dTotal first, and then just copying both inputs with Array.Copy.

You need to call Array.Copy, like this:
double[] d1 = new double[5];
double[] d2 = new double[3];
double[] dTotal = new double[d1.length + d2.length];
Array.Copy(d1, 0, dTotal, 0, d1.Length);
Array.Copy(d2, 0, dTotal, d1.Length, d2.Length);

using System.Linq;
int[] array1 = { 1, 3, 5 };
int[] array2 = { 0, 2, 4 };
// Concat array1 and array2.
var result1 = array1.Concat(array2).ToArray();

Related

<Module> class in C#

I was migrating a function from a dll to C# and I found a line of code that I don't understand:
< Module >.lm_minimize(((Vector<CalPoint>)calPoints).Size, n_par1, par, (ILMCallbacks)callbacks, (lm_data_type)data, control);
When I try to enter the class there is code in hexadecimal (I guess it's auto generated code) that I can't copy.
This is the code:
internal static void lm_minimize(
int m_dat,
int n_par,
double[] par,
ILMCallbacks callbacks,
lm_data_type data,
lm_control_type control)
{
double[] numArray = new double[m_dat];
double[] diag = new double[n_par];
double[] qtf = new double[n_par];
double[] fjac = new double[m_dat * n_par];
double[] wa1 = new double[n_par];
double[] wa2 = new double[n_par];
double[] wa3 = new double[n_par];
double[] wa4 = new double[m_dat];
int[] ipvt = new int[n_par];
control.info = 0;
control.nfev = 0;
\u003CModule\u003E.lm_lmdif(m_dat, n_par, par, numArray, control.ftol, control.xtol, control.gtol, control.maxcall * (n_par + 1), control.epsilon, diag, 1, control.stepbound, ref control.info, ref control.nfev, fjac, ipvt, qtf, wa1, wa2, wa3, wa4, callbacks, data);
callbacks.lm_printout(n_par, par, m_dat, numArray, data, -1, 0, control.nfev);
control.fnorm = \u003CModule\u003E.\u003FA0x6d25334e\u002Elm_enorm(m_dat, 0, numArray);
if (control.info >= 0)
return;
control.info = 10;
}
And this code calls the function lm_mdif. Part of the code is like this:
\u003CModule\u003E.\u003FA0x6d25334e\u002E\u003F\u0024S1\u0040\u003F1\u003F\u003Flm_lmdif\u0040\u0040YMXHHP\u002401AN0NNNHN0HNA\u0024CAH10P\u002401AH00000A\u0024AAUILMCallbacks\u0040\u0040P\u0024AAVlm_data_type\u0040\u0040\u0040Z\u0040\u0024\u0024Q4IA |= 1U;
// ISSUE: fault handler
I wanted to know if anyone knows what the LM_MINIMIZE function is for, I can't find what it is for..
Thanks
I tried migrate the library code to C#.

Difference between int[,] array = new int[4, 2]; and int[] test = { 4, 2 };

I recently discovered that two ways to declare array in C#, those are:
int[,] array = new int[4, 2];
int[] test = { 4, 2 };
Could anyone please describe the difference between those two statements?
int[,] array = new int[4, 2];
creates array with 8 elements having the default value of 0 and 2 dimensions
int[] test = { 4, 2 };
creates array with 2 elements(4 and 2) and 1 dimension

C# - Fastest Way To Sort Array Of Primitives And Track Their Indices

I need a float[] to be sorted. And I need to know where the old indices are in new array. That's why I can't use Array.Sort(); or whatever. So I would like to write a function that sorts the array for me and remembers from what index it took each value:
float[] input = new float[] {1.5, 2, 0, 0.4, -1, 96, -56, 8, -45};
// sort
float[] output; // {-56, -45, -1, 0, 0.4, 1.5, 2, 8, 96};
int[] indices; // {6, 8, 4, 2, 3, 0, 1, 7, 5};
Size of arrays would be around 500. How should I approach this ? What sorting algorithm etc.
After solved: It always surprises me how powerful C# is. I didn't even though of it being able to do that task on it's own. And since I already heard that Array.Sort() is very fast I'll take it.
float[] input = new float[] { 1.5F, 2, 0, 0.4F, -1, 96, -56, 8, -45 };
int[] indices = new int[input.Length];
for (int i = 0; i < indices.Length; i++) indices[i] = i;
Array.Sort(input, indices);
// input and indices are now at the desired exit state
Basically, the 2-argument version of Array.Sort applies the same operations to both arrays, running the actual sort comparisons on the first array. This is normally used the other way around - to rearrange something by the desired indices; but this works too.
You can use the overload of Array.Sort() which takes TWO arrays, and sorts the second one according to how it sorted the first one:
float[] input = new [] { 1.5f, 2, 0, 0.4f, -1, 96, -56, 8, -45 };
int[] indices = Enumerable.Range(0, input.Length).ToArray();
Array.Sort(input, indices);
You can create a new array of indices, and then sort both of them using Array.Sort and treating input as keys:
float[] input = new float[] { 1.5F, 2, 0, 0.4F, -1, 96, -56, 8, -45 };
int[] indicies = Enumerable.Range(0, input.Length).ToArray();
Array.Sort(input, indicies);
if you use linq:
float[] input = new float[] { 1.5F, 2, 0, 0.4F, -1, 96, -56, 8, -45 };
var result = input.Select(x => new { Value = x, Index = input.ToList().IndexOf(x)}).OrderBy(x => x.Value).ToList();
// sort
float[] output = result.Select(x => x.Value).ToArray();
int[] indices = result.Select(x => x.Index).ToArray();
in results you got objects with values and their indexes.
A List<KeyValuePair<int,float>> and a custom sorter would also work. the key for each pair holds the original index.
private void Form1_Load(object sender, EventArgs e)
{
List<KeyValuePair<int,float>> data = new List<KeyValuePair<int,float>>
{
new KeyValuePair<int,float>(0,1.5f),
new KeyValuePair<int,float>(1,2),
new KeyValuePair<int,float>(2,0),
new KeyValuePair<int,float>(3,0.4f),
new KeyValuePair<int,float>(4,-1),
new KeyValuePair<int,float>(5,96),
new KeyValuePair<int,float>(6,-56),
new KeyValuePair<int,float>(7,8),
new KeyValuePair<int,float>(8,-45)
};
data.Sort(SortByValue);
foreach (KeyValuePair<int, float> kv in data)
{
listBox1.Items.Add(kv.Key.ToString() + " - " + kv.Value.ToString());
}
}
private int SortByValue(KeyValuePair<int, float> a, KeyValuePair<int, float> b)
{
return a.Value.CompareTo(b.Value);
}

double[] -> double[,]

Just curious is there a quicker/neater way to achieve this:
double[] source = ... // some initialisation
var target = new double[1, source.Length];
for (var c = 0; c < source.Length; c++)
{
target[0, c] = source[c];
}
Initialiaze the array like this:
double[,] target = { { /* your list of values */ } };
Then you have a two dimentional array with only one row.
Since you are mentioning this is for P/Invoke, BlockCopy is probably reasonable to use:
double[] source = new double [] {1,2,3,4,7,8,9,0};// some initialisation
double[,] target = new double[1, source.Length];
Buffer.BlockCopy(source, 0, target, 0, source.Length * sizeof(double));

C# Filling double[][] manually

I am just trying out a source code.
This source code has the following line:
double[][] inputs = sourceMatrix.Submatrix(null, 0, 1).ToArray();
Originally, this "inputs" is filled using a matrix, but I am still too inexperienced to use this matrix. I would first like to test it with some hand-coded values.
Could anybody please tell me how to populate double[][] with some values?
I am not really experienced with C# yet. I guess that [][] means a threedimensional array.
In VB6 I would simply say
Redim inputs(2,2)
and then:
inputs(0,0) = 64
inputs(0,1) = 92
inputs(0,2) = 33
inputs(1,0) = 4
inputs(1,1) = 84
inputs(1,2) = 449
etc...
But I guess it is not that easy in C#.
If anybody could help, I would be really glad.
Thank you.
a double[][] is a jagged array - it is an array of arrays. To fill that you would fill the outer array with a set of double[] arrays. However, I expect you want a rectangular array: double[,], for example new double[3,2]. There is a short-hand for initializing such arrays:
double[,] data = new double[2, 3] { { 64, 92, 33 }, { 4, 84, 449 } };
double val = data[1, 2]; // 449.0
To keep it very simple:
double[][] inputs = new double[3][];
inputs[0] = new double[3];
inputs[1] = new double[3];
inputs[2] = new double[3];
inputs[0][0] = 1;
inputs[0][1] = 2;
inputs[0][2] = 3;
inputs[1][0] = 4;
inputs[1][1] = 5;
inputs[1][2] = 6;
inputs[2][0] = 7;
inputs[2][1] = 8;
inputs[2][2] = 9;
You have an array of arrays. So you should initialize them like this:
double[][] inputs = new double[][]
{
new double[] { 1, 2, 3 },
new double[] { 4, 5, 6 },
new double[] { 7, 8, 9 }
};
If you had a two dimentional array (inputs[,]) then it would be:
double[,] inputs = new double[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
But as Jon Skeet said, reading about arrays is the first thing you should do.
Take a look at using double[,] this is what you need Arrays
double[][] is a jagged array.
You probably mean double[,] which is two dimensional array.
Initializing a two dimensional array:
double[,] matrix = new double[3,2] = {{1,2} {3,4}, {4,5}}
You already have examples for initializing jagged arrays, from the other answers.
The difference between jagged and true multidimensional arrays in practice is jagged arrays are arrays of arrays so it is possible for them not to be rectangular, i.e. a matrix, hence jagged:
double[][] jagged = new double[2]
{
new double[4],
new double[2]
};
The way they are layed out in memory is also different. A multidimensional array is a single reference to a contiguous memory space. Jagged arrays are true array of arrays.
Read more about it here

Categories