Permalink
Browse files

misc fixes

  • Loading branch information...
1 parent f372583 commit de5cd5c332ea61275a7e6f3560b4cb033a1737a5 @dagss committed Apr 26, 2012
Showing with 51 additions and 15 deletions.
  1. +2 −2 examples/testbed.py
  2. +3 −0 oomatrix/__init__.py
  3. +14 −1 oomatrix/compiler.py
  4. +6 −2 oomatrix/formatter.py
  5. +2 −1 oomatrix/implcore.py
  6. +22 −9 oomatrix/matrix.py
  7. +1 −0 oomatrix/symbolic.py
  8. +1 −0 oomatrix/test/common.py
View
@@ -11,8 +11,8 @@ def ndarray(shape, dtype=np.double):
D = om.Matrix(3 * np.ones(n), 'D', diagonal=True)
u = ndarray((n, 1))
-expr = A.factor() * D * u
+expr = A * D * u
print explain(expr)
-#print compute(expr)
+print compute(expr)
@@ -8,3 +8,6 @@ def compute(x, *args, **kw):
def explain(x, *args, **kw):
return x.explain(*args, **kw)
+
+def compute_array(x, *args, **kw):
+ return x.compute(*args, **kw).as_array()
View
@@ -177,6 +177,16 @@ def all_computations(self, expr, avoid_kinds=(), only_kinds=None,
nrows = expr.nrows
ncols = expr.ncols
dtype = expr.dtype # todo
+ # The no-computation case
+ if isinstance(expr, symbolic.LeafNode):
+ if expr.name == 'D':
+ print 'hi', tried_kinds
+ if (expr.kind not in avoid_kinds and
+ expr.kind not in tried_kinds and
+ (only_kinds is None or expr.kind in only_kinds)):
+ if expr.name == 'D':
+ print 'HI'
+ yield expr
# Generates all computables possible for the match_pattern
key = expr.get_key()
trivial = isinstance(key, MatrixKind)
@@ -189,10 +199,13 @@ def all_computations(self, expr, avoid_kinds=(), only_kinds=None,
if only_kinds is not None and target_kind not in only_kinds:
continue
for computation in computations:
+ if expr.name == 'D':
+ print 'hi', computation.__dict__
matched_key = computation.match
args = expr.as_computable_list(matched_key)
- yield symbolic.ComputableNode(computation, args, nrows,
+ computable = symbolic.ComputableNode(computation, args, nrows,
ncols, dtype, expr)
+ yield computable
def generate_conversions(self, computable, tried_kinds, only_kinds=None):
# Find all possible conversion computables that can be put on top
@@ -144,15 +144,19 @@ def describe_operation(self, result, computation_name, args,
def process_operation(self, node, computation_name):
labels = []
+ expr_str = self.format_expression(node.symbolic_expr)
+ self.putln('Compute {0} [cost={1}]:', expr_str, node.cost)
+ self.indent()
for child in node.children:
labels.append(child.accept_visitor(self, child))
label = self.get_label(node)
self.describe_operation(label,
computation_name,
labels,
node.kind.name,
- node.cost,
+ node.computation_cost,
)
+ self.dedent()
return label
def visit_computable(self, node):
@@ -170,7 +174,7 @@ def visit_conjugate_transpose(self, node):
node.child.accept_visitor(self, node.child)
def visit_decomposition(self, node):
- print node, node.__dict__
+ #print node, node.__dict__
return self.process_operation(node,
node.decomposition.get_name(node.kind))
@@ -1,2 +1,3 @@
from .kind import MatrixImpl, MatrixKind
-from .computation import computation, conversion
+from .computation import (computation, conversion, CostValue,
+ FLOP, MEM, MEMOP, UGLY)
View
@@ -64,6 +64,9 @@ def get_type(self):
"""
return self._expr.get_type()
+ def get_kind(self):
+ return self._expr.get_type()
+
def get_impl(self):
if self.is_expression():
raise ValueError("Matrix not computed")
@@ -108,10 +111,10 @@ def compile(self, compiler=None):
def compute(self, compiler=None):
computable = self.compile(compiler=compiler)
result = Matrix(computable.compute())
- if self.result_type == np.ndarray:
- return result.as_array()
- else:
- return result
+ #if self.result_type == np.ndarray:
+ # return result.as_array()
+ #else:
+ return result
def explain(self, compiler=None):
computable = self.compile(compiler=compiler)
@@ -186,8 +189,14 @@ def __ne__(self, other):
# basic simplifications (elimination of double inverses and transposes)
# here.
#
+ def __radd__(self, other):
+ if other == 0:
+ return self
+ raise NotImplementedError()
def __add__(self, other):
+ if other == 0:
+ return self
if not isinstance(other, Matrix):
raise TypeError('Matrix instance needed') # TODO implement conversions
@@ -200,19 +209,23 @@ def __mul__(self, other):
if isinstance(other, np.ndarray):
other = Matrix(other)
result_type = np.ndarray
- elif not isinstance(other, Matrix):
- # TODO implement some conversion framework for registering vector types
- raise TypeError('Type not recognized')
- else: # Matrix
+ elif isinstance(other, Matrix):
result_type = resolve_result_type(self.result_type,
- other.result_type)
+ other.result_type)
+ elif other == 1:
+ return self
+ else: # Matrix
+ # TODO implement some conversion framework for registering vector types
+ raise TypeError('Type not recognized: %s' % type(other))
if self.ncols != other.nrows:
raise ValueError('Matrices do not conform: ...-by-%d times %d-by-...' % (
self.ncols, other.nrows))
return Matrix(symbolic.MultiplyNode([self._expr, other._expr]),
result_type=result_type)
def __rmul__(self, other):
+ if other == 1:
+ return self
raise NotImplementedError()
# TODO: Made tricky by not wanting to conjugate a complex result
@@ -424,6 +424,7 @@ def __init__(self, computation, children,
self.ncols = ncols
self.dtype = dtype
self.symbolic_expr = symbolic_expr
+ self.precedence = symbolic_expr.precedence
if computation.cost is None:
raise AssertionError('%s has no cost set' % computation.name)
@@ -3,6 +3,7 @@
from nose import SkipTest
from textwrap import dedent
import contextlib
+import os, sys, re
def assert_not_raises(func, *args):
"Turn any exception into AssertionError"

0 comments on commit de5cd5c

Please sign in to comment.