From 38ba1a610eb4d77b1a727bcafe3155db930b6b40 Mon Sep 17 00:00:00 2001 From: Holger Kohr Date: Thu, 26 Nov 2015 15:13:36 +0100 Subject: [PATCH] ENH: optimize usage of tmps in matrix_representation --- odl/operator/oputils.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/odl/operator/oputils.py b/odl/operator/oputils.py index 0c43b951e91..78019585b88 100644 --- a/odl/operator/oputils.py +++ b/odl/operator/oputils.py @@ -90,27 +90,29 @@ def matrix_representation(op): # Generate the matrix matrix = np.zeros([np.sum(n), np.sum(m)]) tmp_ran = op.range.element() # Store for reuse in loop - tmp_dom = op.domain.element() # Store for reuse in loop + tmp_dom = op.domain.zero() # Store for reuse in loop index = 0 + last_i = last_j = 0 for i in range(num_dom): for j in range(m[i]): - tmp_dom.set_zero() if op_dom_is_prod_space: + tmp_dom[last_i][last_j] = 0.0 tmp_dom[i][j] = 1.0 else: + tmp_dom[last_j] = 0.0 tmp_dom[j] = 1.0 op(tmp_dom, out=tmp_ran) if op_ran_is_prod_space: - tmp_result = np.empty(np.sum(n)) tmp_idx = 0 for k in range(num_ran): - tmp_result[tmp_idx: tmp_idx + op.range[k].size] = \ - tmp_ran[k].asarray() + matrix[tmp_idx: tmp_idx + op.range[k].size, index] = ( + tmp_ran[k]) tmp_idx += op.range[k].size else: - tmp_result = tmp_ran.asarray() - matrix[:, index] = tmp_result + matrix[:, index] = tmp_ran.asarray() index += 1 + last_j = j + last_i = i return matrix