|
[PyX-checkins] pyx/test/experimental solve.py,1.3,1.4
From: André Wobst <wobsta@us...> - 2004-07-29 10:25
|
Update of /cvsroot/pyx/pyx/test/experimental
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8503
Modified Files:
solve.py
Log Message:
remove '==' missuse
Index: solve.py
===================================================================
RCS file: /cvsroot/pyx/pyx/test/experimental/solve.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** solve.py 28 Jul 2004 16:52:26 -0000 1.3
--- solve.py 29 Jul 2004 10:25:10 -0000 1.4
***************
*** 32,67 ****
def __init__(self, varname="(no variable name provided)"):
- self.id = id(self) # compare the id to check for the same variable
- # (the __eq__ method is used to define "equalities")
self.varname = varname
self.value = None
def term(self):
! return term([1], [self], 0)
def __add__(self, other):
! return term([1], [self], 0) + other
__radd__ = __add__
def __neg__(self):
! return term([-1], [self], 0)
def __sub__(self, other):
! return term([1], [self], 0) - other
def __rsub__(self, other):
! return term([-1], [self], 0) + other
def __mul__(self, other):
! return term([other], [self], 0)
__rmul__ = __mul__
def __div__(self, other):
! return term([1/other], [self], 0)
!
! def __eq__(self, other):
! return term([1], [self], 0) == other
def is_set(self):
--- 32,63 ----
def __init__(self, varname="(no variable name provided)"):
self.varname = varname
self.value = None
+ self.zero = 0
def term(self):
! return term([1], [self], self.zero)
def __add__(self, other):
! return term([1], [self], self.zero) + other
__radd__ = __add__
def __neg__(self):
! return term([-1], [self], self.zero)
def __sub__(self, other):
! return term([1], [self], self.zero) - other
def __rsub__(self, other):
! return term([-1], [self], self.zero) + other
def __mul__(self, other):
! return term([other], [self], self.zero)
__rmul__ = __mul__
def __div__(self, other):
! return term([1/other], [self], self.zero)
def is_set(self):
***************
*** 92,97 ****
def __init__(self, dimension, varname="(no variable name provided)"):
scalar.__init__(self, varname=varname)
! del self.value # disallow is_set, set, get
self.scalars = [scalar(varname="%s%i" % (varname, i)) for i in range(dimension)]
def __getitem__(self, i):
--- 88,97 ----
def __init__(self, dimension, varname="(no variable name provided)"):
scalar.__init__(self, varname=varname)
! del self.value # XXX disallow is_set, set, get
self.scalars = [scalar(varname="%s%i" % (varname, i)) for i in range(dimension)]
+ self.zero = point(*([0]*len(self.scalars)))
+
+ def __len__(self):
+ return len(self.scalars)
def __getitem__(self, i):
***************
*** 110,129 ****
return term([], [], self)
def __getitem__(self, i):
return self.values[i]
def __add__(self, other):
! if other is 0: # XXX: is 0 ?!
! # the default constant in a term is zero, but it should be interpreted as a point(0, ...) as well
! return self
! else:
! try:
! # other might be a point and we should return a point
! # (this is the typical case for term.const in a vector equation)
! return point(*[x + y for x, y in zip(self.values, other.values)])
! except AttributeError:
! # otherwise its likely, that the other item is a term and we
! # should return a term
! return self.term() + other
__radd__ = __add__
--- 110,127 ----
return term([], [], self)
+ def __len__(self):
+ return len(self.values)
+
def __getitem__(self, i):
return self.values[i]
def __add__(self, other):
! try:
! # other might be a point and we should return a point
! # (this is the typical case for term.const in a vector equation)
! return point(*[x + y for x, y in zip(self.values, other.values)])
! except AttributeError:
! # otherwise perform a term-like addition
! return self.term() + other
__radd__ = __add__
***************
*** 149,158 ****
class term:
# this class represents the linear term:
! # sum([p*v.value for p, v in zip(self.prefactors, self.vars]) + self.const
def __init__(self, prefactors, vars, const):
assert len(prefactors) == len(vars)
! self.id = id(self) # compare the id to check for the same term
! # (the __eq__ method is used to define "equalities")
self.prefactors = prefactors
self.vars = vars
--- 147,172 ----
class term:
# this class represents the linear term:
! # sum([prefactor*var.value for prefactor, var in zip(self.prefactors, self.vars]) + self.const
def __init__(self, prefactors, vars, const):
+ try:
+ l = len(const)
+ except (AttributeError, TypeError):
+ for var in vars:
+ try:
+ len(var)
+ except (AttributeError, TypeError):
+ pass
+ else:
+ raise RuntimeError("vector variable found in a non-vector term")
+ else:
+ for var in vars:
+ try:
+ if l != len(var):
+ raise RuntimeError("vector length mismatch in vector term")
+ except (AttributeError, TypeError):
+ raise RuntimeError("scalar variable found in a vector term")
assert len(prefactors) == len(vars)
!
self.prefactors = prefactors
self.vars = vars
***************
*** 169,181 ****
vars = self.vars[:]
prefactors = self.prefactors[:]
! vids = [v.id for v in vars] # already existing variable ids
! for p, v in zip(other.prefactors, other.vars):
try:
# try to modify prefactor for existing variables
! prefactors[vids.index(v.id)] += p
except ValueError:
# or add the variable
! vars.append(v)
! prefactors.append(p)
return term(prefactors, vars, self.const + other.const)
--- 183,194 ----
vars = self.vars[:]
prefactors = self.prefactors[:]
! for prefactor, var in zip(other.prefactors, other.vars):
try:
# try to modify prefactor for existing variables
! prefactors[vars.index(var)] += prefactor
except ValueError:
# or add the variable
! vars.append(var)
! prefactors.append(prefactor)
return term(prefactors, vars, self.const + other.const)
***************
*** 183,187 ****
def __neg__(self):
! return term([-p for p in self.prefactors], self.vars, -self.const)
def __sub__(self, other):
--- 196,200 ----
def __neg__(self):
! return term([-prefactor for prefactor in self.prefactors], self.vars, -self.const)
def __sub__(self, other):
***************
*** 192,204 ****
def __mul__(self, other):
! return term([p*other for p in self.prefactors], self.vars, self.const*other)
__rmul__ = __mul__
def __div__(self, other):
! return term([p/other for p in self.prefactors], self.vars, self.const/other)
! def __eq__(self, other):
! eq = self - other
if not len(eq.vars):
raise RuntimeError("equation without variables")
--- 205,230 ----
def __mul__(self, other):
! return term([other*prefactor for prefactor in self.prefactors], self.vars, other*self.const)
__rmul__ = __mul__
def __div__(self, other):
! return term([prefactor/other for prefactor in self.prefactors], self.vars, self.const/other)
! def __str__(self):
! return "+".join(["%s*%s" % pv for pv in zip(self.prefactors, self.vars)]) + "+" + str(self.const)
!
!
! class Solver:
! # linear equation solver
!
! def __init__(self):
! self.eqs = [] # equations still to be taken into account
!
! def eq(self, lhs, rhs=None):
! if rhs is None:
! eq = lhs
! else:
! eq = lhs - rhs
if not len(eq.vars):
raise RuntimeError("equation without variables")
***************
*** 219,238 ****
if len(v.scalars) != neqs:
raise RuntimeError("vectors of different dimension")
! if eq.const is 0: # XXX: is 0 ?!
! eq.const = point(*([0]*neqs))
! elif len(eq.const.values) != neqs:
raise RuntimeError("wrong dimension of constant")
for i in range(neqs):
! solver.add(term(eq.prefactors, [var[i] for var in eq.vars], eq.const[i]))
!
! def __str__(self):
! return "+".join(["%s*%s" % pv for pv in zip(self.prefactors, self.vars)]) + "+" + str(self.const)
!
!
! class Solver:
! # linear equation solver
!
! def __init__(self):
! self.eqs = [] # equations still to be taken into account
def add(self, equation):
--- 245,252 ----
if len(v.scalars) != neqs:
raise RuntimeError("vectors of different dimension")
! if len(eq.const.values) != neqs:
raise RuntimeError("wrong dimension of constant")
for i in range(neqs):
! self.add(term(eq.prefactors, [var[i] for var in eq.vars], eq.const[i]))
def add(self, equation):
***************
*** 261,274 ****
l = len(eqs)
if l:
! vids = []
for eq in eqs:
! vids.extend([v.id for v in eq.vars if v.id not in vids and not v.is_set()])
! if len(vids) == l:
a = Numeric.zeros((l, l))
b = Numeric.zeros((l, ))
- index = {}
- for i, vid in enumerate(vids):
- index[vid] = i
- vars = {}
for i, eq in enumerate(eqs):
for p, v in zip(eq.prefactors, eq.vars):
--- 275,284 ----
l = len(eqs)
if l:
! vars = []
for eq in eqs:
! vars.extend([var for var in eq.vars if var not in vars and not var.is_set()])
! if len(vars) == l:
a = Numeric.zeros((l, l))
b = Numeric.zeros((l, ))
for i, eq in enumerate(eqs):
for p, v in zip(eq.prefactors, eq.vars):
***************
*** 276,289 ****
b[i] -= p*v.value
else:
! a[i, index[v.id]] += p
! vars[index[v.id]] = v
b[i] -= eq.const
for i, value in enumerate(LinearAlgebra.solve_linear_equations(a, b)):
vars[i].value = value
for eq in eqs:
! i, = [i for i, selfeq in enumerate(self.eqs) if selfeq.id == eq.id]
del self.eqs[i]
return 1
! elif len(vids) < l:
raise RuntimeError("equations are overdetermined")
return 0
--- 286,298 ----
b[i] -= p*v.value
else:
! a[i, vars.index(v)] += p
b[i] -= eq.const
for i, value in enumerate(LinearAlgebra.solve_linear_equations(a, b)):
vars[i].value = value
for eq in eqs:
! i, = [i for i, selfeq in enumerate(self.eqs) if selfeq == eq]
del self.eqs[i]
return 1
! elif len(vars) < l:
raise RuntimeError("equations are overdetermined")
return 0
***************
*** 298,305 ****
z = vector(2, "z")
! 4*x + y == 2*x - y + point(4, 0) # => x + y = (2, 0)
! x[0] - y[0] == z[1]
! x[1] - y[1] == z[0]
! point(5, 0) == z
print x, y, z
--- 307,314 ----
z = vector(2, "z")
! solver.eq(4*x + y, 2*x - y + point(4, 0)) # => x + y = (2, 0)
! solver.eq(x[0] - y[0], z[1])
! solver.eq(x[1] - y[1], z[0])
! solver.eq(point(5, 0), z)
print x, y, z
|
| Thread | Author | Date |
|---|---|---|
| [PyX-checkins] pyx/test/experimental solve.py,1.3,1.4 | André Wobst <wobsta@us...> |