diff options
Diffstat (limited to 'array.c')
-rw-r--r-- | array.c | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -41,3 +41,41 @@ simplescalar(Array *a) { return simplearray(a) && a->rank == 0; } + +Array * +extend(Array *a, Array *b) +{ + /* extend the singleton a to the shape of b */ + Array *shape = fnShape(b); + Array *res = fnReshape(shape, a); + freearray(shape); + return res; +} + +int +scalarextend(Array *a, Array *b, Array **aa, Array **bb) +{ + /* Extend the arrays a and b to have the same shape. + The resulting arrays are stored in aa and bb, + except when the ranks don't match or extension can't + happen, in which case the function returns 0 and + aa and bb are unchanged. + */ + + if(a->size == 1 && b->size != 1){ + *aa = extend(a, b); + *bb = fnSame(b); + }else if(b->size == 1 && a->size != 1){ + *aa = fnSame(a); + *bb = extend(b, a); + }else if(a->size == b->size && a->rank == b->rank){ + /* Check that each dimension matches */ + for(int i = 0; i < a->rank; i++) + if(a->shape[i] != b->shape[i]) + return 0; + *aa = fnSame(a); + *bb = fnSame(b); + }else + return 0; + return 1; +}
\ No newline at end of file |