[Scipy-svn] r2318 - in trunk/Lib/sandbox/numexpr: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Thu Nov 16 03:24:17 CST 2006
Author: cookedm
Date: 2006-11-16 03:24:11 -0600 (Thu, 16 Nov 2006)
New Revision: 2318
Modified:
trunk/Lib/sandbox/numexpr/compiler.py
trunk/Lib/sandbox/numexpr/expressions.py
trunk/Lib/sandbox/numexpr/interp_body.c
trunk/Lib/sandbox/numexpr/interpreter.c
trunk/Lib/sandbox/numexpr/tests/test_numexpr.py
Log:
[numexpr] whitespace cleanups
Modified: trunk/Lib/sandbox/numexpr/compiler.py
===================================================================
--- trunk/Lib/sandbox/numexpr/compiler.py 2006-11-16 05:41:35 UTC (rev 2317)
+++ trunk/Lib/sandbox/numexpr/compiler.py 2006-11-16 09:24:11 UTC (rev 2318)
@@ -1,11 +1,12 @@
-
import sys
import numpy
import interpreter, expressions
-typecode_to_kind = {'b': 'bool', 'i': 'int', 'f': 'float', 'c': 'complex', 'n' : 'none'}
-kind_to_typecode = {'bool': 'b', 'int': 'i', 'float': 'f', 'complex': 'c', 'none' : 'n'}
+typecode_to_kind = {'b': 'bool', 'i': 'int', 'f': 'float',
+ 'c': 'complex', 'n' : 'none'}
+kind_to_typecode = {'bool': 'b', 'int': 'i', 'float': 'f',
+ 'complex': 'c', 'none' : 'n'}
type_to_kind = expressions.type_to_kind
kind_to_type = expressions.kind_to_type
@@ -477,17 +478,17 @@
collapseDuplicateSubtrees(ast)
input_order = getInputOrder(ast, input_order)
- constants_order, constants = getConstants(ast)
-
- if isReduction(ast):
- ast.reg.temporary = False
-
- optimizeTemporariesAllocation(ast)
-
+ constants_order, constants = getConstants(ast)
+
+ if isReduction(ast):
+ ast.reg.temporary = False
+
+ optimizeTemporariesAllocation(ast)
+
ast.reg.temporary = False
- r_output = 0
- ast.reg.n = 0
-
+ r_output = 0
+ ast.reg.n = 0
+
r_inputs = r_output + 1
r_constants = setOrderedRegisterNumbers(input_order, r_inputs)
r_temps = setOrderedRegisterNumbers(constants_order, r_constants)
@@ -526,12 +527,12 @@
r_constants = 1 + len(nex.signature)
r_temps = r_constants + len(nex.constants)
def getArg(pc, offset):
- arg = ord(nex.program[pc+offset])
- op = rev_opcodes.get(ord(nex.program[pc]))
- code = op.split('_')[1][offset-1]
- if arg == 255:
- return None
- if code != 'n':
+ arg = ord(nex.program[pc+offset])
+ op = rev_opcodes.get(ord(nex.program[pc]))
+ code = op.split('_')[1][offset-1]
+ if arg == 255:
+ return None
+ if code != 'n':
if arg == 0:
return 'r0'
elif arg < r_constants:
@@ -539,8 +540,8 @@
elif arg < r_temps:
return 'c%d[%s]' % (arg, nex.constants[arg - r_constants])
else:
- return 't%d' % (arg,)
- else:
+ return 't%d' % (arg,)
+ else:
return arg
source = []
for pc in range(0, len(nex.program), 4):
@@ -624,5 +625,3 @@
compiled_ex = _numexpr_cache[numexpr_key] = \
numexpr(ex, signature, copy_args, **kwargs)
return compiled_ex(*arguments)
-
-
Modified: trunk/Lib/sandbox/numexpr/expressions.py
===================================================================
--- trunk/Lib/sandbox/numexpr/expressions.py 2006-11-16 05:41:35 UTC (rev 2317)
+++ trunk/Lib/sandbox/numexpr/expressions.py 2006-11-16 09:24:11 UTC (rev 2318)
@@ -32,8 +32,8 @@
for i, x in enumerate(args):
if isConstant(x):
args[i] = x = ConstantNode(x)
- elif not isinstance(x, ExpressionNode):
- raise TypeError( "unsupported object type: %s" % type(x) )
+ if not isinstance(x, ExpressionNode):
+ raise TypeError("unsupported object type: %s" % (type(x),))
return f(*args)
func.__name__ = f.__name__
func.__doc__ = f.__doc__
@@ -213,7 +213,7 @@
'where' : where_func,
'complex' : func(complex, 'complex'),
-
+
'sum' : sum_func,
'prod' : prod_func,
}
Modified: trunk/Lib/sandbox/numexpr/interp_body.c
===================================================================
--- trunk/Lib/sandbox/numexpr/interp_body.c 2006-11-16 05:41:35 UTC (rev 2317)
+++ trunk/Lib/sandbox/numexpr/interp_body.c 2006-11-16 09:24:11 UTC (rev 2318)
@@ -92,8 +92,8 @@
#define reduce_ptr (dest + flat_index(&store_index, j))
#define i_reduce *(long *)reduce_ptr
#define f_reduce *(double *)reduce_ptr
- #define cr_reduce *(double *)ptr
- #define ci_reduce *((double *)ptr+1)
+ #define cr_reduce *(double *)ptr
+ #define ci_reduce *((double *)ptr+1)
#define b_dest ((char *)dest)[j]
#define i_dest ((long *)dest)[j]
#define f_dest ((double *)dest)[j]
@@ -114,7 +114,7 @@
#define f3 ((double *)x3)[j*sf3]
#define c3r ((double *)x3)[j*sf3]
#define c3i ((double *)x3)[j*sf3+1]
-
+
double fa, fb;
cdouble ca, cb;
char *ptr;
@@ -231,14 +231,14 @@
case OP_SUM_CCN: VEC_ARG1(ptr = reduce_ptr;
cr_reduce += c1r;
ci_reduce += c1i);
-
+
case OP_PROD_IIN: VEC_ARG1(i_reduce *= i1);
case OP_PROD_FFN: VEC_ARG1(f_reduce *= f1);
case OP_PROD_CCN: VEC_ARG1(ptr = reduce_ptr;
fa = cr_reduce*c1r - ci_reduce*c1i;
ci_reduce = cr_reduce*c1i + ci_reduce*c1r;
cr_reduce = fa);
-
+
default:
*pc_error = pc;
return -3;
Modified: trunk/Lib/sandbox/numexpr/interpreter.c
===================================================================
--- trunk/Lib/sandbox/numexpr/interpreter.c 2006-11-16 05:41:35 UTC (rev 2317)
+++ trunk/Lib/sandbox/numexpr/interpreter.c 2006-11-16 09:24:11 UTC (rev 2318)
@@ -87,14 +87,14 @@
OP_REAL_FC,
OP_IMAG_FC,
OP_COMPLEX_CFF,
-
+
OP_REDUCTION,
-
+
OP_SUM,
OP_SUM_IIN,
OP_SUM_FFN,
OP_SUM_CCN,
-
+
OP_PROD,
OP_PROD_IIN,
OP_PROD_FFN,
@@ -488,7 +488,7 @@
PyString_AsStringAndSize(program_object, (char **)&program, &n);
return program[n-4];
-}
+}
static int
get_reduction_axis(PyObject* program) {
@@ -545,10 +545,10 @@
continue;
}
if ((op >= OP_REDUCTION) && pc != prog_len-4) {
- PyErr_Format(PyExc_RuntimeError,
+ PyErr_Format(PyExc_RuntimeError,
"invalid program: reduction operations must occur last");
return -1;
- }
+ }
for (argno = 0; ; argno++) {
sig = op_signature(op, argno);
if (sig == -1) {
@@ -839,7 +839,7 @@
struct index_data *index_data;
};
-static inline unsigned int
+static inline unsigned int
flat_index(struct index_data *id, unsigned int j) {
int i, k = id->count - 1;
unsigned int findex = id->findex;
@@ -848,7 +848,7 @@
findex = 0;
for (i = 0; i < id->count; i++)
findex += id->strides[i] * id->index[i];
- }
+ }
id->index[k] += 1;
if (id->index[k] >= id->shape[k]) {
while (id->index[k] >= id->shape[k]) {
@@ -956,7 +956,7 @@
int i, j, size, r, pc_error;
char **inputs = NULL;
intp strides[MAX_DIMS]; /* clean up XXX */
-
+
n_inputs = PyTuple_Size(args);
if (PyString_Size(self->signature) != n_inputs) {
return PyErr_Format(PyExc_ValueError,
@@ -966,7 +966,7 @@
return PyErr_Format(PyExc_ValueError,
"keyword arguments are not accepted");
}
-
+
/* This is overkill - we shouldn't need to allocate all of this space,
but this makes it easier figure out */
a_inputs = PyTuple_New(3*n_inputs);
@@ -974,7 +974,7 @@
inputs = PyMem_New(char *, n_inputs);
if (!inputs) goto cleanup_and_exit;
-
+
inddata = PyMem_New(struct index_data, n_inputs+1);
if (!inddata) goto cleanup_and_exit;
for (i = 0; i < n_inputs+1; i++)
@@ -982,7 +982,7 @@
/* First, make sure everything is some sort of array so that we can work
with their shapes. Count dimensions concurrently. */
-
+
for (i = 0; i < n_inputs; i++) {
PyObject *o = PyTuple_GET_ITEM(args, i); /* borrowed ref */
PyObject *a;
@@ -991,18 +991,18 @@
if (typecode == -1) goto cleanup_and_exit;
/* Convert it just in case of a non-swapped array */
a = PyArray_FROM_OTF(o, typecode, NOTSWAPPED);
- if (!a) goto cleanup_and_exit;
+ if (!a) goto cleanup_and_exit;
PyTuple_SET_ITEM(a_inputs, i, a); /* steals reference */
if (PyArray_NDIM(a) > n_dimensions)
n_dimensions = PyArray_NDIM(a);
}
-
+
/* Broadcast all of the inputs to determine the output shape (this will
- require some modifications if we later allow a final reduction
+ require some modifications if we later allow a final reduction
operation). If an array has too few dimensions it's shape is padded
with ones fromthe left. All array dimensions must match, or be one. */
-
- for (i = 0; i < n_dimensions; i++)
+
+ for (i = 0; i < n_dimensions; i++)
shape[i] = 1;
for (i = 0; i < n_inputs; i++) {
PyObject *a = PyTuple_GET_ITEM(a_inputs, i);
@@ -1014,17 +1014,17 @@
if (shape[delta+j] == 1)
shape[delta+j] = n;
else {
- PyErr_SetString(PyExc_ValueError,
+ PyErr_SetString(PyExc_ValueError,
"cannot broadcast inputs to common shape");
goto cleanup_and_exit;
}
}
}
size = PyArray_MultiplyList(shape, n_dimensions);
-
+
/* Broadcast indices of all of the arrays. We could improve efficiency
by keeping track of what needs to be broadcast above */
-
+
for (i = 0; i < n_inputs; i++) {
PyObject *a = PyTuple_GET_ITEM(a_inputs, i);
PyObject *b;
@@ -1032,12 +1032,12 @@
int delta = n_dimensions - PyArray_NDIM(a);
if (PyArray_NDIM(a)) {
for (j = 0; j < n_dimensions; j++)
- strides[j] = (j < delta || PyArray_DIM(a, j-delta) == 1) ?
+ strides[j] = (j < delta || PyArray_DIM(a, j-delta) == 1) ?
0 : PyArray_STRIDE(a, j-delta);
Py_INCREF(PyArray_DESCR(a));
- b = PyArray_NewFromDescr(a->ob_type,
+ b = PyArray_NewFromDescr(a->ob_type,
PyArray_DESCR(a),
- n_dimensions, shape,
+ n_dimensions, shape,
strides, PyArray_DATA(a), 0, a);
if (!b) goto cleanup_and_exit;
} else { /* Leave scalars alone */
@@ -1045,10 +1045,10 @@
Py_INCREF(b);
}
/* Store b so that it stays alive till we're done */
- PyTuple_SET_ITEM(a_inputs, i+n_inputs, b);
+ PyTuple_SET_ITEM(a_inputs, i+n_inputs, b);
}
-
-
+
+
for (i = 0; i < n_inputs; i++) {
PyObject *a = PyTuple_GET_ITEM(a_inputs, i+n_inputs);
char c = PyString_AS_STRING(self->signature)[i];
@@ -1082,7 +1082,7 @@
}
} else {
PyObject *origA = a;
- int inner_size = -1;
+ int inner_size = -1;
/* Check array is contiguous */
for (j = PyArray_NDIM(a)-1; j >= 0; j--) {
if ((inner_size == -1 && PyArray_STRIDE(a, j) % PyArray_ITEMSIZE(a)) ||
@@ -1109,7 +1109,6 @@
}
}
-
if (last_opcode(self->program) > OP_REDUCTION) {
char retsig = get_return_sig(self->program);
@@ -1146,7 +1145,7 @@
}
}
-
+
}
/* TODO optimize strides -- in this and other inddata cases, strides and
shape can be tweaked to minimize the amount of looping */
@@ -1159,7 +1158,7 @@
inddata[0].index = PyMem_New(int, n_dimensions);
for (j = 0; j < inddata[0].count; j++)
inddata[0].index[j] = 0;
-
+
if (last_opcode(self->program) >= OP_SUM &&
last_opcode(self->program) < OP_PROD) {
PyObject *zero = PyInt_FromLong(0);
@@ -1171,7 +1170,7 @@
Py_DECREF(one);
}
}
- else {
+ else {
char retsig = get_return_sig(self->program);
self->memsteps[0] = size_from_char(retsig);
output = PyArray_SimpleNew(n_dimensions,
@@ -1179,10 +1178,10 @@
typecode_from_char(retsig));
if (!output) goto cleanup_and_exit;
}
-
+
r = run_interpreter(self, size, PyArray_DATA(output), inputs, inddata, &pc_error);
-
+
if (r < 0) {
Py_XDECREF(output);
output = NULL;
@@ -1409,8 +1408,8 @@
#undef add_func
if (PyModule_AddObject(m, "funccodes", d) < 0) return;
-
+
if (PyModule_AddObject(m, "allaxes", PyInt_FromLong(255)) < 0) return;
if (PyModule_AddObject(m, "maxdims", PyInt_FromLong(MAX_DIMS)) < 0) return;
-
+
}
Modified: trunk/Lib/sandbox/numexpr/tests/test_numexpr.py
===================================================================
--- trunk/Lib/sandbox/numexpr/tests/test_numexpr.py 2006-11-16 05:41:35 UTC (rev 2317)
+++ trunk/Lib/sandbox/numexpr/tests/test_numexpr.py 2006-11-16 09:24:11 UTC (rev 2318)
@@ -36,16 +36,16 @@
def check_reductions(self):
# Check that they compile OK.
assert_equal(disassemble(numexpr("sum(x**2+2, axis=None)", [('x', float)])),
- [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
- ('add_fff', 't3', 't3', 'c2[2.0]'),
+ [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
+ ('add_fff', 't3', 't3', 'c2[2.0]'),
('sum_ffn', 'r0', 't3', None)])
assert_equal(disassemble(numexpr("sum(x**2+2, axis=1)", [('x', float)])),
- [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
- ('add_fff', 't3', 't3', 'c2[2.0]'),
+ [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
+ ('add_fff', 't3', 't3', 'c2[2.0]'),
('sum_ffn', 'r0', 't3', 1)])
assert_equal(disassemble(numexpr("prod(x**2+2, axis=2)", [('x', float)])),
- [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
- ('add_fff', 't3', 't3', 'c2[2.0]'),
+ [('mul_fff', 't3', 'r1[x]', 'r1[x]'),
+ ('add_fff', 't3', 't3', 'c2[2.0]'),
('prod_ffn', 'r0', 't3', 2)])
# Check that full reductions work.
x = arange(10.0)
@@ -71,7 +71,7 @@
x = (arange(10) % 2).astype(bool)
assert_equal(evaluate("prod(x,axis=0)"), prod(x,axis=0))
assert_equal(evaluate("sum(x,axis=0)"), sum(x,axis=0))
-
+
def check_axis(self):
y = arange(9.0).reshape(3,3)
try:
@@ -86,13 +86,13 @@
pass
else:
raise ValueError("should raise exception!")
-
-
-
+
+
+
def check_r0_reuse(self):
assert_equal(disassemble(numexpr("x**2+2", [('x', float)])),
- [('mul_fff', 'r0', 'r1[x]', 'r1[x]'),
+ [('mul_fff', 'r0', 'r1[x]', 'r1[x]'),
('add_fff', 'r0', 'r0', 'c2[2.0]')])
class test_evaluate(NumpyTestCase):
@@ -133,8 +133,8 @@
x = sin(complex(a, b)).real + z.imag
y = evaluate("sin(complex(a, b)).real + z.imag")
assert_array_almost_equal(x, y)
-
-
+
+
def check_complex_strides(self):
a = arange(100).reshape(10,10)[::2]
b = arange(50).reshape(5,10)
@@ -144,10 +144,10 @@
c['c2'].fill(0xaaaa)
c1 = c['c1']
a0 = a[0]
- assert_array_equal(evaluate("c1"), c1)
+ assert_array_equal(evaluate("c1"), c1)
assert_array_equal(evaluate("a0+c1"), a0+c1)
-
-
+
+
def check_broadcasting(self):
a = arange(100).reshape(10,10)[::2]
c = arange(10)
@@ -156,20 +156,20 @@
assert_array_equal(evaluate("a+d"), a+d)
expr = numexpr("2.0*a+3.0*c",[('a',float),('c', float)])
assert_array_equal(expr(a,c), 2.0*a+3.0*c)
-
+
def check_all_scalar(self):
a = 3.
b = 4.
assert_equal(evaluate("a+b"), a+b)
expr = numexpr("2*a+3*b",[('a',float),('b', float)])
assert_equal(expr(a,b), 2*a+3*b)
-
+
def check_run(self):
a = arange(100).reshape(10,10)[::2]
b = arange(10)
expr = numexpr("2*a+3*b",[('a',float),('b', float)])
assert_array_equal(expr(a,b), expr.run(a,b))
-
+
def check_illegal_value(self):
a = arange(3)
try:
@@ -178,8 +178,8 @@
pass
else:
self.fail()
-
+
tests = [
('MISC', ['b*c+d*e',
'2*a+3*b',
More information about the Scipy-svn
mailing list