I'm writing a little library and want to be able to sum an array.
A singlethread version works fine, but when I add multithreading, everything breaks.
I'm using a partitioner to split data on blocks and then sum every part in single result. Then I return it. But data is invalid, it's doesn't seems that there is any race condition, because every program reboot leads to same results.
[Pure]
public static double Sum(this double[] source)
{
source.IsNotNull("source");
if (source.Length < Constants.SingleThreadExecutionThreshold)
return Sum(source, 0, source.Length);
double result = 0;
object syncRoot = new object();
Parallel.ForEach(Partitioner.Create(0, source.Length),
() => (double)0,
(range, state, sum) => Sum(source, range.Item1, range.Item2),
x =>
{
lock (syncRoot)
result += x;
});
return result;
}
Sum(source, from, to) always give correct results. Here is an implementation:
[Pure]
private static double Sum(this double[] source, int startIndex, int endIndex)
{
double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
checked
{
int i;
for (i = startIndex; i < endIndex - Constants.Step + 1; i += Constants.Step)
{
sum1 += source[i];
sum2 += source[i + 1];
sum3 += source[i + 2];
sum4 += source[i + 3];
}
if (i == source.Length)
return ((sum1 + sum2) + (sum3 + sum4));
if (i == source.Length - 1)
return ((sum1 + sum2) + (sum3 + sum4) + source[i]);
if (i == source.Length - 2)
return ((sum1 + sum2) + (sum3 + sum4) + (source[i] + source[i + 1]));
return ((sum1 + sum2) + (sum3 + sum4) + (source[i] + source[i + 1] + source[i + 2]));
}
}
internal static class Constants
{
public const int Step = 4;
public const int SingleThreadExecutionThreshold = 1024;
}
How can it be fixed?
Code example: http://ideone.com/8sD0JL
Okay, think I've fixed it. I've found two major bugs.
the array.Length thing
you were misusing the "finally" delegate. There is no guarantee that that code ever gets run
With these changes I get a difference of -0.000576496124267578, which is within expected for double sum rounding errors.
[Pure]
public static double Sum(this double[] source, int startIndex, int endIndex)
{
double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
checked
{
int i;
int j = 0;
for (i = startIndex; i < endIndex - Constants.Step + 1; i += Constants.Step)
{
sum1 += source[i];
sum2 += source[i + 1];
sum3 += source[i + 2];
sum4 += source[i + 3];
j += Constants.Step;
}
var segmentLength = endIndex - startIndex;
if (j == segmentLength)
return ((sum1 + sum2) + (sum3 + sum4));
if (j == segmentLength - 1)
return ((sum1 + sum2) + (sum3 + sum4) + source[i]);
if (j == segmentLength - 2)
return ((sum1 + sum2) + (sum3 + sum4) + (source[i] + source[i + 1]));
return ((sum1 + sum2) + (sum3 + sum4) + (source[i] + source[i + 1] + source[i + 2]));
}
}
internal static class Constants
{
public const int Step = 4;
public const int SingleThreadExecutionThreshold = 1024;
}
[Pure]
public static double Sum(this double[] source)
{
if (source.Length < Constants.SingleThreadExecutionThreshold)
return Sum(source, 0, source.Length);
double result = 0;
object syncRoot = new object();
Parallel.ForEach(Partitioner.Create(0, source.Length),
(range) => {
var x = Sum(source, range.Item1, range.Item2);
lock (syncRoot)
result += x;
});
return result;
}
Related
I am trying to implement the Transvoxel algorithm for a game I am working on but I'm noticing some weird issues in the mesh generated:
Not sure why there are gaps and why the sphere has fins behind it.
For some reason, the mesh has gaps between chunks, and it has these strange fins extending out behind it. I haven't been able to figure out why this is happening but I have an inkling it may have to do with either the function that generates the density values for each Chunk (16x16x16 Block of cells):
private float SampleDensity(float3 _point)
{
var _worldPos = (_point / (m_WorldRadius - 1.0f) - 0.5f) * m_VolumeFieldSize;
var _halfS = m_VolumeFieldSize / 2;
var _maxD= Magnitude(new float3(_halfS, _halfS, _halfS));
var _fudge = 1f;
var _density = Magnitude(_worldPos) / (_maxD + _fudge) - 0.5f;
var _noise = CalculateNoise(_worldPos);
_density += _noise;
return _density;
}
public void Execute()
{
for (int _z = 0; _z < m_ChunkSize; _z++)
{
for (int _y = 0; _y < m_ChunkSize; _y++)
{
for (int _x = 0; _x < m_ChunkSize; _x++)
{
var _point = (new float3(_x, _y, _z) + (m_GPosition)); // Multiplying or dividing this value adjusts the final mesh's scale.
var _valueAtPoint = SampleDensity(_point);
m_Cells[_z * m_ChunkSize * m_ChunkSize + _y * m_ChunkSize + _x] = _valueAtPoint;
}
}
}
}
, or the mesh generating code itself:
for (int _z = 0; _z < _chunk.m_ChunkSize; _z++)
{
for (int _y = 0; _y < _chunk.m_ChunkSize; _y++)
{
for (int _x = 0; _x < _chunk.m_ChunkSize; _x++)
{
var _cubeConfiguration = 0;
for (int _i = 0; _i < 8; _i++)
{
_cornerPos = CornerIndex[_i];
_densityValues[_i] = _chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y, _z + _cornerPos.z);
if (_densityValues[_i] < _chunk.m_World.m_ISOLevel) _cubeConfiguration |= (1 << _i);
}
var _caseCode = (byte) _cubeConfiguration;
if (_caseCode == 0) continue;
for (int _i = 0; _i < _cornerNormals.Length; _i++)
{
_cornerPos = CornerIndex[_i];
_cornerNormals[_i] = new Vector3(
_chunk.GetCell(_x + _cornerPos.x - 1, _y + _cornerPos.y, _z + _cornerPos.z) - _chunk.GetCell(_x + _cornerPos.x + 1, _y + _cornerPos.y, _z + _cornerPos.z),
_chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y - 1, _z + _cornerPos.z) - _chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y + 1, _z + _cornerPos.z),
_chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y, _z + _cornerPos.z - 1) - _chunk.GetCell(_x + _cornerPos.x, _y + _cornerPos.y, _z + _cornerPos.z + 1)
);
}
var _cellClass = Transvoxel.regularCellClass[_caseCode];
Debug.Log($"Cell Class {_cellClass}");
var _vertexLocations = Transvoxel.RegularVertexData[_caseCode];
var _cellData = Transvoxel.RegularCellDatas[_cellClass];
var _vertexCount = _cellData.GetVertexCount();
var _triangleCount = _cellData.GetTriangleCount();
var _indexOffset = _cellData.vertexIndex;
for (int _i = 0; _i < _vertexCount; _i++)
{
ushort _edge = (ushort)(_vertexLocations[_i] & 255);
byte _v0 = (byte)((_edge >> 4) & 0x0F); //First Corner Index
byte _v1 = (byte)(_edge & 0x0F); //Second Corner Index
float _t = (_densityValues[_v1]) / (_densityValues[_v1] - _densityValues[_v0]);
Vector3 _p0 = new Vector3((_x + CornerIndex[_v0].x), (_y + CornerIndex[_v0].y), (_z + CornerIndex[_v0].z));
Vector3 _p1 = new Vector3((_x + CornerIndex[_v1].x), (_y + CornerIndex[_v1].y), (_z + CornerIndex[_v1].z));
var _position = (_p0 * _t) + ((1 - _t) * _p1);
var _n0 = CalculateNormal(_chunk, new Vector3Int((int) _p0.x, (int) _p0.y, (int) _p0.z));
var _n1 = CalculateNormal(_chunk, new Vector3Int((int) _p1.x, (int) _p1.y, (int) _p1.z));
var _normal = (_n0 + _t * (_n1 - _n0)).normalized;
m_Normals.Add(_normal);
m_Vertices.Add(_position);
m_UVs.Add(UvOffset[m_VCase]);
m_VCase = (byte)(m_VCase == 3 ? 0 : m_VCase + 1);
m_LocalVertexMapping[_i] = (ushort)(m_Vertices.Count - 1);
}
for (int _t = 0; _t < _triangleCount; _t++)
{
int _tm = _t * 3;
m_Triangles.Add(m_LocalVertexMapping[_indexOffset[_tm]]);
m_Triangles.Add(m_LocalVertexMapping[_indexOffset[_tm + 1]]);
m_Triangles.Add(m_LocalVertexMapping[_indexOffset[_tm + 2]]);
}
}
}
}
I am at a complete loss and would really appreciate some help as I've been racking my brains for close to 2 months on this, along with reading several papers on different MC Algorithms (Lengyel's included) for potential inspiration. Thank You.
I have a list of items that are determined by what a player enters into several input fields, but for the sake of this example, let's say that it contains "a", "b", "c", "d", and "e". These are then sorted into a dictionary with a list of numbers as the values (unimportant). I then randomize the dictionary with two different randomizer variables (i and j), for the sake of taking two objects from the dictionary and displaying them to the screen, so the player can press various associated buttons. This goes on until x amount of turns have passed. The main problem I'm having is the prevention of semi-duplicates, such as "a b" and "b a" from appearing.
I have tried entering inserting both the randomized pair and its semi-duplicate into another dictionary and then using while loop statements preventing any pairs from that dictionary from appearing. Unfortunately, this hasn't worked.
Below is my code.
public void Start() {
finalList = new Dictionary<string, int>();
for (index = 0; index < allNumbers; index++)
{
finalList.Add(itemList[index], valueList[index]);
Debug.Log(finalList[index.ToString()]);
}
}
public void Update() {
choose();
}
public void choose() {
duplicates = new Dictionary<string, string>();
duplicates.Clear();
while (rounds < insertNum) {
key = "(" + itemList[i].ToString() + " " + itemList[j].ToString() + ")";
reverseKey = "(" + itemList[j].ToString() + " " + itemList[i].ToString() + ")";
while (j == i || (duplicates.ContainsKey(key) || duplicates.ContainsKey(reverseKey))) {
i = UnityEngine.Random.Range(0, allNumbers - 1);
j = UnityEngine.Random.Range(0, allNumbers - 1);
key = "(" + itemList[i].ToString() + " " + itemList[j].ToString() + ")";
reverseKey = "(" + itemList[j].ToString() + " " + itemList[i].ToString() + ")";
Debug.Log("(new keys " + key + ", " + reverseKey + ")");
//break;
} while (j == i || (duplicates.ContainsKey(key) && duplicates.ContainsKey(reverseKey)))
{
i = UnityEngine.Random.Range(0, allNumbers - 1);
j = UnityEngine.Random.Range(0, allNumbers - 1);
key = "(" + itemList[i].ToString() + " " + itemList[j].ToString() + ")";
reverseKey = "(" + itemList[j].ToString() + " " + itemList[i].ToString() + ")";
Debug.Log("(new keys " + key + ", " + reverseKey + ")");
}while (j == i && (duplicates.ContainsKey(key) || dupes.ContainsKey(reverseKey))) {
i = UnityEngine.Random.Range(0, allNumbers - 1);
j = UnityEngine.Random.Range(0, allNumbers - 1);
key = "(" + itemList[i].ToString() + " " + itemList[j].ToString() + ")";
reverseKey = "(" + itemList[j].ToString() + " " + itemList[i].ToString() + ")";
Debug.Log("(new keys " + key + ", " + reverseKey + ")");
}
while (j == i && (duplicates.ContainsKey(key) && duplicates.ContainsKey(reverseKey))) {
i = UnityEngine.Random.Range(0, allNumbers - 1);
j = UnityEngine.Random.Range(0, allNumbers - 1);
key = "(" + itemList[i].ToString() + " " + itemList[j].ToString() + ")";
reverseKey = "(" + itemList[j].ToString() + " " + itemList[i].ToString() + ")";
Debug.Log("(new keys " + key + ", " + reverseKey + ")");
}
duplicates.Add(key, "1"); // the one is just a filler variable
duplicates.Add(reverseKey, "1");
if (buttonOneBool) { //this is in another script, ignore
finalList[itemList[i].ToString()] = valueList[i] += 2;
finalList[itemList[j].ToString()] = valueList[j] -= 2;
i = UnityEngine.Random.Range(0, n - 1);
j = UnityEngine.Random.Range(0, n - 1);
} else if (buttonTwoBool) {
finalList[itemList[i].ToString()] = valueList[i] -= 2;
finalList[itemList[j].ToString()] = valueList[j] += 2;
i = UnityEngine.Random.Range(0, n - 1);
j = UnityEngine.Random.Range(0, n - 1);
} else if (buttonThreeBool) {
finalList[itemList[i].ToString()] = valueList[i] -= 1;
finalList[itemList[j].ToString()] = valueList[j] -= 1;
i = UnityEngine.Random.Range(0, n - 1);
j = UnityEngine.Random.Range(0, n - 1);
} else if (buttonFourBool) {
finalList[itemList[i].ToString()] = valueList[i] += 1;
finalList[itemList[j].ToString()] = valueList[j] += 1;
i = UnityEngine.Random.Range(0, n - 1);
j = UnityEngine.Random.Range(0, n - 1);
}
break;
}
The simplest way to fix this is to guarantee i < j. When selecting a new i and j like this:
i = UnityEngine.Random.Range(min, max);
j = UnityEngine.Random.Range(min, max);
Instead do this:
i = UnityEngine.Random.Range(min, max - 1);
j = UnityEngine.Random.Range(i + 1, max);
Doing this rules out the possibility of selecting a "mirror image" of a previous case and also avoids the "i == j" case.
After these modifications your choose() function should look something like this:
public void choose()
{
duplicates = new HashSet<string>();
while (rounds < insertNum)
{
key = "(" + itemList[i].ToString() + " " + itemList[j].ToString() + ")";
while (duplicates.Contains(key))
{
i = UnityEngine.Random.Range(0, allNumbers - 2);
j = UnityEngine.Random.Range(i + 1, allNumbers - 1);
key = "(" + itemList[i].ToString() + " " + itemList[j].ToString() + ")";
}
duplicates.Add(key); // the one is just a filler variable
if (buttonOneBool) { //bool definitions are in another script, ignore
finalList[itemList[i].ToString()] = valueList[i] += 2;
finalList[itemList[j].ToString()] = valueList[j] -= 2;
i = UnityEngine.Random.Range(0, n - 1);
j = UnityEngine.Random.Range(0, n - 1);
} else if (buttonTwoBool) {
finalList[itemList[i].ToString()] = valueList[i] -= 2;
finalList[itemList[j].ToString()] = valueList[j] += 2;
i = UnityEngine.Random.Range(0, n - 1);
j = UnityEngine.Random.Range(0, n - 1);
} else if (buttonThreeBool) {
finalList[itemList[i].ToString()] = valueList[i] -= 1;
finalList[itemList[j].ToString()] = valueList[j] -= 1;
i = UnityEngine.Random.Range(0, n - 1);
j = UnityEngine.Random.Range(0, n - 1);
} else if (buttonFourBool) {
finalList[itemList[i].ToString()] = valueList[i] += 1;
finalList[itemList[j].ToString()] = valueList[j] += 1;
i = UnityEngine.Random.Range(0, n - 1);
j = UnityEngine.Random.Range(0, n - 1);
}
break;
}
}
You can get two random indexes to a list using code below. The code uses integer as the list type and will work with any type.
List<int> randomList = itemList.Select((x, i) => new { index = i, rand = rand.Next() }).OrderBy(x => x.rand).Select(x => x.index).ToList();
Then the two random indexes into the list are randomList[0] and randomList[1]. The code assigns a random number to each index i of the list. To get the two items from the list use itemList[randomList[0]] and itemList[randomList[1]]. The code assumes there are at least two items in the original list ItemList[].
If your original list has duplicates than you need to use Distinct() as shown in code below
List<int> distinctList = itemList.Distinct().ToList();
List<int> randomList = distinctList.Select((x, i) => new { index = i, rand = rand.Next() }).OrderBy(x => x.rand).Select(x => x.index).ToList();
Attempting the hackerRank Q from https://www.hackerrank.com/challenges/2d-array
static void Main(String[] args)
{
int[][] arr = new int[6][];
for (int arr_i = 0; arr_i < 6; arr_i++)
{
string[] arr_temp = Console.ReadLine().Split(' ');
arr[arr_i] = Array.ConvertAll(arr_temp, Int32.Parse);
}
int[] sum = new int[6];
List<int> n = new List<int>();
for (int i = 0; i + 2 < arr.Length; i++)
{
for (int j = 0; j + 2 < arr.GetLength(0); j++)
{
sum[j] = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] +
arr[i + 1][j + 1] +
arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
n.Add(sum[j]);
}
}
Console.WriteLine(n.Max());
}
}
}
If I run this program and enter the following as contents of 'arr'
111111
222222
333333
444444
555555
666666
Here I am trying to add 1 + 1 + 1
+ 2 +
3 + 3 + 3
using sum[j] = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] +
arr[i + 1][j + 1] +
arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
but arr[i][j] returns 111111
how can I access 1? is this the right answer to the question on hackerrank?
Hope below code helps you
public class Solution {
private static final int _MAX = 6; // size of matrix
private static final int _OFFSET = 2; // hourglass width
private static int matrix[][] = new int[_MAX][_MAX];
private static int maxHourglass = -63; // initialize to lowest possible sum (-9 x 7)
/** Given a starting index for an hourglass, sets maxHourglass
* #param i row
* #param j column
**/
private static void hourglass(int i, int j){
int tmp = 0; // current hourglass sum
// sum top 3 and bottom 3 elements
for(int k = j; k <= j + _OFFSET; k++){
tmp += matrix[i][k];
tmp += matrix[i + _OFFSET][k];
}
// sum middle element
tmp += matrix[i + 1][j + 1];
if(maxHourglass < tmp){
maxHourglass = tmp;
}
}
public static void main(String[] args) {
// read inputs
Scanner scan = new Scanner(System.in);
for(int i=0; i < _MAX; i++){
for(int j=0; j < _MAX; j++){
matrix[i][j] = scan.nextInt();
}
}
scan.close();
// find maximum hourglass
for(int i=0; i < _MAX - _OFFSET; i++){
for(int j=0; j < _MAX - _OFFSET; j++){
hourglass(i, j);
}
}
// print maximum hourglass
System.out.println(maxHourglass);
}
}
In this while loop I'm examining that if the word that I'm trying to put into the matrix can fit in or not.
It checks if the matrix[i] is empty or white-space, or is the same character as the word[i].
My problem is that the while loop doesn't stop when it comes to a place where the characters don't match or the matrix[i] is not empty. With the Console.WriteLine I wrote out the characters to see what can be the problem.
private bool Crossing(string word, int _row, int _col, int x, int y)
{
int i = 0;
bool l;
bool crossing = ((matrix[(i * x) + _row, (i * y) + _col] == word[i]) || (matrix[(i * x) + _row, (i * y) + _col] == ' '));
while(i < word.Length && crossing)
{
Console.WriteLine(i + ". char in matrix: " + matrix[(i * x) + _row, (i * y) + _col]);
Console.WriteLine("i + ". char in word: " + word[i]);
i++;
}
l = i >= word.Length;
Console.WriteLine(l);
return l;
}
Probably you need to put the setting of the boolean variable crossing inside the loop otherwise the only way to exit from that loop is when i reaches the word length
private bool Crossing(string word, int _row, int _col, int x, int y)
{
int i = 0;
bool l;
bool crossing = true;
while(i < word.Length && crossing)
{
Console.WriteLine(i + ". char in matrix: " + matrix[(i * x) + _row, (i * y) + _col]);
Console.WriteLine("i + ". char in word: " + word[i]);
crossing = ((matrix[(i * x) + _row, (i * y) + _col] == word[i]) || (matrix[(i * x) + _row, (i * y) + _col] == ' '));
i++;
}
l = i >= word.Length;
Console.WriteLine(l);
return l;
}
You are not refreshing crossing variable:
while(i < word.Length && crossing)
{
Console.WriteLine(i + ". char in matrix: " + matrix[(i * x) + _row, (i * y) + _col]);
Console.WriteLine("i + ". char in word: " + word[i]);
i++;
crossing = ((matrix[(i * x) + _row, (i * y) + _col] == word[i]) || (matrix[(i * x) + _row, (i * y) + _col] == ' '));
}
i want to fill the data of the charts from values i have in 2D array, one column will present the X-axis and the second is present the Y-axis.. i did it, but its not reading from the array, it gives me the default line when i run the application, i found a solution using List<>, i had an error, so if any one could help me in this, i will be thankful :D
this is the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ICS381Project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int[,] AndFunction = { { 0, 0, 0 }, { 0, 1, 0 }, { 1, 0, 0 }, { 1, 1, 1 } }; // intialized the function
int[,] D_n = new int[4, 1]; // creating the D(n)
int[,] x_n = new int[4, 3]; // creating X(n) vectors X1 --> x4
int[,] W_n = { { 0, 0, 0 } }; // creating and intiallizing W(n) vectors W1 --> w4
int[,] W_n_1 = { { 0, 0, 0 } };
int[,] Delta_W_n = { { 0, 0, 0 } };
int wx = 0; //
int Y_n = 0; //
int d_y = 0; //
for (int i = 0; i < 4; i++)
{
D_n[i, 0] = AndFunction[i, 2];
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
if (j == 2)
x_n[i, j] = 1;
else
x_n[i, j] = AndFunction[i, j];
}
}
int step = 0;
int CheckIfNoLearning = 0;
int RowsPointer = 0;
while (CheckIfNoLearning < 4)
{
//Console.Write("step= " + step+1 + "\n");
wx = (x_n[RowsPointer, 0] * W_n[0, 0]) + (x_n[RowsPointer, 1] * W_n[0, 1]) + (x_n[RowsPointer, 2] * W_n[0, 2]);
//Console.Write("[ " + x_n[RowsPointer, 0] + ", " + x_n[RowsPointer, 1] + ", " + x_n[RowsPointer, 2] + "] \t");
//Console.Write("[ " + W_n[0, 0] + ", " + W_n[0, 1] + ", " + W_n[0, 2] + "] \t");
//Console.Write("" + wx + "\t");
if (wx < 0)
Y_n = 0;
else
Y_n = 1;
// Console.Write("" + Y_n + "\t");
d_y = D_n[RowsPointer, 0] - Y_n;
// Console.Write("" + d_y + "\t");
Delta_W_n[0, 0] = d_y * x_n[RowsPointer, 0];
Delta_W_n[0, 1] = d_y * x_n[RowsPointer, 1];
Delta_W_n[0, 2] = d_y * x_n[RowsPointer, 2];
// Console.Write("[ " + Delta_W_n[0, 0] + ", " + Delta_W_n[0, 1] + ", " + Delta_W_n[0, 2] + "] \t");
for (int i = 0; i < 3; i++)
{
W_n_1[0, i] = W_n[0, i] + Delta_W_n[0, i];
}
//Console.Write("[ " + W_n_1[0, 0] + ", " + W_n_1[0, 1] + ", " + W_n_1[0, 2] + "] \n");
if (W_n_1[0, 0] == W_n[0, 0] && W_n_1[0, 1] == W_n[0, 1] && W_n_1[0, 2] == W_n[0, 2] && CheckIfNoLearning == RowsPointer)
CheckIfNoLearning++;
else
CheckIfNoLearning = 0;
W_n[0, 0] = W_n_1[0, 0];
W_n[0, 1] = W_n_1[0, 1];
W_n[0, 2] = W_n_1[0, 2];
//Console.Write("W_n= [ " + W_n[0, 0] + ", " + W_n[0, 1] + ", " + W_n[0, 2] + "] \n");
RowsPointer = (RowsPointer + 1) % 4;
step++;
}
double[,] equation = {{-10,0},{-9,0},{-8,0},
{-7,0},{-6,0},{-5,0},
{-4,0},{-3,0},{-2,0},
{-1,0},{0,0},{1,0},
{2,0},{3,0},{4,0},
{5,0},{6,0},{7,0},
{8,0},{9,0},{10,0}};
List<XY> xy = new List<XY>();
for (int i = 0; i < 21; i++)
{
equation[i, 1] = (-1 * (W_n_1[0, 1] * equation[i, 0] + W_n_1[0, 2])) / W_n_1[0, 0];
xy.Add(new XY(equation[i,0], equation[i,1]));
}
chart1.DataSource = xy;
chart1.Series[0].XValueMember = "Y";
chart1.Series[0].YValueMembers = "X";
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
chart1.DataBind();
}
}
public class XY
{
private double X;
private double Y;
public double DayOfWeek
{
get { return X; }
set { X = value; }
}
public double Sales
{
get { return Y; }
set { Y = value; }
}
public XY(double X, double Y)
{
this.X = X;
this.Y = Y;
}
}
}
In your XY class, instead of the private fields:
private double X;
private double Y;
use public properties:
public double X { get; private set; }
public double Y { get; private set; }
DataBinding does not work on fields.