Changeset 719
- Timestamp:
- 02/23/10 14:45:49 (5 months ago)
- Files:
-
- 1 modified
-
hodgestar/PythonCode/PyramidProblem/pyramid.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
hodgestar/PythonCode/PyramidProblem/pyramid.py
r718 r719 8 8 self.initial = initial 9 9 self.solution = None 10 self.residual = None 10 11 11 12 def _loc(self, row, col): … … 31 32 n_constraints = n_tri_constraints + n_init_contraints 32 33 33 constraints = np.zeros((n_ unknowns, n_unknowns))34 rhs = np.zeros((n_ unknowns, 1))34 constraints = np.zeros((n_constraints, n_unknowns)) 35 rhs = np.zeros((n_constraints, 1)) 35 36 constraint = 0 36 37 loc = self._loc … … 52 53 rhs[constraint] = val 53 54 constraint += 1 54 # just skip over-constrainted stuff55 if constraint >= n_unknowns:56 break57 55 58 56 # phew! 59 57 #print constraints 60 58 #print rhs 61 self.solution = lg.solve(constraints, rhs) 59 self.solution, self.residuals = lg.lstsq(constraints, rhs)[:2] 60 #print self.solution 62 61 63 62 def __str__(self): … … 72 71 acol = [] 73 72 for col in range(1, row+1): 74 acol.append("%5d " % self.solution[self._loc(row, col)])73 acol.append("%5d" % int(np.round(self.solution[self._loc(row, col)]))) 75 74 s.append(" " + "".join(acol)) 76 75 … … 86 85 if good: 87 86 s.append(" No constraints violated.") 87 s.append(" Residual: %f" % np.sum(self.residuals)) 88 88 89 89 s.append(" -----") … … 101 101 if len(sys.argv) < 3: 102 102 print "Usage: pyramid.py <height> <inital value> ..." 103 print "e.g. pyramid.py 6 1,1=234 4,2=2 8 5,2=28 5,4=56 6,1=836,4=-69 6,5=125"103 print "e.g. pyramid.py 6 1,1=234 4,2=29 5,2=29 5,4=56 6,1=79 6,4=-69 6,5=125" 104 104 print "e.g. pyramid.py 2 1,1=2 2,1=1" 105 105 sys.exit(1)
