示例#1
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (Validate())
     {
         try {
             var system = new LinearSystem(MatrixA, VectorB);
             VectorX = system.XVector;
         } catch (Exception error) {
             MessageBox.Show(error.Message);
         }
     }
 }
示例#2
0
    void Save()
    {
        StreamWriter dataWriter = new StreamWriter(playerDataPath);

        for (int i = 0; i < 14; i++)
        {
            for (int j = 0; j < 11; j++)
            {
                for (int j2 = 0; j2 < 9; j2++)
                {
                    dataWriter.Write(matrix[i, j, j2] + " ");
                }
                dataWriter.WriteLine();
            }
        }
        {
            for (int i = 0; i < 14; i++)
            {
                for (int j = 0; j < 11; j++)
                {
                    dataWriter.Write(boardColor[i, j] + " ");
                }
                dataWriter.WriteLine();
            }
            Gauss.ComplexDouble allResist;

            for (int currentcolor = 0; currentcolor < countColor; currentcolor++)
            {
                allResist = (Gauss.ComplexDouble) 0.0;
                for (int i = 0; i < 14; i++)
                {
                    for (int j = 0; j < 11; j++)
                    {
                        if (boardColor[i, j] < 0 && matrix[i, j, 2] != 3 && matrix[i, j, 2] != 2 && matrix[i, j, 2] != 1)
                        {
                            if (resistorsMatrixArr[i, j].firstUzel == colorArray[currentcolor])
                            {
                                a_matrix[currentcolor, ColorInArray(resistorsMatrixArr[i, j].secondUzel)] += (Gauss.ComplexDouble)(1 / resistorsMatrixArr[i, j].resistorNum);
                                allResist += (Gauss.ComplexDouble)(1 / resistorsMatrixArr[i, j].resistorNum);
                            }
                            if (resistorsMatrixArr[i, j].secondUzel == colorArray[currentcolor])
                            {
                                a_matrix[currentcolor, ColorInArray(resistorsMatrixArr[i, j].firstUzel)] += (Gauss.ComplexDouble)(1 / resistorsMatrixArr[i, j].resistorNum);
                                allResist += (Gauss.ComplexDouble)(1 / resistorsMatrixArr[i, j].resistorNum);
                            }
                        }
                    }
                }
                a_matrix[currentcolor, currentcolor] = (Gauss.ComplexDouble)(-allResist);
            }
            for (int i = 0; i < countColor; i++)
            {
                a_matrix[ColorInArray(resistorsMatrixArr[xPosEds, yPosEds].firstUzel), i]  = (Gauss.ComplexDouble) 0.0;
                a_matrix[ColorInArray(resistorsMatrixArr[xPosEds, yPosEds].secondUzel), i] = (Gauss.ComplexDouble) 0.0;
            }
            a_matrix[ColorInArray(resistorsMatrixArr[xPosEds, yPosEds].firstUzel), ColorInArray(resistorsMatrixArr[xPosEds, yPosEds].firstUzel)]   = (Gauss.ComplexDouble) 1.0;
            a_matrix[ColorInArray(resistorsMatrixArr[xPosEds, yPosEds].secondUzel), ColorInArray(resistorsMatrixArr[xPosEds, yPosEds].secondUzel)] = (Gauss.ComplexDouble) 1.0;
            b_vector[ColorInArray(resistorsMatrixArr[xPosEds, yPosEds].secondUzel)] = (Gauss.ComplexDouble)resistorsMatrixArr[xPosEds, yPosEds].resistorNum;

            Gauss.LinearSystem U = new Gauss.LinearSystem(a_matrix, b_vector, 0.000000001);

            Gauss.ComplexDouble[] result1 = U.XVector;


            for (int i = 0; i < countColor; i++)
            {
                result[i] = result1[i].Abs;
                dataWriter.Write(result[i] + " ");
                if (result[i] > maxPotential)
                {
                    maxPotential = result[i];
                }
            }
            dataWriter.WriteLine();
            for (int i = 0; i < 14; i++)
            {
                for (int j = 0; j < 11; j++)
                {
                    if (ColorInArray(boardColor[i, j]) != 50)
                    {
                        answerMatrix[i, j] = result[ColorInArray(boardColor[i, j])];
                        if (answerMatrix[i, j] != 0 && maxPotential != 0)
                        {
                            potentialSprite.transform.localScale = new Vector3(1f, (float)(answerMatrix[i, j] / maxPotential), 1f);
                            pictureArray[pictureCount]           = Instantiate(potentialSprite, new Vector3(i - 0.5f, j - 0.5f, 0f), Quaternion.Euler(0, 0, 0));
                            pictureCount++;
                            potentialSprite.transform.localScale = new Vector3(1f, (float)(maxPotential / answerMatrix[i, j]), 1f);
                        }
                        else
                        {
                            potentialSprite.transform.localScale = new Vector3(1f, (float)(0.1), 1f);
                            pictureArray[pictureCount]           = Instantiate(potentialSprite, new Vector3(i - 0.5f, j - 0.5f, 0f), Quaternion.Euler(0, 0, 0));
                            pictureCount++;
                            potentialSprite.transform.localScale = new Vector3(1f, (float)(10), 1f);
                        }


                        dataWriter.Write(result[ColorInArray(boardColor[i, j])] + " ");
                    }
                    else
                    {
                        answerMatrix[i, j] = -1;
                        dataWriter.Write(-1 + " ");
                    }
                }
                dataWriter.WriteLine();
            }

            for (int x = 0; x < 14; x++)
            {
                for (int y = 0; y < 11; y++)
                {
                    if (matrix[x, y, 2] == 6)
                    {
                        if ((matrix[x, y, 3] == 1f && matrix[x, y, 6] == 0f) || (matrix[x, y, 3] == 0f && matrix[x, y, 6] == 1f))
                        {
                            if (answerMatrix[x - 1, y] < answerMatrix[x + 1, y])
                            {
                                answerMatrix[x, y]         = Math.Abs(answerMatrix[x - 1, y] - answerMatrix[x + 1, y]) / matrix[x, y, 7];
                                pictureArray[pictureCount] = Instantiate(arrowLeft, new Vector3(x, y, 0f), Quaternion.Euler(0, 0, 0));
                                pictureCount++;
                            }
                            else
                            if (answerMatrix[x - 1, y] > answerMatrix[x + 1, y])
                            {
                                answerMatrix[x, y]         = Math.Abs(answerMatrix[x - 1, y] - answerMatrix[x + 1, y]) / matrix[x, y, 7];
                                pictureArray[pictureCount] = Instantiate(arrowRight, new Vector3(x, y, 0f), Quaternion.Euler(0, 0, 0));
                                pictureCount++;
                            }
                        }
                        if ((matrix[x, y, 3] == 0.7071068f && matrix[x, y, 6] == 0.7071068f) || (matrix[x, y, 3] == -0.7071068f && matrix[x, y, 6] == 0.7071068f))
                        {
                            if (answerMatrix[x, y - 1] < answerMatrix[x, y + 1])
                            {
                                answerMatrix[x, y]         = Math.Abs(answerMatrix[x, y - 1] - answerMatrix[x, y + 1]) / matrix[x, y, 7];
                                pictureArray[pictureCount] = Instantiate(arrowDown, new Vector3(x, y, 0f), Quaternion.Euler(0, 0, 0));
                                pictureCount++;
                            }
                            else
                            if (answerMatrix[x, y - 1] > answerMatrix[x, y + 1])
                            {
                                pictureArray[pictureCount] = Instantiate(arrowUp, new Vector3(x, y, 0f), Quaternion.Euler(0, 0, 0));
                                pictureCount++;
                                answerMatrix[x, y] = Math.Abs(answerMatrix[x, y - 1] - answerMatrix[x, y + 1]) / matrix[x, y, 7];
                            }
                        }
                    }
                }
            }

            for (int x = 0; x < 14; x++)
            {
                for (int y = 0; y < 11; y++)
                {
                    if (matrix[x, y, 2] == 3 && ((matrix[x, y, 3] == 1f && matrix[x, y, 6] == 0f) || (matrix[x, y, 3] == 0f && matrix[x, y, 6] == 1f)) && answerMatrix[x + 1, y] == answerMatrix[x - 1, y])
                    {
                        if (matrix[x + 1, y, 2] != -1 && matrix[x - 1, y, 2] != -1)
                        {
                            shortCircuit = true;
                        }
                    }
                    else
                    if (matrix[x, y, 2] == 3 && ((matrix[x, y, 3] == 0.7071068f && matrix[x, y, 6] == 0.7071068f) || (matrix[x, y, 3] == -0.7071068f && matrix[x, y, 6] == 0.7071068f)) && answerMatrix[x, y + 1] == answerMatrix[x, y - 1])
                    {
                        if (matrix[x, y + 1, 2] != -1 || matrix[x, y - 1, 2] != -1)
                        {
                            shortCircuit = true;
                        }
                    }
                    else
                    if (matrix[x, y, 2] == 1 || matrix[x, y, 2] == 3)
                    {
                        amperage = 0;
                        Initialized();
                        Amperage(x, y);
                        answerMatrix[x, y] = amperage;
                    }
                }
            }

            for (int i = 0; i < countColor; i++)
            {
                for (int j = 0; j < countColor; j++)
                {
                    dataWriter.Write(a_matrix[i, j] + " ");
                }
                dataWriter.WriteLine();
            }


            /*for (int i = 0; i < countColor; i++)
             * {
             *  for (int j = 0; j < countColor; j++)
             *  {
             *      dataWriter.Write(a_matrix[i,j]+ " ");
             *  }
             *  dataWriter.WriteLine(b_vector[i]);
             * }
             *      for (int i = 0; i < 14; i++)
             *      {
             *          for (int j = 0; j < 11; j++)
             *          {
             *              if (boardColor[i,j] < 0)
             *              {
             *                  dataWriter.WriteLine(resistorsMatrixArr[i,j].firstUzel + " " + resistorsMatrixArr[i, j].secondUzel + " " + i + " " + j + " " + resistorsMatrixArr[i,j].resistorNum);
             *              }
             *          }
             *      }*/

            dataWriter.Flush();
            dataWriter.Close();
        }
    }
        static void Main()
        {
            double[,] matrix = {
                {-3, 4, 1, 4},
                {0, 1, 3, 2},
                {4, 0, -2, -3},
                {1000, 3, 1, -5}
                               }; // Коефіцієнти системи рівнянь
            double[] b = { -1, -1, 4, -2 }; // Права частина системи рівнень.

            try
            {
                LinearSystem ls = new LinearSystem(matrix, b, 0.0001);
                Console.WriteLine("Вектор розв'язку Х: ");
                Console.WriteLine(String.Join("\n", ls.XVector));
                Console.WriteLine("\n");
                Console.WriteLine("Вектор нев'язок: ");
                Console.WriteLine(String.Join("\n", ls.UVector));
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }