Vuples where are you lololol?
using linq i want multilpty to multiply (Aarray1 * Array2) from 2.3 to 2.05 then i want to sum the output array base upon the range , can some help me
Array1[0] = {( 2.00, 1.00 )} * Array2[0] = {( 2.00, 2.00 )} = 2
Array1[1] = {( 2.01, 2.00 )} * Array2[1] = {( 2.01, 3.00 )} = 6
Array1[2] = {( 2.02, 3.00 )} * Array2[2] = {( 2.02, 4.00 )} = 12
Array1[3] = {( 2.03, 4.00 )} * Array2[3] = {( 2.03, 5.00 )} = 20
Array1[4] = {( 2.04, 5.00 )} * Array2[4] = {( 2.04, 6.00 )} = 30
Array1[5] = {( 2.05, 6.00 )} * Array2[5] = {( 2.05, 7.00 )} = 42
Array1[6] = {( 2.06, 7.00 )} * Array2[6] = {( 2.06, 8.00 )} = 56
-----------------------------------------------------------------------
Output[0] = {( 2.03, 20)}
Output[1] = {( 2.04, 30 )}
Output[2] = {( 2.05, 42 )}
double result = Output.Sum();
result = 92
Loading
VulpesPosted Jan 30, 2012, 9:55 AM
David SmithPosted Jan 31, 2012, 9:33 AM
VulpesPosted Jan 31, 2012, 9:16 AM
BTW, it's not good coding practice IMO to always cram everything into a single LINQ query. Remember you (or somebody else) may have to maintain this stuff months or years down the line.
Whilst LINQ is a powerful tool, it can produce some very complex expressions and it may therefore be better to break them up into bits that are easy to understand.
David SmithPosted Jan 31, 2012, 8:59 AM
VulpesPosted Jan 31, 2012, 8:54 AM
David SmithPosted Jan 31, 2012, 8:18 AM
Array1[3] = {( 2.03, 4.00 )} * Array2[3] = {( 2.03, 5.00 )} = 20
Array1[4] = {( 2.04, 5.00 )} * Array2[4] = {( 2.04, 6.00 )} = 30
Array1[5] = {( 2.05, 6.00 )} * Array2[5] = {( 2.05, 7.00 )} = 42
--------------------------------------------------------------------
Array1[3] = {( 2.03, 4.00 )}
Array1[4] = {( 2.04, 5.00 )}
Array1[5] = {( 2.05, 6.00 )}
92 / 15 = 6.1333333333
output = 6.13333333
VulpesPosted Jan 31, 2012, 5:43 AM
Here's a version of the program which should work for your ArrayDictionary:
David SmithPosted Jan 30, 2012, 8:06 PM
David SmithPosted Jan 30, 2012, 5:24 PM
for (waveLength = 2.00, index = 0; waveLength <= 5.00; waveLength += 0.01, index++)
{
Output[index] = (ArrayDictionary[ArrayType.Array1.ToString()][index].Item2 * ArrayDictionary[ArrayType.Array2.ToString()][index].Item2);
ArrayDictionary[ArrayType.ArrayType.ToString()].Add(new Tuple
}
double output = ArrayDictionary[ArrayType.Array1.ToString()].Where(a => a[0] >= LowLimit && a[0] <=
HighLimit).Zip(ArrayDictionary[ArrayType.Array2.ToString()].to.Where(a => a[0] >= 2.03 && a[0] <=
2.(t1, t2) => t1[1].Item2 * t2[1].Item2).Sum();
VulpesPosted Jan 30, 2012, 5:00 PM
David SmithPosted Jan 30, 2012, 4:11 PM
double output = ArrayDictionary[ArrayType.Array1.ToString()].Where(a => a[0] >= LowLimit && a[0] <=
HighLimit).Zip(ArrayDictionary[ArrayType.Array1.ToString()].to.Where(a => a[0] >= 2.03 && a[0] <=
2.(t1, t2) => t1[1].Item2 * t2[1].Item2).Sum();
VulpesPosted Jan 30, 2012, 10:26 AM
They therefore implement IEnumerable
As each element in the sequence is an array ('a') and we're interested in the first element, we therefore use a[0].
David SmithPosted Jan 30, 2012, 10:21 AM
Where(a => a[0] >= 2.03 && a[0]