patternpythonMinor
python: less ugly solution to the intersection of 3 lines in 3 dimensional space
Viewed 0 times
uglythespacelesspythonintersectiondimensionalsolutionlines
Problem
I've tagged this as homework because it was originally a school project, it was accepted but I'm not satisfied with the code.
I've used cramers rule to find the intersection of 3 lines in three dimensional space given the
```
def trinomial():
print "Trinomials require 3 equations with three variables each."
firstX = input ("Please enter the coeficcient of X in the first equation: ")
firstY = input ("Please enter the coefficient of Y in the first equation: ")
firstZ = input ("Please enter the coefficient of Z in the first equation: ")
firstA = input ("and what's after the = sign?")
secondX = input ("Please enter the coefficient of X in the second equation: ")
secondY = input ("Please enter the coefficient of Y in the second equation: ")
secondZ = input ("Please enter the coefficient of Z in the second equation: ")
secondA = input ("And what's after the = sign?")
thirdX = input ("Please enter the coefficient of X in the third equation: ")
thirdY = input ("Please enter the coefficient of Y in the third equation: ")
thirdZ = input ("Please enter the coefficient of Z in the third equation: ")
thirdA = input ("And what's after the = sign?")
D= firstXsecondYthirdZ+firstYsecondZthirdX+firstZsecondXthirdY-thirdXsecondYfirstZ- thirdYsecondZfirstX-thirdZsecondXfirstY
DX = firstAsecondYthirdZ+firstYsecondZthirdA+firstZsecondAthirdY-thirdAsecondYfirstZ-thirdYsecondZfirstA-thirdZsecondAfirstY
DY = firstXsecondAthirdZ+firstAsecondZthirdX+firstZsecondXthirdA-thirdXsecondAfirstZ-thirdAsecondZfirstX-thirdZsecondXfirstA
DZ = firstXsecondYthirdA+firstYsecondAthirdX+firstAsecondXthirdY-thirdXsecondYfirstA-thirdYsecondAfirstX-thirdAsecondXfirstY
#the next two if statements evaluate the D's, or determinants, to see if it's an inconsistant or identical systerm
I've used cramers rule to find the intersection of 3 lines in three dimensional space given the
x, y and z coefficients but the code is hideous.```
def trinomial():
print "Trinomials require 3 equations with three variables each."
firstX = input ("Please enter the coeficcient of X in the first equation: ")
firstY = input ("Please enter the coefficient of Y in the first equation: ")
firstZ = input ("Please enter the coefficient of Z in the first equation: ")
firstA = input ("and what's after the = sign?")
secondX = input ("Please enter the coefficient of X in the second equation: ")
secondY = input ("Please enter the coefficient of Y in the second equation: ")
secondZ = input ("Please enter the coefficient of Z in the second equation: ")
secondA = input ("And what's after the = sign?")
thirdX = input ("Please enter the coefficient of X in the third equation: ")
thirdY = input ("Please enter the coefficient of Y in the third equation: ")
thirdZ = input ("Please enter the coefficient of Z in the third equation: ")
thirdA = input ("And what's after the = sign?")
D= firstXsecondYthirdZ+firstYsecondZthirdX+firstZsecondXthirdY-thirdXsecondYfirstZ- thirdYsecondZfirstX-thirdZsecondXfirstY
DX = firstAsecondYthirdZ+firstYsecondZthirdA+firstZsecondAthirdY-thirdAsecondYfirstZ-thirdYsecondZfirstA-thirdZsecondAfirstY
DY = firstXsecondAthirdZ+firstAsecondZthirdX+firstZsecondXthirdA-thirdXsecondAfirstZ-thirdAsecondZfirstX-thirdZsecondXfirstA
DZ = firstXsecondYthirdA+firstYsecondAthirdX+firstAsecondXthirdY-thirdXsecondYfirstA-thirdYsecondAfirstX-thirdAsecondXfirstY
#the next two if statements evaluate the D's, or determinants, to see if it's an inconsistant or identical systerm
Solution
- Instead of having variables firstX, secondX, thirdX, replace them with a list X
- Don't put input and calculations in the same function, separate them
-
You do:
if D == 0:
some stuff
if D != 0:
other stuff
use else instead
- Your three lines calculating D, DX, DY, DZ are very similar. Make it a function.
- The terms being subtractd parallel the ones being added. Extract the pair into a function.
Here is my result:
DEPENDENT = 0
INCONSISTENT = 1
GOOD = 2
def determinant(X, Y, Z):
def term(x, y, z):
return X[x]*Y[y]*Z[z] - X[z]*Y[y]*Z[z]
return term(0, 1, 2) + term(2, 0, 1) + term(1,2,0)
def trinomial(X, Y, Z, A):
D = determinant(X, Y, Z)
DX = determinant(A, Y, Z)
DY = determinant(X, A, Z)
DZ = determinant(X, Y, A)
#the next two if statements evaluate the D's, or determinants, to see if it's an inconsistant or identical systerm
#if it's not, then it does the math to solve the equation
if D == 0:
if DX == 0 and DY == 0 and DZ == 0:
return DEPENDENT, None
else:
return INCONSISTENT, None
else:
return GOOD, [DX/D, DY/D, DZ/D]
def main():
print "Trinomials require 3 equations with three variables each."
X.append( input ("Please enter the coeficcient of X in the first equation: ") )
Y.append( input ("Please enter the coefficient of Y in the first equation: ") )
Z.append( input ("Please enter the coefficient of Z in the first equation: ") )
A.append( = input ("and what's after the = sign?") )
X.append( input ("Please enter the coeficcient of X in the second equation: ") )
Y.append( input ("Please enter the coefficient of Y in the second equation: ") )
Z.append( input ("Please enter the coefficient of Z in the second equation: ") )
A.append( = input ("and what's after the = sign?") )
X.append( input ("Please enter the coeficcient of X in the third equation: ") )
Y.append( input ("Please enter the coefficient of Y in the third equation: ") )
Z.append( input ("Please enter the coefficient of Z in the third equation: ") )
A.append( = input ("and what's after the = sign?") )
result, answer = trinomial(X, Y, Z, A)
if result == DEPENDENT:
print "System is dependant, there are infinite solutions"
print
elif result == INCONSISTENT:
print "System is inconsistant, there are no solutions"
print
else:
print "the solution to the set of equations is ("+ ",".join(map(str, answer)) + ")."
printBut if you do any amount of python programming with this stuff you'll want to learn how to use numpy, which make your code:
def trinomial(X, Y, Z, A):
system = numpy.array([X, Y, Z]).T
unknowns = numpy.array([A])
return numpy.linalg.solve(system, unknowns)Code Snippets
DEPENDENT = 0
INCONSISTENT = 1
GOOD = 2
def determinant(X, Y, Z):
def term(x, y, z):
return X[x]*Y[y]*Z[z] - X[z]*Y[y]*Z[z]
return term(0, 1, 2) + term(2, 0, 1) + term(1,2,0)
def trinomial(X, Y, Z, A):
D = determinant(X, Y, Z)
DX = determinant(A, Y, Z)
DY = determinant(X, A, Z)
DZ = determinant(X, Y, A)
#the next two if statements evaluate the D's, or determinants, to see if it's an inconsistant or identical systerm
#if it's not, then it does the math to solve the equation
if D == 0:
if DX == 0 and DY == 0 and DZ == 0:
return DEPENDENT, None
else:
return INCONSISTENT, None
else:
return GOOD, [DX/D, DY/D, DZ/D]
def main():
print "Trinomials require 3 equations with three variables each."
X.append( input ("Please enter the coeficcient of X in the first equation: ") )
Y.append( input ("Please enter the coefficient of Y in the first equation: ") )
Z.append( input ("Please enter the coefficient of Z in the first equation: ") )
A.append( = input ("and what's after the = sign?") )
X.append( input ("Please enter the coeficcient of X in the second equation: ") )
Y.append( input ("Please enter the coefficient of Y in the second equation: ") )
Z.append( input ("Please enter the coefficient of Z in the second equation: ") )
A.append( = input ("and what's after the = sign?") )
X.append( input ("Please enter the coeficcient of X in the third equation: ") )
Y.append( input ("Please enter the coefficient of Y in the third equation: ") )
Z.append( input ("Please enter the coefficient of Z in the third equation: ") )
A.append( = input ("and what's after the = sign?") )
result, answer = trinomial(X, Y, Z, A)
if result == DEPENDENT:
print "System is dependant, there are infinite solutions"
print
elif result == INCONSISTENT:
print "System is inconsistant, there are no solutions"
print
else:
print "the solution to the set of equations is ("+ ",".join(map(str, answer)) + ")."
printdef trinomial(X, Y, Z, A):
system = numpy.array([X, Y, Z]).T
unknowns = numpy.array([A])
return numpy.linalg.solve(system, unknowns)Context
StackExchange Code Review Q#2895, answer score: 4
Revisions (0)
No revisions yet.