Last active
December 24, 2020 10:24
-
-
Save palapapa/77140d7dbb54461d576899fd950b1a9b to your computer and use it in GitHub Desktop.
Solve linear equation with three variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
class ThreeVariableLinearEquation | |
{ | |
public float ACoefficient; | |
public float BCoefficient; | |
public float CCoefficient; | |
public float Answer; | |
public ThreeVariableLinearEquation(float a, float b, float c, float answer) | |
{ | |
ACoefficient = a; | |
BCoefficient = b; | |
CCoefficient = c; | |
Answer = answer; | |
} | |
} | |
class Matrix3x3 | |
{ | |
public float A {get; set;} | |
public float B {get; set;} | |
public float C {get; set;} | |
public float D {get; set;} | |
public float E {get; set;} | |
public float F {get; set;} | |
public float G {get; set;} | |
public float H {get; set;} | |
public float I {get; set;} | |
public Matrix3x3 | |
( | |
float a, | |
float b, | |
float c, | |
float d, | |
float e, | |
float f, | |
float g, | |
float h, | |
float i | |
) | |
{ | |
A = a; | |
B = b; | |
C = c; | |
D = d; | |
E = e; | |
F = f; | |
G = g; | |
H = h; | |
I = i; | |
} | |
public float CalculateMatrix() | |
{ | |
return A * E * I + | |
B * F * G + | |
C * D * H - | |
C * E * G - | |
B * D * I - | |
A * F * H; | |
} | |
} | |
class MainClass | |
{ | |
private static List<float> SolveThreeVariableLinearEquation(ThreeVariableLinearEquation A, ThreeVariableLinearEquation B, ThreeVariableLinearEquation C) | |
{ | |
float delta = new Matrix3x3 | |
( | |
A.ACoefficient, A.BCoefficient, A.CCoefficient, | |
B.ACoefficient, B.BCoefficient, B.CCoefficient, | |
C.ACoefficient, C.BCoefficient, C.CCoefficient | |
).CalculateMatrix(); | |
float deltaX = new Matrix3x3 | |
( | |
A.Answer, A.BCoefficient, A.CCoefficient, | |
B.Answer, B.BCoefficient, B.CCoefficient, | |
C.Answer, C.BCoefficient, C.CCoefficient | |
).CalculateMatrix(); | |
float deltaY = new Matrix3x3 | |
( | |
A.ACoefficient, A.Answer, A.CCoefficient, | |
B.ACoefficient, B.Answer, B.CCoefficient, | |
C.ACoefficient, C.Answer, C.CCoefficient | |
).CalculateMatrix(); | |
float deltaZ = new Matrix3x3 | |
( | |
A.ACoefficient, A.BCoefficient, A.Answer, | |
B.ACoefficient, B.BCoefficient, B.Answer, | |
C.ACoefficient, C.BCoefficient, C.Answer | |
).CalculateMatrix(); | |
List<float> result = new List<float>(); | |
result.Add(deltaX / delta); | |
result.Add(deltaY / delta); | |
result.Add(deltaZ / delta); | |
return result; | |
} | |
public static void Main (string[] args) | |
{ | |
ThreeVariableLinearEquation A = new ThreeVariableLinearEquation(8, 1, -7, 15); | |
ThreeVariableLinearEquation B = new ThreeVariableLinearEquation(8, 0, 3, 30); | |
ThreeVariableLinearEquation C = new ThreeVariableLinearEquation(4, 3, 0, 27); | |
List<float> abc = SolveThreeVariableLinearEquation(A, B ,C); | |
Console.WriteLine($"{abc[0]}, {abc[1]}, {abc[2]}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment